<div class="collapse" id="collapseAcademic">
	<div class="card card-body">
		<div class="container">
			<div class="row">
				<h4 class="card-title float-left">Academic Education</h4>
				<div class="btn-group ml-auto float-right">
					<button type="button" class="btn btn-primary btn-sm d-print-none <?php echo $btn_hidden; ?>" style="margin-bottom:15px; width: 200px;" onclick="openQualificationModal();" data-trigger="hover" data-tooltip="tooltip" title="Add New Academic Education">
						<i class="fa fa-plus"></i> Add New
					</button>
				</div>
			</div>
		</div>

		<hr>

		<div class="table-responsive">
			<table id="table_academic" class="table table-hover table-bordered">
				<thead>
					<tr>
						<th>Details</th>
						<th>Created At</th>
						<?php if($btn_hidden == ''): ?>
							<th>Action</th>
						<?php endif ?>

					</tr>
				</thead>
				<tbody>	

					<?php if($academics) foreach ($academics as $key => $academic): ?>
					<tr class="link-row" data-id="" >
						<td>
							<?php
							echo '<b>Level : </b>'.$academic->academic_level
							.'<br><b>Program : </b>'.$academic->program
							.'<br/><b>University / Institution : </b>'.$academic->institution
							.'<br/><b>Year graduated: </b>'.$academic->year
							.'<br/><b>Final Results / CGPA : </b>'.$academic->cgpa
							.'<br/><b>Attachment : </b>';
							?>
							<?php
								$attachment_name = explode('->',$academic->attachment_name);
								$attachment_mask = explode('->',$academic->mask_id);

								if($attachment_name) foreach($attachment_name as $attach_key => $attach_name)
								{
									echo '<li>';
									echo '<a href="'.base_url('hr/staffProfile/openAttachment/'.$attachment_mask[$attach_key].'/1').'" target="_blank">'.$attach_name.'</a>';
									echo '</li>';
								}
							?>
							<!-- <a href="<?php //echo base_url('hr/myprofile/openAttachment/'.$academic->mask_id.'/1')?>" target="_blank"><?php //echo $academic->attachment_name; ?></a> -->
						</td>
						<td>
							<?php echo $academic->created_at; ?>
						</td>
						<?php if($btn_hidden == ''){ ?>
							<?php if(strtolower($academic->action_type) == 'personal data update' && date('m-Y',strtotime($academic->created_at)) == date('m-Y', $live_date)){ ?>						
								<td class="text-center">
									<div class="btn-group">
										<button id="editQualification" type="button" class="btn btn-success btn-sm d-print-none" title="Edit Academic Education" data-profilingID="<?php echo $academic->profiling_id; ?>" data-userID="<?php echo $academic->user_id; ?>" onclick="editQualification(<?php echo $academic->id; ?>)"><span class="fa fa-edit"></span></button>
										<button type="button" class="btn btn-danger btn-sm d-print-none" onclick="deleteQualification(<?php echo $academic->id; ?>)" title="Delete Academic Education"><span class="fa fa-trash"></span></button>
									</div>
								</td> 
							<?php } else { ?>
								<td class="text-center">
									<div class="btn-group" role="group">
										<button class="btn btn-success btn-sm" disabled style="cursor: not-allowed;" data-trigger="hover" data-tooltip="tooltip" title="Not allowed to update"><i class="fas fa-edit" ></i></button>
										<button class="btn btn-danger btn-sm" disabled style="cursor: not-allowed;" data-trigger="hover" data-tooltip="tooltip" title="Not allowed to delete"><i class="fas fa-trash-alt"></i></button>
									</div>
								</td> 
							<?php } ?>
						<?php } ?>

					</tr>						
					<?php endforeach ?>						
				</tbody>
			</table>
		</div><br>
	</div><br>
</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>

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

		$('#qualificationModal').find('#racks-modal-title').text('Add Academic Education');
		$('#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 Education');
		$('#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>