<section class="container" style="padding-bottom: 2rem;">
	<div class="card bg-light-transparent">  
        <div class="card-header bg-white">
            <h5>Panel Clinic Claim</h5>
            <a href="#" class="btn btn-outline-dark btn-sm" title="Add New Panel Clinic" data-toggle="modal" data-target="#add_panel">
                <i class="fa fa-plus"></i> Add New Claim
            </a>
            <a href="<?php echo base_url('hr/claim/printClaim/'); ?>" target="_blank" class="btn btn-outline-dark btn-sm" title="Print Panel Clinic">
                <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="row">
                                    <div class="col">
                                        <div class="form-group row">
                                            <label for="filter_year" class="col-lg-2">
                                                Year
                                            </label>                           
                                            <div class="col-lg" >
                                                <select class="custom-select" id="filter_year">
                                                    <option value="<?php echo date('Y'); ?>" selected><?php echo date('Y'); ?></option>
                                                    <?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>
                                    <div class="col">
                                        <div class="form-group row">
                                            <label for="filter_year" class="col-lg-2">
                                                Month
                                            </label>                           
                                            <div class="col-lg" >
                                                <select name="filter_month" id="filter_month" class="form-control">
                                                    <option value="-1" disabled>Select Month</option>
                                                      <?php if(!empty($month)) foreach ($month as $row): ?>
                                                            <?php
                                                            if ($row->id == date('n')) {
                                                                ?>
                                                                <option value="<?php echo $row->id;?>" selected><?php echo $row->month;?></option>
                                                            
                                                                <?php
                                                            }else{
                                                                 ?>
                                                                <option value="<?php echo $row->id;?>"><?php echo $row->month;?></option>
                                                                    
                                                                <?php
                                                            }
                                                            ?>
                                                        <?php endforeach ?> 
                                                            <option value="-1">All</option>   
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-lg-6">
                                        <div class="form-group row">
                                            <label for="filter_year" class="col-lg-2">
                                                Clinic
                                            </label>                           
                                            <div class="col-lg" >
                                                <select class="custom-select" id="filter_clinic">
                                                    <option>All</option>
                                                    <?php foreach ($panels as $key => $panel): ?>
                                                        <option value="<?php echo $panel->id; ?>"><?php echo $panel->name; ?></option>
                                                    <?php endforeach ?>
                                                </select>
                                            </div>
                                        </div>
                                    </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-hover table-bordered p-0 m-0">
                    	<thead>
                    		<tr>
                    			<th class="text-center">#</th>
                    			<th class="text-center">Year</th>
                    			<th class="text-center">Month</th>
                    			<th class="text-center">Clinic Name</th>
                    			<th class="text-center">Invoice Chit</th>
                    			<th class="text-center">Total</th>
                    		
                    			<th class="text-center">Action</th>
                    		</tr>
                    	</thead>
                        <tbody>
                            <?php if(!empty($claims)) foreach ($claims as $key => $claim): ?>
                                <tr>
                                    <td><?php echo $key+1; ?></td>
                                    <td><?php echo (!empty($claim->invoice_year))?$claim->invoice_year:null; ?></td>
                                    <td><?php echo (!empty($claim->invoice_month))? date('F', mktime(0, 0, 0, $claim->invoice_month, 10)):null; ?></td>
                                    <td><?php echo (!empty($claim->panel_name))?$claim->panel_name:null; ?></td>
                                    <td><?php echo (!empty($claim->invoice_chit))?$claim->invoice_chit:null; ?></td>
                                    <td><?php echo (!empty($claim->total_amount))?'RM'.number_format($claim->total_amount,2):'RM'.number_format(0,2) ?></td>
                                    
                                    <td>

                                        <div class="btn-group">
                                            <a href="<?php echo base_url('hr/claim/openAttachment/'.$claim->attachment_id.'/PANEL_CLINIC'); ?>" class="btn btn-warning btn-sm" target="_blank">
                                                <i class="fas fa-paperclip text-white"></i>
                                            </a>
                                            <a href="<?php echo base_url('hr/claim/panelClaimDetail/'.$claim->id)?>" class="btn btn-success btn-sm" data-toggle="tooltip" data-placement="top" title="View Clinic Detail">
                                                <i class="fas fa-sign-in-alt"></i>
                                            </a>
                                        </div>

                                    </td>
                                </tr>
                            <?php endforeach ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>        
    </div>
