<?php (defined('BASEPATH')) OR exit('No direct script access allowed'); ?>

<script>
    $(window).load(function () {
        $('.mm_guides').addClass('active');
        <?php if($guideType == "s"){ ?>
        $('#guides_departure').addClass('active');
        <?php }else{ ?>
        $('#guides_entrance').addClass('active');
        <?php } ?>
    });
</script>
<section class="content">
    <div class="row">
        <div class="col-md-12">
            <?php 
                // echo  $this->session->flashdata('error');
                // echo  $this->session->flashdata('message');
            ?>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <div class="form-group">
                <?php
                    if($guideType=="i"){
                        echo '<a href="'.site_url('guide/addEntrance?guide_type=I').'" class="btn btn-primary">'. lang('create_guide').'</a>';
                    }elseif($guideType=="s"){
                        echo '<a href="'.site_url('guide/addEntrance?guide_type=S').'" class="btn btn-primary">'. lang('create_guide').'</a>';
                    }else{
                        echo "<h3>Error en la carga de guias. Consulte con mesa de soporte </3>";
                    }
                ?>
            </div>
        </div>

        <div class="col-xs-12">
            <div class="box" style="border-top: 2.5px solid #004785;">
                <div class="box-body">
                    <div class="table-responsive">
                        <table id="listados" class="table table-striped table-bordered table-condensed table-hover" style="width:100%">
						
                        </table>
                    </div>
                    <div class="clearfix"></div>
                </div>
            </div>
        </div>
    </div>

    <!-- Modal close guide -->
    <!-- <div class="modal fade" id="closei" tabindex="-1" role="dialog" aria-labelledby="closeiLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="closeiLabel"><?php // lang('close_guide', 'close_guide'); ?></h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <?php // echo form_open("guides/closeS"); ?>
                    <div class="form-group">
                        <label for="dateinput">Fecha</label>
                        <div class='input-group date' id='divFechaInicio'>
                            <input type='text' id="dateinput" name="date" class="input-medium form-control" placeholder="dd/mm/aaaa" readonly required="required"/>
                            <span class="input-group-addon">
                                <span class="fa fa-calendar glyphicon glyphicon-calendar"></span>
                            </span>
                        </div>
                    </div>
                    <div class="form-group">
                        <?php // lang('documents_types', 'documents_types'); ?>
                        <?php //
                            // $arrayDocumentTypes[] = lang("select")." ".lang("document");
                            // foreach($documents['list'] as $documentType) {
                            //     $arrayDocumentTypes[$documentType['id']] = $documentType['code'].'-'.$documentType['description'];
                            // }
                        ?>
                        <?php //form_dropdown('documentType_id',$arrayDocumentTypes, set_value("type",0),'class="form-control" id="documentTypeSelect" name="documentType_id" required="required" style="width:100% !important;"');
                        ?>
                    </div>
                    <div class="form-group">
                        <label for="doc_reference">Nº Doc:</label>
                        <input type="text" class="form-control" id="doc_reference">
                    </div>
                    <div class="form-group">
                        <label for="type_purchase">Tipo:</label>
                        <select class="form-control" id="type_purchase" name="type_purchase">
                            <option value=""> <?php // lang('select').lang('type_purchase'); ?></option>
                            <option value="local"> <?php // lang('local'); ?></option>
                            <option value="import"> <?php // lang('import'); ?></option>
                        </select>
                    </div>
                    <?php // echo form_close(); ?>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal"><?php // lang('close'); ?></button>
                <button type="button" class="btn btn-primary" id="saveClose"><?php// lang('save'); ?></button>
            </div>
            </div>
        </div>
    </div> -->
    <!-- FIN MODAL i -->

      <!-- Modal close guide s -->
    <!-- <div class="modal fade" id="closes" tabindex="-1" role="dialog" aria-labelledby="closesLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="closesLabel"><?php // lang('generate_sale', 'generate_sale'); ?></h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <?php //echo form_open("guides/closeS", $attrib,$hidden); ?>
                    <div class="form-group">
                        <label for="dateinput">Fecha</label>
                        <div class='input-group date' id='divFechaInicio'>
                            <input type='text' id="dateinput" name="date" class="input-medium form-control" placeholder="dd/mm/aaaa" readonly required="required"/>
                            <span class="input-group-addon">
                                <span class="fa fa-calendar glyphicon glyphicon-calendar"></span>
                            </span>
                        </div>
                    </div>
                    <div class="form-group">
                        <?php// lang('documents_types', 'documents_types'); ?>
                        <?php
                            // $arrayDocumentTypes[] = lang("select")." ".lang("document");
                            // foreach($documents['list'] as $documentType) {
                            //     $arrayDocumentTypes[$documentType['id']] = $documentType['code'].'-'.$documentType['description'];
                            // }
                        ?>
                        <?php// form_dropdown('documentType_id',$arrayDocumentTypes, set_value("type",0),'class="form-control" id="documentTypeSelect" name="documentType_id" required="required" style="width:100% !important;"');
                        ?>
                    </div>
                    <div class="form-group">
                        <?php //lang('counter', 'counter'); ?>
                        <?php
                            // $arrayCounter[] = lang("select")." ".lang("document");
                            // foreach($counters['list'] as $counter) {
                            //     $arrayCounter[$counter['id']] = $counter['code'].'-'.$counter['description'];
                            // }
                        ?>
                        <?php // form_dropdown('counter_id',$arrayCounter, set_value("type",0),'class="form-control" id="counterSelect" name="counter_id" required="required" style="width:100% !important;"');
                        ?>
                    </div>
                    <div class="form-group">
                        <label for="doc_reference">Nº Doc:</label>
                        <input type="text" class="form-control" id="doc_reference">
                    </div>
                    <div class="form-group">
                        <label for="type_purchase">Tipo:</label>
                        <select class="form-control" id="type_purchase" name="type_purchase">
                            <option value="local"> <?php // lang('local'); ?></option>
                            <option value="import"> <?php// lang('import'); ?></option>
                        </select>
                    </div>
                    <?php // echo form_close(); ?>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal"><?php // lang('close'); ?></button>
                <button type="button" class="btn btn-primary" id="saveClose"><?php // lang('save'); ?></button>
            </div>
            </div>
        </div>
    </div> -->
    <!-- FIN MODAL S -->
