
<style type="text/css">
    .emp_row{background:#4c0870;margin-top:20px;border:1px solid #000}
    .row_exist{background:lightsteelblue;border:1px solid #000}
    .row_new{background:whitesmoke;border:1px solid #000;}
    .row_no_attendence{background:#ccc;border:1px solid #000;}
    .row_error{border:4px solid #f00;}

</style>
<div class="wrapper">
    <div class="panel ">
        <div class="panel-body">
            <div class="row">
                <h3><?php print CommonLanguage::getMessage('non_att_emp_dates'); ?></h3>
                <div id="alertdiv"></div>
                <div class="col-md-12 p-b-15">
                    <div class="col-md-12">
                        <div class="col-md-3">
                            <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">
                            <label><?php echo CommonLanguage::getMessage('employee_name') ?></label><span class="red-star">*</span>
                            <input type="hidden" id="employee_select_id" name="employee_select_id" class='full-width employee_select_id'/>


                        </div>

                        <div class="col-md-3  ">
                            <label><?php echo CommonLanguage::getMessage('Date') ?></label>
                            <input type="text" id="EmployeeDate" class="form-control EmployeeDate" name="EmployeeDate" />
                        </div>
                        <div class="col-md-3  ">
                            <label><?php echo CommonLanguage::getMessage('days_nb_greater') ?></label>
                            <input type="number" id="day_count" class="form-control day_count" name="day_count" />
                        </div>

                    </div>

                    <div class="col-md-12">


                        <div class='col-md-2 m-t-25 pull-right'>
                            <button class='btn btn-success full-width' id='search_attendance'><?php echo CommonLanguage::getMessage('search') ?></button>
                        </div>
                    </div>
                </div>
                <div id="att-alert" class="alert alert-warning col-md-12 hidden" style="margin: 0px;"><?php echo CommonLanguage::getMessage('att_alert') ?></div>
                <div class="col-md-12" id='AttendanceSetting'>


                </div>

                <div class="col-md-12 p-t-10">
                    <button class="btn btn-success pull-right" disabled="disabled" id='submitdata'><?php echo CommonLanguage::getMessage('Save') ?></button>
                </div>

            </div>
        </div>
    </div>
</div>
<select id="absence_select" class="hidden">
    <option value=""><?php echo CommonLanguage::getMessage('select') ?></option>
    <?php foreach ($absence as $ab) { ?>
        <option value="<?php echo $ab['Absence_Reason_ID'] ?>" data-type="abs"><?php echo $ab['Absence_Reason_Name'] ?></option>
    <?php } ?>
        <option  data-type="leave"><?php echo CommonLanguage::getMessage('leave') ?></option>

</select>

<script>
    var emp_id, from_date, to_date, dep_id;
    var Submitted_Successfully = <?php echo json_encode(CommonLanguage::getMessage('Submitted_Successfully')); ?>,
            Required_Fields = <?php echo json_encode(CommonLanguage::getMessage('Required_Fields')); ?>,
            leave_lang = <?php echo json_encode(CommonLanguage::getMessage('accrued_leave')); ?>,
            absence_lang = <?php echo json_encode(CommonLanguage::getMessage('absence')); ?>,
            Failed_Submit = <?php echo json_encode(CommonLanguage::getMessage('Failed_Submit')); ?>;
    var is_actual = <?php echo json_encode($is_actual); ?>;
    $(function () {

        build_select2($('.department_id'), 'HRMAjax/searchDepartment');
        build_select2($('.employee_select_id'), 'xhr/employeeSearch?is_actual=' + is_actual + '&with_id=1', 1);
//        build_select2($('.lcoation_id'), 'xhr/locationSearch');
        $('.to_time,.from_time').datetimepicker({format: 'YYYY-MM-DD HH:mm', useCurrent: false, extraFormats: ['DD-MM-YYYY']});
        $('#EmployeeDate').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")
        });



        $("#AttendanceSetting").on("click", " .Setting >.fa-plus-circle", function (e) {
            var is_emp = $(this).hasClass('addFromEmployeeRow');
            var Parent = $(this).closest(is_emp ? ".emp_row" : ".AttendanceRowClass");
            var data_id = Parent.attr('data-id');
            var row = '';
            row += '<div class="col-md-12 p-t-10 p-b-10 AttendanceRowClass row_new" data-id="' + data_id + '">' +
                    '<div class="col-md-3">' +
                    '<input type="text"  name="att_date" value="" class="form-control att_date"/>' +
                    '</div>' +
                    '<div class="col-md-3">' +
                    '<select class="off_type full-width" data-init-plugin="select2" >' + ($("#absence_select").html()) + '</select>' +
                    '</div>' +
                    '<div class="col-md-2 Setting">' +
                    '<span class="fa fa-plus-circle" style="color: #4c0870 ;margin-left: 10px;cursor: pointer;font-size: 16px;"></span><span class="fa fa-minus-circle" style="color: #ed9421;margin-left: 5px;cursor: pointer;font-size: 16px;display:"none"  ></span>' +
                    '</div>' +
                    '</div>';
            //
            row = $(row).insertAfter(Parent);
            row.find(".att_date").datepicker({autoclose: true})
            row.find("select.off_type").select2()

        });
        $("#AttendanceSetting").on("click", ".Setting >.fa-minus-circle", function (e) {
            $(this).closest(".AttendanceRowClass").empty().remove();
//            $("#AttendanceSetting .AttendanceRowClass").each(function () {
//                $(".AttendanceRowClass .Setting>.fa-minus-circle", $(this).parent()).css({"display": $(".AttendanceRowClass .Setting", $(this).parent()).length <= 1 ? "none" : "inline-block"})
//            });
        });
        $('#search_attendance').on('click', function () {
            $("#alertdiv").addClass('hidden');
            emp_id = $('#employee_select_id').val();
            from_date = (window.startDate ? window.startDate : today);
            to_date = (window.endDate ? window.endDate : today);
            dep_id = $('#department_id').val();
            $.ajax({
                url: 'HRMAjax/returnNonAttForEmp?from_date=' + from_date + '&to_date=' + to_date + '&emp_id=' + emp_id + '&dep_id=' + dep_id+'&day_count='+($("#day_count").val()||0),
                dataType: 'JSON',
                complete: function (v1, v2) {
                    if (v2 == 'success' && v1.responseJSON != 'error') {
//                        return false;
                        var row = '';
                        var data = v1.responseJSON;
                        for (var i in data) {
                            var emp_row = data[i];
                            var emp_id = i
                            var emp_html = '<div class="col-md-12 emp_row" data-id="' + emp_id + '">' +
                                    '<div class="col-md-6 text-right">' + emp_row.Name + '</div>' +
                                    '<div class="col-md-2 Setting"><span class="fa fa-plus-circle addFromEmployeeRow" style="color: #4c0870 ;margin-left: 10px;cursor: pointer;font-size: 16px;"></span></div>' +
                                    '</div>';
                            //
//                            console.log(emp_row.dates.length);
//                            if (emp_row['dates'].length > 0) {
//                                                                    console.log('heree')

                            for (var dt in emp_row['dates']) {
                                var data_rows = emp_row['dates'][dt];
                                //
                                row += emp_html;
                                emp_html = '';
                                //
                                var row_class = 'row_exist';


                                row += '<div class="col-md-12 p-t-10 p-b-10 AttendanceRowClass ' + row_class + '" data-id="' + emp_id + '">';
//                                                if (j == 'no_attendence') {
//                                                    row += '<div class="col-md-3">' + dt + '</div>' +
//                                                            '<div class="col-md-3">' + data_row + '</div>';
//                                                } else {
                                row +=
//                                                         
                                        '<div class="col-md-3">' +
                                        '<input type="text"  name="att_date" value="' + (data_rows == null ? '' : data_rows) + '" class="form-control att_date"/>' +
                                        '</div>' +
                                        '<div class="col-md-3">' +
                                        '<select class="off_type full-width" data-init-plugin="select2" >' + ($("#absence_select").html()) + '</select>' +
                                        '</div>';
//                                                }
                                row +=
                                        '<div class="col-md-2 Setting">' +
                                        '<span class="fa fa-plus-circle" style="color: #4c0870 ;margin-left: 10px;cursor: pointer;font-size: 16px;"></span>' +
                                        ('<span class="fa fa-minus-circle" style="color: #ed9421;margin-left: 5px;cursor: pointer;font-size: 16px;"  ></span>') +
                                        '</div>' +
                                        '</div>';
                            }
//                            } else {
//                                row += emp_html;
//                                emp_html = '';
//                            }
                        }

                        if (row == '') {
                            $('#submitdata').attr('disabled', 'disabled');
                        } else
                            $('#submitdata').removeAttr('disabled');
                        row = $(row).appendTo($('#AttendanceSetting').empty());
                        row.find(".att_date").datepicker({autoclose: true})
                        row.find("select.off_type").select2()
//                        build_select2($("input.location_id", $(row)), "xhr/CommonSearch?col_id=Location_ID&col_name=Location_Name&table=tbl_employee_location")

                    }


                }

            });
        });
    });

    $('#submitdata').on('click', function () {
        var is_empty = false;
        var Data = {
            emp_arr: []
        };

        $('.AttendanceRowClass:not(.row_no_attendence)').each(function () {
            var me = $(this);
            if ($("select.off_type", me).val() != '') {
                var data_row = {
                    Employee_ID: me.attr('data-id'),
                    Date_From: $(".att_date", me).val(),
                    Date_To: $(".att_date", me).val(),
                    type_id: $("select.off_type", me).val(),
                    type: $("select.off_type >option:selected", me).attr("data-type")
                };

                Data.emp_arr.push(data_row);
            }
        });
        
        $.ajax({
            url: 'HRMAjax/insertNonAttForEmp',
            data: Data,
            type: 'POST',
            complete: function (v1, v2) {
                if (v2 == 'success' && v1.responseText == 'OK') {
                    $('#alertdiv').attr('class', ' alert alert-success').empty().html(Submitted_Successfully);
                } else {
                    $('#alertdiv').attr('class', ' alert alert-danger').empty().html(v1.responseText);
                }
                $(document.body).animate({scrollTop: 0}, 800)
            }
        });

    });
</script>

