
<div class="container">
    <div class="card bg-light-transparent">
        <!-- widget  -->
        <!-- <div class="row py-3 px-1">
            <div class="col-12 col-md-3">
                <div class="card text-white bg-dark">
                    <div class="card-body">
                        <div class="row">
                            <div class="col">
                                <h5 class="card-title">2018 (Unallocated)</h5>
                                <p class="card-text"><?php echo $unallocate; ?></p>
                            </div>  
                            <div class="col">
                                <i class="fa fa-pencil-alt fa-3x pull-right"></i>
                            </div>
                        </div>
                        <p class="more-info tex-white">
                            <a href="" class="text-white no-decoration">
                                <i class="fa fa-arrow-circle-right"></i> More info
                            </a>
                        </p>
                    </div>
                </div>
            </div>
            
            <div class="col-12 col-md-3">
                <div class="card text-white bg-dark">
                    <div class="card-body">
                        <div class="row">
                            <div class="col">
                                <h5 class="card-title">2018 (Active)</h5>
                                <p class="card-text"><?php echo $active; ?></p>
                            </div>  
                            <div class="col">
                                <i class="fa fa-pencil-alt fa-3x pull-right"></i>
                            </div>
                        </div>
                        <p class="more-info tex-white">
                            <a href="" class="text-white no-decoration">
                                <i class="fa fa-arrow-circle-right"></i> More info
                            </a>
                        </p>
                    </div>
                </div>
            </div>

            <div class="col-12 col-md-3">
                <div class="card text-white bg-dark">
                    <div class="card-body">
                        <div class="row">
                            <div class="col">
                                <h5 class="card-title">2018 (Inactive)</h5>
                                <p class="card-text"><?php echo $inactive; ?></p>
                            </div>  
                            <div class="col">
                                <i class="fa fa-pencil-alt fa-3x pull-right"></i>
                            </div>
                        </div>
                        <p class="more-info tex-white">
                            <a href="" class="text-white no-decoration">
                                <i class="fa fa-arrow-circle-right"></i> More info
                            </a>
                        </p>
                    </div>
                </div>
            </div>

            <div class="col-12 col-md-3">
                <div class="card text-white bg-dark">
                    <div class="card-body">
                        <div class="row">
                            <div class="col">
                                <h5 class="card-title">2018 (End)</h5>
                                <p class="card-text"><?php echo $end; ?></p>
                            </div>  
                            <div class="col">
                                <i class="fa fa-pencil-alt fa-3x pull-right"></i>
                            </div>
                        </div>
                        <p class="more-info tex-white">
                            <a href="" class="text-white no-decoration">
                                <i class="fa fa-arrow-circle-right"></i> More info
                            </a>
                        </p>
                    </div>
                </div>
            </div>
        </div> -->
        <!-- widget one -->
    </div>
</div>

