<div class="collapse" id="collapsePCB">	
	<div class="card">
		<div class="card-header text-center" role="tablist">
			<div class="nav nav-tabs nav-fill card-header-tabs">
				<a class="nav-item nav-link active" id="nav-account-tab" data-toggle="tab" href="#nav-account" role="tab" aria-controls="nav-account" aria-selected="false">Under Employee</a>
				<a class="nav-item nav-link" id="nav-audit-tab" data-toggle="tab" href="#nav-audit" role="tab" aria-controls="nav-audit" aria-selected="false">Under Spouse</a>
			</div>
		</div>
		<div class="card-body">
			<div class="tab-content" id="nav-tabContent">
				<div class="tab-pane fade show active" id="nav-account" role="tabpanel" aria-labelledby="nav-account-tab">
					<div class="container">
			            <div class="row">
			                <h4 class="card-title float-left">Under Employee</h4>
				            <?php if(strtotime(date('d-m-Y')) <= $end_date){ ?>    
				                <div class="btn-group ml-auto float-right">
				                    <button type="button" class="btn btn-primary btn-sm d-print-none <?php echo $btn_hidden; ?>" style="margin-bottom:15px; width: 200px;" onclick="openPcbConsiderationModal();" data-trigger="hover" data-tooltip="tooltip" title="Add New PCB Consideration Under Employee">
				                        <i class="fa fa-plus"></i> Add New
				                    </button>
				                </div>
				            <?php } ?>
			            </div>
			            <div class="row">
			            	<small><i style="color: red;">*Compulsory for employees with children</i></small>
			            </div>
			        </div>

			        <hr>
					
					<div class="table-responsive">
						<table id="table_pcb_employee" class="table table-hover table-bordered">
							<thead>
								<tr>
									<th>Name</th>
									<th>NRIC No</th>
									<th>Age</th>
									<th>Status</th>
									<th>Created At</th>
									<?php if($btn_hidden == ''){ ?>	
										<th class="d-print-none">Action</th>
									<?php } ?>
									
								</tr>
							</thead>
							<tbody>
								<?php $counter = 0; ?>
								<?php if($pcb) foreach ($pcb as $key => $row): ?>
									<?php if ($row->responsible_id == 0): ?>
									
									<tr class="link-row">
										<td><?php echo $row->name; ?></td>
										<td><?php echo $row->ic_no; ?></td>
										<td>
											<?php
												$birthYear = date('Y',strtotime($row->birthdate));
												$currentYear = date('Y');

												echo $currentYear - $birthYear.' Years'; 
											?>
										</td>
										<td>
											<label><b>OKU STATUS : </b></label>
											<?php if ($row->oku_status == 0): ?>
												YES <br>
											<?php else: ?>
												NO<br>
											<?php endif ?>

											<label><b>STUDENT STATUS : </b></label>
											<?php if ($row->study_status == 0): ?>
												YES <br>
											<?php else: ?>
												NO <br>
											<?php endif ?>
										</td>
										<td><?php echo $row->created_at; ?></td>
										<?php if($btn_hidden == ''){ ?>	
											<?php if(strtolower($row->type) == 'personal data update' && (date('m-Y',strtotime($row->created_at)) == date('m-Y', $live_date)) && (strtotime(date('d-m-Y')) <= $end_date)){ ?>
												<td class="text-center">
					                                <div class="btn-group">
					                                    <button type="button" class="btn btn-success btn-sm d-print-none" title="Edit PCB Consideration Under Employee" data-profilingID="<?php echo $row->profiling_id; ?>" onclick="editPcbConsideration(<?php echo $row->id; ?>)"><span class="fa fa-edit"></span></button>
														<button type="button" class="btn btn-danger btn-sm d-print-none" onclick="deletePcbConsideration(<?php echo $row->id; ?>)" title="Delete PCB Consideration Under Employee"><span class="fa fa-trash"></span></button>
					                                </div>
					                            </td> 
					                        <?php } else { ?>
					                            <td class="text-center">
					                                <div class="btn-group" role="group">
					                                    <button class="btn btn-success btn-sm" disabled style="cursor: not-allowed;" data-trigger="hover" data-tooltip="tooltip" title="Not allowed to update"><i class="fas fa-edit" ></i></button>
					                                    <button class="btn btn-danger btn-sm" disabled style="cursor: not-allowed;" data-trigger="hover" data-tooltip="tooltip" title="Not allowed to delete"><i class="fas fa-trash-alt"></i></button>
					                                </div>
					                            </td> 
					                        <?php } ?>
										<?php } ?>
									</tr>
									<?php endif ?>
								<?php endforeach ?>
							</tbody>
						</table>
					</div>
				</div>


				<div class="tab-pane fade" id="nav-audit" role="tabpanel" aria-labelledby="nav-audit-tab">
					<div class="container">
			            <div class="row">
			                <h4 class="card-title float-left">Under Spouse</h4>
				            <?php if(strtotime(date('d-m-Y')) <= $end_date){ ?>    
				                <div class="btn-group ml-auto float-right">
				                    <button type="button" class="btn btn-primary btn-sm d-print-none <?php echo $btn_hidden; ?>" style="margin-bottom:15px; width: 200px;" onclick="openPcbConsiderationModal();" data-trigger="hover" data-tooltip="tooltip" title="Add New PCB Consideration Under Spouse">
				                        <i class="fa fa-plus"></i> Add New
				                    </button>
				                </div>
				            <?php } ?>
			            </div>
			            <div class="row">
			            	<small><i style="color: red;">*Compulsory for employees with children</i></small>
			            </div>
			        </div>

			        <hr>
			
					<div class="table-responsive">
						<table id="table_pcb_spouse" class="table table-hover table-bordered">
							<thead>
								<tr>
									<th>Name</th>
									<th>NRIC No</th>
									<th>Age</th>
									<th>Status</th>
									<th>Created At</th>
									<?php if($btn_hidden == ''){ ?>	
										<th class="d-print-none">Action</th>
									<?php } ?>
								</tr>
							</thead>
							<tbody>
								<?php $counter = 0; ?>
								<?php if($pcb) foreach ($pcb as $key => $row): ?>
								
									<?php if ($row->responsible_id == 1): ?>
									<tr class="link-row">
										<td><?php echo $row->name; ?></td>
										<td><?php echo $row->ic_no; ?></td>
										<td>
											<?php
												$birthYear = date('Y',strtotime($row->birthdate));
												$currentYear = date('Y');
												echo $currentYear - $birthYear.' Years'; 
											?>
										</td>
										<td>
											<label><b>OKU STATUS : </b></label>
											<?php if ($row->oku_status == 0): ?>
												YES <br>
											<?php else: ?>
												NO<br>
											<?php endif ?>

											<label><b>STUDENT STATUS</b></label>
											<?php if ($row->study_status == 0): ?>
												YES <br>
											<?php else: ?>
												NO <br>
											<?php endif ?>
										</td>
										<td><?php echo $row->created_at; ?></td>
										<?php if($btn_hidden == ''){ ?>
											<?php if(strtolower($row->type) == 'personal data update' && (date('m-Y',strtotime($row->created_at)) == date('m-Y', $live_date)) && (strtotime(date('d-m-Y')) <= $end_date)){ ?>
												<td class="text-center">
					                                <div class="btn-group">
					                                    <button type="button" class="btn btn-success btn-sm d-print-none" title="Edit PCB Consideration Under Spouse" data-profilingID="<?php echo $row->profiling_id; ?>" onclick="editPcbConsideration(<?php echo $row->id; ?>)"><span class="fa fa-edit"></span></button>
														<button type="button" class="btn btn-danger btn-sm d-print-none" onclick="deletePcbConsideration(<?php echo $row->id; ?>)" title="Delete PCB Consideration Under Spouse"><span class="fa fa-trash"></span></button>
					                                </div>
					                            </td> 
					                        <?php } else { ?>
					                            <td class="text-center">
					                                <div class="btn-group" role="group">
					                                    <button class="btn btn-success btn-sm" disabled style="cursor: not-allowed;" data-trigger="hover" data-tooltip="tooltip" title="Not allowed to update"><i class="fas fa-edit" ></i></button>
					                                    <button class="btn btn-danger btn-sm" disabled style="cursor: not-allowed;" data-trigger="hover" data-tooltip="tooltip" title="Not allowed to delete"><i class="fas fa-trash-alt"></i></button>
					                                </div>
					                            </td> 
					                        <?php } ?>
										<?php } ?>
									</tr>
									<?php endif ?>
								<?php endforeach ?>
							</tbody>
						</table>
					</div>
				</div>

				
			</div>
		</div>
	</div><br>			
