<div class="container">
	<div class="row">
		<div class="col">
			<h4><b style="color: red;">*</b><b>PCB CONSIDERATION</b>
				<button type="button" class="btn btn-default btn-sm d-print-none <?php echo $btn_hidden; ?>" onclick="openPcbConsiderationModal()" title=" Add New PCB Consideration"><span class="fa fa-plus" style="color: #007bff;"></span></button>
			</h4>
			<small><i>Please add/update your PCB Consideration.</i></small>
			<small><i><b style="color: red;">* Compulsory for employees with children.</b></i></small>
		</div>
	</div>
	<div class="row">
		<div class="col">
			<table class="table table-bordered">
				<thead>
					<tr>
						<th>NAME</th>
						<th>NRIC</th>
						<th>AGE</th>
						<th>STATUS</th>
						<th>RESPONSIBILITY</th>
						<th class="d-print-none <?php echo $btn_hidden; ?>">ACTION</th>
					</tr>
				</thead>
				<tbody>
					<?php if($pcb_consideration) foreach ($pcb_consideration as $key => $pcb): ?>
						<tr>
							<td><?php echo (!empty($pcb->name))?strtoupper($pcb->name):'N/A'; ?></td>
							<td><?php echo (!empty($pcb->ic_no))?$pcb->ic_no:'N/A'; ?></td>
							<td>
								<?php 
									$age = 0;
									if (!empty(mysql_to_date_format($pcb->birthdate))) {
										$birth = date('Y',strtotime($pcb->birthdate));
										$current = date('Y');
										$age = $current - $birth;
									}
									echo $age;
								?>
							</td>
							<td>
								<b>OKU STATUS</b>: <?php echo ($pcb->oku_status == 1)?'NO':'YES'; ?> <br>
								<b>STUDENT STATUS</b>: <?php echo ($pcb->study_status == 1)?'NO':'YES'; ?> <br>
							</td>
							<td>
								<?php echo ($pcb->responsible_id == 1)?'SPOUSE':'EMPLOYEE'; ?>
							</td>
							<td class="text-center d-print-none <?php echo $btn_hidden; ?>">
								<div class="btn-group">
									<button type="button" class="btn btn-default btn-sm d-print-none" title="Edit Emergency Contact" data-profilingID="<?php echo $pcb->profiling_id; ?>" onclick="editPcbConsideration(<?php echo $pcb->id; ?>)"><span class="fa fa-edit" style="color: #F0AD4E;"></span></button>
									<button type="button" class="btn btn-default btn-sm d-print-none" onclick="deletePcbConsideration(<?php echo $pcb->id; ?>)" title="Delete Emergency Contact"><span class="fa fa-trash" style="color: #D9534F;"></span></button>
								</div>
							</td>
						</tr>
					<?php endforeach ?>
				</tbody>
			</table>
		</div>
	</div>
</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 class="col-12 col-sm-6">
								<label for="pcb_ic">IC No</label>
								<input type="text"class="form-control" name="pcb_ic" id="pcb_ic">
							</div> -->
						</div>
						<!-- <div class="form-row">
							<div class="col-12 col-sm-6">
								<label for="pcb_dob" class="col-form-label">Date Of Birth</label>
								<input type="text" class="form-control" name="pcb_dob" id="pcb_dob">
							</div>
							<div class="col-12 col-sm-6">
								<label for="pcb_status" class="col-form-label">Status</label>
								<input type="text" class="form-control" name="pcb_status" id="pcb_status">
							</div>
						</div> -->
						<div class="form-row">
							<div class="col-12">
								<div class="form-group">
								<label for="pcb_responsibility" class="col-form-label">Responsibility</label>
								<!-- <input type="text" class="form-control" name="pcb_responsibility" id="pcb_responsibility"> -->
								<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>
<?php  
//1. script for modal address
//2. script to ajax load for update address
//3. script to refresh table address
//4. script document ready to refresh table 
//5. script to save and refresh table after save 
?>
<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')
				{
					$.each(data.pcb_consideration, function(key, content) 
					{
						if(content.relation == 2)
						{
							$("<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")
                {
                    $.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>