<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');


class Myprofile_farid extends MY_Controller
{
	function __construct()
	{
		parent::__construct();
		if(empty($this->session->userdata('user')['id'])) redirect(base_url());
		else 
			$this->logged_user = $this->session->userdata('user');
		
		$this->load->model('admin/model_company');
		$this->load->model('admin/Model_user');
		
		$this->load->model('crm/model_crm');
		$this->load->model('crm/country');
		$this->load->model('crm/general_model');
		
		$this->load->model('hr/model_user_license','license');
		$this->load->model('hr/model_user_license_attachment','license_attachment');
		$this->load->model('hr/model_user_address','address');
		$this->load->model('hr/model_user_family','family');
		$this->load->model('hr/model_academic_qualification','academic');
		$this->load->model('hr/model_academic_qualification_attachment','academic_attachment');
		$this->load->model('hr/model_professional_membership','promember');
		$this->load->model('hr/model_professional_membership_attachment','promember_attachment');
		$this->load->model('hr/model_job_history','jobs');
		$this->load->model('hr/model_bank','bank');
		$this->load->model('hr/model_recognition','recognition');
		$this->load->model('hr/model_recognition_attachment','recognition_attachment');
		$this->load->model('hr/model_staff_action','staff_action');
		$this->load->model('hr/model_user_contract_element','contract');
		$this->load->model('hr/model_third_party_payment','thirdParty');
		$this->load->model('hr/model_personal_attachment','attachment');
		$this->load->model('hr/model_user_pcb','pcb');
		$this->load->model('hr/model_action_type','action');
		$this->load->model('hr/model_basic_pay','basic_pay');
		$this->load->model('hr/model_statutory_detail','stats');
		$this->load->model('hr/model_joining_group','join_group');
		$this->load->model('hr/model_user_recurring','recurring');

		/* newly add */
		$this->load->model('hr/model_user_pursuing_study','pursuing_study');
		$this->load->model('hr/model_user_case_emergency','case_emergency');
		/* end of newly add */

//akmal : check balik kenapa perlu check module access. semua user boleh update myprofile. 
		if($this->logged_user)
		{
			$this->login->checkModuleAccess(4);
			$this->logged_module = $this->session->userdata('module_data');

			if($this->logged_module['role'] != 'admin'){
				redirect(base_url('/hr'));
			}
			// die(debug($this->logged_module));
		}
		else
		{
			redirect(base_url());
		}	 
	}


	function index($profiling_id_new=0)
	{
		$user_id = $this->session->userdata('user')['id'];
		$rs_employment_detail = $this->Model_user->getEmploymentDetail($user_id);

		$profiling_id = (!empty($rs_employment_detail)) ? $rs_employment_detail['profiling_id']:0;
		
		$data['profiling_id_new'] = $profiling_id_new;
		$data['date'] 						= date("d-m-Y"); 
		$data['action_type']				= ($this->staff_action->fetchAction($profiling_id,null))?$this->staff_action->fetchAction($profiling_id,null)[0]:null;
		$data['join_group'] 				= ($this->join_group->fetchJoiningGroup(null,null,0,$user_id))?$this->join_group->fetchJoiningGroup(null,null,0,$user_id)[0]:null;
		$data['addresses']					= $this->address->fetchAddress($user_id,false,null);
		$data['family_info']				= $this->family->fetchFamilyInfo($user_id, false, null);
		$data['academic_qualification'] 	= $this->academic->fetchQualification($user_id,false,null);
		$data['qualifications']				= $this->general_model->get_all('_ref__academic');
		$data['additional_personal_data']	= $this->attachment->fetchPersonalAttachment($user_id,null,false,null);
		$data['professional_membership']	= $this->promember->fetchMembership($user_id,false,null);
		$data['professional_license']		= $this->license->fetchLicense($user_id,null,null,false,null);
		$data['recognition']				= $this->recognition->fetchRecognition($user_id,false,null);
		$data['statutory_details']			= ($this->stats->fetchStatutory($user_id,false,null))?$this->stats->fetchStatutory($user_id,false,null)[0]:null;
		$data['third_party_payments']		= $this->thirdParty->fetchPayment($user_id,false, null);
		$data['pcb_consideration']			= $this->pcb->fetchPCB($user_id,false,null);
		$data['rs_pursuing_study']			= $this->Model_user->get_pursuing_study($user_id,false,null);
		$data['rs_case_or_emergency']		= $this->Model_user->get_case_of_emergency($user_id,false,null);
		$data['residenceType']				= $this->general_model->get_all('_ref__residence_type');
		$data['states']						= $this->general_model->get_all('_ref__state');
		$data['countries']					= $this->country->country_all();
		$data['relations']					= $this->general_model->get_all('_ref__relationship');

		$this->load->view('hr/myprofile/mymainprofile.php', $data);
		$this->load->view('hr/report/footer.php');
	}

