<script type="text/javascript">
	$(document).ready(function(){
		studentStatusFamilyInfo();
	});

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

		$('#mdl-family-info-id').val(json.id);
		$('#mdl-family-name').val(json.name);
		$('#mdl-family-ic').val(json.ic_no);
		$('#mdl-family-relationship').val(json.relation);

		var [yy, mm, dd] = json.birthdate.split('-');
		$('#mdl-family-birthdate').val(dd+'-'+mm+'-'+yy);
		$('#mdl-family-contact').val(json.phone_no);
		$('#mdl-family-email').val(json.email);
		$('#mdl-family-address').val(json.address);
		$('#mdl-family-oku_status').val(json.oku_status);
		$('#mdl-family-student-status').val(json.study_status).trigger('change');
		$('#mdl-family-academic_lavel').val(json.academic_lavel);

		$('#mdl-family-occupation').val(json.occupation);

		$('#mdl-family-academic_institution').val(json.academic_institution);

	}

	function deleteFamily(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) {
	                        _removeFamilyMember(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 _removeFamilyMember(id) {
		var frmData = new FormData();
		frmData.append('id',id);

		$.ajax({
            url:"<?php echo base_url('hr/StaffProfile/removeFamily/'); ?>",
            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 studentStatusFamilyInfo() {
		var status = $('#mdl-family-student-status').val();
		if(status == 0){
			$('#mdl-family-occupation').addClass('d-none');
			// $('#mdl-family-academic').removeClass('d-none');
			$('#mdl-family-occupation').val('');
		}else{
			$('#mdl-family-occupation').removeClass('d-none');
			// $('#mdl-family-academic').addClass('d-none');
			// $('#mdl-family-academic_institution').val('');
		}
	}
</script>