</section>


<!-- Modal add new panel clinic claim -->
<div class="modal fade" id="add_panel" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">

            <div class="modal-header">
                <h5 class="modal-title">Add New Panel Clinic Claim</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('hr/claim/addPanelClinic') ?>" enctype="multipart/form-data">
                <div class="modal-body">        
                	
                    <div class=" form-group">
                        <label>Invoice Chit <span class="text-danger">*</span></label>               
                        <input required data-parsley-error-message="Hospital/Clinic Name is required." type="text" class="form-control" name="inovice_chit" id="add_invoice_chit" placeholder="Enter Invoice Chit" >
                    </div>

                    <div class=" form-group">
                        <label for="clinic_name">Clinic Name <span class="text-danger">*</span></label>               
                        <select name="clinic_name" id="clinic_name" class="form-control">
                            <?php foreach ($panels as $key => $panel): ?>
                                <option value="<?php echo $panel->id; ?>"><?php echo $panel->name; ?></option>
                            <?php endforeach ?>
                        </select>                        
                    </div>
                    
                    <div class=" form-group">
                        <label for="clinic_name">Date of Invoice <span class="text-danger">*</span></label>               
                        <input required data-parsley-error-message="Hospital/Clinic Name is required." type="text" class="datepicker form-control" name="date_inovice" id="add_date_invoice" placeholder="Enter Date of Invoice" autocomplete="off">
                    </div>

                    <div class="form-group">
                        <label>Attachment</label>
                        <input type="file" class="form-control" name="files">
                    </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-plus"></i> Add
                    </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 add new panel clinic  -->

<script>

    $(function(){
        
        $('#searchBtn').click(function(){
            //1. destroy datatables
            var table   = $('.table').DataTable();
            table.clear().draw();
    
            //2. create ajax requests
           $.ajax({
                url: '<?php echo base_url('hr/api/Claim/filter_panel'); ?>',
                type: 'POST',
                data:{
                    token:'<?php echo $this->session->userdata('token'); ?>',
                    year: $('#filter_year').val(),
                    month: $('#filter_month').val(),
                    clinic: $('#filter_clinic').val()
                },
                dataType: 'JSON',
                success: function(data)
                {
                    console.log(data);
                    $.each(data,function(index,value){

                        //3. create datatables
                        let actions = `
                        <div class="btn-group">
                            <a href="<?php echo base_url(); ?>/hr/claim/openAttachment/${value.attachment_id}/PANEL_CLINIC" class="btn btn-warning btn-sm" target="_blank">
                                <i class="fas fa-paperclip text-white"></i>
                            </a>
                            <a href="<?php echo base_url()?>/hr/claim/panelClaimDetail/${value.id}" class="btn btn-success btn-sm" data-toggle="tooltip" data-placement="top" title="View Clinic Detail">
                                <i class="fas fa-sign-in-alt"></i>
                            </a>
                        </div>
                        `;

                        table.row.add({
                            0   : index+1,
                            1   : value.invoice_year,
                            2   : value.invoice_month,  
                            3   : value.panel_name,
                            4   : value.invoice_chit,
                            5   : accounting.formatMoney(value.total_amount,"RM", 2, ",", "."),
                            6   : actions,
                        }).draw();
                    });
                   

                }
           });

        });
    });

    $(".datepicker").datepicker( {
        format: "mm-yyyy",
        startView: "months", 
        minViewMode: "months",
        orientation: "bottom"
    });

	// document.getElementById('master_yes').onchange = function() {
	// 	document.getElementById('group').disabled = this.checked;
	// 	$('#group').val("");
	// };

	// document.getElementById('master_no').onchange = function() {
	// 	document.getElementById('group').disabled = !this.checked;
	// };

    
</script>