<script type="text/javascript">
	$(document).ready(function(){
		console.log('tab academic qualification loaded..');
	});

	function editAcademicQualification(elem) {
		var b64 = $(elem).data('content');
		var json= JSON.parse(atob(b64));

		json['attachment_id'] = json['attachment_id'].split('->');
		json['attachment_name'] = json['attachment_name'].split('->');
		json['file_ext'] = json['file_ext'].split('->');
		json['file_type'] = json['file_type'].split('->');
		json['location'] = json['location'].split('->');
		json['mask_id'] = json['mask_id'].split('->');
		console.log(json);

		$('#mdl-qualification-id').val(json.id);
		$('#mdl-qualification-level-id').val(json.level_id);
		$('#mdl-qualification-program').val(json.program);
		$('#mdl-qualification-institution').val(json.institution);
		$('#mdl-qualification-year').val(json.year);
		$('#mdl-qualification-cgpa').val(json.cgpa);

		var table = $('#table_academic_attachment').DataTable();
        table.clear().draw();

		$('#newQualificationAttachmentBtn').html('');
		$('#newQualificationAttachmentBtn').append('<button type="button" class="btn btn-primary btn-sm" data-trigger="hover" title="Add Attachments" onclick="newQualificationAttachment('+json.id+','+json.user_id+')"><i class="fas fa-plus"></i></button>');

		$.each(json['attachment_id'], function(key, row){
			$('#mdl-qualification-attachment-id').val(row+',');
			var i = key + 1;


			var btn_delete = '<button type="button" class="btn btn-outline-dark" onclick="deleteQualificationAttachment('+json.id+', null, '+row+')" data-trigger="hover" data-tooltip="tooltip" title="Delete Attachment"><i class="far fa-trash-alt"></i></button>';

			table.row.add({
                0   : '<div class="text-center">'+i+'</div>',
                1   : json['attachment_name'][key],
                2   : '<div class="text-center"><div class="btn-group btn-group-sm" role="group">'+btn_delete+'</div></div>',
            }).draw();
		});
	}

	function removeAcademicQualification(id){
	    iziToast.question({
	        title: '',
	        timeout:20000,
	        close: false,
	        toastOnce: true,
	        message: 'Are you sure you want to remove this record? ',
	        position:"center",
	        buttons: [
	                    ['<button><b>YES</b></button>', function (instance, toast) {
	                        _removeAcademic(id);
	                        instance.hide(toast, {
	                            transitionOut: 'fadeOutUp'
	                        });
	                    }],
	                    ["<button>No</button>", function (instance, toast) {
	                        instance.hide(toast, {
	                            transitionOut: 'fadeOutUp'
	                        });

	                        iziToast.warning({
	                            position: 'bottomRight',
	                            message: 'Operation cancelled.'
	                        });

	                    },true]
	                ]
	        });
	}

	function _removeAcademic(id) {
		var frmData = new FormData();
		frmData.append('id',id);

		$.ajax({
            url:"<?php echo base_url('hr/StaffProfile/removeAcademicQualification/'); ?>",
            type:'post',
            dataType:'json', //return type value
            data:frmData,
            processData: false,
            contentType: false,
            success:function(data){
                iziToast.success({
                    title: '',
                    message:'success'
                });
                window.location.replace("<?php echo base_url('hr/StaffProfile/staffDetail/'.$this->uri->segment(4)); ?>");            
            },error:function(){
                iziToast.error({
                    title: '',
                    message: '500 internal server error'
                });
            }
        });
	}

	function newQualificationAttachment(qualification_id = 0, staff_id = 0, mode = null)
	{
		if(mode != null && mode != '' && mode == 'new')
		{
			var frmData = new FormData();
	        frmData.append("academic_id", $('#mdl-qualification-attachmentid').val());
	        frmData.append("staff_id", $('#mdl-qualification-attachmentstaffid').val());

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

			$.ajax({
	            url:"<?php echo base_url('hr/StaffProfile/updateQualificationAttachment/new'); ?>",
	            type:'post',
	            dataType:'json', //return type value
	            data:frmData,
	            processData: false,
	            contentType: false,
	            success:function(data){
	                iziToast.success({
	                    title: '',
	                    message: data.msg,
	                });
	                window.location.replace("<?php echo base_url('hr/StaffProfile/staffDetail/'.$this->uri->segment(4)); ?>");            
	            },error:function(){
	                iziToast.error({
	                    title: '',
	                    message: data.msg,
	                });
	            }
	        });
		}else{
			$('#mdl-qualification-attachmentid').val(qualification_id);
			$('#mdl-qualification-attachmentstaffid').val(staff_id);
			$('#academicAttachmentModal').modal('show');
			$('#academicModal').modal('hide');
		}
	}
	
	function deleteQualificationAttachment(qualification_id, attachment_id)
	{
		var frmData = new FormData();

        frmData.append("academic_id", qualification_id);
        frmData.append("attachmentID", attachment_id);

		$.ajax({
            url:"<?php echo base_url('hr/StaffProfile/updateQualificationAttachment/delete'); ?>",
            type:'post',
            dataType:'json', //return type value
            data:frmData,
            processData: false,
            contentType: false,
            success:function(data){
                iziToast.success({
                    title: '',
                    message: data.msg,
                });
                window.location.replace("<?php echo base_url('hr/StaffProfile/staffDetail/'.$this->uri->segment(4)); ?>");            
            },error:function(){
                iziToast.error({
                    title: '',
                    message: data.msg,
                });
            }
        });
	}
</script>