<section class="container" style="padding-bottom: 2rem;">
	<div class="card bg-light-transparent">  

        <div class="card-header bg-white">
            <h5>Claim Allocation List</h5>
            <a href="#" class="btn btn-outline-dark btn-sm" title="Add New Claim Allocation" data-toggle="modal" data-target="#add_allocation">
                <i class="fa fa-plus"></i> Add Claim Allocation
            </a>
            <!-- 
            todo : enabling after meeting    
            <a href="<?php echo base_url('hr/StaffMovement/list_report')?>" target="_blank" class="btn btn-outline-dark btn-sm" title="Print Staff Movement">
                <i class="fa fa-print"></i>Print
            </a>
            <button 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
            </button> -->
        </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="filter_year" class="col-lg-3">
                                        Year
                                    </label>                           
                                    <div class="col" >
                                        <select class="custom-select" id="filter_year">
                                            <?php
                                                //5 years back from current year or untill 2017 and one year after current
                                                for ($i=-1; $i < 10 ; $i++) { 
                                                    if(date('Y',strtotime('-'.$i.' year')) >= 2017 && date('Y',strtotime('-'.$i.' year')) <= 2018){
                                                        echo '<option value="'.date('Y',strtotime('-'.$i.' year')).'">'.date('Y',strtotime('-'.$i.' year')).'</option>';
                                                    }
                                                }
                                            ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="filter_department" class="col-lg-3">
                                        Department
                                    </label>                           
                                    <div class="col" >
                                        <select class="custom-select select" id="filter_department">
                                        	<option value="-1">Select Department</option>
                                            <?php
                                                if(!empty($department)) foreach($department as $row): ?>
                                                	<option value="<?php echo $row['id'] ?>"><?php echo $row['name'] ?></option>
                                            <?php endforeach ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="filter_name" class="col-lg-3">
                                        Staff Name
                                    </label>                           
                                    <div class="col" >
                                        <select class="custom-select select" id="filter_name">
                                        	<option value="-1">Select Staff</option>
                                            <?php
                                                if(!empty($staff)) foreach($staff as $row): ?>
                                                	<option value="<?php echo $row['id'] ?>"><?php echo $row['name'] ?></option>
                                            <?php endforeach ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="filter_status" class="col-lg-3">
                                        Status
                                    </label>
                                    <div class="col">
                                        <select class="form-control" id="filter_status">
                                            <option value="0">Active</option>
                                            <option value="1">InActive</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="filter_start_date" class="col-lg-3">
                                        Start Date
                                    </label>
                                    <div class="col">
                                        <input type="text" class="datepicker form-control" autocomplete="off" id="filter_start_date">
                                    </div>
                                     <label for="filter_start_date" class="col-lg-3">
                                        End Date
                                    </label>
                                    <div class="col">
                                        <input type="text" class="datepicker form-control" autocomplete="off" id="filter_end_date">
                                    </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">

                    <table class="table table-bordered p-0 m-0">
                        <thead>
                            <tr>
                                <th>Staff (Allocation Date)</th>
                                <!-- <th>Date</th> -->
                                <th>Allocation</th>
                                <!-- <th>Status</th> -->
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if($allocations) foreach ($allocations as $key => $allocate): ?>
                                
                              <tr>
                                    <td>
                                        <?php foreach ($staff as $key => $st): ?>
                                            <?php if ($st['id'] == $allocate->user_id): ?>
                                                    <?php if (!empty($st->file_location)): ?>
                                                        <img src="<?php echo base_url($st['file_location'].$st['file_name']); ?>" class="img img-rounded img-fluid">
                                                    <?php endif ?>
                                            <?php endif ?>
                                        <?php endforeach ?>
                                        <?php echo $allocate->name; ?> <br> 
                                        <b>(<?php echo date('d-M-Y',strtotime($allocate->start_date)); ?> - <?php echo date('d-M-Y',strtotime($allocate->end_date)); ?>)</b>

                                    </td>
                                    
                                    <td>
                                        <table class="p-0 m-0" style="width:100%;">
                                            <tr>
                                                <td></td><td></td>
                                                <td>
                                                    <b>Total</b>
                                                </td>
                                                <td>
                                                    <b>Month</b>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td><b>Outpatient Packge</b></td>
                                                <td><?php echo $allocate->outpatient_description; ?></td>
                                                <td>RM<?php echo number_format($allocate->claim_type_outpatient_amount,2);?></td>
                                                <td>RM<?php echo number_format($allocate->claim_type_outpatient_prorate_amount,2);?></td>
                                            </tr>
                                            <tr>
                                                <td><b>Hospitalization Package</b></td>
                                                <td><?php echo $allocate->hospitalization_description; ?></td>
                                                <td>RM<?php echo number_format($allocate->claim_type_hospitalization_amount,2);?></td>
                                                <td class="text-center"> - </td>
                                                <!-- <td>RM<?php echo number_format($allocate->claim_type_hospitalization_prorate_amount,2);?></td> -->

                                            </tr>
                                            <tr>
                                                <td><b>Room Package</b></td>
                                                <td><?php echo $allocate->room_description; ?></td>
                                                <td>RM<?php echo number_format($allocate->claim_type_room_package_amount,2); ?></td>
                                                <td class="text-center"> - </td>
                                                <!-- <td>RM<?php echo number_format($allocate->claim_type_room_package_prorate_amount,2); ?></td> -->

                                            </tr>
                                            <tr> 
                                                <td><b>Dental & Optical</b></td>
                                                <td><?php echo $allocate->dental_description; ?></td>
                                                <td>RM<?php echo number_format($allocate->claim_type_dental_optical_amount,2); ?></td>
                                                <td>RM<?php echo number_format($allocate->claim_type_dental_optical_prorate_amount,2); ?></td>

                                            </tr>
                                        </table>
                                        
                                    </td>

                                   <!--  <td>
                                        <?php if ($allocate->status == 0): ?>
                                            <span class="badge badge-success">Active</span>
                                        <?php endif ?>

                                        <?php if ($allocate->status == 1): ?>
                                            <span class="badge badge-danger">Inactive</span>
                                        <?php endif ?>

                                    </td> -->

                                    <td >
                                        <div class="btn-group">
                                            <button class="btn btn-info btn-sm" data-placement="top" title="Edit OT" data-toggle="modal" data-target="#edit_allocation" data-json="<?php echo base64_encode(json_encode($allocate)); ?>" onclick="fillModal(this)"><i class="fas fa-edit"></i></button>
                                            <button class="btn btn-danger btn-sm" data-toggle="tooltip" data-placement="top" title="delete OT" data-id="<?php echo $allocate->id; ?>" onclick="deleteAllocation(this)"><i class="fas fa-trash-alt"></i></button>
                                        </div>
                                    </td>
                                </tr>
                            <?php endforeach ?>

                        </tbody>
                    </table>

                </div>
            </div>
        </div>        
    </div>
