<style type="text/css">


    .table thead tr >th{
        background-color: #4c0870 ;
        color:#fff;
    }
    .table thead tr th[class*='sorting_'] {
        color: #FFFFFF;
    }
</style>

<div class="wrapper">
    <div class="panel ">
        <div class="panel-body">
            <h3><?php print CommonLanguage::getMessage('interior_ministry_report'); ?></h3>
            <div id="alertdiv"></div>
            <div class="col-md-12 p-b-15">
                <div class="row">
                    <div class="col-md-12">
                        <div class="col-md-3 form-group" style="margin-left:-8px;margin-bottom:10px;">
                            <label><?php echo CommonLanguage::getMessage('Date') ?></label>
                            <div class="input-group  col-md-12 " >
                                <input id="From_To_Date" type="text" autocomplete="off" class="form-control" name="From_To_Date" >
                            </div> 
                        </div>
                        <div class="col-md-3 form-group" style="margin-left:-8px;margin-bottom:10px;">
                            <label><?php echo CommonLanguage::getMessage('type') ?></label>
                            <div class="input-group  col-md-12 " >
                                <select id="type" class="full-width" data-init-plugin="select2" name="type" >
                                    <option value="0"><?php echo CommonLanguage::getMessage('current') ?></option>
                                    <option value="1"><?php echo CommonLanguage::getMessage('new') ?></option>
                                    <option value="2"><?php echo CommonLanguage::getMessage('leave') ?></option>
                                </select>
                            </div> 
                        </div>
                        <div class="col-md-3 form-group">
                            <label><?php echo CommonLanguage::getMessage('departments') ?></label>
                            <input type="hidden" id="department_id" class="full-width department_id" name="department_id" />
                        </div>
                        <div class="col-md-3 form-group">
                            <label><?php echo CommonLanguage::getMessage('nssf') . ' ?' ?></label>
                            <select id="is_nssf" class="full-width" data-init-plugin="select2" name="is_nssf" >
                                <option value=""><?php echo CommonLanguage::getMessage('All') ?></option>
                                <option value="1"><?php echo CommonLanguage::getMessage('yes') ?></option>
                                <option value="0"><?php echo CommonLanguage::getMessage('no') ?></option>
                            </select>
                        </div>
                        <!--                        <div class="col-md-3 form-group" style="margin-left:-8px;margin-bottom:10px;">
                                                    <label><?php echo CommonLanguage::getMessage('location') ?></label><span class="red-star">*</span>
                                                    <div class="input-group  col-md-12 " >
                                                        <input id="location" type="hidden"  class="full-width" name="location" >
                                                    </div> 
                                                </div>
                                                <div class="col-md-3 form-group" style="margin-left:-8px;margin-bottom:10px;">
                                                    <label><?php echo CommonLanguage::getMessage('employee') ?></label><span class="red-star">*</span>
                                                    <div class="input-group  col-md-12 " >
                                                        <input id="employee" type="hidden"  class="full-width" name="employee" >
                                                    </div> 
                                                </div>-->



                    </div>

                </div>
            </div>

            <div class="col-md-12 p-b-15">
                <div class="col-md-4 form-group">
                    <label><?php echo CommonLanguage::getMessage('saved_reports') ?></label>
                    <input type="hidden" id="saved_reports" class="full-width saved_reports" name="saved_reports" />
                </div>
            </div>
            <div class="col-md-12">
                <div class="row">
                    <div class="table-responsive">

                        <table class="table table-bordered" id="tableWithSearch">
                            <thead>
                                <tr>
                                    <th class="text-center"><?php echo CommonLanguage::getMessage('employee') ?></th>
                                    <th class="text-center"><?php echo CommonLanguage::getMessage('location') ?></th>
                                    <th class="text-center"><?php echo CommonLanguage::getMessage('start_date') ?></th>
                                    <th class="text-center"><?php echo CommonLanguage::getMessage('job') ?></th>
                                    <th class="text-center"><?php echo CommonLanguage::getMessage('shift') ?></th>
                                    <th class="text-center"><?php echo CommonLanguage::getMessage('address') ?></th>
                                    <th class="text-center"><?php echo CommonLanguage::getMessage('mobile_phone') ?></th>
                                    <th class="text-center"><?php echo CommonLanguage::getMessage('Action') ?></th>
                                </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-3 pull-right">
                        <button class="btn btn-success full-width" id="submit_ministry_report" style="margin-top:25px;"><?php echo CommonLanguage::getMessage('Save'); ?></button>
                    </div>
                    <div class="col-md-3 pull-right">
                        <label><?php echo CommonLanguage::getMessage('title'); ?><span class="red-star">*</span></label>
                        <input type="text" class="form-control" id="title"/>
                    </div>

                </div>
            </div>    
        </div>
    </div>
</div>