</section>

<script src="https://cdn.datatables.net/1.10.23/js/jquery.dataTables.min.js" type="text/javascript"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script type="text/javascript" src="<?= $assets ?>plugins/datatables/datatable.min.js"></script>
<script src="<?= $assets ?>plugins/input-mask/jquery.inputmask.js" type="text/javascript"></script>
<script src="<?= $assets ?>plugins/input-mask/jquery.inputmask.date.extensions.js" type="text/javascript"></script>
<script src="<?= $assets ?>plugins/bootstrap-datetimepicker/js/moment.min.js" type="text/javascript"></script>
<script src="<?= $assets ?>plugins/bootstrap-datetimepicker/js/bootstrap-datetimepicker.min.js" type="text/javascript"></script>

<script>

</script>
<script>
    $(document).ready(function() {
        armoTabla();
        // $('#myModal').on('shown.bs.modal', function () {
        //     $('#myInput').trigger('focus')
        // });
        // $( "#btnAddSerial" ).click(function() {
        //     alert('Serie guardada');
        //     var sendData = {
        //         date: $('#dateinput').val(),
        //         documentType_id:$('#documentTypeSelect').val(),
        //         type_purchase:$('#type_purchase').val()
        //     };
                
        //     $.ajax({
        //         type: "get",
        //         url: "get_exchange",
        //         data:  {
        //             'data':sendData
        //         },
        //         success: function(res) {
        //             res=JSON.parse(res);
        //             if(res.status=="failed"){
        //                 //reference
        //             }else{
        //                 var curren =res['currency'];
        //                 $('#exchange').val(res['sell']);
        //                 $('#currency').val(curren);
        //             }

        //         },
        //         error: function (xhr, ajaxOptions, thrownError) {
        //             console.log("Error");
        //             console.log(thrownError);
        //             console.log(ajaxOptions);
        //         }
        //     });     
        // });
        // $('#divFechaInicio').datetimepicker({
		//     format: 'DD/MM/YYYY',
		//     ignoreReadonly: true,
	    // });

        

    });
    function armoTabla() {
        $.fn.dataTable.ext.errMode = 'throw';
        $('#listados').DataTable({
            'bProcessing': true, 'bServerSide': true,
            "responsive": true,
            "order": [[ 0, "desc" ]],
            "processing": true,
            "serverSide": true,
            "ajax": {
                "url": "<?= site_url('guides/get')."/".$guideType ?>",        
            },
            "columns": [
                { "title":"#", "data": "item.id" ,  className: "text-center"},
                { "title":"Nro Guía", "data": "item.guide_number" },
                { "title":"Nro de Pedido", "data": "item.order_number"},
                { "title":"Almacén", "data": "item.warehouse_name" },
                { "title":"Fecha", "data": "item.date" },
                { "title":"Nro Documento", "data": "item.docreference_number" },
                { "title":"Prov/Cli", "data": null,
                    render: function ( data, type, row ) {
                        if(row.item.customer_id > 0){
                            return row.item.customer_name;
                        }else if(row.item.supplier_id > 0){
                            return row.item.supplier_name;
                        }else{
                            return "algo";
                        }
                    }    
                },
                { "title":"Opciones", "data": null,
                    render: function ( data, type, row ) {
                        var option = "<a href='<?= site_url("guide/edit/")?>/"+row.item.id+"' title='<?= lang('edit_guia')?>'  class='btn-edit tip btn btn-warning btn-xs'><i class='fa fa-edit'></i></a> &nbsp; ";
                        option += "<a onClick='confirmar("+row.item.id+")' title='<?=  lang('delete') ?>'class='tip btn btn-danger btn-xs'><i class='fa fa-trash-o'></i></a>";
                        // if(row.item.guide_type == "I"){
                        //     option += "<a data-toggle='modal' data-target='#closei'  onClick='return confirm(Desea cerrar la guía "+row.item.id+")' title='<?= lang('close') ?>'class='tip btn btn-secondary btn-xs'><i class='fa fa-unlock'></i></a>";
                        // }else if(row.item.guide_type == "S"){
                        //     option += "<a data-toggle='modal' data-target='#closes'  onClick='return confirm(Desea cerrar la guía "+row.item.id+")' title='<?= lang('close') ?>'class='tip btn btn-secondary btn-xs'><i class='fa fa-unlock'></i></a>";
                        // }
                        return option;   
                    }    
                },
            ],
            columnDefs: [
                { orderable: false, targets: -1 },
                { orderable: false, targets: -2 },
                { orderable: false, targets: -3 }
            ]               
        });
    };
    function confirmar(id){
        var url = '<?=  site_url("guide/delete") ?>/'+id;
        console.log(url);
        var confirmaste = confirm('Desea eliminar la guía número '+id);
        if(confirmaste === true){
            $.ajax({
                type: "get",
                url: url,
                success: function(res) {
                    alert(res);
                    $('#listados').DataTable().destroy();
                    armoTabla();
                    
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    console.log("Error");
                    console.log(thrownError);
                    console.log(ajaxOptions);
                }
            }); 
        }
        
    }

</script>