<?php
include("Views/HRM/CreateAbsenceSetting.php");
?>
<style>
    .dropdown-toggle {
        background-color:#4c0870;
        border-color : #4c0870;
    }
    .dropdown-toggle:hover{
        background-color:#4c0870;
        border-color : #4c0870;
    }
    .table thead tr >th{
        background-color:#4c0870;
        color:#fff;
        width: 16% !important;
    }
    .table thead tr th[class*='sorting_'] {
        color: #fff;
    }
    .bg-master-lighter {
        border:none;
        background-color:#fff;
        border-top:1px solid #ddd;  
    }
    .btn-default{
        border: none;
        background-color: #eee;
    }
    .btn-default:hover{
        background-color:#4c0870!important;
        color:#fff;
        border: none;
    }
    .table-responsive {
        overflow-x: visible; 
    }
    .dropdown-menu>li>a:hover, .dropdown-menu>li>a:focus{
        background-color:#4c0870!important ;
        color:#fff;
    }
    .dataTables_wrapper .dataTables_paginate .paginate_button.current, .dataTables_wrapper .dataTables_paginate .paginate_button.current:hover,.dataTables_wrapper .dataTables_paginate .paginate_button:hover{
        color:#fff !important;
        border-radius: 50%;
        background: #4c0870 !important;
        border-color: #4c0870
    }
</style>
<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('absence_setting_list'); ?></div>
            <div class="clearfix"></div>

            <?php
            if ($permissions->hasPermission('AddAbsenceSetting')) {
                echo Forms::FloatingAddButton('4c0870', '#CreateAbsenceSetting', CommonLanguage::getMessage('add'));
            }
            ?>
        </div>
        <div class="panel-body">
            <table class="table table-bordered table-hover demo-table-search" id="AbsenceSettingList">
                <thead>
                <th  class="text-center"><?php print CommonLanguage::getMessage('absence_reason_name'); ?></th>
                <th  class="text-center"><?php print CommonLanguage::getMessage('day_number'); ?></th>

                <?php if ($permissions->hasPermission('DeleteAbsenceSetting') || $permissions->hasPermission('EditAbsenceSetting')) { ?>
                    <th class="text-center"><?php print CommonLanguage::getMessage('absence_setting_list'); ?></th>
                <?php } ?>

                </thead>
                <tbody>

                </tbody>
            </table>
        </div>

    </div>
</div>
<div class="modal fade bs-example-modal-md" id="DeleteAbsenceFromList" 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="DeleteAbsence_modalAlert" class="alert alert-success"></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('about_delete_absence'); ?> <strong class="Name"></strong> <strong class="Days">  </strong> </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="Absence_Delete" style="width:auto"><?php print CommonLanguage::getMessage('Save'); ?></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('absence_setting_list')) ?>

    ;
    var CurrentAbsence_ToDelete;
    $(function () {

        RebuildDataTable(title, [0], 'HRMAjax/AbsenceSettingRequest', '', '#AbsenceSettingList');


        $(".floatingContainer ").on("click", function () {
            $("#AbsenceSetting  .Setting >.fa-plus-circle").removeClass("hidden")
            $("input[type=checkbox]").prop("checked", false);
        })
        $("#AbsenceSettingList ").on("click", ".Action .Edit", function () {
            CurrentAbsence_ToDelete = $(this).parents('div.Action').attr('data-id');
            $("#CreateAbsenceSetting").modal('show')
            $("#AbsenceSetting  .Setting >.fa-plus-circle").addClass("hidden")
            $("#IsEdit").val(1)
            $.ajax({
                type: 'POST',
                url: 'HRMAjax/AbsenceSettingRequest',
                data: {EditID: CurrentAbsence_ToDelete},
                complete: function (data, vv) {
                    if (vv == "success") {
                        var Data = $.parseJSON(data.responseText);
//                        console.log(Data);
                        $("#AbsenceSetting .Setting").find(".Absence_Reason_Name").val(Data[0]['Absence_Reason_Name'])
                        $("#AbsenceSetting .Setting").find(".Absence_Reason_Days").val(Data[0]['Absence_Reason_Days'])
                        if (Data[0]['exceed_days'] == 1)
                            $("#AbsenceSetting .Setting").find(".exceed_days").prop('checked', true)
                        else
                            $("#AbsenceSetting .Setting").find(".exceed_days").prop('checked', false)

                    }
                }
            });
        })

    })



















    $("#AbsenceSettingList").on("click", ".Action .Delete", function () {
        var AbsenceName = $(this).parents("tr").find("td:eq(0)").text()//parent tr keep searching till it reach tr 
        var AbsenceDays = $(this).parents("tr").find("td:eq(1)").text()//parent tr keep searching till it reach tr 
        $("#DeleteAbsenceFromList").find(".Name").text(AbsenceName).end().modal("show") // to set the name of employee in modal to delete 
        $("#DeleteAbsenceFromList").find(".Days").text("has " + AbsenceDays + " Days").end().modal("show") // to set the name of employee in modal to delete 
        CurrentAbsence_ToDelete = $(this).parents("tr").find('.Action').attr('data-id') // current id to delete
    })
    $("#DeleteAbsence_modalAlert").hide(); // hide the alert when submit if true or false
    //on click the delete button in modal get the id of the selected employee to delete it 
    $("#Absence_Delete").on("click", function () {
        $.ajax({
            type: 'POST',
            url: 'HRMAjax/AbsenceSettingRequest',
            data: {GetID: CurrentAbsence_ToDelete},
            complete: function (data, vv) {
                if (vv == "success" && data.responseText == "OK") {
                    RebuildDataTable(title, [0], 'HRMAjax/AbsenceSettingRequest', '', '#AbsenceSettingList');
                    $("#DeleteAbsence_modalAlert").attr("class", "alert alert-success").html(Submitted_Successfully).show();
                    setTimeout(function () {
                        $("#DeleteAbsenceFromList").modal("hide")
                        $("#DeleteAbsence_modalAlert").remove()
                    }, 1000)
                } else {
                    $("#DeleteAbsence_modalAlert").show().empty().removeClass('alert-success').addClass('alert-danger').html(Failed_Submit);
                }
            }});
    })

</script>