<?php
$v->layout("_themesis");
?>
<div class="container-fluid nav-hidden" id="content">
    <div id="">
        <div class="container-fluid">
            <div class="row">
                <div class="col-sm-12">
                    <div class="box">
                        <div class="box-title">
                            <h3>
                                Sped
                            </h3>
                        </div>
                        <div class="box-content">
                            <form class="wc_form_search" action="" method="POST">
                                <fieldset>
                                    <div class="row">
                                        <div class="form-group col-lg-4">
                                            <label>EMPRESA</label>
                                            <input type="hidden" class="autocomplete_company_completo_id" name="id_empresa"/>
                                            <input type="text" class="form-control autocomplete_company_completo" name="empresa" onblur="set_blank();" autocomplete="off"/>
                                        </div>
                                        <div class="form-group col-lg-3">
                                            <label>REFERÊNCIA</label>
                                            <input type="text" class="form-control referencia_faturamento_mask" name="referencia" autocomplete="off"/>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <div class="form-group col-lg-7">
                                            <label>.</label><br />
                                            <button class="btn btn-primary">PESQUISAR</button>
                                            <a class="btn btn-success" href="<?php echo url('sped/create');?>">CADASTRAR NOVO</a>
                                        </div>
                                    </div>
                                </fieldset>
                            </form>
                            <hr />
                            <div class="row">
                                <div class="load_sped_arquivo"></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    function set_blank(){
        var name = $(".autocomplete_company_completo").val();
        if(name == ''){
            $(".autocomplete_company_completo_id").val('');
        }
    }
    $('.wc_form_search').submit(function () {
        var Form = $(this);
        var Param = "&action=search_company";
        var Data = Form.serialize()+Param;


        $.ajax({
            url: "<?= $router->route("company.search"); ?>",
            data: Data,
            type: 'POST',
            dataType: 'json',
            beforeSend: load_in(),
            success: function (data) {
                redirect('<?php echo url('company/index');?>');
                load_out();
            },
            error: function (request, status, error) {
                notify('Opss', 'Operação não pode ser finalizada!!');
            }
        });
        return false;
    });
    function load_select(){
        var Data = "&action=select_company";
        $.ajax({
            url: "<?= $router->route("sped.select"); ?>",
            data: Data,
            type: 'POST',
            dataType: 'json',
            beforeSend: load_in(),
            success: function (data) {
                if(data.type === 'ok'){
                    $(".load_sped_arquivo").html(data.retorno);
                }
                load_out();
            },
            error: function (request, status, error) {
                load_out();
            }
        });
        return false;
    }
    function load_search(){
        var Data = "&action=search_company";
        $.ajax({
            url: "<?= $router->route("company.searchRead"); ?>",
            data: Data,
            type: 'POST',
            dataType: 'json',
            beforeSend: load_in(),
            success: function (data) {
                if(data.type === 'ok'){
                    var Form = $(".wc_form_search");
                    $.each(data.info, function (key, value) {
                        Form.find("input[name='" + key + "'], select[name='" + key + "']").val(value);
                    });
                }
                load_out();
            },
            error: function (request, status, error) {
                load_out();
            }
        });
        return false;
    }
    $(function(){
        load_select();
    });
</script>