</section>

<!-- Modal Add Allocation -->
<div class="modal fade" id="add_allocation" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-lg" style="min-width:80%;">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLongTitle">Add Allocation</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <form method="post" action="<?php echo base_url(''); ?>" id="addAllocation">
                <div class="modal-body">
                    <div class="row">
                        <div class="col">
                            <h3><b>Staff</b> <span class="text-danger">*</span></h3>
                        </div>
                    </div>
                    <div class="row form-group">
                        <table class="table table-hovered table-bordered" id="table_staff">
                            <thead>

                                <tr>
                                    <th class='no-sort'><input type="checkbox" onchange="allCheckbox()" id="check_all_id"></th>
                                    <!-- <th></th> -->
                                    <th class="col">Name</th>
                                    <th class="col">Department</th>
                                </tr>

                            </thead>
                            <tbody>
                                <?php if(!empty($staff)) foreach ($staff as $row): ?>
                                    <tr>
                                        <td><input type="checkbox" name="staff_id" class="staff_checked" value="<?php echo $row['id']?>"></td>
                                        <!-- <td></td> -->
                                        <td><?php echo $row['name']; ?></td>
                                        <td><?php echo $row['department_name']; ?></td>
                                    </tr>
                                <?php endforeach ?>
                            </tbody>
                        </table>
                    </div>

                    <div class="row">
                        <div class="col">
                            <h3><b>Allocation Type </b><span class="text-danger">*</span></h3>
                        </div>
                    </div>
                    <div class="row form-group">
                        <table class="table table-hovered table-bordered">
                            <thead>
                               
                                    <tr>
                                        <th><b>Allocation Type per Year</b></th>
                                        <th><b>Description</b></th>
                                        <th><b>Amount (RM) </b></th>
                                    </tr> 
                             
                            </thead>
                            <tbody>
                                <tr>
                                    <td><b>Outpatient Benefit (year)<span class="text-danger">*</span></b></td>
                                    <td>
                                        <select class="form-control" onchange="allocationSelect(this)" data-target="#id-add-outpatient-amount" name="outpatient_benefit">
                                            <option value="-1">Please select outpatient benefits</option>
                                            <?php if(!empty($outpatient)) foreach ($outpatient as $row): ?>
                                                <option value="<?php echo $row->id; ?>" data-amount="<?php echo number_format($row->amount,2); ?>" ><?php echo $row->description; ?></option>
                                            <?php endforeach ?>
                                        </select>
                                    </td>
                                    <td>
                                        <input type="text" class="form-control" id="id-add-outpatient-amount" disabled>
                                    </td>
                                </tr>
                                <tr>
                                    <td><b>Hospitalization Benefit (year) <span class="text-danger">*</span></b></td>
                                    <td>
                                        <select class="form-control" onchange="allocationSelect(this)" data-target="#id-add-hospitalization-amount" name="hospitalization_benefit">
                                            <option value="-1">Please select hospitalization benefits</option>
                                            <?php if(!empty($hospitalization)) foreach ($hospitalization as $row): ?>  
                                                <option value="<?php echo $row->id; ?>" data-amount="<?php echo number_format($row->amount); ?>" ><?php echo $row->description; ?></option> 
                                            <?php endforeach ?>
                                        </select>
                                    </td>
                                    <td>
                                        <input type="text" class="form-control" id="id-add-hospitalization-amount" disabled>
                                    </td>
                                </tr>
                                <tr>
                                    <td><b>Room Package <span class="text-danger">*</span></b></td>
                                    <td>
                                        <select class="form-control" onchange="allocationSelect(this)" data-target="#id-add-room-package" name="room_package">
                                            <option value="-1">Please select room package</option>
                                            <?php if(!empty($room)) foreach ($room as $row): ?>
                                                <option value="<?php echo $row->id; ?>" data-amount="<?php echo $row->amount; ?>"><?php echo $row->description; ?></option>
                                            <?php endforeach ?>
                                        </select>
                                    </td>
                                    <td>
                                        <input type="text" class="form-control" id="id-add-room-package" disabled>
                                    </td>
                                </tr>
                                <tr>    
                                    <td>
                                        <b>Dental & Optical Benefit (year) <span class="text-danger">*</span></b>
                                    </td>
                                    <td>
                                        <select class="form-control" onchange="allocationSelect(this)" data-target="#id-add-dental-optical" name="dental_and_optical">
                                            <option value="-1">Please select dental and optical benefits</option>
                                            <?php if(!empty($dental)) foreach ($dental as $row): ?>
                                                <option value="<?php echo $row->id; ?>" data-amount="<?php echo $row->amount; ?>" ><?php echo $row->description; ?></option>
                                            <?php endforeach ?>
                                        </select>
                                    </td>
                                    <td>
                                        <input type="text" class="form-control" id="id-add-dental-optical" disabled>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>

                    <div class="row form-group">
                        <div class="col-lg-3">
                            <b>Status</b> <span class="text-danger">*</span>
                        </div>
                        <div class="col">
                        	 <select class="form-control" name="status" onchange="isPass()">
                                <option value="0">Active</option>
                                <option value="1">Inactive</option>
                             </select>
                        </div>
                    </div>

	                <div class="row form-group">
	                    <div class="col-lg-3">
	                        <b>Start Date</b> <span class="text-danger">*</span>
	                    </div>
	                    <div class="col">
	                       <input type="text" class="form-control datepicker-mm-yyyy" id="start_date" autocomplete="off" name="start_date" onchange="isPass()" onkeyup="isPass()">
                           <br>
                            <small><span class="text-danger" id="notice_add_start_date"></span></small>
	                    </div>

                        <div class="col-lg-3">
                            <b>End Date</b> <span class="text-danger">*</span>
                        </div>
                        <div class="col">
                            <input type="text" class="form-control datepicker-mm-yyyy" id="end_date" autocomplete="off" name="end_date" onchange="isPass()" onkeyup="isPass()">
                            <br>
                            <small><span class="text-danger" id="notice_add_end_date"></span></small>
                        </div>
	                </div>

                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-success btn-sm" id="btn-modal-add" ><i class="fa fa-plus"></i> Add</button>
                    <button type="button" class="btn btn-danger btn-sm" data-dismiss="modal"><i class="fa fa-ban"></i> Cancel</button>
                </div>
            </form>
        </div>
    </div>
