<?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>
                                Download Webservice
                                <a href="<?php echo url('company/index');?>" class="btn btn-primary">VOLTAR</a>
                            </h3>
                        </div>
                        <div class="box-content">
                            <form class="wc_form" action="" method="POST">
                                <div class="row">

                                    <div class="form-group col-lg-3">
                                        <label>Ciência da Operação Automatica?</label>
                                        <select class="form-control" name="company_ciencia_operacao">
                                            <option value="0">SIM</option>
                                            <option value="1">NÃO</option>
                                        </select>
                                    </div>

                                    <div class="form-group col-lg-3">
                                        <label>Busca Automatica</label>
                                        <select class="form-control" name="company_busca_automatica">
                                            <option value="0">NÃO</option>
                                            <option value="1">SIM</option>
                                        </select>
                                    </div>

                                    <div class="form-group col-lg-3">
                                        <label>Intervalo Busca</label>
                                        <select class="form-control" name="company_intervalo_busca">
                                            <option value="60">A cada 60 minutos</option>
                                            <option value="120">A cada 2 horas</option>
                                            <option value="360">A cada 6 horas</option>
                                            <option value="720">A cada 12 horas</option>
                                        </select>
                                    </div>
                                </div>

                                <input type="hidden" class="inicio" value="0"/>
                                <input type="hidden" class="final" value="25"/>

                                <div class="row">
                                    <div class="form-group col-lg-12">
                                        <button class="btn btn-primary">GRAVAR</button>
                                    </div>
                                </div>
                            </form>
                            <hr />

                            <button class="btn btn-blue" onclick="buscar_nfe();">BUSCAR XML</button>


                            <button class="btn btn-blue" onclick="converter_nfe();">CONVERTER NOTA</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div id="modal-manifesta" class="modal fade" role="dialog">
    <div class="modal-dialog modal-lg" style="width: 75%">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">Manifestar Eventos</h4>
            </div>
            <div class="modal-body">
                <div class="row" style="display: none;">
                    <div class="form-group col-lg-12">
                        <label>EVENTO</label>
                        <select class="form-control evento_id">
                            <option value="210210">Ciência da Operação</option>
                        </select>
                    </div>
                </div>
                <div class="row" style="display: none;">
                    <div class="form-group col-lg-12">
                        <label>DESCRIÇÃO</label>
                        <input type="text" class="form-control evento_descricao" />
                        <input type="hidden" class="ide_nfs"/>
                        <input type="hidden" class="inicio_nfs" value="0">
                    </div>
                </div>
                <hr />
                <div class="row">
                    <span class="load_info_new" style="display: none;"><img src="<?= asset("/load.gif");?>"/> - Aguarde verificando</span>
                    <div class="col-lg-12">
                        <div class="load_retorno_manifesta"></div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">FECHAR</button>
            </div>
        </div>
    </div>
</div>
<script>
    function buscar_nfe(){
        var Data = "&action=search_company&id=<?php echo $param['uri'];?>";
        $.ajax({
            url: "<?= $router->route("doc_nfe.searchDoc"); ?>",
            data: Data,
            type: 'POST',
            dataType: 'json',
            beforeSend: load_in(),
            success: function (data) {
                if(data[0].type === 'error'){
                    notify(data[0].title, data[0].message);
                }else{
                    notify(data[0].title, data[0].message);
                    converter_nfe();
                }
            },
            error: function (request, status, error) {
                load_out();
            }
        });
        return false;
    }
    function converter_nfe(){
        var inicio = $(".inicio").val();
        var final = $(".final").val();
        var Data = "&action=search_company&id=<?php echo $param['uri'];?>&inicio="+inicio+"&final="+final;
        $.ajax({
            url: "<?= $router->route("doc_nfe.processDoc"); ?>",
            data: Data,
            type: 'POST',
            dataType: 'json',
            beforeSend: load_in(),
            success: function (data) {
                if(data[0].type === 'error'){
                    notify(data[0].title, data[0].message);
                }else if(data[0].type === 'info'){
                    setTimeout(function(){
                        converter_nfe();
                    }, 2000);
                }else{
                    if(data.tipo == 'tudo'){
                        $(".load_retorno_manifesta").html('');
                        $("#modal-manifesta").modal('show');
                        $(".ide_nfs").val(data.ids);
                        $(".inicio_nfs").val('0');

                        setTimeout(function(){
                            manifesta();
                        }, 2000);
                    }

                    notify(data[0].title, data[0].message);
                    load_out();
                }
            },
            error: function (request, status, error) {
                load_out();
            }
        });
        return false;
    }
    function manifesta(){
        var evento_id = $(".evento_id").val();
        var evento_descricao = $(".evento_descricao").val();
        var ide_nfs = $(".ide_nfs").val();
        var inicio_nfs = $(".inicio_nfs").val();

        var Data = "&action=search_doc_nfe&code="+evento_id+"&descricao="+evento_descricao+"&nfs="+ide_nfs+"&inicio="+inicio_nfs;
        $.ajax({
            url: "<?= $router->route("doc_nfe.manifestaConsulta"); ?>",
            data: Data,
            type: 'POST',
            dataType: 'json',
            beforeSend: load_in_new(),
            success: function (data) {
                if(data[0].type === 'ok'){
                    $(".load_retorno_manifesta").html(data.retorno);
                }else if(data[0].type === 'info') {
                    $(".load_retorno_manifesta").html(data.retorno);
                    $(".inicio_nfs").val(data.count);
                    setTimeout(manifesta, 2000);
                }else{
                    $(".load_retorno_manifesta").html('<p>'+data[0].message+'</p>');
                }
                load_out_new();
            },
            error: function (request, status, error) {
                load_out_new();
            }
        });
        return false;
    }

    $('.wc_form').submit(function () {
        var Form = $(this);
        var Param = "&id=<?php echo $param['uri'];?>";
        var Data = Form.serialize()+Param;

        $.ajax({
            url: "<?= $router->route("company.edit"); ?>",
            data: Data,
            type: 'POST',
            dataType: 'json',
            beforeSend: load_in(),
            success: function (data) {
                if(data[0].type === 'error'){
                    notify(data[0].title, data[0].message);
                }else{
                    notify(data[0].title, data[0].message);
                }
                load_out();
            },
            error: function (request, status, error) {
                notify('Opss', 'Operação não pode ser finalizada!!');
            }
        });
        return false;
    });
    function load_update(){
        var Data = "&id=<?php echo $param['uri'];?>";
        $.ajax({
            url: "<?= $router->route("company.selectId"); ?>",
            data: Data,
            type: 'POST',
            dataType: 'json',
            beforeSend: load_in(),
            success: function (data) {
                var Form = $(".wc_form");
                $.each(data, function (key, value) {
                    Form.find("input[name='" + key + "'], select[name='" + key + "']").val(value);
                });
                load_out();
            },
            error: function (request, status, error) {
                notify('Opss', 'Operação não pode ser finalizada!');
                setTimeOutPage('<?php echo url('company/index');?>');
            }
        });
        return false;
    }
    $(function(){
        load_update();
    });
</script>
