<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="printLeaveReport()" 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 Leave 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">Leave Details Report</h4>
				<hr>
				<form id="filterForm">
					<div class="form-row">
						<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_leaveType">Leave Type</label>
							<select id="filter_leaveType" type="text" class="form-control form-control-sm">
								<option value="" selected="selected">ALL</option>
								<?php foreach($leave_types as $leaveType){ ?>
	                                <option 
	                                <?php if($leaveType['id']) { 
	                            		//echo 'selected';
	                            	}?> 
	                            	value="<?php echo $leaveType['id']; ?>"><?php echo $leaveType['name']; ?></option>
	                            <?php } ?>
							</select>
						</div>
					</div>
					<div class="form-row">
						<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="1">Pending</option>
								<option value="2">Approved</option>
								<option value="3">Rejected</option>
								<option value="4">Canceled</option>
							</select>
						</div>
						<div class="col">
							<label for="filter_year">Year</label>
							<select class="form-control form-control-sm" id="filter_year">
			                    <?php foreach($years as $year): ?>
			                        <option <?php if($year['year'] == $curr_year) echo 'selected'; ?> value="<?php echo $year['year'] ?>"><?php echo $year['year'] ?></option>
			                    <?php 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_LeaveReport" class="table table-hover table-bordered">
						<thead>
							<tr>
								<th>No</th>
								<th>Department</th>
                                <th>Name</th>
                                <th>Leave Type</th>
                                <th width="20%">Date [Days]</th>
                                <th>Date Applied</th>
                                <th>Status</th>
							</tr>
						</thead>
						<tbody>
                                <?php if(isset($applications)): ?>
                                    <?php $i = 1; ?>
                                    <?php foreach($applications as $application): ?>
                                        <tr id="application-<?php echo $application['id']; ?>" data-leave="<?php echo htmlspecialchars(json_encode($application), ENT_QUOTES, 'UTF-8'); ?>">
                                            <td><?php echo $i++; ?></td>
                                            <td><?php echo $application['dept_name']; ?>[<?php echo $application['branch_name']; ?>]</td>
                                            <td><?php echo $application['user_name']; ?></td>
                                            <td><?php echo $application['leave_type']; ?></td>
                                            <td><?php echo date_format(date_create($application['date_from']),"d/m/y").' - '.date_format(date_create($application['date_to']),"d/m/y").' ['.$application['days_applied'].']'; ?></td>
                                            <td><?php echo date_format(date_create($application['created_ts']),"d/m/y"); ?></td>
                                            <td class="text-center">
                                                <?php if ($application['status'] == 1): ?>
                                                    <a class="badge badge-warning" data-trigger="hover" data-tooltip="tooltip"><?php echo $application['leave_status'] ?></a>
                                                <?php endif; ?>
                                                <?php if ($application['status'] == 2): ?>
                                                    <a class="badge badge-success" data-trigger="hover" data-tooltip="tooltip"><?php echo $application['leave_status'] ?></a>
                                                <?php endif; ?>
                                                <?php if ($application['status'] == 3): ?>
                                                    <a class="badge badge-danger" data-trigger="hover" data-tooltip="tooltip"><?php echo $application['leave_status'] ?></a>
                                                <?php endif; ?>
                                                <?php if ($application['status'] == 4): ?>
                                                    <a class="badge badge-secondary" data-trigger="hover" data-tooltip="tooltip"><?php echo $application['leave_status'] ?></a>
                                                <?php endif; ?>
                                            </td>
                                        </tr>
                                    <?php endforeach; ?>
                                <?php endif; ?>
                            </tbody>
					</table>
				</div>
			</div>
		</div>
	</section>
</div>

<script type="text/javascript">

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

      	var postData = {
            'department_id'	: $('#filter_department').val(),
            'staff_id'		: $('#filter_staff').val(),
            'leaveType_id'	: $('#filter_leaveType').val(),
            'status'		: $('#filter_status').val(),
            'year'		    : $('#filter_year').val(),
        };

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

                  	if(value.status == 1){
                  		status = '<span class="badge badge-warning text-white">Pending</span>';
                  	}else if(value.status == 2){
                  		status = '<span class="badge badge-success text-white">Approved</span>';
                  	}else if(value.status == 3){
                  		status = '<span class="badge badge-danger text-white">Rejected</span>';
                  	}else if(value.status == 4){
                  		status = '<span class="badge badge-secondary text-white">Canceled</span>';
                  	}
                  	
                    var row = oTable.fnAddData([
                    ++index,
                    value.deptName,
                    value.staffName,
                    value.leave_type,
                    value.date,
                    value.date_applied,
					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 printLeaveReport()
	{
		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_leaveType').val() == ""){ 
			var leaveType = "ALL"; 
		} else { 
			var leaveType = $('#filter_leaveType').val(); 
		};

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

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


		window.open("<?php echo base_url().'hr/Leave_report/printLeaveReport/'?>" + department + "/" + staff + "/" + leaveType + "/" + status + "/" + year , "_blank");
	}

</script>