</div>
<!-- Modal Add Allocation -->

<!-- Modal edit allocation -->
<div class="modal fade" id="edit_allocation" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-lg" style="min-width:80%;">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLongTitle">Edit Allocation</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <form method="post" action="<?php echo base_url(); ?>" id="editAllocation">
                <div class="modal-body">

                    <input type="hidden" class="form-control" id="edit-allocation-id" name="id">
                    <input type="hidden" class="form-control" id="edit-hidden-user-id" name="user_id">
                    <div class="row form-group">
                        <div class="col-lg-3">
                            <label for="name">Name</label>
                        </div>
                        <div class="col">
                            <select class="form-control select" id="edit-allocation-user_id" placeholder="Select Staff" disabled>
                                <option value="-1">Select Staff</option>
                                <?php if(!empty($staff)) foreach ($staff as $row): ?>
                                    <option value="<?php echo $row['id'];?>"><?php echo $row['name'];?></option>
                                <?php endforeach ?>                                                        
                            </select>
                        </div>
                    </div> 

                    <div class="row">
                        <div class="col">
                            <h3><b>Allocation Type </b><span class="text-danger">*</span></h3>
                        </div>
                    </div>
                    <div class="row form-group">
                        <table class="table table-hovered table-bordered">
                            <thead>
                                <tr>
                                    <th><b>Allocation Type per Year</b></th>
                                    <th><b>Description</b></th>
                                    <th><b>Amount (RM) </b></th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td><b>Outpatient Benefit <span class="text-danger">*</span></b></td>
                                    <td>
                                        <select class="form-control" onchange="allocationSelect(this)" data-target="#id-edit-outpatient-amount" id="edit-allocation-outpatient-benefit" name="outpatient_id">
                                            <option value="-1">Please select outpatient benefits</option>
                                            <?php if(!empty($outpatient)) foreach ($outpatient as $row): ?>
                                                <option value="<?php echo $row->id; ?>" data-amount="<?php echo number_format($row->amount,2); ?>" ><?php echo $row->description; ?></option>
                                            <?php endforeach ?>
                                        </select>
                                    </td>
                                    <td>
                                        <input type="text" class="form-control" id="id-edit-outpatient-amount" name="year_outpatient" disabled >
                                    </td>
                                </tr>
                                <tr>
                                    <td><b>Hospitalization Benefit</b><span class="text-danger">*</span></td>
                                    <td>
                                        <select class="form-control" onchange="allocationSelect(this)" data-target="#id-edit-hospitalization-amount" id="edit-allocation-hospitalization-benefits" name="hospitalization_id">
                                            <option value="-1">Please select hospitalization benefits</option>
                                            <?php if(!empty($hospitalization)) foreach ($hospitalization as $row): ?>  
                                                <option value="<?php echo $row->id; ?>" data-amount="<?php echo number_format($row->amount); ?>" ><?php echo $row->description; ?></option> 
                                            <?php endforeach ?>
                                        </select>
                                    </td>
                                    <td>
                                        <input type="text" class="form-control" id="id-edit-hospitalization-amount" disabled name="year_hospitalization">
                                    </td>
                                </tr>
                                <tr>
                                    <td><b>Room Package</b><span class="text-danger">*</span></td>
                                    <td>
                                        <select class="form-control" onchange="allocationSelect(this)" data-target="#id-edit-room-package" id="edit-allocation-room" name="room_package_id">
                                            <option value="-1">Please select room package for hospitalization benefits</option>
                                            <?php if(!empty($room)) foreach ($room as $row): ?>
                                                <option value="<?php echo $row->id; ?>" data-amount="<?php echo $row->amount; ?>"><?php echo $row->description; ?></option>
                                            <?php endforeach ?>
                                        </select>
                                    </td>
                                    <td>
                                        <input type="text" class="form-control" id="id-edit-room-package" disabled name="year_room_package">
                                    </td>
                                </tr>
                                <tr>    
                                    <td>
                                        <b>Dental & Optical Benefit</b><span class="text-danger">*</span>
                                    </td>
                                    <td>
                                        <select class="form-control" onchange="allocationSelect(this)" data-target="#id-edit-dental-optical" id="edit-allocation-dental-optical" name="dental_optical_id">
                                            <option value="-1">Please select dental and optical benefits</option>
                                            <?php if(!empty($dental)) foreach ($dental as $row): ?>
                                                <option value="<?php echo $row->id; ?>" data-amount="<?php echo $row->amount; ?>" ><?php echo $row->description; ?></option>
                                            <?php endforeach ?>
                                        </select>
                                    </td>
                                    <td>
                                        <input type="text" class="form-control" id="id-edit-dental-optical" name="year_dental_optical" disabled>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>

                    <div class="row form-group">
                        <div class="col-lg-3">
                            <label for="status">Status</label>
                        </div>
                        <div class="col">
                             <select class="form-control" id="edit-allocation-status" name="status">
                                <option value="0">Active</option>
                                <option value="1">Inactive</option>
                             </select>
                        </div>
                    </div>

                    <div class="row form-group">
                        <div class="col-lg-3">
                            <label for="start_date">Start Date</label>
                        </div>
                        <div class="col">
                           <input type="text" class="form-control datepicker-mm-yyyy" name="start_date" id="edit-allocation-start_date" autocomplete="off">
                        </div>

                        <div class="col-lg-3">
                            <label for="end_date">End Date</label>
                        </div>
                        <div class="col">
                            <input type="text" class="form-control datepicker-mm-yyyy" name="end_date" id="edit-allocation-end_date" autocomplete="off">
                        </div>
                    </div> 
                </div>
                <div class="modal-footer">
                    <button type="submit" type="button"  class="btn btn-success btn-sm" style="margin-bottom:15px; width: 200px;"><i class="fa fa-save"></i> save</button>
                    <button type="button" class="btn btn-danger btn-sm" style="margin-bottom:15px; width: 200px;" data-dismiss="modal"><i class="fa fa-ban"></i> Cancel</button>
                </div>
            </form>
        </div>
    </div>
