<!-- ariff's part -->
<div class="container">
	<div class="row">
		<div class="col">
			<h4><b>ACADEMIC EDUCATION</b>
				<button type="button" class="btn btn-default btn-sm d-print-none <?php echo $btn_hidden; ?>" onclick="openQualificationModal()" title=" Add New Qualification"><span class="fa fa-plus" style="color: #007bff;"></span></button> </h4>
				<small><i>Please add/update your Academic Education.</i></small>
		</div>
	</div>

	<div class="row">
		<div class="col">
			<table class="table table-bordered">
				<thead>
					<tr>
						<th>LEVEL</th>
						<th>PROGRAM</th>
						<th>UNIVERSITY / INSTITUTION</th>
						<th>YEAR GRADUATED</th>
						<th>FINAL RESULTS / CGPA</th>
						<th>ATTACHMENTS</th>
						<th class="d-print-none <?php echo $btn_hidden; ?>">Action</th>
					</tr>
				</thead>
				<tbody id="qualification_tableListingBody">
					<?php if(!empty($academic_qualification)) foreach ($academic_qualification as $key => $row){ //debug($row); ?>	
					<tr>
						<td><?php echo ($row->academic_level)? strtoupper($row->academic_level):'N/A'; ?></td>
						<td><?php echo ($row->program)? strtoupper($row->program):'N/A'; ?></td>
						<td><?php echo ($row->institution)? strtoupper($row->institution):'N/A'; ?></td>
						<td><?php echo ($row->year)? strtoupper($row->year):'N/A'; ?></td>
						<td><?php echo ($row->cgpa)? strtoupper($row->cgpa):'N/A'; ?></td>
						<td><?php echo ($row->attachment_name)?strtoupper($row->attachment_name):'N/A'; ?></td>
						<td class="text-center d-print-none <?php echo $btn_hidden; ?>">
							<div class="btn-group">
								<button id="editQualification" 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="editQualification(<?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="deleteQualification(<?php echo $row->id; ?>)" title="Delete Academic Qualification"><span class="fa fa-trash" style="color: #D9534F;"></span></button>
							</div>
						</td>
					</tr>
					<?php } ?>

				</tbody>
			</table>
		</div>
	</div>
</div>

<!-- Modal -->
<div class="modal fade d-print-none" role="dialog" aria-hidden="true" id="qualificationModal">
    <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="qualificationForm" class="form">
				<div class="modal-body">
						<div class="form-row">
							<div class="col-12 col-sm-6">
								<label for="qualification_levels">Level</label>
								<select class="form-control" name="qualification_levels" id="qualification_levels">
									<option value="-1" selected disabled>-- Please Select --</option>
									<?php foreach ($qualifications as $key => $level): ?>
										<option value="<?php echo $level->id; ?>"><?php echo $level->level; ?></option>
									<?php endforeach ?>	
								</select>
							</div>
							<div class="col-12 col-sm-6">
								<label for="qualification_year_grads">Year Graduated</label>
								<input class="form-control datepicker-yyyy" name="qualification_year_grads" id="qualification_year_grads">
							</div>
						</div>
						<div class="form-row">
							<div class="col-12">
								<label for="qualification_institutions" class="col-form-label">University / Institution</label>
								<input type="text" class="form-control" name="qualification_institutions" id="qualification_institutions">
							</div>
						</div>
						<div class="form-row">
							<div class="col-12">
								<label for="qualification_programs" class="col-form-label">Program</label>
								<input type="text" class="form-control" name="qualification_programs" id="qualification_programs">
							</div>
						</div>
						<div class="form-row">
							<div class="col-12 col-sm-6">
								<label for="qualification_cgpas" class="col-form-label">Final Result / CGPA</label>
								<input type="text" class="form-control" name="qualification_cgpas" id="qualification_cgpas">
							</div>
							<div class="col-12 col-sm-6">
								<label for="qualification_attachments" class="col-form-label">Attachment</label>
								<input type="file" class="form-control" name="qualification_attachments" id="qualification_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="savequalificationButton" class="btn btn-primary">Save</button>
					</div>
				</div>
			</form>
        </div>
    </div>
</div>
<?php  
//1. script for modal qualification
//2. script to ajax load for update qualification
//3. script to refresh table qualification
//4. script document ready to refresh table 
//5. script to save and refresh table after save 
?>

<script type="text/javascript">
	function openQualificationModal()
	{
		clearForm("#qualificationForm");

		$('#qualificationModal').find('#racks-modal-title').text('Add Academic Qualification');
        $('#savequalificationButton').attr('onclick', 'saveQualification()');
		$('#qualificationModal').modal('show');
	}

	function saveQualification()
	{
        var postData = new FormData();

        postData.append("profiling_id", $("#profiling_id_new").val());
		postData.append("level_id", $('#qualification_levels').val());
		postData.append("program", $('#qualification_programs').val());
		postData.append("institution", $('#qualification_institutions').val());
		postData.append("year", $('#qualification_year_grads').val());
		postData.append("cgpa", $('#qualification_cgpas').val());

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

		$.ajax({
			url:'<?php echo base_url('hr/Myprofile/saveQualification') ?>',
			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 editQualification(qualification_id)
	{
		clearForm("#qualificationForm");
		var profiling_id = $("#profiling_id_new").val();

		$('#qualificationModal').find('#racks-modal-title').text('Edit Academic Qualification');
		$('#savequalificationButton').attr('onclick', 'save_editQualification('+ qualification_id +')');
		$('#qualificationModal').modal('show');

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

                    $("#qualification_levels").val(qualification_datas[0]['level_id']);
                    $("#qualification_programs").val(qualification_datas[0]['program']);
                    $("#qualification_institutions").val(qualification_datas[0]['institution']);
                    $("#qualification_year_grads").val(qualification_datas[0]['year']);
                    $("#qualification_cgpas").val(qualification_datas[0]['cgpa']);
                    // $("#qualification_attachments").val(qualification_datas[0]['attachment_name']);
				}
			}
		});
	}

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

        postData.append("id", id);
        postData.append("profiling_id", $("#profiling_id_new").val());
		postData.append("level_id", $('#qualification_levels').val());
		postData.append("program", $('#qualification_programs').val());
		postData.append("institution", $('#qualification_institutions').val());
		postData.append("year", $('#qualification_year_grads').val());
		postData.append("cgpa", $('#qualification_cgpas').val());

		if($('#qualification_attachments').prop('files').length > 0){
			postData.append("attachment_file", $('#qualification_attachments').prop('files')[0]);
			postData.append("attachment_name", $('#qualification_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_editQualification'); ?>',
			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 deleteQualification(ID)
	{
		toastConfirm("Are you sure you want to delete this qualification?", function(){
			var profiling_id = $("#profiling_id_new").val();

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