<div class="container">
	<div class="row">
		<div class="col">
			<h4><b>EDUCATION IN PROGRESS</b>
				<button type="button" class="btn btn-default btn-sm d-print-none <?php echo $btn_hidden; ?>" onclick="openStudyModal()" title=" Add New Study"><span class="fa fa-plus" style="color: #007bff;"></span></button>
			</h4>
			<small><i>Please add/update your Education In Progress (if applicable).</i></small>
		</div>
	</div>
	<div class="row">
		<div class="col table-responsive">
			<table class="table table-bordered">
				<thead>
					<tr>
						<th>PROGRAM</th>
						<th>UNIVERSITY / INSTITUTION</th>
						<th>PROGRESS PAPER IN HAND</th>
						<th>START DATE</th>
						<th>EXPECTED DATE OF COMPLETION</th>
						<th>STATUS</th>
						<th class="d-print-none <?php echo $btn_hidden; ?>">ACTION</th>
					</tr>
				</thead>
				<tbody>
					<?php if ($rs_pursuing_study) foreach ($rs_pursuing_study as $key => $row): ?>
						<tr>
							<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->progress_paper; ?></td>
							<td><?php echo (!empty(mysql_to_date_format($row->date_start)))?strtoupper(mysql_to_date_format($row->date_start)):'N/A'; ?></td>
							<td><?php echo (!empty(mysql_to_date_format($row->date_end)))?strtoupper(mysql_to_date_format($row->date_end)):'N/A'; ?></td>
							<td><?php echo ($row->status == 0)?strtoupper('ongoing'):strtoupper('end'); ?></td>
							<td class="text-center d-print-none <?php echo $btn_hidden; ?>">
								<div class="btn-group">
									<button id="editstudy" type="button" class="btn btn-default btn-sm d-print-none" title="Edit Current Pursuing Study"  onclick="editStudy(<?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="deleteStudy(<?php echo $row->id; ?>)" title="Delete Current Pursuing Study"><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="studyModal">
    <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="studyForm" class="form">
				<div class="modal-body">
					<div class="form-row">
						<div class="col-12 col-sm-6">
							<label for="study_date_start">Start Date</label>
							<input type="text" class="form-control datepicker" name="study_date_start" id="study_date_start">
						</div>
						<div class="col-12 col-sm-6">
							<label for="study_date_end">Expectation Of Completion Date</label>
							<input type="text" class="form-control datepicker" name="study_date_end" id="study_date_end">
						</div>
					</div>
					<div class="form-row">
						<div class="col-12">
							<label for="study_institution" class="col-form-label">University / Institution</label>
							<input type="text" class="form-control" name="study_institution" id="study_institution">
						</div>
					</div>
					<div class="form-row">
						<div class="col-12">
							<label for="study_program" class="col-form-label">Program</label>
							<input type="text" class="form-control" name="study_program" id="study_program">
						</div>
					</div>
					<div class="form-row">
						<div class="col-12 col-sm-6">
							<label for="study_status">Status</label>
							<select class="form-control" name="study_status" id="study_status">
								<option value="0">On Going</option>
								<option value="1">Completed</option>	
							</select>
						</div>
						<div class="col-12 col-sm-6">
						</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="saveStudyButton" 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 openStudyModal(){

		clearForm("#studyForm");

		$('#studyModal').find('#racks-modal-title').text('Add Study Info');
        $('#saveStudyButton').attr('onclick', 'saveStudy()');
		$('#studyModal').modal('show');
	}
	
	function saveStudy()
	{
		var postData = {
			'profiling_id' 		: $("#profiling_id_new").val(),
			'study_date_start' 	: $("#study_date_start").val(),
			'study_date_end' 	: $("#study_date_end").val(),
			'study_institution' : $("#study_institution").val(),
			'study_program' 	: $("#study_program").val(),
			'study_status' 		: $("#study_status").val()
		};

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

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

	function editStudy(study_id)
	{
		clearForm("#studyForm");
		var profiling_id = $("#profiling_id_new").val();

		$('#studyModal').find('#racks-modal-title').text('Edit Current Pursuing Study');
        $('#saveStudyButton').attr('onclick', 'save_editStudy('+ study_id +')');
		$('#studyModal').modal('show');

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

                    $("#study_date_start").val(moment(study_data[0]['date_start']).format('DD-MM-YYYY'));
                    $("#study_date_end").val(moment(study_data[0]['date_end']).format('DD-MM-YYYY'));
                    $("#study_program").val(study_data[0]['program']);
                    $("#study_institution").val(study_data[0]['institution']);
                    $("#study_status").val(study_data[0]['status']);
				}
			}
		});
	}

	function save_editStudy(id) 
	{
        var postData = {
        	'study_id'			: id,
			'profiling_id' 		: $("#profiling_id_new").val(),
			'study_date_start' 	: $("#study_date_start").val(),
			'study_date_end' 	: $("#study_date_end").val(),
			'study_institution' : $("#study_institution").val(),
			'study_program' 	: $("#study_program").val(),
			'study_status' 		: $("#study_status").val()
		};

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

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

	function deleteStudy(ID)
	{
		toastConfirm("Are you sure you want to delete this Study record?", function(){

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