<div class="container-fluid" style="padding-bottom: 2rem;">
	<div class="card bg-light-transparent">  
		<div class="card-header bg-white">
			<div class="float-left">
				<h4>Talent Referral Lists</h4>
				<a href="<?php echo base_url('hr/serviceform/talent_referral_form'); ?>" class="btn btn-outline-dark btn-sm" title="Add New talent_referral" ><i class="fa fa-plus"></i> Add New Request</a>

				<a href="<?php echo base_url('hr/Serviceform_report/talent_referral_print')?>" target="_blank" class="btn btn-outline-dark btn-sm" title="Print"><i class="fa fa-print"></i>Print</a>

				<a 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</a>
			</div>
		</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">
								<?php if($_SESSION['module_data']['role'] == 'admin' || $_SESSION['user']['division_id'] == 5){ ?>	
									<div class="form-group row">
										<label for="sv_talent_referral_filter_dept" class="col-lg-3">
											Department
										</label>                           
										<div class="col-lg-4" >
											<select class="custom-select" name="sv_talent_referral_filter_dept" id="sv_talent_referral_filter_dept">
												<option value="-1">[All]</option>
												<?php if($department) foreach($department as $row){ ?>
													<option value="<?php echo $row['id']; ?>"><?php echo $row['name'].' ['.$row['branch_name'].']'; ?></option> 
												<?php } ?>
											</select>
										</div>
									</div>
								<?php } ?>
								<div class="form-group row">
									<label for="sv_talent_referral_filter_status" class="col-lg-3">
										Status
									</label>                           
									<div class="col-lg-4" >
										<select class="custom-select" id="sv_talent_referral_filter_status" name="sv_talent_referral_filter_status">
											<option value="-1">[All]</option>
											<option value="Pending">Pending</option>
											<option value="In_progress">Processed</option>
											<option value="Verified">Verified</option>
											<option value="Rejected">Rejected</option>
										</select>
									</div>
								</div>
								<div class="form-group row">
									<label for="sv_talent_referral_filter_year" class="col-lg-3">
										Year
									</label>                           
									<div class="col-lg-4" >
										<input type="text" name="sv_talent_referral_filter_year" id="sv_talent_referral_filter_year" class="form-control datepicker-yyyy">
									</div>
								</div>
								<div class="form-group row">
									<label for="sv_talent_referral_filter_month" class="col-lg-3">
										Requested Month
									</label>                           
									<div class="col-lg-4" >
										<input type="text" name="sv_talent_referral_filter_month" id="sv_talent_referral_filter_month" class="form-control datepicker-mm">
									</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="tab-content">
			<div class="tab-pane fade active show" id="tableView" role="tabpanel" aria-labelledby="nav-table-tab">
				<div class="card-body">
					<div class="card no-shadow">
						<div class="card-body">
							<div class="table-responsive">
								<table id="filter_table_talent_referral" class="table table-bordered p-0 m-0" width="100%" id="table_talent_referral">
									<thead>
										<th class="text-center" width="5%">#</th>
										<th class="text-center" width="38%">Employee's Information</th>
										<th class="text-center" width="38%">Candidate's Information</th>
										<th class="text-center" width="12%">Status</th>
										<th class="text-center" width="7%">Action</th>         
									</thead>
									<tbody>
										<?php if($talentReferralList) foreach($talentReferralList as $key => $row) { ?>
											<tr>
												<td class="text-center"><?php echo $key+1; ?></td>
												<td>
													<b>Name</b> : <?php echo $row['requester_name'].' ['.$row['requester_email'].']'; ?><br>
													<b>Department</b> : <?php echo $row['dept_name'] . ' [' . $row['branchName'].']'; ?><br>
													<b>Date</b> : <?php echo date('d-m-Y',strtotime($row['createdTS'])); ?>
												</td>
												<td>
													<b>Name</b> : <?php echo $row['candidate_name'].' ['.$row['candidate_email'].']'; ?><br>
													<b>Position Applied</b> : <?php echo $row['candidate_position']; ?><br>
													<b>Telephone Number</b> : <?php echo $row['candidate_contact']; ?>
												</td>
												<td class="text-center">
													<?php 
													if(($row['status_app']==null || $row['status_app']=='Pending'))
													{
														$status = 'Pending <i class="fa fa-exclamation-circle" style="color: #FFC107;"></i>';
														$disabled = '';
													}
													if($row['status_app'] == 'In_progress')
													{
														$status = 'Processed <i class="fa fa-hourglass-start" style="color: grey;"></i>';
														$disabled = 'disabled style="cursor: not-allowed;"';
													}
													if($row['status_app'] == 'Verified')
													{
														$status = $row['status_app'].' <i class="fa fa-check-circle" style="color: green;"></i><p style="font-style: italic; font-size: 13px;"></p>';
														$disabled = 'disabled style="cursor: not-allowed;"';
													}
													if($row['status_app'] == 'Completed')
													{
														$status = $row['status_app'].' <i class="fa fa-check-circle" style="color: green;"></i>';
														$disabled = 'disabled style="cursor: not-allowed;"';
													}
													if($row['status_app'] == 'Rejected')
													{
														$status = $row['status_app'].' <i class="fa fa-times-circle" style="color: red;"></i>';
														$disabled = 'disabled style="cursor: not-allowed;"';
													}

													echo $status;
													?>
												</td>
												<td class="text-center"> 
													<div class="btn-group btn-group-sm" role="group">
														<a href="<?php echo base_url('hr/serviceform/talent_referral_form/'.$row['ID'].'/view'); ?>" target="_blank" class="btn btn-outline-dark" onclick="" data-trigger="hover" data-tooltip="tooltip" title="View Detail"><i class="far fa-eye"></i></a>

														<?php if(($_SESSION['user']['id'] == $row['requester_id']) || ($_SESSION['user']['id'] == $row['createdBY'])){ ?>
															<?php if($disabled == ''){ ?>
																<a href="<?php echo base_url('hr/serviceform/talent_referral_form/'.$row['ID']); ?>" target="_blank" class="btn btn-outline-dark" onclick="" data-trigger="hover" data-tooltip="tooltip" title="Edit"><i class="far fa-edit"></i></a>
															<?php }else{ ?>
																<button class="btn btn-outline-dark" data-trigger="hover" data-tooltip="tooltip" title="Edit" disabled style="cursor: not-allowed;"><i class="far fa-edit"></i></button>
															<?php } ?>
														<?php } ?>

														<a class="btn btn-outline-dark" data-trigger="hover" data-tooltip="tooltip" title="Print Detail" href="<?php echo base_url('hr/serviceform_report/talent_referral_print/'.$row['ID']); ?>" target="_blank" class="btn-outline-dark"><i class="fa fa-print"></i></a>

														<?php if(($_SESSION['user']['id'] == $row['requester_id']) || ($_SESSION['user']['id'] == $row['createdBY'])){ ?>
															<button id="deleteButton" type="button" class="btn btn-outline-dark" onclick="deleteData(<?php echo $row['ID']; ?>);" data-trigger="hover" data-tooltip="tooltip" title="Delete" <?php echo ($disabled == '')?'':'disabled style="cursor: not-allowed;"'; ?>><i class="far fa-trash-alt"></i></button>
														<?php } ?>
													</div>
												</td>
											</tr>
										<?php } ?>
									</tbody>
								</table>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>      
	</div>