	public function test(){
		$user_id = $this->session->userdata('user')['id'];
		$rs_employment_detail = $this->Model_user->getEmploymentDetail($user_id);

		$profiling_id = (!empty($rs_employment_detail)) ? $rs_employment_detail['profiling_id']:0;

		$rs = $data['professional_license']		= $this->license->fetchLicense(null,null,null,true,$profiling_id);
		// var_dump($rs);
	}	

	private function get_profiling_id($profiling_id=0)
	{
		if($profiling_id ==0) {
			$this->db->query('insert into  user__profiling (user_id, staff_id, title_id, gender_id, ic_no, passport_expired_date, working_expired_date, place_issue, birth_date, birth_place, hobby, citizen_id, religion_id, race_id, residential_id, marital_status, married_date, primary_email, secondary_email, action_type_id, action_efective_date)  select user_id, staff_id, title_id, gender_id, ic_no, passport_expired_date, working_expired_date, place_issue, birth_date, birth_place, hobby, citizen_id, religion_id, race_id, residential_id, marital_status, married_date, primary_email, secondary_email, 10000, now() from user__profiling where user_id = '.$this->session->userdata('user')['id'].' order by id desc limit 1',false);
			$new_profiling_id = $this->db->insert_id();
			return($new_profiling_id);
		}else return $profiling_id; 
	}

	function getAddress()
	{
		$address_id  	= $this->input->post('address_id');
		$profiling_id   = $this->input->post('profiling_id');
		$addressData 	= $this->address->getAddress($address_id, $profiling_id);

		if($addressData)
        {
          	print json_encode(array(
                "status" 	=> "success",
                "address"	=> $addressData
            ));

        }
        else
        {
        	print json_encode(array(
                "status" 	=> "failed",
                "message" 	=> "failed"
            ));
        }
	}

	function saveAddress()
	{
		$insertData = array(
			'profiling_id' 	=> $this->get_profiling_id($this->input->post('profiling_id')),
			'user_id'		=> $this->session->userdata('user')['id'],
			'type'   		=> $this->input->post('type'),
	        'address' 		=> $this->input->post('address'),
	        'postcode'  	=> $this->input->post('postcode'),
	        'city'  		=> $this->input->post('city'),
	        'state_id'  	=> $this->input->post('state_id'),
	        'country_id'	=> $this->input->post('country_id'),
	        'contact_no'	=> $this->input->post('contact_no'),

		);
        
		$newAddress = $this->address->createAddress($insertData);

        if($newAddress)
        {
			print json_encode(array(
			    "status"     => "success",
			    "newAddress" => $newAddress,
			    "redirect"	 => base_url('hr/Myprofile/index/'.$insertData['profiling_id'])
			));

        } else {
			print json_encode(array(
				"status" 	=> "failed",
				"message" 	=>  "failed",
				"redirect"	=> base_url('hr/Myprofile/index/'.$insertData['profiling_id'])
			));
        }
	}

	function save_editAddress()
	{
		$address_id = $this->input->post('id');

		$updateData = array(
			'is_delete' => 1
		);

		$deleteAddress = $this->address->editAddress($updateData,$address_id);

		$insertData = array(
			'profiling_id' 	=> $this->get_profiling_id($this->input->post('profiling_id')),
			'user_id'		=> $this->session->userdata('user')['id'],
			'type'   		=> $this->input->post('type'),
	        'address' 		=> $this->input->post('address'),
	        'postcode'  	=> $this->input->post('postcode'),
	        'city'  		=> $this->input->post('city'),
	        'state_id'  	=> $this->input->post('state_id'),
	        'country_id'	=> $this->input->post('country_id'),
	        'contact_no'	=> $this->input->post('contact_no'),

		);
        
		$newAddress = $this->address->createAddress($insertData);

        if($newAddress)
        {
			print json_encode(array(
			    "status"     	=> "success",
			    "updateAddress" => $newAddress,
			    "redirect"	=> base_url('hr/Myprofile/index/'.$insertData['profiling_id'])
			));

        } else {
			print json_encode(array(
				"status" 	=> "failed",
				"message" 	=> "failed",
				"redirect"	=> base_url('hr/Myprofile/index/'.$insertData['profiling_id'])
			));
        }
	}

	function deleteAddress()
	{
		$address_id     =  $this->input->post('id');
		$profiling_id 	= $this->get_profiling_id($this->input->post('profiling_id'));

		$updateData = array(
			'is_delete' => 1
		);

		$deleteAddress = $this->address->editAddress($updateData,$address_id);

		if($deleteAddress)
        {
          print json_encode(array(
                "status"     	=> "success",
                "deleteAddress" => $deleteAddress,
                "msg" 		    => "Address deleted.",
                "redirect"	=> base_url('hr/Myprofile/index/'.$profiling_id)
            ));

        }
        else
        {
        	 print json_encode(array(
                "status" 	=> "failed",
                "msg" 		=> "failed",
                "redirect"	=> base_url('hr/Myprofile/index/'.$profiling_id)
            ));
        }
	}

