@include('led/led_header')
<link href="{{asset('/css/modal/custom-modal.css')}}" rel="stylesheet">
<script type="text/javascript" src="{{ URL::asset('/js/fancy/fancy.min.js') }}?v={{time()}}"> </script>
<!--script type="text/javascript" src="{{ URL::asset('/js/modal/custom-modal.js') }}"> </script-->

<div class="container">

    <div class="row">
        @if ($status == 'PENDIENTES')
        <div class="col-md-6 h3">@traducir('Documentos pendientes de verificar','Documentos pendientes de verificar')</div>
        <div class="col-md-3">
            <button type="button" class="btn btn-success form-control btnverificados"><i class="fa fa-check"></i> @traducir('Documentos verificados','Documentos verificados')</button>
        </div>
        <div class="col-md-3">
            <button type="button" class="btn btn-danger form-control btnrechazados"><i class="fa fa-times"></i> @traducir('Documentos rechazados','Documentos rechazados')</button>
        </div>
        @elseif ($status == 'VERIFICADOS')
        <div class="col-md-6 h3">@traducir('Documentación verificada','Documentación verificada')</div>
        <div class="col-md-3">
            <button type="button" class="btn btn-fidelitas btn-primary form-control btnpendientes"><i class="fa fa-pause"></i> @traducir('Documentos pendientes','Documentos pendientes')</button>
        </div>
        <div class="col-md-3">
            <button type="button" class="btn btn-danger form-control btnrechazados"><i class="fa fa-times"></i> @traducir('Documentos rechazados','Documentos rechazados')</button> 
        </div>
        @else
        <div class="col-md-6 h3">@traducir('Documentación rechazada','Documentación rechazada')</div>
        <div class="col-md-3">
            <button type="button" class="btn btn-fidelitas btn-primary form-control btnpendientes"><i class="fa fa-pause"></i> @traducir('Documentos pendientes','Documentos pendientes')</button>
        </div>
        <div class="col-md-3">
            <button type="button" class="btn btn-success form-control btnverificados"><i class="fa fa-check"></i> @traducir('Documentos verificados','Documentos verificados')</button>
        </div>
        @endif
    </div>
    <div class="row" id="loading-verif" style="display:block">
        <div class="col-md-2 col-md-offset-5">
            <br />
            <br />
            <img src="/images/loading.gif" class="img img-responsive" />
        </div>
    </div>

    <div class="row" id="verifDocs" style="display:none">
        <div class="col-md-12" id="tablaDocs"></div>
    </div>
</div>


<div rel="custom-modal:open"  id="modalDocumento" aria-labelledby="" style="display:none;" class="modal-documento">
	<div class="modal-content">
     	<div class="modal-header">
			<h3 class="modal-title" id="modal-title"></h3>
		</div>
		<div class="modal-body" id="modal-body"></div>
	</div>
</div>

<div rel="custom-modal:open" id="modalConfirm" aria-labelledby="" style="display:none;width:650px" class="modal-confirm">
	<div class="modal-content">
     	<div class="modal-header">
			<h3 class="modal-title" id="confirm-title">@traducir('Confirmar operación','Confirmar operación')</h3>
		</div>
		<div class="modal-body" id="confirm-body"></div>
	</div>
</div>

<div rel="custom-modal:open" id="modalPdf" aria-labelledby="" style="display:none;width:750px" class="modal-pdf">
	<div class="modal-content">
     	<div class="modal-header">
			<h3 class="modal-title" id="pdf-title">@traducir('Documento','Documento')</h3>
		</div>
		<div class="modal-body" id="pdf-body"></div>
	</div>
</div>


<form id="frmVerifDocumentos" name="frmVerifDocumentos" method="post" action="/imperium/documentos-verificados">
    {{csrf_field()}}
    <input type="hidden" name="doc_id" value="">
    <input type="hidden" name="doc_version" value="">
    <input type="hidden" name="fecha_vigencia" value="">
    <input type="hidden" name="status" value="{{$status}}">

</form>

<style>
    .container {
        min-width:100%
    }
