<div class="container-fluid" style="">

	<div class="jumbotron">
		<div class="row">
			<div class="col-md-4 text-center align-self-center">
				<img class="image-responsive" src="<?php echo base_url($company['file_location'].$company['file_name']); ?>" style="max-height: 15rem; max-width: 15rem;">
			</div>
			<div class="col-md-8">
				<h1 class="display-4" id="cardName"><?php echo $company['name']; ?></h1>
				<p class="lead" id="cardProfile"><?php echo $company['profile']; ?></p>
				<hr class="my-4">
				<p id="cardPhone"><?php echo $company['phone']; ?></p>
				<p><a id="cardWebsite" target="_blank" href="<?php echo 'http://'.$company['website']; ?>"><?php echo $company['website']; ?></a></p>
			</div>
		</div>
	</div>
	<div class="row">
		<div class="col">
			<button onclick="printClaimReport()" type="button" class="btn btn-dark btn-round btn-sm pull-right" style="height:100px; width: 100%; ">
				<h5 class="card-title"><i class="fa fa-print"></i> Print</h5>
				<p class="card-text">Print OT Claim Details Report.</p>
			</button>
		</div>
	</div>

	<section style="padding-top: 2rem; padding-bottom: 2rem;">
		<div class="card">
			<div class="card-body">
				<h4 class="card-title">OT Claim Details Report</h4>
				<hr>
				<form id="filterForm">
					<div class="form-row">
						<div class="col">
							<label for="filter_be">Business Entity</label>
							<select name="filter_be" id="filter_be" class="form-control form-control-sm" onchange="changeStaffList(this)">
								<option value="">ALL</option>  
                                    <?php if(!empty($businessEntity)) foreach ($businessEntity as $row): ?>
                                        <option value="<?php echo $row['id'];?>"><?php echo $row['name']; ?></option>
                                    <?php endforeach ?> 
                            </select>
						</div>
						<div class="col">
							<label for="filter_department">Department</label>
							<select name="filter_department" id="filter_department" class="form-control form-control-sm" onchange="changeStaffList(this)">
								<option value="">ALL</option>  
                                    <?php if(!empty($department)) foreach ($department as $row): ?>
                                        <option value="<?php echo $row['id'];?>"><?php echo $row['name']; ?> [<?php echo $row['branch_name']; ?>]</option>
                                    <?php endforeach ?> 
                            </select>
						</div>
						<div class="col">
							<label for="filter_staff">Staff Name</label>
							<select type="text" id="filter_staff" class="form-control form-control-sm">
								<option value="" selected="selected">ALL</option>
	                            <?php foreach($staff as $staff){ ?>
	                                <option 
	                                <?php if($staff['id']) { 
	                            		//echo 'selected';
	                            	}?> 
	                            	value="<?php echo $staff['id']; ?>"><?php echo $staff['name']; ?></option>
	                            <?php } ?>
							</select>
						</div>
						<div class="col">
							<label for="filter_status">Status</label>
							<select id="filter_status" type="text" class="form-control form-control-sm">
								<option value="-1" selected="selected">ALL</option>
								<option value="0">Pending</option>
								<option value="1">Submitted</option>
								<option value="2">Verified</option>
							</select>
						</div>
						<div class="col">
							<label for="filter_paid_status">Paid Status</label>
							<select id="filter_paid_status" type="text" class="form-control form-control-sm">
								<option value="-1" selected="selected">ALL</option>
								<option value="0">Unpaid</option>
								<option value="1">Paid</option>
							</select>
						</div>
					</div>
					<div class="form-row">
						<div class="col">
							<label for="filter_year">Year</label>
							<select id="filter_year" type="text" class="form-control form-control-sm">
								<option value="" selected="selected">ALL</option>
                                    <?php if($yearList) foreach($yearList as $row){
                                    echo '<option value="'.$row->year.'" ';
                                    //echo ($row->year == date('Y'))?'selected':''; 
                                    echo ' >'.$row->year.'</option>'; 
                                }
                                ?>
							</select>
						</div>
						<div class="col">
							<label for="filter_month">Month</label>
							<select id="filter_month" type="text" class="form-control form-control-sm">
								<option value="" selected="selected">ALL</option>
	                             <?php if(!empty($month)) foreach ($month as $row): 
                                    echo '<option value="'. $row->id.'" ';
                                    //echo ($row->id == date('n'))?'selected':''; 
                                    echo ' >'. $row->month.'</option>'; 
                                 endforeach ?> 
							</select>
						</div>
						<div class="col">
							<label for="filter_submit"><br></label>
							<button type="button" class="btn btn-primary btn-sm btn-block" id="filter_submit" name="filter_submit"><i class="fas fa-search"></i> Submit</button>
						</div>
					</div>
				</form><br><hr>
				<div class="table-responsive">
					<table id="table_ClaimReport" class="table table-hover table-bordered">
						<thead>
							<tr>
								<th class="text-center">#</th>
								<th class="text-center">Business Entity</th>
								<th class="text-center">Department</th>
								<th class="text-center">Staff Name</th>	
								<th class="text-center">Year</th>
								<th class="text-center">Month</th>
								<th class="text-center">Total Hours Verified</th>
								<th class="text-center">Total Convert CO (Hours)</th>
								<th class="text-center">Total Claim (RM)</th>
								<th class="text-center">Claim Status</th>
								<th class="text-center">Paid Status</th>
							</tr>
						</thead>
						<tbody>
							<?php 
								$i = 1;
								if($claimDetail) foreach($claimDetail as $row) {  ?>
									<tr>
										<td scope="row"><small><?php echo $i; ?></small></td>
										<td><small><?php echo $row->be_name; ?></small></td>
										<td><small><?php echo $row->deptName; ?></small></td>
										<td><small><?php echo $row->staffName; ?></small></td>
										<td><small><?php echo $row->year; ?></small></td>
										<td><small><?php echo date("F", mktime(0, 0, 0, $row->month, 10)); ?></small></td>
										<td><small><?php echo $row->totalHoursApproved; ?></small></td>
										<td><small><?php echo $row->totalConvertToLeave; ?></small></td>
										<td><small><?php echo $row->totalClaim; ?></small></td>
										<td class=""><?php if($row->claim_status == 0) { ?>
                                                <span class="badge badge-warning text-white">PENDING</span><br>
                                            <?php } else if($row->claim_status == 1) { ?>
                                                <span class="badge badge-info text-white">SUBMITTED</span><br>
                                            <?php } else { ?>
                                                <span class="badge badge-success text-white">VERIFIED</span><br>
                                            <?php } ?>
										</td>
										<td class=""><?php if($row->paid_status == 0) { ?>
                                                <span class="badge badge-warning text-white">UNPAID</span><br>
                                            <?php } else { ?>
                                                <span class="badge badge-success text-white">PAID</span><br>
                                            <?php } ?>
										</td>
									</tr>
								    <?php
	                                $i++;
	                        } ?>
						</tbody>
					</table>
				</div>
			</div>
		</div>
	</section>