</div>


<script>
	function deleteData(ID) {
		validateForm("#deleteButton", "Delete this from Talent Referral Program form list?", function() {
			$.ajax({
				url:'<?php echo base_url('hr/serviceform/talent_referral_delete'); ?>',
				data: {
					talentReferralID : ID,
				},
				type: 'post',
				dataType: 'json',
				success: (data) => {
					if(data.status == "Success") {

						localStorage.setItem("status", data.status);
						localStorage.setItem("msg", data.msg);

						location.reload();
					}
				}
			});
		});
	}

	$(document).ready(function(){
		<?php $this->session->unset_userdata('sv_talent_referral_filter_dept'); ?>
		<?php $this->session->unset_userdata('sv_talent_referral_filter_status'); ?>
		<?php $this->session->unset_userdata('sv_talent_referral_filter_year'); ?>
		<?php $this->session->unset_userdata('sv_talent_referral_filter_month'); ?>
		<?php $this->session->unset_userdata('sv__talent_referral'); ?>

		$('#searchBtn').click(function(){
			getData();
		});
	});

	function getData() 
	{
		var table = $('#filter_table_talent_referral').DataTable();
		table.clear().draw();
		var frmData = new FormData();

		frmData.append('sv_talent_referral_filter_dept',$('#sv_talent_referral_filter_dept').val());
		frmData.append('sv_talent_referral_filter_status',$('#sv_talent_referral_filter_status').val());
		frmData.append('sv_talent_referral_filter_year',$('#sv_talent_referral_filter_year').val());
		frmData.append('sv_talent_referral_filter_month',$('#sv_talent_referral_filter_month').val());

		$.ajax({
			url:"<?php echo base_url('/hr/api/serviceform/get_talent_referral'); ?>",
			type:'post',
			dataType:'json',
			data:frmData,
			processData: false,
			contentType: false,
			success:function(data)
			{
				iziToast.success({
					position: 'bottomRight',
					message: 'filtering data',
				});

				let talent_referral = data.table;

				if(talent_referral != null)
				{
					$.each(talent_referral,function(key,content)
					{
						console.log(talent_referral);
						var i = key;

						var btn_view = '<a href="<?php echo base_url(); ?>hr/serviceform/talent_referral_form/'+content.ID+'/view" target="_blank" class="btn btn-outline-dark" onclick="" data-trigger="hover" data-tooltip="tooltip" title="View Detail"><i class="far fa-eye"></i></a>';

						var btn_print = '<a type="button" class="btn btn-outline-dark" href="<?php echo base_url();?>hr/serviceform_report/talent_referral_print/'+content.ID+'" target="_blank" data-trigger="hover" data-tooltip="tooltip" title="Print Detail"><i class="fa fa-print"></i></a>';

						if(content.status_app == null || content.status_app == 'Pending')
						{
							var status      = 'Pending <i class="fa fa-exclamation-circle" style="color: #FFC107;"></i>';
							var disabled    = '';
						}
						if(content.status_app == 'In_progress')
						{
							var status      = 'Processes <i class="fa fa-hourglass-start" style="color: grey;"></i>';
							var disabled    = 'disabled style="cursor: not-allowed;"';
						}
						if(content.status_app == 'Verified')
						{
							var status      = content.status_app+' <i class="fa fa-check-circle" style="color: green;"></i><p style="font-style: italic; font-size: 13px;">'+content.status_reason+'</p>';
							var disabled    = 'disabled style="cursor: not-allowed;"';
						}
						if(content.status_app == 'Completed')
						{
							var status      = content.status_app+' <i class="fa fa-check-circle" style="color: green;"></i><p style="font-style: italic; font-size: 13px;">'+content.status_reason+'</p>';
							var disabled    = 'disabled style="cursor: not-allowed;"';
						}
						if(content.status_app == 'Rejected')
						{
							var status      = content.status_app+' <i class="fa fa-times-circle" style="color: red;"></i>';
							var disabled    = 'disabled style="cursor: not-allowed;"';
						}

						if(disabled == '')
						{
							var btn_edit = '<a type="button" class="btn btn-outline-dark" href="<?php echo base_url();?>hr/serviceform/talent_referral_form/'+content.ID+'" data-trigger="hover" data-tooltip="tooltip" title="Edit"><i class="far fa-edit"></i></a>';
						}else{
							var btn_edit = '<button type="button" class="btn btn-outline-dark" data-trigger="hover" data-tooltip="tooltip" title="Edit"'+disabled+'><i class="far fa-edit"></i></button>';
						}

						var btn_delete = '<button type="button" class="btn btn-outline-dark" onclick="deleteButton('+content.ID+')" data-trigger="hover" data-tooltip="tooltip" title="Delete"'+disabled+'><i class="far fa-trash-alt"></i></button>';

						if(('<?php echo $_SESSION['user']['id']; ?>' == content.requester_id) || ('<?php echo $_SESSION['user']['id']; ?>' == content.createdBY))
						{
							var btn = btn_view+btn_edit+btn_print+btn_delete;
						}else{
							var btn = btn_view+btn_print;
						}

						var createdTS = moment(content.createdTS).format("DD-MM-YYYY");

						table.row.add({
							0   : '<div class="text-center">'+(i+1)+'</div>',
							1	: '<b>Name</b> : '+content.requester_name+' ['+content.requester_email+']<br>\
							<b>Department</b> : '+content.dept_name+' ['+content.branchName+']<br>\
							<b>Date</b> : '+createdTS,
							2	: '<b>Name</b> : '+content.candidate_name+' ['+content.candidate_email+']<br>\
							<b>Position Applied</b> : '+content.candidate_position+'<br>\
							<b>Telephone Number</b> : '+content.candidate_contact,
							3	: '<div class="text-center">'+status+'</div>',
							4	: '<div class="text-center"><div class="btn-group btn-group-sm" role="group">'+btn+'</div></div>',
						}).draw();
						console.log(data);
					});                   
				}

			},
			error:function(){
				iziToast.warning({
					position: 'bottomRight',
					message: '500 Internal Server Error.'
				});
			}
		});
	}
</script>