<section class="container-fluid" style="padding-bottom: 2rem;">
    <div class="card bg-light-transparent">
        <div class="card-header bg-white">
            <div class="float-left">
                <h5>Professional Exam / Courses List</h5>
            </div>
            <div class="float-right">
                <div class="btn-group btn-group-sm">
                    <a href="<?php echo base_url('hr/exam/print_examList') ?>" target="_blank" class="btn btn-dark btn-sm" title="Print Professional Exam / Courses List">
                        <i class="fa fa-print"></i> Print
                    </a>
                    <button id="btnShowFilters" type="button" class="btn btn-light btn-sm collapsed" data-trigger="hover" title="Show Filters Box" data-toggle="collapse" data-target="#collapseFilter"><i class="fas fa-filter"></i>Show Filters</button>
                </div>
            </div>
        </div>

        <div class="collapse" id="collapseFilter">
            <div class="card-header">
                <div class="row">
                    <div class="col">
                        <div class="card no-shadow">
                            <div class="card-body">
                                <div class="form-group row">
                                    <label for="exam_filter_staff" class="col-lg-3">
                                        Staff Name
                                    </label> 
                                    <div class="col-lg-4" >
                                        <select class="custom-select" name="exam_filter_staff" id="exam_filter_staff">
                                            <option value="-1">[All]</option>
                                            <?php if ($staff_list) foreach ($staff_list as $row) { ?>
                                                    <option value="<?php echo $row['id']; ?>"><?php echo $row['name'] . ' [' . $row['branch_name'] . ']'; ?></option> 
                                                <?php } ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="exam_filter_exam_status" class="col-lg-3">
                                        Exam Status
                                    </label>                           
                                    <div class="col-lg-4" >
                                        <select class="custom-select" id="exam_filter_exam_status" name="exam_filter_exam_status">
                                            <option value="-1">[All]</option>
                                            <option value="0">New</option>
                                            <option value="1">Repeat</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="exam_filter_sl_status" class="col-lg-3">
                                        SL Allocation Status
                                    </label>                           
                                    <div class="col-lg-4" >
                                        <select class="custom-select" id="exam_filter_sl_status" name="exam_filter_sl_status">
                                            <option value="-1">[All]</option>
                                            <option value="0">No</option>
                                            <option value="1">Yes</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="exam_filter_exam_result" class="col-lg-3">
                                        Exam Result
                                    </label>                           
                                    <div class="col-lg-4" >
                                        <select class="custom-select" id="exam_filter_exam_result" name="exam_filter_exam_result">
                                            <option value="-1">[All]</option>
                                            <option value="1">Pass</option>
                                            <option value="0">Failed</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="exam_filter_exam_date" class="col-lg-3">Exam Date</label>
                                    <div class="col-lg-4" >
                                        <input type="text" class="form-control datepicker" id="exam_filter_exam_date" placeholder="Exam Date" value="">
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col d-print-none">
                                        <button class="btn btn-info" id="searchBtn">Search</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="card-body">
            <div class="card no-shadow">
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table display responsive table-striped" id="filter_table_exam">
                            <thead>
                                <tr>
                                    <th>No.</th>
                                    <th>Staff Name</th>
                                    <th>Course Study/Programme</th>
                                    <th>Paper Code</th>
                                    <th>Title of Paper/Subject</th>
                                    <th>Exam Date</th>
                                    <th>Result</th>
                                    <th>Status</th>
                                    <th>Leave History</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $i = 0;
                                if ($exam_list) {
                                    foreach ($exam_list as $value) { ?>
                                        <tr>
                                            <td><?php echo ++$i; ?></td>
                                            <td><?php echo $value['staff_name']; ?> <br><small><i><?php echo $value['department_name']; ?> - <?php echo $value['branch_name']; ?></i></small></td>
                                            <td><?php echo $value['programme']; ?> <br><small><i><?php echo $value['tab']; ?></i></small></td>
                                            <td>
                                                <?php echo $value['paper_code']; ?>
                                                <br><br>
                                                <small><b>Attachment : </b>
                                                    <br><a href="<?php echo base_url('hr/myprofile/openAttachment/'.$value['mask_id'].'/9')?>" target="_blank"><i><?php echo $value['attachment_name']; ?></i></a>
                                                </small>
                                            </td>
                                            <td><?php echo $value['title']; ?></td>
                                            <td><?php if (($value['exam_date'] != null) && ($value['exam_date'] != '1970-01-01')) {
                                            echo date("d-m-Y", strtotime($value['exam_date']));
                                        } else {
                                            echo '';
                                        } ?>
                                            </td>
                                            <td><?php if ($value['result'] == '0') {
                                            echo "FAILED";
                                        } else if ($value['result'] == 1) {
                                            echo "PASS";
                                        } ?></td>
                                            <td>
                                                <?php if ($value['status'] == 0) { ?>
                                                    <span class="badge badge-pill badge-info"> New ! </span>
                                                <?php } else { ?>
                                                    <span class="badge badge-pill badge-danger"> Repeat ! </span>
                                                <?php } ?>
                                                <br>
        <?php if ($value['request_sl_status'] == 0) { ?>
                                                    <span class="badge badge-pill badge-warning" title="No Study Leave Requested" > No Request &nbsp;</span>
        <?php } else { ?>
                                                    <span class="badge badge-pill badge-success" title="Study Leave Requested"> SL Requested &nbsp;<i class="fas fa-check"></i></span>
            <?php if ($value['allocation_status'] == 0) { ?>
                                                        <button type="button" class="badge badge-pill badge-success" title="Allocate Study Leave" data-toggle="modal" data-target="#allocationModal" onclick="allocate_leave(this)" data-id="<?php echo $value['id']; ?>" data-staffid="<?php echo $value['staff_id']; ?>" data-staffname="<?php echo $value['staff_name']; ?>" data-papercode="<?php echo $value['paper_code']; ?>" data-status="<?php echo $value['status']; ?>"> Allocate SL <i class="far fa-hand-point-up"></i></button>
                                            <?php } else { ?>
            <?php } ?>
        <?php } ?>
                                            </td>
                                            <td class="text-center">
                                                <button type="button" class="btn btn-dark" title="Leave History" data-toggle="modal" data-target="#LeaveHistory">
                                                    <i class="fas fa-file"></i>
                                                </button>
                                            </td>
                                        </tr>
    <?php }
} ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

