<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('manage_emp_leaves'); ?></div>
        </div>
        <div class="panel-body">
            <form id="leave_form" action="javascript:void(0);">
                <div class="row form-group">

                </div>
            </form>
            <div class="row m-b-10 form-group">
                <div class="col-md-3">
                    <label><?php echo CommonLanguage::getMessage('job') ?></label>
                    <input type="hidden" id="job_filter" class="full-width"/>
                </div>
                <div class="col-md-3">
                    <label><?php echo CommonLanguage::getMessage('department') ?></label>
                    <input type="hidden" id="dep_filter" class="full-width"/>
                </div>
                <div class="col-md-3">
                    <label><?php echo CommonLanguage::getMessage('shift') ?></label>
                    <input type="hidden" id="shift_filter" class="full-width"/>
                </div>
                <div class="col-md-3">
                    <button class="btn btn-success m-t-25" id="copy_rem"><?php echo CommonLanguage::getMessage('copy_rem_previous') ?></button>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12 table-responsive">
                    <table class="table table-bordered hrm" id="tableWithSearch" >
                        <thead>
                            <tr>
                                <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('Name'); ?></th>
                                <th class="text-center" style="width:15%" ><input style="width: 90%;" type="number" class="main_previous form-control"  placeholder="<?php print CommonLanguage::getMessage('previous_days_nb'); ?>" min="0" id="total_amount" /></th>
                                <?php if ($permissions->hasPermission('change_fixed_holiday')) { ?>
                                    <th class="text-center no-print"  ><input style="width: 90%;" type="checkbox" class="select_all_emp"/></th>
                                    <th class="text-center" style="width:15%" ><input style="width: 90%;" type="number" class="main_holiday form-control"  placeholder="<?php print CommonLanguage::getMessage('holiday_nb'); ?>" min="0" id="total_amount" /></th>
                                <?php } ?>
                                <th class="text-center" style="width:15%" ><input style="width: 90%;" type="number" class="main_leave form-control" placeholder="<?php print CommonLanguage::getMessage('leave_days_nb'); ?>" min="0" id="total_amount" /></th>
                                <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('remaining_days'); ?></th>

                                <!--<th class="text-center no-print" style="width:15%"  ><input type="number" class="total_amount form-control" name="total_amount" min="0" id="total_amount" onkeyup="applyMainAmount(this)" onchange="applyMainAmount(this)"/></th>-->
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                        <tfoot>
                        <th></th>
                        <th></th>
                        <?php if ($permissions->hasPermission('change_fixed_holiday')) { ?>
                            <th></th>
                            <th></th>
                        <?php } ?>
                        <th></th>
                        <th><button class="btn btn-success full-width" id="submit_all"><?php echo CommonLanguage::getMessage('Save'); ?></button></th>
                        </tfoot>
                    </table>
                </div>
            </div>
            <?php echo Forms::ModalLoader('leave_loader'); ?>
        </div>
    </div>
</div>
<script>
    $(function () {
        build_select2($("#job_filter"), 'xhr/jobSearch')
        build_select2($("#dep_filter"), 'HRMAjax/searchDepartment')
        build_select2($("#shift_filter"), 'xhr/searchshift')
        buildCurrentTable();
        $("#tableWithSearch").on("click", "input.select_all_emp", function (e) {
            var is_checked = $(this).is(":checked");
            var row = $("#tableWithSearch").dataTable().fnGetNodes();
            for (var i = 0; i < row.length; i++) {
                $(row[i]).find("input.is_fixed").prop("checked", is_checked);
            }
        });
        $("#submit_all").on("click", function () {

            var emp_arr = [], row = $("#tableWithSearch").dataTable().fnGetNodes();

            for (var i = 0; i < row.length; i++) {
                if ($(row[i]).find('input.previous').val() != '' && $(row[i]).find('input.leave').val() != '') {
                    emp_arr.push({
                        Employee_ID: $(row[i]).find('input.previous').attr("data-id"),
                        remaining: $(row[i]).find('input.remaining').val(),
                        previous: $(row[i]).find('input.previous').val(),
                        leave: $(row[i]).find('input.leave').val(),
                        holiday: $(row[i]).find('input.holiday').val(),
                        is_fixed: $(row[i]).find('input.is_fixed').is(":checked") ? 1 : 0
                    });
                }

            }
            $('#leave_loader').removeClass('hidden');
            $.ajax({
                url: "HRMAjax/update_multi_emp_leave_setting",
                data: {emp_data: emp_arr},
                method: "POST",
                complete: function (v1, v2) {
                    if (v2 == "success") {
                        if (v1.responseText === 'OK') {
                            toastr.success('<?php echo CommonLanguage::getMessage('Submitted_Successfully'); ?>')
                            buildCurrentTable();
                        }
                        $('#leave_loader').addClass('hidden');
                    }
                }
            })
        })
        $(".main_previous").on("input", function () {
            applyMainAmount($(this).val(), 'previous')
        })
        $(".main_holiday").on("input", function () {
            applyMainAmount($(this).val(), 'holiday')
        })
        $(".main_leave").on("input", function () {
            applyMainAmount($(this).val(), 'leave')
        })
        $("#tableWithSearch tbody").on("click", ".is_fixed", function () {
            if ($(this).is(":checked")) {
                $(this).parents("tr").find(".holiday").removeAttr("readonly")

            } else {
                $(this).parents("tr").find(".holiday").attr("readonly", "readonly")
            }
        })
        $("#tableWithSearch tbody").on("input", ".leave,.holiday,.previous", function () {
            var tr = $(this).parents("tr"),
                    holiday = parseFloat($(".holiday", tr).val() || 0),
                    leave = parseFloat($(".leave", tr).val() || 0),
                    previous = parseFloat($(".previous", tr).val() || 0),
                    remaining = previous + holiday + leave;
            $(".remaining", tr).val(remaining)

        })
        $("#job_filter,#shift_filter,#dep_filter").on("change", function () {
            buildCurrentTable()
        })
        $("#copy_rem").on("click", function () {
            var row = $("#tableWithSearch").dataTable().fnGetNodes();
            for (var i = 0; i < row.length; i++) {
                $(row[i]).find('input.previous').val($(row[i]).find('input.remaining').val()).trigger("input")
            }
            $(this).attr("disabled","disabled")
        })
    });
    function buildCurrentTable() {
        var data = {
            job_id: $("#job_filter").val(),
            dep_id: $("#dep_filter").val(),
            shift_id: $("#shift_filter").val(),
        };
        $.ajax({
            url: "HRMAjax/manage_emp_leaves",
            data: data,
            complete: function (v1, v2) {
                if (v2 == "success") {
                    $("#tableWithSearch").DataTable().destroy();
                    $("#tableWithSearch tbody").empty().html(v1.responseText)
                    RebuildDataTable($('.panel-heading .panel-title').html(), [], "", {}, "#tableWithSearch");
                }
            }
        });
    }
    function applyMainAmount(current_input, input_class) {
        var row = $("#tableWithSearch").dataTable().fnGetNodes();
        for (var i = 0; i < row.length; i++) {
            if ((input_class == 'holiday' && $(row[i]).find(".is_fixed").is(":checked")) || input_class != 'holiday')
                $(row[i]).find("input." + input_class).val(current_input ? current_input : '');
            $(row[i]).find("input." + input_class).trigger("input");
        }

    }
</script>
