<div class="container">
	<div class="row">
		<div class="col">
			<h4><b style="color: red;">*</b><b>EMERGENCY CONTACT</b>
			<button type="button" class="btn btn-default btn-sm d-print-none <?php echo $btn_hidden; ?>" onclick="openEmergencyModal()" title=" Add New Emergency Contact"><span class="fa fa-plus" style="color: #007bff;"></span></button></h4>
			<small><i><b style="color: red;">* Please add/update at least one of your family members/housemate.</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>NAME</th>
						<th>RELATIONSHIP</th>
						<th class="d-print-none <?php echo $btn_hidden; ?>">ACTION</th>
					</tr>
				</thead>
				<tbody>
					<?php if(!empty($rs_case_or_emergency)) foreach ($rs_case_or_emergency as $key => $row): ?>
						<tr>
							<td>
								<b>NAME</b>: <?php echo ($row->name)?strtoupper($row->name):'N/A'; ?><br>
								<b>EMAIL</b>: <?php echo ($row->email)?$row->email:'N/A'; ?> <br>
								<b>CONTACT NO</b>: <?php echo ($row->phone_no)?strtoupper($row->phone_no):'N/A'; ?> <br>
								<b>ADDRESS</b>: <?php echo ($row->address)?strtoupper($row->address):'N/A'; ?> <br>
							</td>
							<td>
								<?php echo ($row->relation)?strtoupper($row->relation):'N/A'?> <br>
							</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 $row->profiling_id; ?>" onclick="editEmergency(<?php echo $row->id; ?>)"><span class="fa fa-edit" style="color: #F0AD4E;"></span></button>
									<button type="button" class="btn btn-default btn-sm d-print-none" onclick="deleteEmergency(<?php echo $row->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="emergencyModal">
    <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="emergencyForm" class="form">
				<div class="modal-body">
						<div class="form-row">
							<div class="col-12 col-sm-6">
								<label for="emergency_name">Name</label>
								<input type="text"class="form-control" name="emergency_name" id="emergency_name">
							</div>
							<div class="col-12 col-sm-6">
								<label for="emergency_relation">Relation</label>
								<input class="form-control" name="emergency_relation" id="emergency_relation">
							</div>
						</div>
						<div class="form-row">
							<div class="col-12 col-sm-6">
								<label for="emergency_phone" class="col-form-label">Phone No</label>
								<input type="text" class="form-control" name="emergency_phone" id="emergency_phone">
							</div>
							<div class="col-12 col-sm-6">
								<label for="emergency_email" class="col-form-label">Email</label>
								<input type="text" class="form-control" name="emergency_email" id="emergency_email">								
							</div>
						</div>
						<div class="form-row">
							<div class="col-12">
								<label for="emergency_address" class="col-form-label">Address</label>
								<textarea class="form-control" name="emergency_address" id="emergency_address" rows="2"></textarea>
							</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="saveEmergencyButton" 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 openEmergencyModal(){

		clearForm("#emergencyForm");

		$('#emergencyModal').find('#racks-modal-title').text('Add Emergency Contact');
        $('#saveEmergencyButton').attr('onclick', 'saveEmergency()');
		$('#emergencyModal').modal('show');
	}

	function saveEmergency()
	{
		var postData = {
			'profiling_id' 	: $("#profiling_id_new").val(),
			'name' 			: $("#emergency_name").val(),
			'relation' 		: $("#emergency_relation").val(),
			'email' 		: $("#emergency_email").val(),
			'phone' 		: $("#emergency_phone").val(),
			'address' 		: $("#emergency_address").val(),
		};

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

		$('#emergencyModal').find('#racks-modal-title').text('Edit Emergency Contact');
        $('#saveEmergencyButton').attr('onclick', 'save_editEmergency('+ emergency_id +')');
		$('#emergencyModal').modal('show');

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

                    $("#emergency_name").val(case_emergency_data[0]['name']);
                    $("#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_editEmergency(emergency_id) 
	{
		var postData = {
			'emergency_id' 	: emergency_id,
			'profiling_id' 	: $("#profiling_id_new").val(),
			'name' 			: $("#emergency_name").val(),
			'relation' 		: $("#emergency_relation").val(),
			'email' 		: $("#emergency_email").val(),
			'phone' 		: $("#emergency_phone").val(),
			'address' 		: $("#emergency_address").val(),
		};

		$.ajax({
			type: 'POST',
			url: '<?php echo base_url('hr/Myprofile/save_editEmergency'); ?>',
			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 deleteEmergency(emergency_id)
	{
		toastConfirm("Are you sure you want to delete this Emergency Contact?", function(){

			$.ajax({
				type: 'POST',
				url: '<?php echo base_url('hr/Myprofile/deleteEmergency'); ?>',
				data: {
					'emergency_id' 			: emergency_id,
					'profiling_id' 	: $("#profiling_id_new").val(),
				},
				dataType: 'JSON',
				success: function(data) {
					if(data.status == 'success')
					{
						localStorage.setItem("status",data.status);
						localStorage.setItem("msg",data.msg);

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


</script>