<div class="container">
	<div class="row">
		<div class="col">
			<h4><b style="color: red;">*</b><b>FAMILY INFORMATION</b>
				<button type="button" class="btn btn-default btn-sm d-print-none <?php echo $btn_hidden; ?>" onclick="openFamilyInfoModal()" title=" Add New Family Member"><span class="fa fa-plus" style="color: #007bff;"></span></button>
			</h4>
			<small><i>Please add/update your family info. </i></small>
			<small><i><b style="color: red;">* Please add at least one Next of Kin from your family members.</b></i></small>
			<div class="pull-right btn-group">
			</div>
		</div>
	</div>

	<div class="row">
		<div class="col">

			<table class="table table-bordered">
				<thead>
					<tr>
						<th>DETAIL</th>
						<th>STATUS</th>
						<th class="d-print-none <?php echo $btn_hidden; ?>">ACTION</th>
					</tr>
				</thead>
				<tbody>
					<?php if(!empty($family_info)) foreach ($family_info as $key => $fmly_info): ?>
						<tr>
							<td>
								<b>NAME</b>: <?php echo ($fmly_info->name)?strtoupper($fmly_info->name):'N/A'; ?><br>
								<b>NRIC</b>: <?php echo ($fmly_info->ic_no)?strtoupper($fmly_info->ic_no):'N/A'; ?> <br>
								<b>RELATION</b>: <?php echo ($fmly_info->relations)?strtoupper($fmly_info->relations):'N/A'?> <br>
								<!-- <b>BIRTHDATE</b>: <?php echo ($fmly_info->birthdate)?strtoupper(date('d-F-Y',strtotime($fmly_info->birthdate))):'N/A'; ?> <br> -->
								<b>BIRTHDATE</b>: <?php echo (mysql_to_date_format($fmly_info->birthdate))?strtoupper(mysql_to_date_format($fmly_info->birthdate)):'N/A'; ?> <br>
								
								<b>OCCUPATION</b>: <?php echo ($fmly_info->occupation)?strtoupper($fmly_info->occupation):'N/A'; ?> <br>
								<b>EMAIL</b>: <?php echo ($fmly_info->email)?$fmly_info->email:'N/A'; ?> <br>
								<b>CONTACT NO</b>: <?php echo ($fmly_info->phone_no)?strtoupper($fmly_info->phone_no):'N/A'; ?> <br>
								<b>ADDRESS</b>: <?php echo ($fmly_info->address)?strtoupper($fmly_info->address):'N/A'; ?> <br>
								<b>ACADEMIC LEVEL</b>: <?php echo ($fmly_info->level_academic)?strtoupper($fmly_info->level_academic):'N/A'; ?> <br>
								<b>ACADEMIC INSTITUTION</b>: <?php echo ($fmly_info->academic_institution)?strtoupper($fmly_info->academic_institution):'N/A'; ?>
							</td>
							<td>
								<b>OKU STATUS </b> <?php echo ($fmly_info->oku_status == 0)?'YES':'NO'; ?> <br>
								<b>STUDENT STATUS </b> <?php echo ($fmly_info->study_status == 0)?'YES':'NO'; ?>
							</td>
							<td class="d-print-none <?php echo $btn_hidden; ?>">
								<div class="btn-group" role="group">
									<button id="editFamilyInfoButton" type="button" class="btn btn-default btn-sm d-print-none" onclick="editFamilyInfo(<?php echo $fmly_info->id; ?>)" title="Edit fmly_info" data-profilingID="<?php echo $fmly_info->profiling_id; ?>" data-userID="<?php echo $fmly_info->user_id; ?>"><span class="fa fa-edit" style="color: #F0AD4E;"></span></button>
									<button type="button" class="btn btn-default btn-sm d-print-none" onclick="deleteFamilyInfo(<?php echo $fmly_info->id; ?>)" title="Delete fmly_info"><span class="fa fa-trash" style="color: #D9534F;"></span></button>
								</div>
							</td>
						</tr>
					<?php endforeach ?>
				</tbody>
			</table>
		</div>
	</div>
</div>