	function saveFamilyInfo()
	{
		$insertData = array(
			'profiling_id' 	=> $this->get_profiling_id($this->input->post('profiling_id')),
			'staff_id'		=> $this->session->userdata('user')['id'],
			'name' 			=> $this->input->post('name'),
			'ic_no' 		=> $this->input->post('ic_no'),
			'relation' 		=> $this->input->post('relation'),
			'birthdate' 	=> date("Y-m-d", strtotime($this->input->post('birthdate'))),
			'phone_no' 		=> $this->input->post('phone_no'),
			'email' 		=> $this->input->post('email'),
			'address' 		=> $this->input->post('address'),
			'oku_status' 			=> $this->input->post('oku_status'),
			'study_status' 			=> $this->input->post('study_status'),
			'academic_lavel' 		=> $this->input->post('academic_level'),
			'academic_institution' 	=> $this->input->post('academic_institution'),
			'occupation' 			=> $this->input->post('occupation'),
		);
        
		$newFamilyInfo = $this->family->createFamily($insertData);

        if($newFamilyInfo)
        {
			print json_encode(array(
			    "status"     => "success",
			    "newFamilyInfo" => $newFamilyInfo,
			    "redirect"	 => base_url('hr/Myprofile/index/'.$insertData['profiling_id'])
			));

        } else {
			print json_encode(array(
				"status" 	=> "failed",
				"message" 	=>  "failed",
				"redirect"	=> base_url('hr/Myprofile/index/'.$insertData['profiling_id'])
			));
        }
	}

	function getFamilyInfo()
	{
		$familyInfo_id  = $this->input->post('familyInfo_id');
		$profiling_id   = $this->input->post('profiling_id');
		$familyInfoData = $this->family->find($familyInfo_id);

		if($familyInfoData)
        {
          	print json_encode(array(
                "status" 	=> "success",
                "familyInfo"=> $familyInfoData
            ));

        }
        else
        {
        	print json_encode(array(
                "status" 	=> "failed",
                "message" 	=> "failed"
            ));
        }
	}

	function save_editFamilyInfo()
	{
		$familyInfo_id  = $this->input->post('id');

		$updateData = array(
			'is_delete' => 1
		);

		$deleteFamilyInfo = $this->family->removeFamily($updateData,$familyInfo_id);

		$insertData = array(
			'profiling_id' 	=> $this->get_profiling_id($this->input->post('profiling_id')),
			'staff_id'		=> $this->session->userdata('user')['id'],
			'name' 			=> $this->input->post('name'),
			'ic_no' 		=> $this->input->post('ic_no'),
			'relation' 		=> $this->input->post('relation'),
			'birthdate' 	=> date("Y-m-d", strtotime($this->input->post('birthdate'))),
			'phone_no' 		=> $this->input->post('phone_no'),
			'email' 		=> $this->input->post('email'),
			'address' 		=> $this->input->post('address'),
			'oku_status' 			=> $this->input->post('oku_status'),
			'study_status' 			=> $this->input->post('study_status'),
			'academic_lavel' 		=> $this->input->post('academic_level'),
			'academic_institution' 	=> $this->input->post('academic_institution'),
			'occupation' 			=> $this->input->post('occupation'),
		);
        
		$newFamilyInfo = $this->family->createFamily($insertData);

        if($newFamilyInfo)
        {
			print json_encode(array(
			    "status"     	   => "success",
			    "updateFamilyInfo" => $newFamilyInfo,
			    "redirect"	=> base_url('hr/Myprofile/index/'.$insertData['profiling_id'])
			));

        } else {
			print json_encode(array(
				"status" 	=> "failed",
				"message" 	=> "failed",
				"redirect"	=> base_url('hr/Myprofile/index/'.$insertData['profiling_id'])
			));
        }
	}

	function deleteFamilyInfo()
	{
		$familyInfo_id  = $this->input->post('id');
		$profiling_id 	= $this->get_profiling_id($this->input->post('profiling_id'));

		$updateData = array(
			'is_delete' => 1
		);

		$deleteFamilyInfo = $this->family->removeFamily($updateData,$familyInfo_id);

		if($deleteFamilyInfo)
        {
          print json_encode(array(
                "status"     		=> "success",
                "deleteFamilyInfo" 	=> $deleteFamilyInfo,
                "msg" 		    	=> "Family Info deleted.",
                "redirect"			=> base_url('hr/Myprofile/index/'.$profiling_id)
            ));

        }
        else
        {
        	 print json_encode(array(
                "status" 	=> "failed",
                "msg" 		=> "failed",
                "redirect"	=> base_url('hr/Myprofile/index/'.$profiling_id)
            ));
        }
	}

}
?>