<div class="container-fluid" style="padding-bottom: 2rem;">
    <div class="card bg-light-transparent">  
        <div class="card-header bg-white">
            <div class="float-left">
                <h4>Training Evaluation List</h4>

                <a href="<?php echo base_url('hr/serviceform/evaluation_form'); ?>" class="btn btn-outline-dark btn-sm" title="Add New evaluation">
                    <i class="fa fa-plus"></i> Add New Evaluation 
                </a>

                <a href="<?php echo base_url('hr/Serviceform_report/evaluation_print')?>" target="_blank" class="btn btn-outline-dark btn-sm" title="Print">
                    <i class="fa fa-print"></i>Print
                </a>

                <a id="btnShowFilters" type="button" class="btn btn-outline-dark btn-sm collapsed" data-trigger="hover" title="Show Filters Box" data-toggle="collapse" data-target="#collapseFilter" aria-expanded="false">
                    <i class="fas fa-filter"></i>Show Filters
                </a> 
            </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">
                                <input type="hidden" name="user_id" id="user_id" value="<?php echo $_SESSION['user']['id']; ?>">
                                <?php if($_SESSION['module_data']['role']=='admin'){ ?>    
                                    <div class="form-group row">
                                        <label for="sv_evaluation_filter_dept" class="col-lg-3">
                                            Department
                                        </label>
                                        <div class="col-lg-4" >
                                            <select class="custom-select" name="sv_evaluation_filter_dept" id="sv_evaluation_filter_dept">
                                                <option value="-1">[All]</option>
                                                <?php if($department) foreach($department as $row){ ?>
                                                    <option value="<?php echo $row['id']; ?>"><?php echo $row['name'].' ['.$row['branch_name'].']'; ?></option> 
                                                <?php } ?>
                                            </select>
                                        </div>
                                    </div>
                                <?php } ?>
                                <div class="form-group row">
                                    <label for="sv_evaluation_filter_package" class="col-lg-3">
                                        Packages
                                    </label>
                                    <div class="col-lg-4">
                                        <select class="custom-select" name="sv_evaluation_filter_package" id="sv_evaluation_filter_package">
                                            <option value="">[All]</option>
                                            <option name="package" value="External">External</option>
                                            <option name="package" value="Internal">Internal</option>
                                            <option name="package" value="In-house">In-house</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="sv_evaluation_filter_year" class="col-lg-3">
                                        Year
                                    </label>                           
                                    <div class="col-lg-4" >
                                        <input type="text" name="sv_evaluation_filter_year" id="sv_evaluation_filter_year" class="form-control datepicker-yyyy">
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="sv_evaluation_filter_month" class="col-lg-3">
                                        Evaluation Month
                                    </label>                           
                                    <div class="col-lg-4" >
                                        <input type="text" name="sv_evaluation_filter_month" id="sv_evaluation_filter_month" class="form-control datepicker-mm">
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="sv_evaluation_filter_training_date" class="col-lg-3">
                                        Training Date
                                    </label>                           
                                    <div class="col-lg-4" >
                                        <input type="text" name="sv_evaluation_filter_training_date" id="sv_evaluation_filter_training_date" class="form-control datepicker">
                                    </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="tab-content">
            <div class="tab-pane fade active show" id="tableView" role="tabpanel" aria-labelledby="nav-table-tab">
                <div class="card-body">
                    <div class="card no-shadow">
                        <div class="card-body">
                            <div class="table-responsive">
                                <table id="filter_evaluation_training" class="table table-bordered p-0 m-0" width="100%">
                                    <thead>
                                        <th class="sorting text-center" tabindex="0" aria-controls="table_contact" rowspan="1" colspan="1" aria-label="#: activate to sort column ascending" style="width: 0px;">
                                            #
                                        </th>
                                        <th class="sorting text-center" tabindex="0" aria-controls="table_contact" rowspan="1" colspan="1" style="width: 0px;">
                                            Training title
                                        </th>
                                        <th class="sorting text-center" tabindex="0" aria-controls="table_contact" rowspan="1" colspan="1" style="width: 0px;">
                                            Date of training
                                        </th>
                                        <th class="sorting text-center" tabindex="0" aria-controls="table_contact" rowspan="1" colspan="1" style="width: 0px;">
                                            Packages
                                        </th>
                                        <th class="sorting text-center" tabindex="0" aria-controls="table_contact" rowspan="1" colspan="1" style="width: 0px;">
                                            Venue
                                        </th>
                                        <th class="sorting text-center" tabindex="0" aria-controls="table_contact" rowspan="1" colspan="1" style="width: 0px;">
                                            Respective Department / Branch
                                        </th>
                                        <th class="sorting text-center" tabindex="0" aria-controls="table_contact" rowspan="1" colspan="1" style="width: 0px;">
                                            CPD / CPE hours
                                        </th>
                                        <th class="sorting text-center" tabindex="0" aria-controls="table_contact" rowspan="1" colspan="1" style="width: 0px;">
                                            Name of Trainer/Speaker
                                        </th>
                                        <th class="sorting text-center" tabindex="0" aria-controls="table_contact" rowspan="" colspan="1" style="width: 0px;">
                                            Action
                                        </th>
                                    </thead>
                                    <tbody>
                                        <?php if(!empty($listTrainingEvaluation))  { //die(debug($listTrainingEvaluation));?> 
                                            <?php foreach($listTrainingEvaluation as $key => $row) {  ?>
                                              <tr>
                                                <td class="text-center"><?php echo $key+1; ?></td>
                                                <td><?php echo $row['training_title']; ?></td>
                                                <td class="text-center"><?php echo (!empty($row['training_date'])&&$row['training_date']!='0000-00-00')?date('d-m-Y', strtotime($row['training_date'])):'N/A'; ?><br>
                                                    <?php echo (!empty($row['time']) && $row['time'] != '00:00:00')?$row['time']:'N/A'; ?>
                                                </td>
                                                <td class="text-center"><?php echo $row['packages']; ?></td>
                                                <td><?php echo $row['venue']; ?></td>
                                                <td><?php echo $row['dept_name']. ' / ' . $row['branchName']; ?></td>
                                                <td class="text-center"><?php echo $row['cpd_hours']; ?></td>
                                                <td><?php echo (!empty($row['trainer_name']))?$row['trainer_name']:'N/A'; ?></td>
                                                <td class="text-center"> 
                                                    <div class="btn-group btn-group-sm" role="group">
                                                        <a href="<?php echo base_url('hr/serviceform/evaluation_form/'.$row['ID']); ?>" class="btn btn-outline-dark btn-sm" data-tooltip="tooltip" data-trigger="hover" title="View Training Evaluation"  data-original-title="Go To My Department's Applications" >
                                                            <i class="far fa-eye"></i>
                                                        </a>
                                                        <a type="button" class="btn btn-outline-dark" href="<?php echo base_url('hr/serviceform_report/evaluation_print/').$row['ID']; ?>" target="_blank" data-trigger="hover" data-tooltip="tooltip" title="Print Detail">
                                                            <i class="fa fa-print"></i>
                                                        </a>

                                                        <button type="button" id="deleteButton" class="btn btn-outline-dark" onclick="deleteData(<?php echo $row['ID']; ?>);" data-trigger="hover" data-tooltip="tooltip" title="Delete"><i class="far fa-trash-alt"></i></button>
                                                    </div>
                                                </td>
                                            </tr>
                                        <?php } } ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">

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

    function getData() 
    {
        var table = $('#filter_evaluation_training').DataTable();
        // $('#table_equipment').DataTable().destroy();
        table.clear().draw();
        var frmData = new FormData();

        frmData.append('sv_evaluation_filter_dept',$('#sv_evaluation_filter_dept').val());
        frmData.append('sv_evaluation_filter_year',$('#sv_evaluation_filter_year').val());
        frmData.append('sv_evaluation_filter_package',$('#sv_evaluation_filter_package').val());
        frmData.append('sv_evaluation_filter_month',$('#sv_evaluation_filter_month').val());
        frmData.append('sv_evaluation_filter_training_date',$('#sv_evaluation_filter_training_date').val());
        frmData.append('user_id',$('#user_id').val());

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

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

                let evaluation = data.table;

                console.log(evaluation);

                if(evaluation != null){
                    $.each(evaluation,function(key,content)
                    {
                        if((content.training_date != null || content.training_date != '' || content.training_date != undefined) && content.training_date != '0000-00-00'){
                            var training_date = moment(content.training_date).format('DD-MM-YYYY');
                        }else{
                            var training_date = '';
                        }
                        
                        var i = key + 1;

                        var btn_view = '<a type="button" href="<?php echo base_url();?>hr/serviceform/evaluation_form/'+content.ID+'" class="btn btn-outline-dark" data-trigger="hover" data-tooltip="tooltip" title="View Training Evaluation"><i class="far fa-eye"></i></a>';

                            //var btn_edit = '<a type="button" class="btn btn-outline-dark" href="<?php //echo base_url();?>hr/serviceform/evaluation_form/'+content.ID+'" data-trigger="hover" data-tooltip="tooltip" title="Edit"><i class="far fa-edit"></i></a>';

                            var btn_print = '<a type="button" class="btn btn-outline-dark" href="<?php echo base_url();?>hr/serviceform_report/evaluation_print/'+content.ID+'" data-trigger="hover" data-tooltip="tooltip" title="Print Detail"><i class="fa fa-print"></i></a>';

                            //var btn_delete = '<button type="button" class="btn btn-outline-dark" onclick="deleteButton('+content.ID+')" data-trigger="hover" data-tooltip="tooltip" title="Delete"><i class="far fa-trash-alt"></i></button>';

                            table.row.add({
                                0   : i,
                                1   : content.training_title,
                                2   : training_date,
                                3   : content.packages,
                                4   : content.venue,
                                5   : content.dept_name+' / '+content.branchName,
                                6   : content.cpd_hours,
                                7   : content.trainer_name,
                                8   : '<div class="btn-group btn-group-sm" role="group">'+btn_view+btn_print+'</div>',
                            }).draw();
                            console.log(data);
                        });                        
                }
            },
            error:function(){
                iziToast.warning({
                    position: 'bottomRight',
                    message: '500 Internal Server Error.'
                });
            }
        });
    }

    function deleteData(ID) 
    {
        validateForm("#deleteButton", "Delete this training evaluation?", function() {
           $.ajax({
            url:'<?php echo base_url('hr/serviceform/delete_evaluation_form') ?>',
            data: {
             evaluation_id : ID,
         },
         type: 'post',
         dataType: 'json',
         success: (data) => {
             if(data.status == "Success") {

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

                 location.reload();

                    //redirect(base_url('hr/serviceform/car_booking'));
                }
            }
        });
       });
    }
</script>