<div class="container-fluid" style="padding-bottom: 2rem;">
    <div class="container-fluid">
        <div class="card bg-light-transparent">  
            <div class="card-header bg-white">
                <div class="float-left">
                    <h4>New Client / Resignation Request List</h4>
                    <button class="btn btn-outline-dark btn-sm" title="Add New Request" data-toggle="modal" data-target="#modal_client_new_resignation" data-trigger="hover" data-tooltip="tooltip"><i class="fa fa-plus"></i> Add New Request</button>
                    <a href="<?php echo base_url('hr/Serviceform_report/client_new_resignation_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">
                                    <?php if($_SESSION['all_modules_access'][1]['access_lvl'] == 'admin'){ ?>
                                        <div class="form-group row">
                                            <label for="sv_client_new_resignation_filter_dept" class="col-lg-3">
                                                Department
                                            </label>                           
                                            <div class="col-lg-4" >
                                                <select class="custom-select" name="sv_client_new_resignation_filter_dept" id="sv_client_new_resignation_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_client_new_resignation_filter_client_id" class="col-lg-3">
                                            Client
                                        </label>                           
                                        <div class="col-lg-4" >
                                            <select class="select" name="sv_client_new_resignation_filter_client_id" id="sv_client_new_resignation_filter_client_id">
                                                <option value="-1">[All]</option>
                                                <?php if($client) foreach($client as $row){ ?>
                                                    <option value="<?php echo $row->id; ?>"><?php echo $row->name; ?></option> 
                                                <?php } ?>
                                            </select>
                                        </div>
                                    </div>    
                                    <div class="form-group row">
                                        <label for="sv_client_new_resignation_filter_status" class="col-lg-3">
                                            Status
                                        </label>                           
                                        <div class="col-lg-4" >
                                            <select class="custom-select" id="sv_client_new_resignation_filter_status" name="sv_client_new_resignation_filter_status">
                                                <option value="-1">[All]</option>
                                                <option value="Pending">Pending</option>
                                                <option value="In_progress">In Progress</option>
                                                <option value="Verified">Verified</option>
                                                <option value="Approved">Approved</option>
                                                <option value="Rejected">Rejected</option>
                                                <option value="Cancel">Cancel</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label for="sv_client_new_resignation_filter_year" class="col-lg-3">
                                            Year
                                        </label>                           
                                        <div class="col-lg-4" >
                                            <input type="text" name="sv_client_new_resignation_filter_year" id="sv_client_new_resignation_filter_year" class="form-control datepicker-yyyy">
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label for="sv_client_new_resignation_filter_month" class="col-lg-3">
                                            Requested Month
                                        </label>                           
                                        <div class="col-lg-4" >
                                            <input type="text" name="sv_client_new_resignation_filter_month" id="sv_client_new_resignation_filter_month" class="form-control datepicker-mm">
                                        </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_table_client_new_resignation" class="table table-bordered p-0 m-0" width="100%">
                                        <thead>
                                            <th class="text-center" width="1%">#</th>
                                            <th class="text-center" width="20%">Requestor</th>
                                            <th class="text-center" width="20%">Department</th>
                                            <th class="text-center" width="15%">Client</th>
                                            <th class="text-center" width="25%">Detail</th>
                                            <th class="text-center" width="10%">Status</th>
                                            <th class="text-center" width="9%">Action</th>        
                                        </thead>

                                        <tbody id="list_client_new_resignation_list">
                                            <?php if(!empty($request_list)) { ?> 
                                                <?php foreach($request_list as $key => $row) { //debug($row); ?>
                                                    <tr>
                                                        <td class="text-center"><?php echo $key+1; ?></td>
                                                        <td><b>Requested By : </b><br><?php echo $row['requester_name'].' ['.$row['requester_email'].']'; ?></td>
                                                        <td><?php echo $row['dept_name'].' ['.$row['branchName'].']'; ?></td>
                                                        <td><?php echo $row['client_name']; ?></td>
                                                        <td>
                                                            <?php 
                                                                $request_type = ($row['type'] == 1)?"New Client":(($row['type'] == 2)?"Client Resignation":"N/A");
                                                                echo '<b>Request Type </b> : '.$request_type; 
                                                                echo ($row['type'] == 2)?'<br><b>Reason </b> : '.$row['reason']:'';
                                                                echo ($row['attachment_id'])?'<br><b>Attachment </b> : <a download href="'.base_url($row['file_location'].$row['filename']).'">'.$row['filename'].'</a>':'';
                                                            ?>
                                                        </td>
                                                        <td class="text-center">
                                                            <?php 
                                                            if(($row['status_app']==null || $row['status_app']=='Pending'))
                                                            {
                                                                $status = 'Pending <i class="fa fa-exclamation-circle" style="color: #FFC107;"></i>';
                                                                $disabled = '';
                                                            }
                                                            if($row['status_app'] == 'In_progress')
                                                            {
                                                                $status = str_replace('_', ' ', $row['status_app']).' <i class="fa fa-hourglass-start" style="color: grey;"></i>';
                                                                $disabled = 'disabled style="cursor: not-allowed;"';
                                                            }
                                                            if($row['status_app'] == 'Verified')
                                                            {
                                                                $status = $row['status_app'].' <i class="fa fa-check-circle" style="color: green;"></i><p style="font-style: italic; font-size: 13px;">'.$row['status_reason'].'</p>';
                                                                $disabled = '';
                                                            }
                                                            if($row['status_app'] == 'Approved')
                                                            {
                                                                $status = $row['status_app'].' <i class="fa fa-check-circle" style="color: green;"></i>';
                                                                $disabled = 'disabled style="cursor: not-allowed;"';
                                                            }
                                                            if($row['status_app'] == 'Rejected')
                                                            {
                                                                $status = $row['status_app'].' <i class="fa fa-times-circle" style="color: red;"></i>';
                                                                $disabled = 'disabled style="cursor: not-allowed;"';
                                                            }
                                                            if($row['status_app'] == 'Cancel')
                                                            {
                                                                $status = $row['status_app'].' <i class="fa fa-times-circle" style="color: red;"></i>';
                                                                $disabled = 'disabled style="cursor: not-allowed;"';
                                                            }

                                                            echo $status;
                                                            ?>
                                                        </td>

                                                        <td class="text-center"> 
                                                            <div class="btn-group btn-group-sm" role="group">
                                                                <a href="<?php echo base_url('hr/serviceform/client_new_resignation_form/'.$row['type'].'/'.$row['ID'].'/view') ?>" class="btn btn-outline-dark" data-trigger="hover" data-tooltip="tooltip" title="View Detail"><i class="far fa-eye"></i></a>

                                                                <?php if(($_SESSION['user']['id'] == $row['requester_id']) || ($_SESSION['user']['id'] == $row['createdBY'])){ ?>
                                                                    <?php if($disabled == ''){ ?>
                                                                        <a href="<?php echo base_url('hr/serviceform/client_new_resignation_form/'.$row['type'].'/'.$row['ID']); ?>" target="_blank" class="btn btn-outline-dark" onclick="" data-trigger="hover" data-tooltip="tooltip" title="Edit"><i class="far fa-edit"></i></a>
                                                                    <?php }else{ ?>
                                                                        <button class="btn btn-outline-dark" data-trigger="hover" data-tooltip="tooltip" title="Edit" <?php echo $disabled; ?>><i class="far fa-edit"></i></button>
                                                                    <?php } ?>
                                                                <?php } ?>

                                                                <a href="<?php echo base_url('hr/serviceform_report/client_new_resignation_print/'.$row['ID']); ?>" class="btn btn-outline-dark" target="_blank" date-trigger="hover" data-tooltip="tooltip" title="Print Detail"><i class="fa fa-print"></i></a>

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

<div class="modal fade" id="modal_client_new_resignation" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLongTitle"></h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>

            <div class="modal-body">
                <!-- <form class="form" id="crmcompany" method="post" action="<?php //echo base_url('hr/serviceform/invoice_form') ?>"> -->
                    <div class="row p-3">
                        <div class="col-12">
                            <label>Type</label>
                            <select class="form-control" id="type" name="type" required>
                                <option value="1">New Client</option>
                                <option value="2">Client Resignation</option>
                            </select>
                        </div>
                    </div>

                    <div class="float-right">
                        <button type="button" class="btn btn-light" data-dismiss="modal">Cancel</button>
                        <button type="submit" class="btn btn-primary" onclick="to_client_new_resignation_form()">Go</button>
                    </div> 
                <!-- </form> -->
            </div>
        </div>
    </div>
</div>

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

        <?php $this->session->unset_userdata('sv_client_new_resignation_filter_dept'); ?>
        <?php $this->session->unset_userdata('sv_client_new_resignation_filter_client_id'); ?>
        <?php $this->session->unset_userdata('sv_client_new_resignation_filter_year'); ?>
        <?php $this->session->unset_userdata('sv_client_new_resignation_filter_month'); ?>
        <?php $this->session->unset_userdata('sv_client_new_resignation_filter_status'); ?>
        <?php $this->session->unset_userdata('sv__client_new_resignation'); ?>
    });
    
    function getData() {
        var table = $('#filter_table_client_new_resignation').DataTable();
        table.clear().draw();
        var frmData = new FormData();

        frmData.append('sv_client_new_resignation_filter_dept',$('#sv_client_new_resignation_filter_dept').val());
        frmData.append('sv_client_new_resignation_filter_client_id',$('#sv_client_new_resignation_filter_client_id').val());
        frmData.append('sv_client_new_resignation_filter_status',$('#sv_client_new_resignation_filter_status').val());
        frmData.append('sv_client_new_resignation_filter_year',$('#sv_client_new_resignation_filter_year').val());
        frmData.append('sv_client_new_resignation_filter_month',$('#sv_client_new_resignation_filter_month').val());

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

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

                let client_new_resignation = data.table;

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

                        if(content.status_app == null || content.status_app == 'Pending')
                        {
                            var status      = 'Pending <i class="fa fa-exclamation-circle" style="color: #FFC107;"></i>';
                            var disabled    = '';
                        }
                        if(content.status_app == 'In_progress')
                        {
                            var status      = ((content.status_app).replace('_',' '))+' <i class="fa fa-hourglass-start" style="color: grey;"></i>';
                            var disabled    = 'disabled style="cursor: not-allowed;"';
                        }
                        if(content.status_app == 'Verified')
                        {
                            var status      = content.status_app+' <i class="fa fa-check-circle" style="color: green;"></i><p style="font-style: italic; font-size: 13px;">'+content.status_reason+'</p>';
                            var disabled    = '';
                        }
                        if(content.status_app == 'Approved')
                        {
                            var status      = content.status_app+' <i class="fa fa-check-circle" style="color: green;"></i>';
                            var disabled    = 'disabled style="cursor: not-allowed;"';
                        }
                        if(content.status_app == 'Rejected')
                        {
                            var status      = content.status_app+' <i class="fa fa-times-circle" style="color: red;"></i>';
                            var disabled    = 'disabled style="cursor: not-allowed;"';
                        }
                        if(content.status_app == 'Cancel')
                        {
                            var status      = content.status_app+' <i class="fa fa-times-circle" style="color: red;"></i>';
                            var disabled    = 'disabled style="cursor: not-allowed;"';
                        }

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

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

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

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

                        if(('<?php echo $_SESSION['user']['id']; ?>' == content.requester_id) || ('<?php echo $_SESSION['user']['id']; ?>' == content.createdBY))
                        {
                            var btn = btn_view+btn_edit+btn_print+btn_delete;
                        }else{
                            var btn = btn_view+btn_print;
                        }

                        var requestor_name  = '<b>Requested By : </b><br>'+content.requester_name+' ['+content.requester_email+']';
                        var department      = content.dept_name+' ['+content.branchName+']';
                        
                        if(content.type == 1) 
                        {
                            var request_type = "New Client";
                            var reason = '';
                        }
                        else if(content.type == 2) 
                        {
                            var request_type = "Client Resignation";
                            var reason = '<br><b>Reason </b> : '+content.reason;
                        }
                        else 
                        {
                            var request_type = "N/A";
                            var reason = '';
                        }
                        if(content.attachment_id != null && content.attachment_id > 0) var attachment = '<br><b>Attachment </b> : <a download href="<?php echo base_url();?>/'+content.file_location+content.filename+'">'+content.filename+'</a>';
                        else var attachment = '';

                        var details = '<b>Request Type </b> : '+request_type+reason+attachment;

                        // console.log(requestor_name);
                        table.row.add({
                            0   : '<div class="text-center">'+i+'</div>',
                            1   : requestor_name,
                            2   : department,
                            3   : content.client_name,
                            4   : details,
                            5   : '<div class="text-center">'+status+'</div>',
                            6   : '<div class="text-center"><div class="btn-group btn-group-sm" role="group">'+btn+'</div></div>',
                        }).draw();
                    });
                }
            },
            error:function(){
                iziToast.warning({
                    position: 'bottomRight',
                    message: '500 Internal Server Error.'
                });
            }
        });
    }

    function to_client_new_resignation_form()
    {
        var type = $('#type').val();

        if(type > 0)
        {
            url = '<?php echo base_url('hr/serviceform/client_new_resignation_form/'); ?>'+type;

            window.open(url, '_blank');
        }
    }

    function deleteData(elem)
    {
        var request_id = $(elem).data('id');

        $.ajax({
            url:"<?php echo base_url('/hr/serviceform/client_new_resignation_delete'); ?>",
            type:'post',
            dataType:'json',
            data:{
                request_id : request_id,
            },
            success:function(data){

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

                location.reload();
            },
            error:function(){
                iziToast.warning({
                    position: 'bottomRight',
                    message: '500 Internal Server Error.'
                });
            }
        });
    }

</script>