</div>

<div class="modal fade d-print-none" role="dialog" aria-hidden="true" id="pcbConsiderationModal">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="racks-modal-title"></h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
			<form id="pcbConsiderationForm" class="form">
				<div class="modal-body">
						<div class="form-row">
							<div class="col-12">
								<label for="pcb_name">Name</label>
								<select class="select form-control" name="pcb_name" id="pcb_name"></select>
							</div>
						</div>
						<div class="form-row">
							<div class="col-12">
								<div class="form-group">
								<label for="pcb_responsibility" class="col-form-label">Responsibility</label>
								<select class="select form-control" name="pcb_responsibility" id="pcb_responsibility">
									<option value="0">Employee</option>
									<option value="1">Spouse</option>
								</select>
								</div>
							</div>
						</div>
				</div>
				<div class="modal-footer">
					<div class="btn-group float-right">
						<button type="button" class="btn btn-light" data-dismiss="modal">Cancel</button>
						<button type="button" id="savePcbConsiderationButton" class="btn btn-primary">Save</button>
					</div>
				</div>
			</form>
        </div>
    </div>
</div>

<script type="text/javascript">
	function openPcbConsiderationModal()
	{

		clearForm("#pcbConsiderationForm");

		$('#pcb_name').html('');
		$('#pcbConsiderationModal').find('#racks-modal-title').text('Add Pcb Consideration');
        $('#savePcbConsiderationButton').attr('onclick', 'savePcbConsideration()');
		$('#pcbConsiderationModal').modal('show');

		postData = {'user_id' : <?php echo $this->session->userdata('user')['id'] ?>};

		$.ajax({
			type: 'POST',
			url: '<?php echo base_url('hr/Myprofile/getPcbConsideration/1'); ?>',
			data: postData,
			dataType: 'JSON',
			success: function(data) {
				if(data.status == 'success')
				{
					var existing_data = data.pcb_consideration.existing_pcb_consideration;
					var pcb_consideration = data.pcb_consideration;
					
					$("<option/>", {
						text: 'Please Select',
						value: '-1',
						selected: true,
						disabled: true,
					}).appendTo('#pcb_name');

					if(pcb_consideration != null && pcb_consideration != undefined && pcb_consideration != '')
					{	
						$.each(pcb_consideration, function(key, content) 
						{
							if(content.relation == 2)
							{
								var flag = true;

								if(existing_data != null && existing_data != undefined && existing_data != '')
								{
									$.each(existing_data, function(e_key, e_content) 
									{
										if(e_content.family_id == content.id)
										{
											flag = false;
										}
									});
								}

								// if(flag)
								// {	
									$("<option/>", {
										text: content.name,
										value: content.id
									}).appendTo('#pcb_name');
								// }
							}
						});
					}
				}
			}
		});
	}

	function savePcbConsideration()
	{
		var postData = {
			'profiling_id' 		: $("#profiling_id_new").val(),
			'family_id' 				: $("#pcb_name").val(),
			// 'ic' 				: $("#pcb_ic").val(),
			// 'dob' 				: $("#pcb_dob").val(),
			// 'status' 			: $("#pcb_status").val(),
			'responsible_id' 	: $("#pcb_responsibility").val(),
		};

		$.ajax({
			type: 'POST',
			url: '<?php echo base_url('hr/Myprofile/savePcbConsideration'); ?>',
			data: postData,
			dataType: 'JSON',
			success: function(data) {
				if(data.status == 'success')
				{
					localStorage.setItem("status",data.status);
					localStorage.setItem("msg",data.msg);

					location.replace(data.redirect);
				}
			}
		});
	}
		
	function editPcbConsideration(pcb_id)
	{
		clearForm("#pcbConsiderationForm");
		var profiling_id = $("#profiling_id_new").val();

		$('#pcb_name').html('');
		$('#pcbConsiderationModal').find('#racks-modal-title').text('Edit Pcb Consideration');
        $('#savePcbConsiderationButton').attr('onclick', 'save_editPcbConsideration('+ pcb_id +')');
		$('#pcbConsiderationModal').modal('show');

		$.ajax({
			type: 'POST',
			url: '<?php echo base_url('hr/Myprofile/getPcbConsideration'); ?>',
			data: {
				'pcb_id' : pcb_id,
				'profiling_id' : profiling_id
			},
			dataType: 'JSON',
			success: function(data) {
				if ($.trim(data.status) == "success")
                {
                	// <option value="-1" selected disabled>Please Select</option>
                    $.each(data.pcb_consideration.family_list, function(key, content) 
					{
						if(content.relation == 2)
						{
							$("<option/>", {
								text: content.name,
								value: content.id
							}).appendTo('#pcb_name');
						}
					});

					$("#pcb_name").val(data.pcb_consideration.pcb_detail[0]['family_id']);
					$("#pcb_responsibility").val(data.pcb_consideration.pcb_detail[0]['responsible_id']);

                    // $("#emergency_address").val(case_emergency_data[0]['address']);
                    // $("#emergency_relation").val(case_emergency_data[0]['relation']);
                    // $("#emergency_phone").val(case_emergency_data[0]['phone_no']);
                    // $("#emergency_email").val(case_emergency_data[0]['email']);
                }
			}
		});

	}

	function save_editPcbConsideration(pcb_id) 
	{
		var postData = {
			'pcb_id' 			: pcb_id,
			'profiling_id' 		: $("#profiling_id_new").val(),
			'family_id' 		: $("#pcb_name").val(),
			// 'ic' 				: $("#pcb_ic").val(),
			// 'dob' 				: $("#pcb_dob").val(),
			// 'status' 			: $("#pcb_status").val(),
			'responsible_id' 	: $("#pcb_responsibility").val(),
		};

		$.ajax({
			type: 'POST',
			url: '<?php echo base_url('hr/Myprofile/save_editPcbConsideration'); ?>',
			data: postData,
			dataType: 'JSON',
			success: function(data) {
				if(data.status == 'success')
				{
					localStorage.setItem("status",data.status);
					localStorage.setItem("msg",data.msg);

					location.replace(data.redirect);
				}
			}
		});
	}

	function deletePcbConsideration(pcb_id)
	{
		toastConfirm("Are you sure you want to delete this Pcb Consideration?", function(){

			var profiling_id = $("#profiling_id_new").val();

			$.ajax({
				type: 'POST',
				url: '<?php echo base_url('hr/Myprofile/deletePcbConsideration'); ?>',
				data: {
					'pcb_id' : pcb_id,
					'profiling_id' : profiling_id
				},
				dataType: 'JSON',
				success: function(data) {

					if(data.status == 'success')
					{
						localStorage.setItem("status",data.status);
						localStorage.setItem("msg",data.msg);

						location.replace(data.redirect);
					}
				}
			});
		})
	}
</script>