<?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>
                                Lotes XMLs CTe Chave
                            </h3>
                        </div>
                        <div class="box-content">
                            <form class="wc_form_search" action="" method="POST">
                                <fieldset>
                                    <div class="row">
                                        <div class="form-group col-lg-3">
                                            <label>DATA INICIAL</label>
                                            <input type="datetime-local" class="form-control" name="data_inicial" autocomplete="off"/>
                                        </div>
                                        <div class="form-group col-lg-2">
                                            <label>DATA FINAL</label>
                                            <input type="datetime-local" class="form-control" name="data_final" autocomplete="off"/>
                                        </div>
                                        <div class="form-group col-lg-2">
                                            <label>STATUS</label>
                                            <select name="status" class="form-control">
                                                <option value=""></option>
                                                <option value="0">PENDENTE</option>
                                                <option value="1">FINALIZADO</option>
                                                <option value="0">CANCELADO</option>
                                            </select>
                                        </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-blue" onclick="link_key();">VER CHAVES</a>
                                            <a class="btn btn-danger" onclick="link_download();">BAIXAR LOTE</a>
                                            <a class="btn btn-success" href="<?php echo url('xml_key_cte/create');?>">CADASTRAR NOVO</a>
                                        </div>
                                    </div>
                                </fieldset>
                            </form>
                            <hr />
                            <div id="load_key_cte"></div>
                            <div class="row">
                                <div class="col-sm-12">
                                    <div class="box box-color blue box-condensed box-bordered">
                                        <div class="box-title" align="center">
                                            <h3>
                                                QUANTIDADE
                                            </h3>
                                        </div>
                                        <div class="box-content">
                                            <div id="key_cte_quantify" align="center"><h4></h4></div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var selectionFormatter = function(value, data, cell, row, options, formatterParams){
        var rowSelect = $("<input type='checkbox' class='row-select' value='"+data.lote_id+"'>");

        rowSelect.on("change", function(){
            if($(this).is(":checked")){
                $(this).closest(".tabulator-row").addClass("selected");
            }else{
                $(this).closest(".tabulator-row").removeClass("selected");
            }
        });
        return rowSelect;
    }
    $("#load_key_cte").tabulator({
        height: "450px",
        fitColumns: true,
        ajaxURL: "<?= $router->route("xml_key_cte.select"); ?>",
        ajaxParams: {action: "search_key_cte"},
        pagination: "remote",
        paginationSize: 100,
        paginationDataSent: {"page": "pageNo"},
        columns: [
            {title:"<input type='checkbox' class='select-all'>", formatter:selectionFormatter, width: 35},
            {title: "ID", field: "lote_id", sorter: "int", width: 75},
            {title: "CONTATO", field: "company_name", sorter: "string"},
            {title: "STATUS", field: "lote_status", sorter: "string"},
            {title: "DATA HORA", field: "lote_date_hour", sorter: "string"},
            {title: "DATA HORA DOWNLOAD", field: "lote_date_init_download", sorter: "string"},
            {title: "DATA HORA FINAL", field: "lote_date_hour_end", sorter: "string"},
            {title: "TEMPO", field: "lote_temp", sorter: "string"},
            {title: "GASTO", field: "lote_quantify", sorter: "string"}
        ],
        ajaxResponse:function(url, params, response){
            $("#key_cte_quantify").html('<h4>'+response.quantify+'</h4>');
            return response;
        }
    });
    $("#load_key_cte .select-all").on("change", function(){
        if($(this).is(":checked")){
            $("#load_key_cte .row-select").prop("checked", true).closest(".tabulator-row").addClass("selected");
        }else{
            $("#load_key_cte .row-select").prop("checked", false).closest(".tabulator-row").removeClass("selected");
        }
    });
    $('.wc_form_search').submit(function () {
        var Form = $(this);
        var Param = "&action=search_key_cte";
        var Data = Form.serialize()+Param;


        $.ajax({
            url: "<?= $router->route("xml_key_cte.search"); ?>",
            data: Data,
            type: 'POST',
            dataType: 'json',
            beforeSend: load_in(),
            success: function (data) {
                redirect('<?php echo url('xml_key_cte/index');?>');
                load_out();
            },
            error: function (request, status, error) {
                notify('Opss', 'Operação não pode ser finalizada!!');
            }
        });
        return false;
    });
    function load_search(){
        var Data = "&action=search_key_cte";
        $.ajax({
            url: "<?= $router->route("xml_key_cte.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 link_key(){
        var objCheckBox = $("#load_key_cte .row-select");
        var sel = 0;
        var selecionadas = "";
        for (i=0; i < objCheckBox.length; i++) {
            if (objCheckBox[i].checked) {
                sel++;
                selecionadas += objCheckBox[i].value+",";
            }
        }
        if(Number(sel) === 0){
            notify('Ops', 'É preciso selecionar um registro');
        }else if(Number(sel) > 1){
            notify('Ops', 'É preciso selecionar apenas um registro');
        }else{
            var url = selecionadas.substr(0,(selecionadas.length - 1));
            window.location = '<?php echo url('xml_key_cte/key/');?>'+url;
        }
    }
    function link_delete(){
        var objCheckBox = $("#load_key_cte .row-select");
        var sel = 0;
        var selecionadas = "";
        for (i=0; i < objCheckBox.length; i++) {
            if (objCheckBox[i].checked) {
                sel++;
                selecionadas += objCheckBox[i].value+",";
            }
        }
        if(Number(sel) === 0){
            notify('Ops', 'É preciso selecionar um registro');
        }else if(Number(sel) > 1){
            notify('Ops', 'É preciso selecionar apenas um registro');
        }else{
            var url = selecionadas.substr(0,(selecionadas.length - 1));
            window.location = '<?php echo url('xml_key_cte/delete/');?>'+url;
        }
    }
    function link_download(){
        var objCheckBox = $("#load_key_cte .row-select");
        var sel = 0;
        var selecionadas = "";
        for (i=0; i < objCheckBox.length; i++) {
            if (objCheckBox[i].checked) {
                sel++;
                selecionadas += objCheckBox[i].value+",";
            }
        }
        if(Number(sel) === 0){
            notify('Ops', 'É preciso selecionar um registro');
        }else{
            var url = selecionadas.substr(0,(selecionadas.length - 1));
            window.open('../shared/zip/lote'+url+'.rar', '_blank');
        }
    }

    $(function(){
        load_search();
    });
</script>