<!-- Modal -->
<div class="modal fade" id="familyInfoModal">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="racks-modal-title">Add Family Member</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
			<div class="modal-body">
				<form id="familyInfoForm" class="form">
					<div class="form-row">
						<div class="col-12 col-sm-6">
							<label for="familyInfo_name">Name</label>
							<input type="text"class="form-control" name="familyInfo_name" id="familyInfo_name">
						</div>
						<div class="col-12 col-sm-6">
							<label for="familyInfo_relation">Relation</label>
							<select class="form-control" name="familyInfo_relation" id="familyInfo_relation">
								<?php foreach ($relations as $key => $relation): ?>
									<option value="<?php echo $relation->id; ?>"><?php echo $relation->relations; ?></option>
								<?php endforeach ?>
							</select>
						</div>
					</div>
					<div class="form-row">
						<div class="col-12 col-sm-6">
							<label for="familyInfo_nric">NRIC</label>
							<input type="text"class="form-control" name="familyInfo_nric" id="familyInfo_nric">
						</div>
						<div class="col-12 col-sm-6">
							<label for="familyInfo_bod">Birthdate</label>
							<input class="datepicker form-control" name="familyInfo_bod" id="familyInfo_bod">
						</div>
					</div>
					<div class="form-row">
						<div class="col-12 col-sm-6">
							<label for="familyInfo_phone" class="col-form-label">Phone No</label>
							<input type="text" class="form-control" name="familyInfo_phone" id="familyInfo_phone">
						</div>
						<div class="col-12 col-sm-6">
							<label for="familyInfo_email" class="col-form-label">Email</label>
							<input type="text" class="form-control" name="familyInfo_email" id="familyInfo_email">
						</div>
					</div>
					<div class="form-row">
						<div class="col-12">
							<label for="familyInfo_address" class="col-form-label">Address</label>
							<input type="text" class="form-control" name="familyInfo_address" id="familyInfo_address">
						</div>
					</div>
					<div class="form-row">
						<div class="col-12 col-sm-6">
							<label for="familyInfo_oku" class="col-form-label">OKU Status (Yes/No)</label>
							<select class="form-control" name="familyInfo_oku" id="familyInfo_oku">
								<option value="0">Yes</option>
								<option value="1">No</option>
							</select>
						</div>
						<div class="col-12 col-sm-6">
							<label for="familyInfo_student" class="col-form-label">Student (Yes/No)</label>
							<select class="form-control" name="familyInfo_student" id="familyInfo_student">
								<option value="0">Yes</option>
								<option value="1">No</option>
							</select>
						</div>
					</div>
					<div class="form-row d-none" id="sect-family-occupation">
						<div class="col-12">
							<label for="familyInfo_occupation">Occupation</label>
							<input type="text"class="form-control" name="familyInfo_occupation" id="familyInfo_occupation">
						</div>
					</div>
					<div class="form-row">
						<div class="col-12">
							<label for="familyInfo_academic">Academic Level</label>
							<select class="form-control" name="familyInfo_academic" id="familyInfo_academic">
								<?php foreach ($qualifications as $key => $qualification): ?>
									<option value="<?php echo $qualification->id; ?>"><?php echo $qualification->level; ?></option>
								<?php endforeach ?>
							</select>
						</div>
					</div>
					<div class="form-row">
						<div class="col-12">
							<label for="familyInfo_institution" class="col-form-label">Academic Institution</label>
							<input type="text" class="form-control" name="familyInfo_institution" id="familyInfo_institution">
						</div>
					</div>
				</form>
			</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="saveFamilyInfoButton" onclick="saveFamilyInfo();" class="btn btn-primary">Save</button>
				</div>
			</div>
		</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">

	$('#familyInfo_student').change(function(){

		let select = $('#familyInfo_student').val();

		if (select == 1) {
			$('#sect-family-occupation').removeClass('d-none');
			$('#sect-family-occupation').addClass('d-block');
		}else {
			$('#sect-family-occupation').addClass('d-none');
			$('#sect-family-occupation').removeClass('d-block');
		}
	});

	function openFamilyInfoModal()
	{

		clearForm("#familyInfoForm");

		$('#familyInfoModal').find('#racks-modal-title').text('Add Family Member');
        $('#saveFamilyInfoButton').attr('onclick', 'saveFamilyInfo()');
		$('#familyInfoModal').modal('show');
	}

	function saveFamilyInfo()
	{
		var postData = {
			'profiling_id' 	:$("#profiling_id_new").val(),
			'name' 			: $("#familyInfo_name").val(),
			'ic_no' 		: $("#familyInfo_nric").val(),
			'relation' 		: $("#familyInfo_relation").val(),
			'birthdate' 	: $("#familyInfo_bod").val(),
			'phone_no' 		: $("#familyInfo_phone").val(),
			'email' 		: $("#familyInfo_email").val(),
			'address' 		: $("#familyInfo_address").val(),
			'oku_status' 			: $("#familyInfo_oku").val(),
			'study_status' 			: $("#familyInfo_student").val(),
			'academic_level' 		: $("#familyInfo_academic").val(),
			'academic_institution' 	: $("#familyInfo_institution").val(),
			'occupation' 			: $("#familyInfo_occupation").val(),
		};

		$.ajax({
			type: 'POST',
			url: '<?php echo base_url('hr/Myprofile/saveFamilyInfo'); ?>',
			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 editFamilyInfo(familyInfo_id)
	{
		clearForm("#familyInfoForm");
		var profiling_id = $("#profiling_id_new").val();

		$.ajax({
			type: 'POST',
			url: '<?php echo base_url('hr/Myprofile/getFamilyInfo'); ?>',
			data: {
				'familyInfo_id' : familyInfo_id,
				'profiling_id'  : profiling_id
			},
			dataType: 'JSON',
			success: function(data) {
				if ($.trim(data.status) == "success")
                {
                    var familyInfoData = data.familyInfo;

                    $("#familyInfo_name").val(familyInfoData[0]['name']);
                    $("#familyInfo_address").val(familyInfoData[0]['address']);
                    $("#familyInfo_nric").val(familyInfoData[0]['ic_no']);
                    $("#familyInfo_relation").val(familyInfoData[0]['relation']);
                    $("#familyInfo_bod").val(moment(familyInfoData[0]['birthdate']).format('DD-MM-YYYY'));
                    $("#familyInfo_phone").val(familyInfoData[0]['phone_no']);
                    $("#familyInfo_email").val(familyInfoData[0]['email']);
					$("#familyInfo_oku").val(familyInfoData[0]['oku_status']);
					$("#familyInfo_student").val(familyInfoData[0]['study_status']);
					$("#familyInfo_academic").val(familyInfoData[0]['academic_lavel']);
					$("#familyInfo_institution").val(familyInfoData[0]['academic_institution']);
					$("#familyInfo_occupation").val(familyInfoData[0]['occupation']);

					if(familyInfoData[0]['study_status'] == 0){
						$('#sect-family-occupation').addClass('d-none');
						$('#sect-family-occupation').removeClass('d-block');
					}else{
						$('#sect-family-occupation').removeClass('d-none');
						$('#sect-family-occupation').addClass('d-block');
					}
					
                }
			}
		});

		$('#familyInfoModal').find('#racks-modal-title').text('Edit Family Member');
        $('#saveFamilyInfoButton').attr('onclick', 'save_editFamilyInfo('+ familyInfo_id +')');
		$('#familyInfoModal').modal('show');
	}

	function save_editFamilyInfo(familyInfo_id) 
	{
		var postData = {
			'id'		: familyInfo_id,
			'name' 		: $("#familyInfo_name").val(),
			'ic_no' 	: $("#familyInfo_nric").val(),
			'relation' 	: $("#familyInfo_relation").val(),
			'birthdate' : $("#familyInfo_bod").val(),
			'phone_no' 	: $("#familyInfo_phone").val(),
			'email' 	: $("#familyInfo_email").val(),
			'address' 	: $("#familyInfo_address").val(),
			'oku_status' 			: $("#familyInfo_oku").val(),
			'study_status' 			: $("#familyInfo_student").val(),
			'academic_level' 		: $("#familyInfo_academic").val(),
			'academic_institution' 	: $("#familyInfo_institution").val(),
			'occupation' 			: $("#familyInfo_occupation").val(),
		};

		$.ajax({
			type: 'POST',
			url: '<?php echo base_url('hr/Myprofile/save_editFamilyInfo'); ?>',
			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 deleteFamilyInfo(familyInfo_id)
	{
		toastConfirm("Are you sure you want to delete this family member?", function(){

			$.ajax({
				type: 'POST',
				url: '<?php echo base_url('hr/Myprofile/deleteFamilyInfo'); ?>',
				data: {
					'id' : familyInfo_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>	