<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title"><?php print CommonLanguage::getMessage('missed_schedule_checklist'); ?></div>
            <div class="row">
                <div class=" col-md-3 input-daterange">
                    <label><?php echo CommonLanguage::getMessage('Date');?></label>
                    <input id="current_date" type="text" autocomplete="off" class="form-control" name="current_date" >
                </div>
                <div class="col-md-2">  
                    <label><?php echo CommonLanguage::getMessage('line');?></label>
                    <input  type="hidden" name="line_id" id="line_id" class="full-width"/>
                </div>
                <div class="col-md-2">  
                    <label><?php echo CommonLanguage::getMessage('machine');?></label>
                    <input  type="hidden" name="Product_ID" id="Product_ID" class="full-width"/>
                </div>
                <div class="col-md-2">  
                    <label><?php echo CommonLanguage::getMessage('section');?></label>
                    <input  type="hidden" name="section_id" id="section_id" class="full-width"/>
                </div>
                <div class="col-md-1 m-t-20 pull-right">
                    <button id="print_missed_checklist" class="btn btn-danger text-white" ><i class="fa fa-print"></i> <?php echo CommonLanguage::getMessage('print');?></button>
                </div>
            </div>
        </div>
        <div class="panel-body">
            <div class="row">
                <div class="col-md-12">
                    <div class="table-responsive">
                        <table class="table table-bordered maintenance-table" id="tableWithSearch">
                            <thead>
                                <tr>
                                    <th><?php print CommonLanguage::getMessage('schedule') ?></th>
                                    <th><?php print CommonLanguage::getMessage('checklist_name') ?></th>
                                    <th><?php print CommonLanguage::getMessage('line') ?></th>
                                    <th><?php print CommonLanguage::getMessage('machine') ?></th>
                                    <th><?php print CommonLanguage::getMessage('part') ?></th>
                                    <th style="width:5%;"><?php print CommonLanguage::getMessage('next_schedule_date') ?></th>
                                    <th><?php print CommonLanguage::getMessage('last_run_date') ?></th>
                                    <th style="width:7%;"><?php print CommonLanguage::getMessage('diff') ?></th>
                                    <th style="width:5%;"><?php print CommonLanguage::getMessage('schedule_checklist') ?></th>
                                    <th><?php print CommonLanguage::getMessage('Action') ?></th>
                                </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            <div class="row hidden">
                <div class="col-md-12">
                    <table class="table table-bordered mytable" id="current_table_data">
                        <thead>
                            <th><?php echo  CommonLanguage::getMessage('line');?></th>
                            <th><?php echo CommonLanguage::getMessage('machine');?></th>
                            <th><?php echo CommonLanguage::getMessage('part');?></th>
                            <th><?php echo  CommonLanguage::getMessage('next_schedule_date');?></th>
                            <th><?php echo  CommonLanguage::getMessage('questions');?></th>
                            <th><?php echo  CommonLanguage::getMessage('answers');?></th>
                        </thead>
                        <tbody></tbody>
                    </table>
                </div>
            </div>
            <?php echo Forms::ModalLoader('missedChecklistLoader'); ?>
        </div>
    </div>
