
<div class="wrapper">
    <div class="container">
        <!-- Page-Title -->
        <div class="row">
            <div class="col-sm-10 pull-left">
                <h4 class="page-title">Leave Applications: <?php echo $business[0]['business_name']; ?></h4>
            </div>
            <div class="col-sm-2">
                <div class="pull-right m-t-15">
                     <div class="btn-group">
                        
                    </div>
                </div>
            </div>
        </div>
        <?php if ($this->session->flashdata('Success') && $this->session->flashdata('Success') != "") { ?>
                    <div class="row hidden-print">
                        <div class="col-sm-12">
                            <div class="alert alert-success">
                                <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                                <strong>Success!</strong> <?php echo $this->session->flashdata('Success'); ?>
                            </div>
                        </div>
                    </div>
                <?php } ?>
                <?php if ($this->session->flashdata('Error') && $this->session->flashdata('Error') != "") { ?>
                    <div class="row hidden-print">
                        <div class="col-sm-12">
                            <div class="alert alert-danger">
                                <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                                <strong>Error!</strong> <?php echo $this->session->flashdata('Error'); ?>
                            </div>
                        </div>
                    </div>
                <?php } ?>
        <div class="row" id="divselection">
            <div class="col-sm-12">
                <div class="card-box ">
                    <h4 class="header-title m-t-0 m-b-30">Filters:</h4>
                    <form action="<?php echo base_url();?>leave_applications" method="post">
                        <input type="hidden" name="csrf_test_name" id="members_csrf" value=""/>
                    <div class="row">
                        <div class="col-lg-12">
                            <div class="row">
                                <div class="col-lg-2">
                                    <div class="row">
                                        <div class="col-lg-12"> <label class="control-label">Filter by Status:</label></div>
                                    </div>
                                    <div class="row">
                                        <div class="col-lg-12"> 
                                            <select  class="form-control" id="statusfilter" name="statusfilter">
                                                <option <?php if($status == 'Pending'){echo "selected='selected'";} ?> value="Pending">Pending</option>
                                                <option <?php if($status == 'Approved'){echo "selected='selected'";} ?> value="Approved">Approved</option>
                                                <option <?php if($status == 'Rejected'){echo "selected='selected'";} ?> value="Rejected">Rejected</option>                                               
                                            </select>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-lg-2">
                                    <div class="row">
                                        <div class="col-lg-12"> <label class="control-label">Filter by Staff:</label></div>
                                    </div>
                                    <div class="row">
                                        <div class="col-lg-12"> 
                                           <select class="form-control" name="stafffilter" id="stafffilter" >
                                               <option value="0">All</option>
                                               <?php foreach($staff_list as $staff){?>
                                               <option <?php if($staffid == $staff['id_staff']){echo "selected='selected'";} ?> value="<?php echo $staff['id_staff']; ?>"><?php echo $staff['staff_fullname']; ?></option>
                                               <?php } ?>
                                           </select>
                                        </div>
                                    </div>
                                </div>
                                
                               
                                <div class="col-lg-2">
                                    <div class="row">
                                        <div class="col-lg-12"> <label class="control-label">Get</label></div>
                                    </div>
                                    <div class="row">
                                        <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6" > 
                                            <button onclick="$('#members_csrf').val($('#cook').val());"  type="submit" class="btn btn-pink btn-bordred waves-effect w-md waves-light m-b-5">Run</button>
                                        </div>
                                        <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6"><i id="cog" class="fa fa-spin fa-cog" style="display:none; font-size:26px;width: auto;margin-right: 10px;"></i></div>
                                    </div>
                                </div>

                            </div>
                        </div>
                    </div>
                    </form>
                </div>
            </div>
        </div>
        
         <div class="row" >
            <div class="col-sm-12">
                <div class="card-box ">
                    <div class="row">
                        <div class="col-md-12">
                            <table id="app_table" class="table table-detail">
                                
                                <thead>
                                    <tr>
                                        <th>Application ID</th>
                                        <th>Staff ID</th>
                                        <th>Staff Name</th>
                                        <th>Leave Type</th>
                                        <th>Required From</th>
                                        <th>Required To</th>                                       
                                        <th>Status</th>
                                        <th>Approved By</th>
                                        <th>Approved On</th>
                                        <th>Action</th>
                                    </tr>
                                    
                                </thead>
                                <tbody>
                                    <?php foreach($applications as $application){?>
                                    <tr>
                                        <td><?php echo $application['id_leave_applications']; ?></td>
                                        <td><?php echo $application['staff_id']; ?></td>
                                        <td><?php echo $application['staff_name']; ?></td>
                                        <td><?php echo $application['leave_type']; ?></td>
                                        <td><?php echo $application['required_from']; ?></td>
                                        <td><?php echo $application['required_to']; ?></td>
                                        <td><?php echo $application['application_status']; ?></td>
                                        <td><?php echo $application['approved_by']; ?></td>
                                        <td><?php echo $application['approved_on']; ?></td>
                                        <td> <button onclick="open_leave_app(<?php echo $application['id_leave_applications'].','.$application['staff_id'].',\''.$application['staff_name'].'\',\''.$application['leave_type'].'\',\''.$application['required_from'].'\',\''.$application['required_to'].'\',\''.$application['application_status'].'\''.',\''.$application['comments'].'\'';?>);" class="btn btn-sm">update</button> </td>
                                    </tr>
                                    <?php } ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    
      <!--Edit Leave App Modal-->
        <div id="editleaveapp" class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="editleaveapp" aria-hidden="true" style="display: none;">
            <div class="modal-dialog">
                <div class="modal-content">
                    <form id="updateLeaveApp" action="<?php echo base_url("attendance_controller/update_leave_app"); ?>" method="POST">
                        <input type="hidden" name="csrf_test_name" id="payment_csrf" value=""/>
                        <input type="hidden" name="txtleaveid" id="txtleaveid" value=""/>
                        <input type="hidden" name="txtstaffid" id="txtstaffid" value=""/>
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                            <h4 class="modal-title" id="custom-width-modalLabel">Update Leave Application</h4>
                        </div>
                        <div class="modal-body">
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="txtstaffname" class="control-label date">Staff Name:</label>                                        
                                    </div> 
                                </div> 
                                <div class="col-md-6">
                                    <div class="form-group">                                        
                                        <input id="txtstaffname" name="txtstaffname" class="form-control">
                                    </div> 
                                </div>                                 
                            </div>
                            
                            <div class="row">
                                <div class="col-md-6">
                                    <label for="txtstaffname" class="control-label date">Leave Type:</label>
                                </div>                                
                                <div class="col-md-6">
                                    <select class="form-control" name="txt_leave_type" id="txt_leave_type">
                                        <?php foreach($leave_types as $leave_type){?>
                                        <option value="<?php echo $leave_type['leave_type'];?>"><?php echo $leave_type['leave_type'];?></option>
                                        <?php } ?>
                                    </select>
                                </div>                                
                            </div>
                            
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="txtreqfrom" class="control-label date">Required From:</label>                                        
                                    </div> 
                                </div> 
                                <div class="col-md-6">
                                    <div class="form-group">                                        
                                        <input id="txtreqfrom" name="txtreqfrom"  class="form-control txtDate1" required="required">
                                    </div> 
                                </div>                                 
                            </div>
                            
                            <div class="row">
                                <div class="col-md-6">
                                    <strong>Required To: </strong>
                                </div>                                
                                <div class="col-md-6">
                                    <input class="form-control txtDate1" name="txtreqto" id="txtreqto" value=""/>
                                </div>                                
                            </div>
                            
                             <div class="row">
                                <div class="col-md-6">
                                    <strong>Comments: </strong>
                                </div>                                
                                <div class="col-md-6">
                                    <input class="form-control " name="txt_comment" id="txt_comment" value=""/>
                                </div>                                
                            </div>
                            <hr>
                            <div class="row">
                                <div class="col-md-6">
                                    <strong>Approval: </strong>
                                </div>                                
                                <div class="col-md-6">
                                    <select class="form-control" id="txt_leave_status" name="txt_leave_status">
                                        <option value='Pending'>Pending</option>
                                        <option value='Approved'>Approved</option>
                                        <option value='Rejected'>Rejected</option>
                                    </select>
                                </div>                                
                            </div>                                 
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default waves-effect" data-dismiss="modal">Close</button>
                            <button id="btnsubmit" type="submit" onclick="$('#payment_csrf').val($('#cook').val());" class="btn btn-custom waves-effect waves-light">Save</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    
    
    
    <script>
        
     $(document).ready(function() {
         
         $("#stafffilter").select2();
         
         $('.txtDate1').datepicker({
            default: today,
            autoclose: true,
            todayHighlight: true,
            format: 'yyyy-mm-dd',
            orientation: 'bottom'
        }).datepicker('setDate', new Date());
         
          $('#app_table').DataTable({
                dom: "Bfrtlip",
                buttons: [{extend: "copy", className: "btn-sm btn-custom btn-trans"}, {extend: "csv", className: "btn-sm btn-pink btn-trans"}, {
                extend: "excel",
                className: "btn-sm btn-warning btn-trans"
                }, {extend: "pdf", className: "btn-sm btn-purple btn-trans"}],
                responsive: !0
            });
         
     });
        
        
    function open_leave_app(id, staffid, staffname,  leave_type, reqfrom, reqto, status, comment ){
        console.log(leave_type);
        
        $("#txtleaveid").val(id);
        $("#txtstaffid").val(staffid);
        $("#txtstaffname").val(staffname);
        $("#txtreqfrom").val(reqfrom);
        $("#txtreqto").val(reqto);
        $("#txt_leave_type").val(leave_type);
        $("#txt_comment").val(comment);
        $("#txt_leave_status text").text(status);
        
        $("#editleaveapp").modal('show');
        
        
    }    
    </script>