<div class="collapse" id="collapseEmergencyContact">
	<div class="card card-body">
	    <h4 class="card-title">Emergency Contact</h4>
			<hr>
			<div class="container">
				<div class="row">
					<div class="btn-group ml-auto">
						<?php if ($role == 'admin'): ?>
							<a class="btn btn-info btn-sm" style="margin-bottom:15px; width: 200px;" href="<?php echo base_url('/hr/StaffProfile/actionReport/'.$staff_id.'/21'); ?>" target="_blank">
								<i class="fa fa-print"></i> Print
							</a>
						<?php endif ?>
					</div>
				</div>
			</div>
			<div class="table-responsive">
				<table id="table_academic" class="table table-hover table-bordered">
					<thead>
						<tr>
							<th>Action type (Effective date)</th>
							<th>Details</th>
							<th>Created At</th>
							<!-- <?php if($role == 'admin'): ?>
								<th>Action</th>
							<?php endif ?> -->
						</tr>
					</thead>
					<tbody>	

						<?php if($emergency_contact) foreach ($emergency_contact as $key => $emergency): ?>
							<tr class="link-row" data-id="<" >
								<td scope="row">
									<?php
										echo $emergency->action_type.'<br><b>( '.date('d-F-Y',strtotime($emergency->action_efective_date)).' )</b>';
									?>
								</td>
								<td>
									<?php
										echo '<b>Name : </b>'.$emergency->name
											.'<br/><b>Relation : </b>'.$emergency->relation
											.'<br/><b>Contact Number : </b>'.$emergency->phone_no
											.'<br/><b>Address : </b>'.$emergency->address
									?>
								</td>
								<td>
									<?php echo $emergency->created_at; ?>
								</td>
								<?php if($role == 'admin'): ?>
									<!-- <td class="text-center">
										<div class="btn-group">
											<button type="button" class="btn btn-success btn-sm d-print-none" title="Edit Emergency Contact" data-profilingID="<?php //echo $emergency->profiling_id; ?>" onclick="editEmergency(<?php //echo $emergency->id; ?>)"><span class="fa fa-edit"></span></button>
											<button type="button" class="btn btn-danger btn-sm d-print-none" onclick="deleteEmergency(<?php //echo $emergency->id; ?>)" title="Delete Emergency Contact"><span class="fa fa-trash"></span></button>
										</div>
									</td> -->
							<?php endif; ?>
							</tr>						
						<?php endforeach ?>						
											
					</tbody>
				</table>
			</div><br>
	</div><br>
</div>

<!-- modal -->
<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">
					<button type="button" class="btn btn-danger btn-sm" style="margin-bottom:15px; width: 200px;" data-dismiss="modal">Cancel</button>
					<button type="button" id="saveEmergencyButton" class="btn btn-success btn-sm" style="margin-bottom:15px; width: 200px;">Save</button>
				</div>
			</form>
        </div>
    </div>
</div>

<script type="text/javascript">
	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/StaffProfile/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/StaffProfile/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);
					location.reload();
				}
			}
		});
	}

	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/StaffProfile/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);
						location.reload();
					}
				}
			});
		})
	}
</script>