</div>
<!-- Modal edit allocation -->

<script>
    var token = '<?php echo $_SESSION['token']; ?>';
    var table = null;

    $(function(){
        
        $('.table').DataTable({
                destroy: true,
                "columnDefs": [{
                    "targets": [0],
                    "orderable": false,
                    'ordering': false,
                    'checkboxes': {
                        'selectRow': true
                    }
            }
        ]});


        table = $('#table_staff').DataTable({
                destroy: true,
                "columnDefs": [{
                    "targets": [0],
                    "orderable": false,
                    'ordering' : false,
                    'sortable': false,
                    'checkboxes': {
                        'selectRow': true
                    }
            }
        ]});


        $('#addAllocation').on('submit',function(event){
            event.preventDefault();
            var serialize = formIdToInputObject('#addAllocation');

            var url         = '<?php echo base_url('/hr/api/claim/createAllocation'); ?>';
            var type        = 'POST';
            var data        = {
                'token': token,
                'input':serialize
            };
            var redirect    = '<?php echo current_url(); ?>';
            ajxRequest(url, type, data, redirect);
        });


        $('#editAllocation').on('submit',function(event){
            event.preventDefault();
            var serialize = formIdToInputObject('#editAllocation');

            var url     = '<?php echo base_url('hr/api/claim/editAllocation'); ?>';
            var type    = 'POST';
            var data    = {
                'token': token,
                'input': serialize
            };
            var redirect = '<?php echo current_url(); ?>';
            // var redirect = null;
            ajxRequest(url, type, data, redirect);
        });

        $('#searchBtn').on('click',function(event){
            event.preventDefault();

            // var year        = $('#filter_year').val();
            // var department  = $('#filter_department').val();
            // var staff       = $('#filter_name').val();
            // var status      = $('#filter_status').val();
            // var start_date  = $('#filter_start_date').val();
            // var end_date    = $('#filter_end_date').val();

            console.log('searching..');
        });

    });


	$('.datepicker').datepicker({
		uiLibrary: 'bootstrap4',
		format: 'dd-mm-yyyy',
        defaultDate: '<?php echo date('d/m/Y'); ?>',
		autoclose: true,
		orientation: 'top'
	});

    function deleteAllocation(elem) {
        toastConfirm('Are you sure you want to remove this allocation',function () {
            var id  = $(elem).data('id');
            var url         = '<?php echo base_url('/hr/api/claim/removeAllocation'); ?>';
            var type        = 'POST';
            var data        = {
                'token': token,
                'id':id
            };
            var redirect    = '<?php echo current_url(); ?>';

            ajxRequest(url, type, data, redirect);

        });
    }

    function fillModal(elem) 
    {
        var json = JSON.parse(atob($(elem).data('json')));

        $('#editAllocation').trigger('reset');
        $('#edit-allocation-id').val(json.id);

        $('#edit-allocation-user_id').val(json.user_id).trigger('change');
        $('#edit-hidden-user-id').val(json.user_id);

        $('#edit-allocation-dental-optical').val(json.claim_type_dental_optical_id).trigger('change');
        $('#edit-allocation-hospitalization-benefits').val(json.claim_type_hospitalization_id).trigger('change');
        $('#edit-allocation-outpatient-benefit').val(json.claim_type_outpatient_id).trigger('change');
        $('#edit-allocation-room').val(json.claim_type_room_id).trigger('change');

        $('#edit-allocation-status').val(json.status).trigger('change');

        $('#edit-allocation-start_date').val(moment(json.start_date, 'YYYY-MM-DD').format('MM/YYYY'));
        $('#edit-allocation-end_date').val(moment(json.end_date, 'YYYY-MM-DD').format('MM/YYYY'));
    }

    //MODAL
    function allCheckbox() 
    {
        var bool = $('#check_all_id').is(':checked');
        rows = $('#table_staff').find('tbody tr');

        if(bool){
            $(".staff_checked").prop("checked",true);
            var rows = table.rows({ 'search': 'applied' }).nodes();
              $('input[type="checkbox"]', rows).prop('checked', this.checked);
              $(rows).each(function(index,value){
                console.log('loop rows');
                console.log(value);
              });

        }else{
            $(".staff_checked").prop("checked",false);
        }

    }

    function isPass() {
        var flag = true;
        var serialize = formIdToInputObject('#addAllocation');

        //check if staff_id is empty
        if(!serialize.staff_id){
            flag = false;
        }

        //check if allocation is set
         if(serialize.hospitalization_benefit == -1){
            flag = false;
        }

        if (serialize.dental_and_optical == -1) {
            flag = false;
        }

        if (serialize.outpatient_benefit == -1) {
            flag = false;
        }

        if (serialize.room_package == -1) {
            flag = false;
        }

        //check if approver is set 
        // if(serialize.approver_id == -1){
        //     flag = false;
        // }

        //check if startdate and end date is set
        if(serialize.start_date != '' && serialize.end_date != ''){
            //this part not sure why not working 
            if(moment(serialize.start_date,'MM/YYYY',true).isValid() == false){
                flag = false;
                $('#notice_add_start_date').html('Please enter a valid date format : day-month-year (<?php echo date('m/Y'); ?>) ');
            }else{
                $('#notice_add_start_date').empty();
            }

            if(moment(serialize.end_date,'MM/YYYY',true).isValid() == false){
                flag = false;
                $('#notice_add_start_date').html('Please enter a valid date format : day-month-year (<?php echo date('m/Y'); ?>) ');
            }else{
                $('#notice_add_start_date').empty();
            }

        }else{
            flag = false;
        }

        if(flag){
            $('#btn-modal-add').removeAttr('disabled');
        }else{
            $('#btn-modal-add').attr('disabled',true);
        }

    }

    function allocationSelect(elem) {
        var val     = $(elem).val();
        var target  = $(elem).data('target');
        if(val != -1){
            var amount = $(elem).find('option:selected').data('amount');
            $(target).val(amount);
        }else{
            $(target).val('0.00');
        }

        isPass();
    }


</script>