</style>
<script>
    $(document).ready(function() {

        var boton_excel = '@traducir("Exportar a excel", "Exportar a excel")'; 
        var registros = '@traducir("Registros", "Registros")';  
        var pagina = '@traducir("Pagina", "Página")'; 
        var dede = '@traducir("de", "de")'; 

        $('.btnverificados').on('click',function() {
            $('#frmVerifDocumentos').prop('action', '/imperium/documentos-verificados');
            $('#frmVerifDocumentos [name="status"]').val('VERIFICADOS');
            $('#frmVerifDocumentos').submit();
        });
        $('.btnrechazados').on('click',function() {
            $('#frmVerifDocumentos').prop('action', '/imperium/documentos-rechazados');
            $('#frmVerifDocumentos [name="status"]').val('RECHAZADOS');
            $('#frmVerifDocumentos').submit();
        });
        $('.btnpendientes').on('click',function() {
            location.href = '/imperium/verificacion-documentos'
        });


        $.ajax({
            method : 'post',
            url : '/imperium/verificacion-documentos-data',
            data : $('#frmVerifDocumentos').serialize(),
            dataType : 'json',
            beforeSend : function() {
                $('#loading-verif').show();
                $('#verifDocs').hide();
                $('#tablaDocs').html('');
            },
            success : function(response) {
                var docs = response.docs;
                console.log(docs);
                $.each(docs, function(i, el) {
                    //docs[i].periodo_my = el.periodo.substring(4,6) + '-' + el.periodo.substring(0,4);
                    docs[i].periodo_my = el.periodo.substring(0,4) + '-' + el.periodo.substring(4,6);
                    docs[i].nombre_contratista = (el.contratista == el.contratista_1) ? null : el.nombre_contratista;
                    docs[i].ci_killer = (el.ci_killer == 1) ? 'SI' : 'NO';
                });

                var periodos = response.periodos;
                var secciones = response.secciones;
                var acceso = response.acceso;
                var nomdocs = response.nomdocs;

                var gridDocs;

                var columns = [
                    {type:'string',title:"@traducir('Documento','Documento')",index:'nombre_tipo_doc',flex:1,align:'center',cellAlign:'left',resizable:true,sortable:true,locked:true,filter: {header:true}},
                    {type:'string',title:"id",index:'doc_id',hidden:true,align:'center',cellAlign:'center',resizable:true,sortable:true},
                    {type:'date',title:"@traducir('Período','Período')",index:'periodo_my',align:'center',cellAlign:'center',resizable:true,sortable:true,locked:true,filter: {header:true},format : {read:'YYYY-mm',write:'dd-mm-YYYY'} },
                    {type:'string',title:"@traducir('Sección','Sección')",index:'nombre_seccion',align:'center',cellAlign:'center',resizable:true,sortable:true,locked:true,filter: {header:true}},
                    {type:'string',title:"@traducir('Contratista','Contratista')",index:'nombre_contratista_1',flex:1,align:'center',cellAlign:'left',resizable:true,sortable:true,locked:true,filter: {header:true}},
                    {type:'string',title:"@traducir('CUIT','CUIT')",index:'numero_1',align:'center',cellAlign:'center',resizable:true,sortable:true,locked:true,filter: {header:true}},
                    {type:'string',title:"@traducir('Subcontratista','Subcontratista')",index:'nombre_contratista',flex:1,align:'center',cellAlign:'left',resizable:true,sortable:true,locked:true,filter: {header:true}},
                    {type:'string',title:"@traducir('Empleado','Empleado')",index:'nombre_empleado',align:'center',flex:1,cellAlign:'left',resizable:true,sortable:true,locked:true,filter: {header:true}},
                    {type:'string',title:"@traducir('CUIL','CUIL')",index:'numero_e',align:'center',cellAlign:'center',resizable:true,sortable:true,locked:true,filter: {header:true}},
                    {type:'string',title:"@traducir('Vehículo','Vehículo')",index:'nombre_vehiculo',align:'center',flex:1,cellAlign:'left',resizable:true,sortable:true,locked:true,filter: {header:true}},
                    {type:'string',title:"@traducir('Dominio','Dominio')",index:'dominio',align:'center',cellAlign:'center',resizable:true,sortable:true,locked:true,filter: {header:true}},
                    {type:'string',title:"@traducir('Acceso','Acceso')",index:'ci_killer',align:'center',cellAlign:'center',resizable:false,sortable:false,locked:true,filter: {header:true}},
                ];

                var col = columns[0];
                col.type = 'combo';
                col.multiSelect = true;
                col.itemCheckBox = true;
                col.minListWidth = 100;
                col.data = nomdocs;
                col.displayKey = 'nombre_documento';
                col.valueKey = 'nombre_documento';
                col.render = function (o) {
                    o.value = $.trim(o.value);
                    return o;
                }
                columns[0] = col;

                var col = columns[2];
                col.type = 'combo';
                col.multiSelect = true;
                col.itemCheckBox = true;
                col.minListWidth = 100;
                col.data = periodos;
                col.displayKey = 'periodo_my';
                col.valueKey = 'periodo';
                col.render = function (o) {
                    o.value = $.trim(o.value);
                    return o;
                }
                columns[2] = col;

                var col = columns[3];
                col.type = 'combo';
                col.multiSelect = true;
                col.itemCheckBox = true;
                col.minListWidth = 100;
                col.data = secciones;
                col.displayKey = 'nombre_seccion';
                col.valueKey = 'nombre_seccion';
                col.render = function (o) {
                    o.value = $.trim(o.value);
                    return o;
                }
                columns[3] = col;

                var col = columns[11];
                col.type = 'combo';
                col.multiSelect = true;
                col.itemCheckBox = true;
                col.minListWidth = 100;
                col.data = acceso;
                col.displayKey = 'acceso';
                col.valueKey = 'acceso';
                col.render = function (o) {
                    o.value = $.trim(o.value);
                    return o;
                }
                columns[11] = col;



                gridDocs = new FancyGrid({
                    renderTo : 'tablaDocs',
                    //width: window.innerWidth - (window.innerWidth * .10),
                    width: $('.container').width() - ($('.container').width() * .00),

                    //height: 640,
                    height: 'fit',

                    cellHeight: 26,
                    theme: {
                        name: 'gray',
                    },
                    lang: {
                        paging: {
                            info: "Página Actual [0] - [1] de [2]",
                            page: "Página",
                            of: "de [0]",
                        },
                    },                    

                    selModel: { type : 'rows', memory : true },
                    paging : true,
                    paging : {
                        pageSize: 25,
                        pageSizeData: [10,25,50],
                        barType: 'tbar'
                    },  
                    scrollable: true,
                    cellHeight: 26,
                    
                    //Habilita el agregado de css en todas las celdas de un fila cuando se hace mouseover.
                    trackOver: true,                        
                    data: docs,
                    columns: columns,
                    events : [
                        {
                            init: function (g) { 
                                $('#tablaDocs .fancy').removeClass('fancy-panel-shadow');
                                $('#tablaDocs .fancy-bar-button').removeAttr("style");
                                $('#tablaDocs .fancy-panel-tbar').css('border-bottom', '0px'); 
                                $('#tablaDocs .fancy-panel-tbar .fancy-bar-left-scroller').remove(); 
                                $('#tablaDocs .fancy-panel-tbar .fancy-bar-right-scroller').remove();    
                            },
                            scope: {}
                                        
                        }, 
                        {
                            cellclick : function(grid, obj) {
                                selection = grid.getSelection();
                                var rec = selection[0];

                                $('#frmVerifDocumentos [name="doc_id"]').val(rec.doc_id);

                                $('#modal-title').html(rec.nombre_tipo_doc)
                                $.ajax({
                                    method : 'post',
                                    url : '/imperium/verificacion-documentos-doc',
                                    data : $('#frmVerifDocumentos').serialize(),
                                    beforeSend : function() {
                                        $('#modal-body').html('');
                                    },
                                    success : function(response) {
                                        $('#modal-body').html(response)
                                    }
                                });
                                $('#modalDocumento').customModal({
                                    closeExisting: false,
                                    escapeClose: false,
                                    clickClose: false,
                                });
                            }
                        }
                    ]
                });
                

                $('#loading-verif').hide();
                $('#verifDocs').show();

            }
        });

        $(document).on('click', '.actualizarVigencia, .actualizarVigenciaReemp', function() {
            var fld = $(this).hasClass('actualizarVigenciaReemp') ? 'fecha_vigencia_reemp' : 'fecha_vigencia';

            if($('#' + fld).val() != '') {
                $('#frmVerifDocumentos [name="fecha_vigencia"]').val($('#' + fld).val());
                $('#frmVerifDocumentos [name="doc_version"]').val($(this).data('docVersion'));
                $.ajax({
                        method: 'post',
                        url: '/imperium/actualizar-fecha-vigencia',
                        data: $('#frmVerifDocumentos').serialize(),
                        dataType: 'json',
                        beforeSend: function() {
                            $(this).prop('disabled', true);
                        },
                        success: function(response) {
                            alert(response.message);
                            $(this).prop('disabled', false);
                        }
                });
            }
        });               


        $(document).on('submit', '#frmVerificarDoc', function(e) {
            e.preventDefault();
            $.ajax({
                    method: 'post',
                    url: '/imperium/verificar-documento',
                    data: $(this).serialize(),
                    dataType: 'json',
                    beforeSend: function() {
                        $('.btnverificardoc1').prop('disabled', true);
                        $('.btnverificardoc2').prop('disabled', true);
                        $('#verificarDocLoading').show();
                        $('#verificarMsgResult').html('');
                        $('#verificarDocMessage').hide();
                    },
                    success: function(response) {
                        if (response.result == 0) {
                            $('#verificarMsgResult').removeClass('btn-danger');
                            $('#verificarMsgResult').addClass('btn-success');
                        }
                        else {
                            $('#verificarMsgResult').removeClass('btn-succes');
                            $('#verificarMsgResult').addClass('btn-danger');
                        }
                        $('#verificarMsgResult').html(response.message);

                        $('#verificarDocLoading').hide();
                        $('#verificarDocMessage').show();

                        setTimeout(function() {
                            $('.btnverificardoc1').prop('disabled', false);
                            $('.btnverificardoc2').prop('disabled', false);
                            $('.close-modal').trigger('click');
                        }, 2500);

                        // Refresh del modal del documento
                        $.ajax({
                                method : 'post',
                                url : '/imperium/verificacion-documentos-doc',
                                data : $('#frmVerifDocumentos').serialize(),
                                beforeSend : function() {
                                    $('#modal-body').html('');
                                },
                                success : function(response) {
                                    $('#modal-body').html(response)
                                }
                        });

                    }
            });
        });


        $(document).on('submit', '#frmRechazarDoc', function(e) {
            e.preventDefault();
            $.ajax({
                    method: 'post',
                    url: '/imperium/rechazar-documento',
                    data: $(this).serialize(),
                    dataType: 'json',
                    beforeSend: function() {
                        $('.btnrechazardoc1').prop('disabled', true);
                        $('.btnrechazardoc2').prop('disabled', true);
                        $('#rechazarDocLoading').show();
                        $('#rechazarMsgResult').html('');
                        $('#rechazarDocMessage').hide();
                    },
                    success: function(response) {
                        if (response.result == 0) {
                            $('#rechazarMsgResult').removeClass('btn-danger');
                            $('#rechazarMsgResult').addClass('btn-success');
                        }
                        else {
                            $('#rechazarMsgResult').removeClass('btn-succes');
                            $('#rechazarMsgResult').addClass('btn-danger');
                        }
                        $('#rechazarMsgResult').html(response.message);

                        $('#rechazarDocLoading').hide();
                        $('#rechazarDocMessage').show();

                        setTimeout(function() {
                            $('.btnrechazardoc1').prop('disabled', false);
                            $('.btnrechazardoc2').prop('disabled', false);
                            if (response.result != 2) {
                                $('.close-modal').trigger('click');
                            }
                            else {
                                $('#rechazarDocMessage').hide();
                            }
                        }, 2500);

                        // Refresh del modal del documento
                        $.ajax({
                                method : 'post',
                                url : '/imperium/verificacion-documentos-doc',
                                data : $('#frmVerifDocumentos').serialize(),
                                beforeSend : function() {
                                    $('#modal-body').html('');
                                },
                                success : function(response) {
                                    $('#modal-body').html(response)
                                }
                        });

                    }
            });
        });

        

        $(document).on('submit', '#frmVerificarDocParcial', function(e) {
            e.preventDefault();
            $.ajax({
                    method: 'post',
                    url: '/imperium/verificar-documento-parcial',
                    data: $(this).serialize(),
                    dataType: 'json',
                    beforeSend: function() {
                        $('.btnverificardocparcial1').prop('disabled', true);
                        $('.btnverificardocparcial2').prop('disabled', true);
                        $('#verificarDocParcialLoading').show();
                        $('#verificarParcialMsgResult').html('');
                        $('#verificarDocParcialMessage').hide();
                    },
                    success: function(response) {
                        if (response.result == 0) {
                            $('#verificarParcialMsgResult').removeClass('btn-danger');
                            $('#verificarParcialMsgResult').addClass('btn-success');
                        }
                        else {
                            $('#verificarParcialMsgResult').removeClass('btn-succes');
                            $('#verificarParcialMsgResult').addClass('btn-danger');
                        }
                        $('#verificarParcialMsgResult').html(response.message);

                        $('#verificarDocParcialLoading').hide();
                        $('#verificarDocParcialMessage').show();

                        setTimeout(function() {
                            $('.btnverificardocparcial1').prop('disabled', false);
                            $('.btnverificardocparcial2').prop('disabled', false);
                            if (response.result != 2) {
                                $('.close-modal').trigger('click');
                            }
                            else {
                                $('#verificarDocParcialMessage').hide();
                            }
                        }, 2500);

                        // Refresh del modal del documento
                        $.ajax({
                                method : 'post',
                                url : '/imperium/verificacion-documentos-doc',
                                data : $('#frmVerifDocumentos').serialize(),
                                beforeSend : function() {
                                    $('#modal-body').html('');
                                },
                                success : function(response) {
                                    $('#modal-body').html(response)
                                }
                        });

                    }
            });
        });



        $(document).on('submit', '#frmUndoVerificarDoc', function(e) {
            e.preventDefault();

            $.ajax({
                    method: 'post',
                    url: '/imperium/undo-verificar-documento',
                    data: $(this).serialize(),
                    dataType: 'json',
                    beforeSend: function() {
                        $('.btnundoverificardoc1').prop('disabled', true);
                        $('.btnundoverificardoc2').prop('disabled', true);
                        $('#undoVerificarDocLoading').show();
                        $('#undoVerificarMsgResult').html('');
                        $('#undoVerificarDocMessage').hide();
                    },
                    success: function(response) {
                        if (response.result == 0) {
                            $('#undoVerificarMsgResult').removeClass('btn-danger');
                            $('#undoVerificarMsgResult').addClass('btn-success');
                        }
                        else {
                            $('#undoVerificarMsgResult').removeClass('btn-succes');
                            $('#undoVerificarMsgResult').addClass('btn-danger');
                        }
                        $('#undoVerificarMsgResult').html(response.message);

                        $('#undoVerificarDocLoading').hide();
                        $('#undoVerificarDocMessage').show();

                        setTimeout(function() {
                            $('.btnundoverificardoc1').prop('disabled', false);
                            $('.btnundoverificardoc2').prop('disabled', false);
                            if (response.result != 2) {
                                $('.close-modal').trigger('click');
                            }
                            else {
                                $('#undoVerificarDocMessage').hide();
                            }
                        }, 2500);

                        // Refresh del modal del documento
                        $.ajax({
                                method : 'post',
                                url : '/imperium/verificacion-documentos-doc',
                                data : $('#frmVerifDocumentos').serialize(),
                                beforeSend : function() {
                                    $('#modal-body').html('');
                                },
                                success : function(response) {
                                    $('#modal-body').html(response)
                                }
                        });

                    }
            });
            
        });



        $(document).on('submit', '#frmUndoRechazarDoc', function(e) {
            e.preventDefault();

            $.ajax({
                    method: 'post',
                    url: '/imperium/undo-rechazar-documento',
                    data: $(this).serialize(),
                    dataType: 'json',
                    beforeSend: function() {
                        $('.btnundorechazardoc1').prop('disabled', true);
                        $('.btnundorechazardoc2').prop('disabled', true);
                        $('#undoRechazarDocLoading').show();
                        $('#undoRechazarMsgResult').html('');
                        $('#undoRechazarDocMessage').hide();
                    },
                    success: function(response) {
                        if (response.result == 0) {
                            $('#undoRechazarMsgResult').removeClass('btn-danger');
                            $('#undoRechazarMsgResult').addClass('btn-success');
                        }
                        else {
                            $('#undoRechazarMsgResult').removeClass('btn-succes');
                            $('#undoRechazarMsgResult').addClass('btn-danger');
                        }
                        $('#undoRechazarMsgResult').html(response.message);

                        $('#undoRechazarDocLoading').hide();
                        $('#undoRechazarDocMessage').show();

                        setTimeout(function() {
                            $('.btnundorechazardoc1').prop('disabled', false);
                            $('.btnundorechazardoc2').prop('disabled', false);
                            if (response.result != 2) {
                                $('.close-modal').trigger('click');
                            }
                            else {
                                $('#undoRechazarDocMessage').hide();
                            }
                        }, 2500);

                        // Refresh del modal del documento
                        $.ajax({
                                method : 'post',
                                url : '/imperium/verificacion-documentos-doc',
                                data : $('#frmVerifDocumentos').serialize(),
                                beforeSend : function() {
                                    $('#modal-body').html('');
                                },
                                success : function(response) {
                                    $('#modal-body').html(response)
                                }
                        });

                    }
            });
            
        });

    });
</script>
@include('led/led_footer')