</div>
<script>
    var startDate = today, endDate = today;
    $(function(){
        build_select2($("#Product_ID"), "xhr/MachinePartSearch?type=1");
        build_select2($("#section_id"), "xhr/CommonSearch?col_name=section_name&col_id=section_id&table=tbl_product_sections");
        build_select2($("#line_id"), "xhr/CommonSearch?col_name=line_name&col_id=line_id&table=tbl_line");
        RebuildDataTable($('.panel-heading .panel-title').html(), [0,1,2,3,4,5,6], 'MaintenanceAjax/getMissedScheduledChecklist',{Product_ID:$("#Product_ID").val(),start_date:startDate,end_date:endDate,section_id:$("#section_id").val(),line_id:$('#line_id').val()});
        $('#current_date').daterangepicker({locale: {
            format: 'YYYY-MM-DD'
        }, ranges: {
            'Today': [moment(), moment()],
            'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
            'Last 7 Days': [moment().subtract(6, 'days'), moment()],
            'Last 30 Days': [moment().subtract(29, 'days'), moment()],
            'This Month': [moment().startOf('month'), moment().endOf('month')],
            'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
        }},function (start, end, label) {
            window.startDate = start.format("YYYY-MM-DD");
            window.endDate = end.format("YYYY-MM-DD");
            RebuildDataTable($('.panel-heading .panel-title').html(), [0,1,2,3,4,5,6], 'MaintenanceAjax/getMissedScheduledChecklist',{Product_ID:$("#Product_ID").val(),start_date:startDate,end_date:endDate,section_id:$("#section_id").val(),line_id:$('#line_id').val()});
        });
        $("#Product_ID,#section_id,#line_id").on('change',function(){
            RebuildDataTable($('.panel-heading .panel-title').html(), [0,1,2,3,4,5,6], 'MaintenanceAjax/getMissedScheduledChecklist',{Product_ID:$("#Product_ID").val(),start_date:startDate,end_date:endDate,section_id:$("#section_id").val(),line_id:$('#line_id').val()});
        });
        $('#tableWithSearch').on('click','input[name=answer_schedule]',function(){
            $("#missedChecklistLoader").removeClass('hidden');
            $.ajax({
                url: "MaintenanceAjax/submitDefaultScheduleAnswers",
                method: "POST",
                data: {schedule_id:$(this).attr('data-id')},
                dataType: "JSON",
                complete: function (jqXHR, status) {
                    if (status == 'success') {
                        if (jqXHR.responseJSON[1] == "success") {
                            RebuildDataTable($('.panel-heading .panel-title').html(), [0,1,2,3,4,5,6], 'MaintenanceAjax/getMissedScheduledChecklist',{Product_ID:$("#Product_ID").val(),start_date:startDate,end_date:endDate,section_id:$("#section_id").val(),line_id:$('#line_id').val()});
                        }
                    }
                    $("#missedChecklistLoader").addClass('hidden');
                }
            });
        });
        $('#print_missed_checklist').on('click',function(){
            var data={Product_ID:$("#Product_ID").val(),start_date:startDate,end_date:endDate,
                      section_id:$("#section_id").val(),line_id:$('#line_id').val()}
            $.ajax({
                url: "MaintenanceAjax/missedChecklistScheduleData",
                data: data,
                complete: function (v1, v2) {
                    if (v2 == "success") {
                        $('#data_d').html(v1.responseText)
                        var divToPrint = v1.responseText;
                        $("#current_table_data").DataTable().destroy();
                        $("#current_table_data tbody").empty().html(v1.responseText);
                        RebuildDataTable($('.panel-heading .panel-title').html(), [], "", {}, "#current_table_data",[0,1,2],{paging:false});
                        $('#current_table_data .filter_columns_row').remove();
                        var divToPrint = document.getElementById('current_table_data');
                        var htmlToPrint =  '<style>.text-center{text-align:center;}.table-bordered{width:100% !important;},.table-bordered>thead>tr>th{border: 1px solid rgba(230,230,230,0.7) !important;}.table-bordered>tbody>tr>td{border: 1px solid rgba(230,230,230,0.7);text-align:center;}.table{width:100%;border-spacing:0;border-collapse: collapse;}.table>thead>tr>th{border: 1px solid rgba(230,230,230,0.7) !important;}</style><h4 class="text-center">Maintenance Schedule</h4><h4 class="text-center">'+$('#current_date').val()
                        +'</h4>'+divToPrint.outerHTML;
                        newWin = window.open("");
                        newWin.document.write(htmlToPrint);
                        newWin.print();
                        newWin.close();
                    }
                }
            })
        });
    });
</script>