<div class="container-fluid mb-3">
    <div class="card bg-light-transparent">
       <div class="card-header bg-white">
            <h5>Staff Special Allowance Allocation List (<?php echo $year; ?>)</h5>
            <nav>
                <div class="nav nav-tabs" id="nav-tab" role="tablist">
                    <a class="nav-item nav-link active" id="nav-dressing-tab" data-toggle="tab" href="#nav-dressing" role="tab" aria-controls="nav-dressing" aria-selected="true">Special Allowance - Dressing</a>
                    <a class="nav-item nav-link" id="nav-gadget-tab" data-toggle="tab" href="#nav-gadget" role="tab" aria-controls="nav-gadget" aria-selected="false">Special Allowance - Gadget</a>
                </div>
            </nav>
        </div>
        <div class="tab-content" id="nav-tabContent">
            <div class="tab-pane fade show active" id="nav-dressing" role="tabpanel" aria-labelledby="nav-dressing-tab">
                <div class="card-body">
                    <div class="card no-shadow">
                        <div class="card-body">
                            <div class="row">
                                <div class="col mb-3 text-left">
                                    <h6>Staff List - Dressing</h6>
                                </div>
                                <div class="col mb-3 text-right">
                                    <div class="btn-group">
                                        <button class="btn btn-primary btn-sm" data-toggle="modal" data-target="#dressing_allocation"><i class="fa fa-plus"></i> Add Staff Allocation</button>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col table-responsive">
                                    <table class="table table-hover">
                                        <thead>
                                            <tr>
                                                <th>#</th>
                                                <th>Staff Name</th>
                                                <th>Department</th>
                                                <th>Designation</th>
                                                <th>Year</th>
                                                <th>Amount(RM)</th>
                                                <th>Action</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php if($dressing_allocation){ foreach ($dressing_allocation as $key => $value) { ?>
                                                <tr>
                                                    <td><?php echo $key+1; ?></td>
                                                    <td><?php echo $value->staff_name; ?></td>
                                                    <td><?php echo $value->department_name; ?> | <?php echo $value->branch_name; ?></td>
                                                    <td><?php echo $value->designation; ?></td>
                                                    <td><?php echo $value->year; ?></td>
                                                    <td><?php echo $value->amount; ?></td>
                                                    <td>
                                                        <div class="btn-group" id="edit_button_group_dressing" data-year="<?php echo $value->year; ?>" data-staff_id="<?php echo $value->staff_id; ?>">
                                                            <button class="btn btn-warning btn-sm text-white" data-toggle="modal" data-target="#edit_dressingAllocation" data-modal="<?php echo base64_encode(json_encode($value)); ?>" onclick="editDressingAllocation(this)" ><i class="fa fa-edit"></i></button>
                                                            <button class="btn btn-danger btn-sm" onclick="checkExistingClaim_dressing(<?php echo $value->id; ?>)"><i class="fa fa-trash"></i></button>
                                                        </div>
                                                    </td>
                                                </tr>
                                            <?php }} ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="tab-pane fade" id="nav-gadget" role="tabpanel" aria-labelledby="nav-gadget-tab">
                <div class="card-body">
                    <div class="card no-shadow">
                        <div class="card-body">
                            <div class="row">
                                <div class="col mb-3 text-left">
                                    <h6>Staff List - Gadget</h6>
                                </div>
                                <div class="col mb-3 text-right">
                                    <div class="btn-group">
                                        <button class="btn btn-primary btn-sm" data-toggle="modal" data-target="#gadget_allocation"><i class="fa fa-plus"></i> Add Staff Allocation</button>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col table-responsive">
                                    <table class="table table-hover">
                                        <thead>
                                            <tr>
                                                <th>#</th>
                                                <th>Staff Name</th>
                                                <th>Department</th>
                                                <th>Designation</th>
                                                <th>Year</th>
                                                <th>Amount(RM)</th>
                                                <th>Action</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php if($gadget_allocation){ foreach ($gadget_allocation as $key => $value) { ?>
                                                <tr>
                                                    <td><?php echo $key+1; ?></td>
                                                    <td><?php echo $value->staff_name; ?></td>
                                                    <td><?php echo $value->department_name; ?> | <?php echo $value->branch_name; ?></td>
                                                    <td><?php echo $value->designation; ?></td>
                                                    <td><?php echo $value->year_start; ?> - <?php echo $value->year_end; ?></td>
                                                    <td><?php echo $value->amount; ?></td>
                                                    <td>
                                                        <div class="btn-group" id="edit_button_group_gadget" data-year_start="<?php echo $value->year_start; ?>" data-year_end="<?php echo $value->year_end; ?>" data-staff_id="<?php echo $value->staff_id; ?>">
                                                            <button class="btn btn-warning btn-sm text-white" data-toggle="modal" data-target="#edit_gadgetAllocation" data-modal="<?php echo base64_encode(json_encode($value)); ?>" onclick="editGadgetAllocation(this)" ><i class="fa fa-edit"></i></button>
                                                            <button class="btn btn-danger btn-sm" onclick="checkExistingClaim_gadget(<?php echo $value->id; ?>)"><i class="fa fa-trash"></i></button>
                                                        </div>
                                                    </td>
                                                </tr>
                                            <?php }} ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="dressing_allocation" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
            <h5 class="modal-title">Add Staff Dressing Allocation</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>

            <div class="modal-body">
                <div class="row mb-3">
                    <div class="col-4">
                        Staff Name
                    </div>
                    <div class="col">
                        <select name="dressing_staff_id" id="dressing_staff_id" class="form-control form-control-sm select" required>
                            <option>Please Select</option>
                            <?php if(!empty($all_users)) foreach($all_users as $key => $row): ?>
                                <option value="<?php echo $row['id']; ?>"><?php echo $row['name']; ?> [<?php echo $row['department_name']; ?> | <?php echo $row['branch_name']; ?>] </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </div>

                <div class="row mb-3">
                    <div class="col-4">
                        Year
                    </div>
                    <div class="col">
                        <input type="text" class="form-control form-control-sm datepicker-yy" id="dressing_year" name="dressing_year" required>
                    </div>
                </div>

                <div class="row mb-3">
                    <div class="col-4">
                        Amount(RM)
                    </div>
                    <div class="col">
                        <input type="text" class="form-control form-control-sm" id="dressing_amount" name="dressing_amount" required>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="submit" class="btn btn-primary" onclick="saveDressingAllocation()">Save</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="gadget_allocation" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
            <h5 class="modal-title">Add Staff Gadget Allocation</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>

            <div class="modal-body">
                <div class="row mb-3">
                    <div class="col-4">
                        Staff Name
                    </div>
                    <div class="col">
                        <select name="gadget_staff_id" id="gadget_staff_id" class="form-control form-control-sm select" required>
                            <option>Please Select</option>
                            <?php if(!empty($all_users)) foreach($all_users as $key => $row): ?>
                                <option value="<?php echo $row['id']; ?>"><?php echo $row['name']; ?> [<?php echo $row['department_name']; ?> | <?php echo $row['branch_name']; ?>] </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </div>

                <div class="row mb-3">
                    <div class="col-4">
                        Year Start
                    </div>
                    <div class="col">
                        <input type="text" class="form-control form-control-sm datepicker-yy" id="gadget_year_start" name="gadget_year_start" value="" required onchange="CheckExistingYear()">
                    </div>
                </div>

                <div class="row mb-3">
                    <div class="col-4">
                        Year End
                    </div>
                    <div class="col">
                        <input type="text" class="form-control form-control-sm datepicker-yy" id="gadget_year_end" name="gadget_year_end" value="" disabled>
                    </div>
                </div>

                <div class="row mb-3">
                    <div class="col-4">
                        Amount(RM)
                    </div>
                    <div class="col">
                        <input type="text" class="form-control form-control-sm" id="gadget_amount" name="gadget_amount" required>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="submit" class="btn btn-primary" onclick="saveGadgetAllocation()">Save</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="edit_dressingAllocation" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
            <h5 class="modal-title">Edit Staff Dressing Allocation</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>

            <div class="modal-body">
                <div class="row mb-3">
                    <div class="col-4">
                        Staff Name
                    </div>
                    <div class="col">
                        <input type="hidden" class="form-control form-control-sm" id="edit_dressing_allocation_id" name="edit_dressing_allocation_id" required disabled>
                        <input type="hidden" class="form-control form-control-sm" id="edit_dressing_staff_id" name="edit_dressing_staff_id" required disabled>
                        <input type="text" class="form-control form-control-sm" id="edit_dressing_staff_name" name="edit_dressing_staff_name" required disabled>
                    </div>
                </div>

                <div class="row mb-3">
                    <div class="col-4">
                        Year
                    </div>
                    <div class="col">
                        <input type="text" class="form-control form-control-sm datepicker-yy" id="edit_dressing_year" name="edit_dressing_year" required disabled>
                    </div>
                </div>

                <div class="row mb-3">
                    <div class="col-4">
                        Amount(RM)
                    </div>
                    <div class="col">
                        <input type="text" class="form-control form-control-sm" id="edit_dressing_amount" name="edit_dressing_amount" required>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="submit" class="btn btn-primary" onclick="edit_checkExistingClaim_dressing()">Save</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="edit_gadgetAllocation" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
            <h5 class="modal-title">Edit Staff Gadget Allocation</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>

            <div class="modal-body">
                <div class="row mb-3">
                    <div class="col-4">
                        Staff Name
                    </div>
                    <div class="col">
                        <input type="hidden" class="form-control form-control-sm" id="edit_gadget_allocation_id" name="edit_gadget_allocation_id" required disabled>
                        <input type="hidden" class="form-control form-control-sm" id="edit_gadget_staff_id" name="edit_gadget_staff_id" required disabled>
                        <input type="text" class="form-control form-control-sm" id="edit_gadget_staff_name" name="edit_gadget_staff_name" required disabled>
                    </div>
                </div>

                <div class="row mb-3">
                    <div class="col-4">
                        Year Start
                    </div>
                    <div class="col">
                        <input type="text" class="form-control form-control-sm datepicker-yy" id="edit_gadget_year_start" name="edit_gadget_year_start" value="" required onchange="edit_CheckExistingYear()" disabled>
                    </div>
                </div>

                <div class="row mb-3">
                    <div class="col-4">
                        Year End
                    </div>
                    <div class="col">
                        <input type="text" class="form-control form-control-sm datepicker-yy" id="edit_gadget_year_end" name="edit_gadget_year_end" value="" disabled>
                    </div>
                </div>

                <div class="row mb-3">
                    <div class="col-4">
                        Amount(RM)
                    </div>
                    <div class="col">
                        <input type="text" class="form-control form-control-sm" id="edit_gadget_amount" name="edit_gadget_amount" required>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="submit" class="btn btn-primary" onclick="edit_checkExistingClaim_gadget()">Save</button>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    $('.datepicker-yy').datepicker({
        uiLibrary: 'bootstrap4',
        autoclose: true,
        orientation: 'bottom',
        format: "yyyy",
        viewMode: "years", 
        minViewMode: "years"
    });

    function CheckExistingYear(){
        var year_start   = moment($('#gadget_year_start').val()).format("YYYY");
        
        var postData = {
            'staff_id'   : $('#gadget_staff_id').val(),
            'year_start' : year_start
        };

        $.ajax({
            type: "POST",
            dataType: "json",
            url: "<?php echo base_url(); ?>hr/claim/getGadgetYear",
            data: postData,
            success: function(content) {
                if (content.status == "success") {
                    var year_end     = moment(year_start).add(1,'Y');
                    var year_end_sel = moment(year_end).format("YYYY");

                    $('#gadget_year_end').val(year_end_sel);

                }else{
                    alert(content.msg);
                    return;
                }
            }
        });
    }

    function edit_CheckExistingYear(){
        var year_start   = moment($('#edit_gadget_year_start').val()).format("YYYY");
        
        var postData = {
            'staff_id'   : $('#edit_gadget_staff_id').val(),
            'year_start' : year_start
        };

        $.ajax({
            type: "POST",
            dataType: "json",
            url: "<?php echo base_url(); ?>hr/claim/getGadgetYear",
            data: postData,
            success: function(content) {
                if (content.status == "success") {
                    var year_end     = moment(year_start).add(1,'Y');
                    var year_end_sel = moment(year_end).format("YYYY");

                    $('#edit_gadget_year_end').val(year_end_sel);

                }else{
                    alert(content.msg);
                    return;
                }
            }
        });
    }

    function saveDressingAllocation() {
        var year = moment($('#dressing_year').val()).format("YYYY");

        var postData = {
            'staff_id'  : $('#dressing_staff_id').val(),
            'year'      : year,
            'amount'    : $('#dressing_amount').val(),
        };

        $.ajax({
            type: "POST",
            dataType: "json",
            url: "<?php echo base_url(); ?>hr/claim/saveDressingAllocation",
            data: postData,
            success: function(content) {
                if (content.status == "success") {
                    window.location.href = "<?php echo base_url().'hr/claim/staffAllocation'?>";
                }else{
                    alert('System failed to create dressing allocation. Allocation year already exist.');
                    return;
                }
            }
        });
    }

    function saveGadgetAllocation() {
        var year_start  = moment($('#gadget_year_start').val()).format("YYYY");
        var year_end    = moment($('#gadget_year_end').val()).format("YYYY");
        
        var postData = {
            'staff_id'   : $('#gadget_staff_id').val(),
            'year_start' : year_start,
            'year_end'   : year_end,
            'amount'     : $('#gadget_amount').val()
        };

        $.ajax({
            type: "POST",
            dataType: "json",
            url: "<?php echo base_url(); ?>hr/claim/saveGadgetAllocation",
            data: postData,
            success: function(content) {
               if (content.status == "success") {
                    window.location.href = "<?php echo base_url().'hr/claim/staffAllocation'?>";
                }else{
                    alert('System failed to create gadget allocation. Allocation year already exist.');
                    return;
                }
            }
        });
    }

    function checkExistingClaim_dressing($id) {
        var postData = {
            'id'        : $id,
            'staff_id'  : $('#edit_button_group_dressing').attr('data-staff_id'),
            'year'      : $('#edit_button_group_dressing').attr('data-year')
        };

        $.ajax({
            type: "POST",
            dataType: "json",
            url: "<?php echo base_url(); ?>hr/claim/checkExistingClaim_dressing",
            data: postData,
            success: function(content) {
                if (content.status == "success") {
                    deleteDressingAllocation($id);
                }else{
                    alert(content.msg);
                    return;
                }
            }
        });
    }

    function checkExistingClaim_gadget($id) {
        var postData = {
            'id'        : $id,
            'staff_id'  : $('#edit_button_group_gadget').attr('data-staff_id'),
            'year_start': $('#edit_button_group_gadget').attr('data-year_start'),
            'year_end'  : $('#edit_button_group_gadget').attr('data-year_end')
        };

        $.ajax({
            type: "POST",
            dataType: "json",
            url: "<?php echo base_url(); ?>hr/claim/checkExistingClaim_gadget",
            data: postData,
            success: function(content) {
                if (content.status == "success") {
                    deleteGadgetAllocation($id);
                }else{
                    alert(content.msg);
                    return;
                }
            }
        });
    }

    function deleteDressingAllocation($id) {
        var postData = {
            'id'  : $id
        };

        $.ajax({
            type: "POST",
            dataType: "json",
            url: "<?php echo base_url(); ?>hr/claim/deleteDressingAllocation",
            data: postData,
            success: function(content) {
                window.location.href = "<?php echo base_url().'hr/claim/staffAllocation'?>";
            }
        });
    }

    function deleteGadgetAllocation($id) {
        var postData = {
            'id'  : $id
        };

        $.ajax({
            type: "POST",
            dataType: "json",
            url: "<?php echo base_url(); ?>hr/claim/deleteGadgetAllocation",
            data: postData,
            success: function(content) {
                window.location.href = "<?php echo base_url().'hr/claim/staffAllocation'?>";
            }
        });
    }

    function editDressingAllocation(elem) {
        let json = JSON.parse(atob($(elem).data('modal')));
        $('#edit_dressing_allocation_id').val(json.id);
        $('#edit_dressing_staff_id').val(json.staff_id);
        $('#edit_dressing_staff_name').val(json.staff_name + ' [' + json.department_name + ' | ' + json.branch_name + ']');
        $('#edit_dressing_year').val(json.year);
        $('#edit_dressing_amount').val(json.amount);
    }

    function edit_checkExistingClaim_dressing() {
        var postData = {
            'staff_id'  : $('#edit_dressing_staff_id').val(),
            'year'      : $('#edit_dressing_year').val()
        };

        $.ajax({
            type: "POST",
            dataType: "json",
            url: "<?php echo base_url(); ?>hr/claim/checkExistingClaim_dressing",
            data: postData,
            success: function(content) {
                if (content.status == "success") {
                    saveEditDressingAllocation();
                }else{
                    alert(content.msg);
                    return false;
                }
            }
        });
    }

    function saveEditDressingAllocation() { 

        var year = moment($('#edit_dressing_year').val()).format("YYYY");

        var postData = {
            'allocation_id' : $('#edit_dressing_allocation_id').val(),
            'year'          : year,
            'amount'        : $('#edit_dressing_amount').val(),
        };

        $.ajax({
            type: "POST",
            dataType: "json",
            url: "<?php echo base_url(); ?>hr/claim/updateDressingAllocation",
            data: postData,
            success: function(content) {
                window.location.href = "<?php echo base_url().'hr/claim/staffAllocation'?>";
            }
        });
    }

    function editGadgetAllocation(elem) {
        let json = JSON.parse(atob($(elem).data('modal')));
        $('#edit_gadget_allocation_id').val(json.id);
        $('#edit_gadget_staff_id').val(json.staff_id);
        $('#edit_gadget_staff_name').val(json.staff_name + ' [' + json.department_name + ' | ' + json.branch_name + ']');
        $('#edit_gadget_year_start').val(json.year_start);
        $('#edit_gadget_year_end').val(json.year_end);
        $('#edit_gadget_amount').val(json.amount);
    }

    function edit_checkExistingClaim_gadget() {
        var postData = {
            'staff_id'  : $('#edit_gadget_staff_id').val(),
            'year_start': $('#edit_gadget_year_start').val(),
            'year_end'  : $('#edit_gadget_year_end').val()
        };

        $.ajax({
            type: "POST",
            dataType: "json",
            url: "<?php echo base_url(); ?>hr/claim/checkExistingClaim_gadget",
            data: postData,
            success: function(content) {
                if (content.status == "success") {
                    saveEditGadgetAllocation();
                }else{
                    alert(content.msg);
                    return;
                }
            }
        });
    }

    function saveEditGadgetAllocation() {
        var year_start  = moment($('#edit_gadget_year_start').val()).format("YYYY");
        var year_end    = moment($('#edit_gadget_year_end').val()).format("YYYY");
        
        var postData = {
            'allocation_id' : $('#edit_gadget_allocation_id').val(),
            'year_start'    : year_start,
            'year_end'      : year_end,
            'amount'        : $('#edit_gadget_amount').val()
        };

        $.ajax({
            type: "POST",
            dataType: "json",
            url: "<?php echo base_url(); ?>hr/claim/updateGadgetAllocation",
            data: postData,
            success: function(content) {
                window.location.href = "<?php echo base_url().'hr/claim/staffAllocation'?>";
            }
        });
    }

</script>