<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>Petty Cash / Advance Request List</h4>

                <a href="<?php echo base_url('hr/serviceform/petty_cash_advance_form'); ?>" class="btn btn-outline-dark btn-sm" target="_blank"><i class="fa fa-plus"></i> Add New Petty Cash / Advance Request</a>

                <a href="<?php echo base_url('hr/Serviceform_report/petty_cash_advance_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']; ?>">
                                <div class="form-group row">
                                    <label for="sv_petty_cash_advance_filter_dept" class="col-lg-1">
                                        Department
                                    </label>                           
                                    <div class="col-lg-5">
                                        <select class="custom-select" name="sv_petty_cash_advance_filter_dept" id="sv_petty_cash_advance_filter_dept" <?php echo ($finance_staff || $_SESSION['user']['division_id'] == 10)?'':'disabled'; ?>>
                                         <option value="-1">[All]</option>
                                         <?php if($department) foreach($department as $row){ ?>
                                            <option value="<?php echo $row['id']; ?>" <?php echo ($finance_staff || $_SESSION['user']['division_id'] == 10)?'':($_SESSION['user']['department_id'] == $row['id'])?'selected':''; ?>><?php echo $row['name'].' ['.$row['branch_name'].']'; ?></option> 
                                        <?php } ?>
                                        </select>
                                    </div>

                                    <label for="sv_petty_cash_advance_filter_entity" class="col-lg-1">
                                        Entity
                                    </label>
                                    <div class="col-lg-5">
                                        <select class="custom-select" name="sv_petty_cash_advance_filter_entity" id="sv_petty_cash_advance_filter_entity">
                                         <option value="-1">[All]</option>
                                         <?php if($business_entity) foreach($business_entity 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_petty_cash_advance_filter_status" class="col-lg-1">
                                        Status
                                    </label>                           
                                    <div class="col-lg-5">
                                        <select class="custom-select" id="sv_petty_cash_advance_filter_status" name="sv_petty_cash_advance_filter_status">
                                            <option value="-1">[All]</option>
                                            <option value="Pending">Pending</option>
                                            <option value="Approved">Approved</option>
                                            <option value="Rejected">Rejected</option>
                                            <option value="Completed">Completed</option>
                                            <option value="Duplicate">Duplicate</option>
                                        </select>
                                    </div>

                                    <label for="sv_petty_cash_advance_filter_requester" class="col-lg-1">
                                        Requester
                                    </label>                           
                                    <div class="col-lg-5">
                                        <select class="custom-select" id="sv_petty_cash_advance_filter_requester" name="sv_petty_cash_advance_filter_requester">
                                            <option value="-1">[All]</option>
                                            <?php if($requester) foreach($requester 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_petty_cash_advance_filter_approver" class="col-lg-1">
                                        Approver
                                    </label>                           
                                    <div class="col-lg-5">
                                        <select class="custom-select" id="sv_petty_cash_advance_filter_approver" name="sv_petty_cash_advance_filter_approver">
                                            <option value="-1">[All]</option>
                                            <?php if($approver) foreach($approver as $row){ ?>
                                                <option value="<?php echo $row['id'] ?>"><?php echo $row['name']; ?></option>
                                            <?php } ?>
                                        </select>
                                    </div>

                                    <label for="sv_petty_cash_advance_filter_preparer" class="col-lg-1">
                                        Prepare
                                    </label>                           
                                    <div class="col-lg-5">
                                        <select class="custom-select" id="sv_petty_cash_advance_filter_preparer" name="sv_petty_cash_advance_filter_preparer">
                                            <option value="-1">[All]</option>
                                            <?php if($preparer) foreach($preparer 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_petty_cash_advance_filter_year" class="col-lg-1">
                                        Year
                                    </label>                           
                                    <div class="col-lg-5">
                                      <input type="text" name="sv_petty_cash_advance_filter_year" id="sv_petty_cash_advance_filter_year" class="form-control datepicker-yyyy">
                                    </div>

                                    <label for="sv_petty_cash_advance_filter_month" class="col-lg-1">
                                      Month
                                    </label>                           
                                    <div class="col-lg-5">
                                        <input type="text" name="sv_petty_cash_advance_filter_month" id="sv_petty_cash_advance_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_petty_cash_advance" class="table table-bordered p-0 m-0" width="100%">
                                    <thead> 
                                        <th class="text-center" width="5%">#</th>
                                        <th class="text-center" width="15%">Client</th>
                                        <th class="text-center" width="15%">Entity</th>
                                        <th class="text-center" width="15%">Department </th>
                                        <th class="text-center" width="15%">Requester</th>
                                        <th class="text-center" width="15%">Status</th>
                                        <th class="text-center" width="10%">Amount (RM)</th>
                                        <th class="text-center" width="10%">Action</th>
                                    </thead>
                                    <tbody>
                                        <?php if(!empty($petty_advance_list)) { ?> 
                                            <?php foreach($petty_advance_list as $key => $row) {
                                                ?>
                                                <tr>
                                                    <td class="text-center"><?php echo $key+1; ?></td>
                                                    <td>
                                                        <?php echo '<b>Client : </b>'.$row['customerName']; ?>
                                                        <?php echo ($row['job_id'])?'<br><br><b>Job ID : </b>'.str_pad($row['job_id'],5,0,STR_PAD_LEFT):''; ?>
                                                        
                                                    </td>
                                                    <td><?php echo $row['entity_name']; ?></td>
                                                    <td><?php echo $row['dept_name'].' / '.$row['branchName']; ?></td>
                                                    <td><?php echo $row['requester_name'];//.' ['.$row['requester_email'].']'; ?></td>
                                                    <td>
                                                        <?php if($row['approval_status'] == 0){ ?>
                                                            <b>Approval Status : </b><br>Pending <i class="fa fa-exclamation-circle" style="color: #FFC107;"></i>
                                                            <?php $disabled = ''; ?>
                                                        <?php } else { $disabled = 'disabled style="cursor: not-allowed;"'; ?>
                                                            <b>Approval Status : </b><br>
                                                            <?php if($row['approval_status'] == 1)
                                                            {
                                                                echo 
                                                                'Approved <i class="fa fa-check-circle" style="color: green;"></i>
                                                                <br>
                                                                <b>Approved By : </b>'.$row['approved_by_name'];
                                                            }else{
                                                                echo 
                                                                'Rejected <i class="fa fa-times-circle" style="color: red;"></i>
                                                                <br>
                                                                <b>Rejected By : </b>'.$row['approved_by_name'];
                                                            }
                                                            ?>
                                                            <br><br>
                                                            <b>Finance Status : </b><br>
                                                            <?php if($row['prepared_status'] == 0)
                                                            {
                                                                echo 'Pending <i class="fa fa-exclamation-circle" style="color: #FFC107;"></i>';
                                                            }
                                                            elseif($row['prepared_status'] == 1)
                                                            {
                                                                echo 
                                                                'Completed <i class="fa fa-check-circle" style="color: green;"></i>
                                                                <br>
                                                                <b>Prepared By : </b>'.$row['prepared_by_name']; 
                                                            }else{
                                                                echo 
                                                                'Duplicate <i class="fa fa-exclamation-triangle" style="color: #FFC107;"></i><br>
                                                                <b>Prepared By : </b>'.$row['prepared_by_name'];
                                                            }
                                                            ?>
                                                        <?php } ?>
                                                    </td>
                                                    <td>
                                                        <?php echo ($row['total_approved'] > 0)?'<b>Amount Approved</b> : RM'.number_format($row['total_approved'],2):'<b>Amount Request</b> : RM'.number_format($row['total'],2) ?>
                                                        <?php echo ($row['cash_balance'] > 0)?'<br><b>Cash Balance</b> : RM'.number_format($row['cash_balance'],2):''; ?>
                                                        <?php echo ($row['balance_received'] > 0)?'<br><b>Balance Received</b> : RM'.number_format($row['balance_received'],2):''; ?>
                                                    </td>
                                                    <td class="text-center">
                                                        <div class="btn-group btn-group-sm" role="group">
                                                            <a type="button" class="btn btn-outline-dark" data-trigger="hover" data-tooltip="tooltip" 
                                                            title="View Detail" href="<?php echo base_url('hr/serviceform/petty_cash_advance_form/'.$row['ID'].'/view'); ?>" target="_blank"><i class="far fa-eye"></i></a>

                                                            <?php if(($_SESSION['user']['id'] == $row['requester_id']) || ($_SESSION['user']['id'] == $row['createdBY'])){ ?>
                                                                <?php if($disabled == ''){ ?>
                                                                    <a type="button" class="btn btn-outline-dark" data-trigger="hover" data-tooltip="tooltip" title="Edit" href="<?php echo base_url('hr/serviceform/petty_cash_advance_form/'.$row['ID']); ?>" target="_blank"><i class="far fa-edit"></i></a>
                                                                <?php }else{ ?>
                                                                    <button type="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 type="button" class="btn btn-outline-dark" href="<?php echo base_url('hr/serviceform_report/petty_cash_advance_print/').$row['ID']; ?>" target="_blank" data-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="deleteButtonPettyCashAdvance" type="button" class="btn btn-outline-dark" onclick="deletePettyAdvance(<?php echo $row['ID']; ?>);" data-trigger="hover" data-tooltip="tooltip" title="Delete" <?php echo ($disabled == '')?'':$disabled; ?>><i class="far fa-trash-alt"></i></button>
                                                            <?php } ?>

                                                            <?php if($row['prepared_status'] == 1 &&  $row['request_type'] == 1){ ?>
                                                                <a type="button" class="btn btn-outline-dark" data-trigger="hover" data-tooltip="tooltip" title="Cash Advance Listing" href="<?php echo base_url('hr/serviceform/petty_cash_advance_form/'.$row['ID'].'/cash_advance_listing'); ?>" target="_blank"><i class="far fa-file-alt"></i></a>
                                                            <?php } ?>
                                                        </div>
                                                    </td>  
                                                </tr>
                                            <?php } ?>
                                        <?php } ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>      
    </div>
</div>

<script>
    $(document).ready(function()
    {
        <?php $this->session->unset_userdata('sv_petty_cash_advance_filter_dept'); ?>
        <?php $this->session->unset_userdata('sv_petty_cash_advance_filter_entity'); ?>
        <?php $this->session->unset_userdata('sv_petty_cash_advance_filter_status'); ?>
        <?php $this->session->unset_userdata('sv_petty_cash_advance_filter_requester'); ?>
        <?php $this->session->unset_userdata('sv_petty_cash_advance_filter_approver'); ?>
        <?php $this->session->unset_userdata('sv_petty_cash_advance_filter_preparer'); ?>
        <?php $this->session->unset_userdata('sv_petty_cash_advance_filter_year'); ?>
        <?php $this->session->unset_userdata('sv_petty_cash_advance_filter_month'); ?>
        <?php $this->session->unset_userdata('sv__petty_cash_advance'); ?>
        $('#searchBtn').click(function(){
            getData();
        });
    });

    function deletePettyAdvance(ID) 
    {
        validateForm("#deleteButtonPettyCashAdvance", "Delete this from list?", function() {
            $.ajax({
                url:'<?php echo base_url('hr/serviceform/deletePettyCashAdvanceList') ?>',
                data: {
                    petty_cash_advanceID : ID,
                },
                type: 'post',
                dataType: 'json',
                success: (data) => {
                    if(data.status == "Success") {

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

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

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

        frmData.append('sv_petty_cash_advance_filter_dept',$('#sv_petty_cash_advance_filter_dept').val());
        frmData.append('sv_petty_cash_advance_filter_entity',$('#sv_petty_cash_advance_filter_entity').val());
        frmData.append('sv_petty_cash_advance_filter_branch',$('#sv_petty_cash_advance_filter_branch').val());
        frmData.append('sv_petty_cash_advance_filter_status',$('#sv_petty_cash_advance_filter_status').val());
        frmData.append('sv_petty_cash_advance_filter_requester',$('#sv_petty_cash_advance_filter_requester').val());
        frmData.append('sv_petty_cash_advance_filter_approver',$('#sv_petty_cash_advance_filter_approver').val());
        frmData.append('sv_petty_cash_advance_filter_preparer',$('#sv_petty_cash_advance_filter_preparer').val());
        frmData.append('sv_petty_cash_advance_filter_year',$('#sv_petty_cash_advance_filter_year').val());
        frmData.append('sv_petty_cash_advance_filter_month',$('#sv_petty_cash_advance_filter_month').val());
        // frmData.append('user_id',$('#user_id').val());

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

                let petty_cash_advance = data.table;

                if(petty_cash_advance != null){
                    $.each(petty_cash_advance,function(key,content)
                    {
                        var i = key + 1;
                        
                        if(content.approval_status == 0)
                        {
                            var approval_status = '<b>Approval Status : </b><br>'; 
                            var status = 'Pending <i class="fa fa-exclamation-circle" style="color: #FFC107;"></i>';
                            var disabled = '';

                            var petty_status = approval_status+status;
                        } else 
                        {
                            var disabled = 'disabled style="cursor: not-allowed;"';

                            var approval_status = '<b>Approval Status : </b><br>';
                            if(content.approval_status == 1)
                            {
                                var status = 'Approved <i class="fa fa-check-circle" style="color: green;"></i><br><b>Approved By : </b>'+content.approved_by_name;
                            }else{
                                var status = 'Rejected <i class="fa fa-times-circle" style="color: red;"></i><br><b>Rejected By : </b>'+content.approved_by_name; 
                            }
                            
                            var finance_status = '<b>Finance Status : </b><br>';
                            if(content.prepared_status == 0)
                            {
                                var f_status = 'Pending <i class="fa fa-exclamation-circle" style="color: #FFC107;"></i>';
                            }
                            else if(content.prepared_status == 1)
                            {
                                var f_status = 'Completed <i class="fa fa-check-circle" style="color: green;"></i><br><b>Prepared By : </b>'+content.prepared_by_name; 
                            }else{
                                var f_status = 'Duplicate <i class="fa fa-exclamation-triangle" style="color: #FFC107;"></i><br><b>Prepared By : </b>'+content.prepared_by_name;
                            }

                            var petty_status = approval_status+status+'<br><br>'+finance_status+f_status;
                        }


                        var btn_view = '<a type="button" class="btn btn-outline-dark" data-trigger="hover" data-tooltip="tooltip" title="View Detail" href="<?php echo base_url();?>hr/serviceform/petty_cash_advance_form/'+content.business_entity_id+'/'+content.ID+'/view" target="_blank"><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/petty_cash_advance_print/'+content.ID+'" target="_blank" data-trigger="hover" data-tooltip="tooltip" title="Print Detail"><i class="fa fa-print"></i></a>';

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

                        var btn_delete = '<button type="button" class="btn btn-outline-dark" onclick="delete_equipment('+content.ID+')" 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;
                        }

                        if(content.job_id != '' && content.job_id != null && content.job_id > 0)
                        {
                            var job_id = '<b>Client : </b>'+content.customerName+'<br><br><b>Job ID : </b>'+(content.job_id).padStart(5, '0');
                        }else{
                            var job_id = '<b>Client : </b>'+content.customerName;
                        }

                        table.row.add({
                            0   : '<div class="text-center">'+i+'</div>',
                            1   : job_id,
                            2   : content.entity_name,
                            3   : content.dept_name+' / '+content.branchName,
                            4   : content.requester_name,
                            5   : petty_status,
                            6   : '<div class="text-center">'+content.total+'</div>',
                            7	: '<div class="text-center"><div class="btn-group btn-group-sm" role="group">'+btn+'</div></div>',
                        }).draw();
                        // console.log(data);

                    });                        
                }

            },
            error:function(){
                iziToast.warning({
                    position: 'bottomRight',
                    message: '500 Internal Server Error.'
                });
            }
        });
    }
</script>