<script>
    var info_lang = <?php echo json_encode(CommonLanguage::getMessage('info')); ?>,
            title = <?php echo json_encode(CommonLanguage::getMessage('att_location_report')); ?>,
            Submitted_Successfully = <?php echo json_encode(CommonLanguage::getMessage('Submitted_Successfully')); ?>,
            Failed_Submit = <?php echo json_encode(CommonLanguage::getMessage('Failed_Submit')); ?>,
            startDate =<?php echo json_encode($today); ?>, endDate =<?php echo json_encode($today); ?>,
            today = <?php echo json_encode($today) ?>;
    $(function () {

        build_select2($('#department_id'), 'HRMAjax/searchDepartment');
        build_select2($('#saved_reports'), 'xhr/ministry_report_search');

        build_table()
//                build_select2($('#employee'), 'xhr/employeeSearch?is_actual=1');
//
//                    build_select2($("#location"), "xhr/CommonSearch?col_id=Location_ID&col_name=Location_Name&table=tbl_employee_location")

        $("input[name=From_To_Date]").daterangepicker({
            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')]
            }, locale: {
                cancelLabel: 'Clear'
            }, allowEmpty: true,
            //            autoApply: true,
//            autoUpdateInput: false,
        }, function (start, end, label) {
            window.startDate = start.format("YYYY-MM-DD"),
                    window.endDate = end.format("YYYY-MM-DD")
        });

        $("input[name=From_To_Date],#type,#department_id,#is_nssf").on("change", function () {
            build_table()
        })

        $("#tableWithSearch").on("click", ".delete", function () {
            var table = $('#tableWithSearch').dataTable();
            table.fnDeleteRow($(this).parents("tr"));
//            $(this).parents("tr").empty().remove()
        })
        $("#saved_reports").on("change", function () {
            $.ajax({
                url: "HRMAjax/interior_ministry_saved_report_ajax",
                data: {id: $(this).val()},
                dataType: 'JSON',
                complete: function (v1, v2) {
                    if (v2 == 'success') {
                        $("#tableWithSearch").DataTable().destroy();
                        $("#tableWithSearch tbody").empty().html(v1.responseJSON['html'])
                        RebuildDataTable($('.panel-heading .panel-title').html(), [], "", {}, "#tableWithSearch");
                    }
                }
            });
        })
        $("#submit_ministry_report").on("click", function () {
            var data = {
                from_date: (window.startDate ? window.startDate : today),
                to_date: (window.endDate ? window.endDate : today),
                title: $("#title").val(),
                emp_data: []};
            var row = $("#tableWithSearch").dataTable().fnGetNodes();
            for (var i = 0; i < row.length; i++) {
                data.emp_data.push({
                    employee_name: $("td:eq(0)", $(row[i])).text(),
                    location_address: $("td:eq(1)", $(row[i])).text(),
                    start_date: $("td:eq(2)", $(row[i])).text(),
                    job: $("td:eq(3)", $(row[i])).text(),
                    shift_note: $("td:eq(4)", $(row[i])).text(),
                    employee_address: $("td:eq(5)", $(row[i])).text(),
                    mobile_phone: $("td:eq(6)", $(row[i])).text(),
                })
            }
            if ($("#title").val() == '' || data.emp_data == 0) {
                $("#alertdiv").attr('class', 'alert alert-danger').html(Required_Fields)
                $(document.body).animate({scrollTop: 0}, 800)
                return;
            }
            $.ajax({
                url: "HRMAjax/save_interior_ministry_report",
                data: data,
                method: 'POST',
                complete: function (v1, v2) {
                    console.log(v2)
                    if (v2 == 'success') {
                        if (v1.responseText == 'OK') {
                            $("#alertdiv").attr('class', 'alert alert-success').html(Submitted_Successfully)
                            $("#title").val("")
                            setTimeout(function () {
                                $("#alertdiv").addClass("hidden")
                            }, 1500)
                        } else {
                            $("#alertdiv").attr('class', 'alert alert-danger').html(Failed_Submit)

                        }
                    } else {
                        $("#alertdiv").attr('class', 'alert alert-danger').html(Failed_Submit)

                    }
                    $(document.body).animate({scrollTop: 0}, 800)
                }
            })
        })


    });
    function build_table() {
        var data = {
            from_date: (window.startDate ? window.startDate : today),
            to_date: (window.endDate ? window.endDate : today),
            type: $("#type").val(),
            dep_id: $('#department_id').val(),
            is_nssf: $("#is_nssf").val()
        }
        $.ajax({
            url: "HRMAjax/interior_ministry_report_ajax",
            data: data,
            dataType: 'JSON',
            complete: function (v1, v2) {
                if (v2 == 'success') {
                    $("#tableWithSearch").DataTable().destroy();
                    $("#tableWithSearch tbody").empty().html(v1.responseJSON['html'])
                    RebuildDataTable($('.panel-heading .panel-title').html(), [], "", {}, "#tableWithSearch");
                }
            }
        });
//        RebuildDataTable(title, [0], 'HRMAjax/interior_ministry_report_ajax', data)


    }
</script>