<?php
if ($permissions->hasPermission('addEmployee')) {
    include("Views/HRM/addEmployee.php");
    include("Views/HRM/addLocation.php");
    include("Views/HRM/addNationality.php");
}
?>
<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('employees'); ?></div>
            <div class="clearfix"></div>
            <div class="row form-group">
                <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_type') ?></label>
                    <select id="employee_type_filter" class="full-width" data-init-plugin="select2">
                        <option value=""><?php print CommonLanguage::getMessage('select'); ?></option>
                        <option value="1"><?php print CommonLanguage::getMessage('IS_Employee'); ?></option>
                        <option value="0"><?php print CommonLanguage::getMessage('not_IS_Employee'); ?></option>
                        <option value="2"><?php print CommonLanguage::getMessage('sign_emp'); ?></option>
                        <option value="3"><?php print CommonLanguage::getMessage('temp_emp'); ?></option>
                        <option value="5"><?php print CommonLanguage::getMessage('daily_emp'); ?></option>
                        <option value="4"><?php print CommonLanguage::getMessage('retired_emp'); ?></option>
                    </select>
                </div>
                <div class="col-md-3 pull-right">
                    <?php
                    if ($permissions->hasPermission('addEmployee')) {
                        echo Forms::FloatingAddButton('4c0870', '#Add_Employee', CommonLanguage::getMessage('add'));
                    }
                    ?>
                </div>
            </div>

        </div>
        <div class="panel-body">
            <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:10%" ><?php print CommonLanguage::getMessage('work_id'); ?></th>
                                <th class="text-center"  style="width:30%" ><?php print CommonLanguage::getMessage('Name'); ?></th>
                                <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('mobile_phone'); ?></th>
                                <th class="text-center" style="width:15%" ><?php print CommonLanguage::getMessage('Email'); ?></th>
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('start_date'); ?></th>
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('department'); ?></th>
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('job'); ?></th>
                                <th class="text-center" style="width:5%" ><?php print CommonLanguage::getMessage('Action'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<!--</div>-->
<div class="modal fade bs-example-modal-md" id="DeleteFromList" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
    <div class="modal-dialog" style="width:50%">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
                <h4 class="modal-title"><?php print CommonLanguage::getMessage('Confirm_Delete'); ?></h4>
                <div id="DeleteEmployee_modalAlert" ></div>
            </div>
            <div class="modal-body">
                <form>
                    <div class="row">
                        <div class="col-md-12 ">
                            <div class="col-md-12" style="margin-top: 20px;">
                                <p><?php print CommonLanguage::getMessage('delete_emp'); ?> <strong class="Name"></strong><?php print CommonLanguage::getMessage('delete_emp_hint'); ?> <span class="gender"></span> <?php print CommonLanguage::getMessage('cannot_access'); ?></p>
                                <p><?php print CommonLanguage::getMessage('Proceed_Delete'); ?></p>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal"><?php print CommonLanguage::getMessage('Close'); ?></button>
                <button type="button" class="btn btn-danger" id="Employee_Delete" style="width:auto"><?php print CommonLanguage::getMessage('delete'); ?></button>
            </div>
        </div>
    </div>
</div>
<script>
    var Submitted_Successfully = <?php echo json_encode(CommonLanguage::getMessage('Submitted_Successfully')); ?>,
            Failed_Submit = <?php echo json_encode(CommonLanguage::getMessage('Failed_Submit')); ?>,
            title = <?php echo json_encode(CommonLanguage::getMessage('employees')); ?>;
    var is_actual = <?php echo json_encode($is_actual); ?>;
    var is_new = <?php echo json_encode($is_new); ?>;
    var CuurentEmployee_ToDelete;
    $(function () {
        build_select2($('.department_id'), 'HRMAjax/searchDepartment');
        RebuildDataTable(title, [0, 1, 2, 3, 4, 5], 'HRMAjax/viewEmployeesRequest', {is_actual: is_actual, dep_id: $('input.department_id').val(), emp_type: $('#employee_type_filter').val()})
        $('input.department_id,#employee_type_filter').on("change", function () {
            RebuildDataTable(title, [0, 1, 2, 3, 4, 5], 'HRMAjax/viewEmployeesRequest', {is_actual: is_actual, dep_id: $('input.department_id').val(), emp_type: $('#employee_type_filter').val()})

        })

        if (is_new)
            $('#Add_Employee').modal('show');
        $("#tableWithSearch").on("click", ".copy_row", function () {
            var current_employee_id = $(this).attr('data-id');
            CommonViewById('HRMAjax/getEmployeeInfo', {employee_id: current_employee_id},
                    '#employeeLoader', function (response) {
                        var employee_data = response[0], Keys = Object.keys(response[0])
                        $.each(Keys, function (key, value) {
                            $('#' + value).val(employee_data[value])
                        });
                        console.log(employee_data);

                        $("#Employee_FirstName,#Employee_MiddleName,#Employee_LastName").val('');
                        $('input[name=Wife_Works][value=' + employee_data.Employee_Wife_SSNF + ']').prop('checked', true)
                        $('input[name=Employee_Wife_SSNF][value=' + employee_data.Employee_Wife_SSNF + ']').prop('checked', true)
                        $('input[name=Employee_Gender][value=' + employee_data.Employee_Gender + ']').prop('checked', true)
                        $('input[name=Merital_Status_ID][value=' + employee_data.Merital_Status_ID + ']').prop('checked', true)
                        $('input[name=IS_Employee][value=' + employee_data.IS_Employee + ']').prop('checked', true)
                        if (employee_data.Employee_Wife_Nationality != 0) {
                            $("#select.Employee_Wife_Nationality").val(employee_data.Employee_Wife_Nationality).select2();
                        }

                        $("#Employee_Location").val(employee_data.Employee_Location).select2();
                        $("#trans_unit_time").val(employee_data.trans_unit_time).select2();
                        $("#select.Employee_Country").val(184).select2();
                        $("#select.Employee_City").val(employee_data.Employee_City).select2();
                        $("#select.Employee_BloodGroup").val(employee_data.Employee_BloodGroup).select2();
                        $("#select.Employee_Nationality").val(employee_data.Employee_Nationality).select2();
                        $("#basic_salary_currency").val(employee_data.basic_salary_currency);
                        $("#CurrencyIDContainer .basic-cur-div").html(employee_data.basic_currency_name);
                        //
                        console.log(employee_data.Employee_Country);

                        $("#bank_id").val(employee_data.bank_id).attr('data-select-name', employee_data.bank_name);
                        build_select2($("#bank_id"), "HRMAjax/searchBanks");
                        $("#Branch_ID").val(employee_data.Branch_ID).attr('data-select-name', employee_data.Branch_Name);
                        build_select2($('#Branch_ID'), 'HRMAjax/searchDepartment');
                        $("#job_id").val(employee_data.Job_ID).attr('data-select-name', employee_data.Job_Name);
                        build_select2($('#job_id'), 'xhr/jobSearch');
                        $("#Shift_ID").val(employee_data.Shift_ID).attr('data-select-name', employee_data.shift_type);
                        build_select2($('#Shift_ID'), 'xhr/searchshift');
                        $("#period_id").val(employee_data.period_id).attr('data-select-name', employee_data.period_name);
                        build_select2($("#period_id"), "xhr/CommonSearch?col_id=period_id&col_name=period_name&table=tbl_hrm_basic_salary_periods");
                        $("#Paygrade_ID").val(employee_data.Paygrade_ID).attr('data-select-name', employee_data.Paygrade_Name);
                        build_select2($("#Paygrade_ID"), 'xhr/paygradeSearch?job_id=' + employee_data.job_id)

                    });
                    $("#employeeLoader").hide();

        });
        $("#tableWithSearch").on("click", ".Action .Delete", function () {
            var EmployeeName = $(this).parents("tr").find("td:eq(0)").text();
            $("#DeleteFromList").find(".Name").text(EmployeeName).end().modal("show"); // to set the name of employee in modal to delete 
            CuurentEmployee_ToDelete = $(this).parents("tr").find('.Action').attr('data-id'); // current id to delete
        });
        $("#tableWithSearch").on("click", ".print_emp", function () {
            var myurl = "HRM/printEmpDetails/" + $(this).parents("tr").find('.Action').attr('data-id');
            printExternal(myurl);
        });
        //on click the delete button in modal get the id of the selected employee to delete it 
        $("#Employee_Delete").on("click", function () {
            $.ajax({
                type: 'POST',
                url: 'HRMAjax/viewEmployeesRequest',
                data: {GetID: CuurentEmployee_ToDelete},
                complete: function (data, vv) {
                    if (vv == "success" && data.responseText == "OK") {
                        $("#DeleteEmployee_modalAlert").attr("class", "alert alert-success").html(Submitted_Successfully);
                        RebuildDataTable(title, [0, 1, 2, 3, 4, 5], 'HRMAjax/viewEmployeesRequest',{is_actual: is_actual, dep_id: $('input.department_id').val(), emp_type: $('#employee_type_filter').val()});
                        setTimeout(function () {
                            $("#DeleteFromList").modal("hide");
                            $("#DeleteEmployee_modalAlert").addClass('hidden');
                        }, 1000);
                    } else {
                        $("#DeleteEmployee_modalAlert").attr("class", "alert alert-danger").html(Failed_Submit);
                    }
                }
            });
        });
        if ($('#is_modal_shown').val() == 1)
            $('#Edit_Employee').modal('show');
    });
</script>