<div class="modal fade" id="allocationModal">
    <div class="modal-dialog modal-m">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="allocation-modal-title">Study Leave Allocation</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <form action="<?php echo base_url('hr/exam/create_study_leave_allocation'); ?>" method="post" enctype="multipart/form-data"> 
                <div class="modal-body">
                    <div class="form-group">
                        <label for="leave_staffName">Staff Name</label>
                        <input type="text" name="leave_staffName" id="leave_staffName" class="form-control" disabled>
                        <input type="hidden" name="leave_staffid" id="leave_staffid" class="form-control">
                    </div>
                    <div class="form-group">
                        <div class="row">
                            <div class="col-6">
                                <label for="leave_paper">Paper Code</label>
                                <input type="text" name="leave_paper" id="leave_paper" class="form-control" disabled>
                                <input type="hidden" name="leave_examid" id="leave_examid" class="form-control">
                            </div>
                            <div class="col-6">
                                <label for="leave_addDays">Additional Days</label>
                                <input type="text" name="leave_addDays" id="leave_addDays" class="form-control" value="5">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <div class="btn-group float-right">
                        <button type="button" class="btn btn-light" data-dismiss="modal">Cancel</button>
                        <button type="submit" class="btn btn-primary">Save</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<div class="modal fade" id="LeaveHistory">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="allocation-modal-title">Study Leave History</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <form action="<?php //echo base_url('hr/exam/create_study_leave_allocation');  ?>" method="post" enctype="multipart/form-data"> 
                <div class="modal-body">
                    <div class="form-group">
                        <div class="row">
                            <div class="col-6">
                                <label for="leave_staffName">Staff Name</label>
                                <input type="text" name="leave_staffName" id="leave_staffName" class="form-control" disabled>
                                <input type="hidden" name="leave_staffid" id="leave_staffid" class="form-control">
                            </div>
                            <div class="col-6">
                                <label for="leave_paper">Paper Code</label>
                                <input type="text" name="leave_paper" id="leave_paper" class="form-control" disabled>
                                <input type="hidden" name="leave_examid" id="leave_examid" class="form-control">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <div class="btn-group float-right">
                        <button type="button" class="btn btn-light" data-dismiss="modal">Cancel</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<script type="text/javascript">

    $(document).ready(function () {

        $('#searchBtn').click(function () {
            getData();
        });

    });

    function delete_exam(exam_id) {
        $.ajax({
            url: '<?php echo base_url('hr/exam/delete_exam') ?>',
            data: {
                exam_id: exam_id,
            },
            type: 'post',
            dataType: 'json',
            success: (data) => {
                if (data.status == "Success") {

                    localStorage.setItem("status", data.status);
                    localStorage.setItem("msg", data.msg);

                    location.reload();
                }
            }
        });
    }

    function check_leave(exam_id) {
        validateForm("#deleteButtonExam", "Delete this list?", function () {
            $.ajax({
                url: '<?php echo base_url('hr/exam/check_study_leave') ?>',
                data: {
                    exam_id: exam_id,
                },
                type: 'post',
                dataType: 'json',
                success: (data) => {
                    if (data.status == "Error") {

                        localStorage.setItem("status", data.status);
                        localStorage.setItem("msg", data.msg);

                        location.reload();
                    } else {
                        delete_exam(exam_id);
                    }
                }
            });
        });
    }

    function edit_exam(elem)
    {
        let exam_id = $(elem).data('id');
        let staffid = $(elem).data('staffid');
        let programme = $(elem).data('programme');
        let paper_code = $(elem).data('paper_code');
        let title = $(elem).data('title');
        let exam_date = $(elem).data('exam_date');
        let status = $(elem).data('status');
        let result = $(elem).data('result');

        $('#exam_examid_edit').val(exam_id);
        $('#exam_staffid_edit').val(staffid);
        $('#exam_programme_edit').val(programme);
        $('#exam_papercode_edit').val(paper_code);
        $('#exam_papertitle_edit').val(title);
        $('#exam_date_edit').val(exam_date);
        $('#exam_status_edit').val(status);
        $('#exam_result_edit').val(result);
    }

    function clone_exam(elem)
    {
        let exam_id = $(elem).data('id');
        let staffid = $(elem).data('staffid');
        let programme = $(elem).data('programme');
        let paper_code = $(elem).data('paper_code');
        let title = $(elem).data('title');
        let exam_date = $(elem).data('exam_date');
        let status = $(elem).data('status');
        let result = $(elem).data('result');

        $('#exam_staffid_clone_hidden').val(staffid);
        $('#exam_staffid_clone').val(staffid);
        $('#exam_programme_clone_hidden').val(programme);
        $('#exam_programme_clone').val(programme);
        $('#exam_papercode_clone_hidden').val(paper_code);
        $('#exam_papercode_clone').val(paper_code);
        $('#exam_papertitle_clone_hidden').val(title);
        $('#exam_papertitle_clone').val(title);
        $('#exam_date_clone').val(exam_date);
        $('#exam_status_clone').val(status);
        $('#exam_result_clone').val(result);
    }

    function allocate_leave(elem) {
        let staff_name = $(elem).data('staffname');
        let staff_id = $(elem).data('staffid');
        let exam_id = $(elem).data('id');
        let paper_code = $(elem).data('papercode');

        $('#leave_staffName').val(staff_name);
        $('#leave_staffid').val(staff_id);
        $('#leave_examid').val(exam_id);
        $('#leave_paper').val(paper_code);
    }

    function getData()
    {
        var table = $('#filter_table_exam').DataTable();
        table.clear().draw();
        var frmData = new FormData();

        frmData.append('exam_filter_staff', $('#exam_filter_staff').val());
        frmData.append('exam_filter_exam_status', $('#exam_filter_exam_status').val());
        frmData.append('exam_filter_sl_status', $('#exam_filter_sl_status').val());
        frmData.append('exam_filter_exam_result', $('#exam_filter_exam_result').val());
        frmData.append('exam_filter_exam_date', $('#exam_filter_exam_date').val());

        $.ajax({
            url: "<?php echo base_url('/hr/exam/get_exam_filter'); ?>",
            type: 'post',
            dataType: 'json',
            data: frmData,
            processData: false,
            contentType: false,
            success: function (data) {

                iziToast.success({
                    position: 'bottomRight',
                    message: 'filtering data',
                });

                let exam = data.table;

                // console.log(exam);

                if (exam != null) {
                    var index = 1;
                    $.each(exam, function (key, content)
                    {
                        var i = index + key;

                        if (content.exam_date == null || content.exam_date == '1970-01-01') {
                            var date = '';
                        } else {
                            var date = moment(content.exam_date).format('DD-MM-YYYY');
                        }

                        if (content.status == 0) {
                            var exam_status = '<span class="badge badge-pill badge-info"> New ! </span>';
                        } else {
                            var exam_status = '<span class="badge badge-pill badge-danger"> Repeat ! </span>';
                        }

                        if (content.allocation_status == 0) {
                            if ('<?php echo $_SESSION['all_modules_access'][1]['access_lvl']; ?>' == 'admin') {
                                var allocation_status = '<button type="button" class="badge badge-pill badge-warning" title="Allocate Study Leave" data-toggle="modal" data-target="#allocationModal" onclick="allocate_leave(this)" data-id="' + content.id + '" data-staffid="' + content.staff_id + '" data-staffname="' + content.staff_name + '" data-papercode="' + content.paper_code + '" data-status="' + content.status + '"> No &nbsp;<i class="far fa-hand-point-up"></i></button>';
                            } else {
                                var allocation_status = '<button type="button" class="badge badge-pill badge-warning" title="Study Leave - No Allocation"> No &nbsp;<i class="far fa-hand-point-up"></i></button>';
                            }
                        } else {
                            var allocation_status = '<button type="button" class="badge badge-pill badge-success" title="Study Leave - Allocated"> Yes &nbsp;<i class="far fa-hand-point-up"></i></button>';
                        }

                        if (content.result == null) {
                            var result = '';
                        } else if (content.result == 0) {
                            var result = 'FAILED';
                        } else {
                            var result = 'PASS';
                        }

                        if (content.result != null) {
                            var clone = '<button type="button" class="btn btn-success" title="Clone" data-toggle="modal" data-target="#cloneModal" onclick="clone_exam(this)" data-id="' + content.id + '" data-staffid="' + content.staff_id + '" data-programme="' + content.programme + '" data-paper_code="' + content.paper_code + '" data-title="' + content.title + '" data-status="' + content.status + '" data-result="' + content.result + '" data-exam_date="' + date + '"><i class="fas fa-redo-alt"></i></button>';

                            var edit = '<button type="button" class="btn btn-warning" title="Not allowed to edit this list." disabled><i class="fas fa-edit"></i></button>';

                            var trash = '<button type="button" class="btn btn-danger" title="Not allowed to delete this list." disabled><i class="fas fa-trash"></i></button>';
                        } else {
                            var clone = '<button type="button" class="btn btn-success" title="Clone" data-toggle="modal" data-target="#cloneModal" onclick="clone_exam(this)" data-id="' + content.id + '" data-staffid="' + content.staff_id + '" data-programme="' + content.programme + '" data-paper_code="' + content.paper_code + '" data-title="' + content.title + '" data-status="' + content.status + '" data-result="' + content.result + '" data-exam_date="' + date + '"><i class="fas fa-redo-alt"></i></button>';

                            var edit = '<button type="button" class="btn btn-warning" title="Edit" data-toggle="modal" data-target="#editModal" onclick="edit_exam(this)" data-id="' + content.id + '" data-staffid="' + content.staff_id + '" data-programme="' + content.programme + '" data-paper_code="' + content.paper_code + '" data-title="' + content.title + '" data-status="' + content.status + '" data-result="' + content.result + '" data-exam_date="' + date + '"><i class="fas fa-edit"></i></button>';

                            var trash = '<button type="button" class="btn btn-danger" title="Delete" id="deleteButtonExam" onclick="check_leave(' + content.id + ');"><i class="fas fa-trash"></i></button>';
                        }

                        table.row.add({
                            0: i,
                            1: content.staff_name + '<br><small><i>' + content.department_name + ' - ' + content.branch_name + '</i></small>',
                            2: content.programme,
                            3: content.paper_code,
                            4: content.title,
                            5: date,
                            6: 'Exam Status ' + exam_status + '<br> SL Allocation ' + allocation_status,
                            7: result,
                            8: '<div class="btn-group btn-group-sm" role="group" aria-label="Basic example">' + clone + edit + trash + '<div>',
                        }).draw();
                    });
                }
            },
            error: function () {
                iziToast.warning({
                    position: 'bottomRight',
                    message: '500 Internal Server Error.'
                });
            }
        });
    }

</script>