<div class="container">
	<div class="row">
		<div class="col">
			<h4><b>ADDITIONAL PERSONAL DATA </b>
				<button type="button" class="btn btn-default btn-sm d-print-none" onclick="openPersonalDataModal()" title=" Add New Additional Peronal Data"><span class="fa fa-plus" style="color: #007bff;"></span></button>
			</h4>
		</div>
	</div>

	<div class="row">
		<div class="col">
			<table class="table table-bordered">
				<thead>
					<tr>
						<th>DETAIL</th>
						<th>DESCRIPTION</th>
						<th>ATTACHMENTS</th>
						<th class="d-print-none">ACTION</th>
					</tr>
				</thead>
				<tbody>
					<?php if (!empty($additional_personal_data))foreach ($additional_personal_data as $key => $row): ?>	
					<tr>
						<td><?php echo ($row->detail)?strtoupper($row->detail):'N/A'; ?></td>
						<td><?php echo ($row->description)?strtoupper($row->description):'N/A'; ?></td>
						<td><?php echo ($row->attachment_name)?strtoupper($row->attachment_name):'N/A'; ?></td>
						<td class="text-center d-print-none">
							<div class="btn-group">
								<button id="editPersonalData" type="button" class="btn btn-default btn-sm d-print-none" title="Edit Academic Qualification" data-profilingID="<?php echo $row->profiling_id; ?>" data-userID="<?php echo $row->user_id; ?>" onclick="editPersonalData(<?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="deletePersonalData(<?php echo $row->id; ?>)" title="Delete This Personal Data"><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 d-print-none" role="dialog" aria-hidden="true" id="personalDataModal">
    <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="personalDataForm" class="form">
				<div class="modal-body">
						<div class="form-row">
							<div class="col-12">
								<label for="detail" class="col-form-label">Detail</label>
								<input type="text" class="form-control" name="detail" id="detail">
							</div>
						</div>
						<div class="form-row">
							<div class="col-12">
								<label for="description" class="col-form-label">Description</label>
								<input type="text" class="form-control" name="description" id="description">
							</div>
						</div>
						<div class="form-row">
							<div class="col-12">
								<label for="personalData_attachments" class="col-form-label">Attachment</label>
								<input type="file" class="form-control" name="personalData_attachments" id="personalData_attachments">
								<small id="attachmentHelp" class="form-text text-muted">We only accept the following formats: pdf, doc, docx, jpg, jpeg, bmp, png, gif, svg, ico.</small>
							</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="savePersonalDataButton" 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 openPersonalDataModal()
	{

		clearForm("#personalDataForm");

		$('#personalDataModal').find('#racks-modal-title').text('Add Additional Personal Data');
        $('#savePersonalDataButton').attr('onclick', 'savePersonalData()');
		$('#personalDataModal').modal('show');
	}
	function savePersonalData()
	{
        var postData = new FormData();

        postData.append("profiling_id", $("#profiling_id_new").val());
		postData.append("detail", $('#detail').val());
		postData.append("description", $('#description').val());

		if($('#personalData_attachments').prop('files').length > 0){
			postData.append("attachment_file", $('#personalData_attachments').prop('files')[0]);
			postData.append("attachment_name", $('#personalData_attachments').prop('files')[0]['name']);
		}else{
			postData.append("attachment_file", null);
			postData.append("attachment_name", null);
		}

		$.ajax({
			url:'<?php echo base_url('hr/Myprofile/savePersonalData') ?>',
			data: postData,
			type: 'post',
			dataType: 'json',
			processData: false,
			contentType: false,
			success: (data) => {
				if(data.status == "success") 
				{
					localStorage.setItem("status",data.status);
					localStorage.setItem("msg",data.msg);

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

	function editPersonalData(personalData_id)
	{
		clearForm("#personalDataForm");
		var profiling_id = $("#profiling_id_new").val();

		$('#personalDataModal').find('#racks-modal-title').text('Edit Additional Personal Data');
		$('#savePersonalDataButton').attr('onclick', 'save_editPersonalData('+ personalData_id +')');
		$('#personalDataModal').modal('show');

		$.ajax({
			type: 'POST',
			url: '<?php echo base_url('hr/Myprofile/getPersonalData'); ?>',
			data: {
				'personalData_id' 	: personalData_id,
				'profiling_id' 		: profiling_id
			},
			dataType: 'JSON',
			success: function(data) {
				if(data.status == 'success')
				{
					var personalData_data = data.personalData;

					console.log(data)

                    $("#detail").val(personalData_data[0]['detail']);
                    $("#description").val(personalData_data[0]['description']);
                    // $("#personalData_attachments").val(personalData_data[0]['attachment_name']);
				}
			}
		});
	}

	function save_editPersonalData(id) 
	{
        var postData = new FormData();

        postData.append("personalData_id", id);
        postData.append("profiling_id", $("#profiling_id_new").val());
		postData.append("detail", $('#detail').val());
		postData.append("description", $('#description').val());

		if($('#personalData_attachments').prop('files').length > 0){
			postData.append("attachment_file", $('#personalData_attachments').prop('files')[0]);
			postData.append("attachment_name", $('#personalData_attachments').prop('files')[0]['name']);
		}else{
			postData.append("attachment_file", null);
			postData.append("attachment_name", null);
		}

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

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

	function deletePersonalData(ID)
	{
		toastConfirm("Are you sure you want to delete this Personal Data?", function(){
        	var profiling_id = $("#profiling_id_new").val();

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