</div>

<script type="text/javascript">

	$('#ot_start_date, #ot_end_date').datepicker({
		uiLibrary: 'bootstrap4',
		format: 'dd-mm-yyyy'
	});

	$('#filter_submit').click(function(){
      $('#table_ClaimReport').dataTable().fnDraw();

      	var postData = {
      		'be_id'			: $('#filter_be').val(),
            'department_id'	: $('#filter_department').val(),
            'staff_id'		: $('#filter_staff').val(),
            'status'		: $('#filter_status').val(),
            'paid_status'	: $('#filter_paid_status').val(),
            'month'			: $('#filter_month').val(),
            'year'			: $('#filter_year').val()
        };

        $.ajax({
          type:'POST',
          dataType:'json',
          url: '<?php echo base_url(); ?>hr/Overtime_report/fnLoadClaimDetailList',
          data: postData, 
          success: function(data) {
             var oTable = $('#table_ClaimReport').dataTable();
              oTable.fnClearTable();
              if ($.trim(data.status)== 'success') {
                  $.each(data.message,function(index, value){

                  	if(value.claim_status == 0){
                  		claim_status = '<span class="badge badge-warning text-white">PENDING</span>';
                  	}else if(value.claim_status == 1){
                  		claim_status = '<span class="badge badge-info text-white">SUBMITTED</span>';
                  	}else{
                  		claim_status = '<span class="badge badge-success text-white">VERIFIED</span>';
                  	}

                  	if(value.paid_status == 0){
                  		paid_status = '<span class="badge badge-warning text-white">UNPAID</span>';
                  	}else{
                  		paid_status = '<span class="badge badge-success text-white">PAID</span>';
                  	}
                  	
                    var row = oTable.fnAddData([
                    '<small>'+ ++index + '</small>',
                    '<small>'+ value.be_name + '</small>',
                    '<small>'+ value.deptName + '</small>',
                    '<small>'+ value.staffName + '</small>',
                    '<small>'+ value.year + '</small>',
                    '<small>'+ moment().month(value.month - 1).format("MMMM") + '</small>',
					'<small>'+ value.totalHoursApproved + '</small>',
					'<small>'+ value.totalConvertToLeave + '</small>',
					'<small>'+ value.totalClaim + '</small>',
					claim_status,
					paid_status
                    ]);
                  });
                  oTable.fnDraw();
                    } else {
                  console.log(data.message);
                    }
          },
          error: function(data){
          console.log('failed ' + data);
          }
        });
    });

	function changeStaffList(obj) 
    {
        var id = obj.value;

        $.ajax({
            type: "POST",
            url: "<?php echo base_url() ?>hr/Overtime_report/getStaffByDept",
            data: "dept_id=" + id,
            dataType: "json",
            success: function(content) {

                if (content.status == "success") {

                    var items = [];
                    items.push('<option>ALL</option>');
                    for (var i = 0; i < content.message.length; i++) {
                        items.push('<option value="' + content.message[i].id + '">'
                                + content.message[i].name + '</option>');
                    }
                    jQuery("#filter_staff").empty();
                    jQuery("#filter_staff").append(items.join('</br>'));
                } else {
                    $("#error").html('<p>' + content.message + '</p>');
                }
            }
        });
        return false;
    }

    function printClaimReport()
	{
		if($('#filter_be').val() == ""){ 
			var be = "ALL"; 
		} else { 
			var be = $('#filter_be').val(); 
		};

		if($('#filter_department').val() == ""){ 
			var department = "ALL"; 
		} else { 
			var department = $('#filter_department').val(); 
		};

		if($('#filter_staff').val() == ""){ 
			var staff = "ALL"; 
		} else { 
			var staff = $('#filter_staff').val(); 
		};

		if($('#filter_status').val() == "-1"){ 
			var claim_status = "-1"; 
		} else { 
			var claim_status = $('#filter_status').val(); 
		};

		if($('#filter_paid_status').val() == "-1"){ 
			var paid_status = "-1"; 
		} else { 
			var paid_status = $('#filter_paid_status').val(); 
		};

		if($('#filter_month').val() == ""){ 
			var month = "ALL"; 
		} else { 
			var month = $('#filter_month').val(); 
		};

		if($('#filter_year').val() == ""){ 
			var year = "ALL"; 
		} else { 
			var year = $('#filter_year').val(); 
		};


		window.open("<?php echo base_url().'hr/Overtime_report/printClaimReport/'?>" + be + "/" + department + "/" + staff + "/" + claim_status + "/" + paid_status + "/" + month + "/" + year , "_blank");
	}

</script>