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

class Data_diri extends MY_Controller {

	public function __construct(){
        parent::__construct();
        $this->load->model('Data_diri_m');
		$this->load->library('Uploadfile');
    }

    public function index(){
		$this->session->set_userdata('id_kar',0);
		$this->load->view('karyawan/data_diri/form', $data);
	}

	public function edit($id_karyawan = null){
		if ($id_karyawan != null) {
			$data['id_karyawan'] = $id_karyawan;
			$data['id_karyawan_sess'] = $this->session->userdata('id_karyawan');
			$data['data'] = $this->Data_diri_m->getDataById($id_karyawan);
		}
		$this->load->view('karyawan/data_diri/form', $data);
	}

	public function detail_datadiri($id_karyawan = null){
		$data['row'] = $this->Data_diri_m->getDataById($id_karyawan);
		$data['jml'] = $this->Data_diri_m->getDataById_numrows();
		$this->load->view('karyawan/data_diri/detail', $data);
	}

	public function save(){
		$id_company = $this->session->userdata('id_company');
		$id_karyawan = $this->input->post('id_karyawan');
		$no_induk_karyawan = $this->input->post('no_induk_karyawan');
		$level_user = $this->input->post('level_user');
		$foto_lama = $this->input->post('foto_lama');

		$file1 = $_FILES['foto'];
        if($file1['name'] == ""){
            $foto_profil = $foto_lama;
        }else{
			$fileUpload['fname']    = 'foto';
            $fileUpload['location'] = 'karyawan/'.$id_company.$no_induk_karyawan.'/profil';
            $fileUpload['allowed']  = 'jpg|png|jpeg';
            $fileUpload['compress_image']  = true;
            $fileUpload['filename']  = preg_replace('/[^A-Za-z0-9\-]/', '', $no_induk_karyawan);

            $upload = $this->uploadfile->doUpload($fileUpload);
			if($upload["success"] == true){
				$foto_profil = $upload['url'];
				if(!empty($foto_lama)){
					if($foto_profil != $foto_lama){
						$this->fungsi->delete_fileupload($foto_lama);
					}
				}

			}else{
				$message = ["message"=>$upload["message"]];
				return __response_save(false,["params"=>$message]);
			}
        }

        if ($level_user == 1 || $level_user == 6) {
        	$id_cabang_pusat = $this->db->query("SELECT id_cabang FROM master_cabang WHERE id_company = '".$id_company."' AND flag = '1'")->row()->id_cabang;

        	$id_cabang 			= $id_cabang_pusat;
        	$tgl_aktif_bekerja 	= date('Y-m-d');
        	$status 			= '2';
        }else {
        	$id_cabang 			= $this->input->post('cabang');
        	$tgl_aktif_bekerja 	= date('Y-m-d', strtotime($this->input->post('tgl_aktif_bekerja')));
        	$status 			= $this->input->post('status');
        }

        	
    	$data_pribadi = array('nik'				=>$no_induk_karyawan,
						'nama_lengkap'			=>$this->input->post('nama_lengkap'),
						'gelar_depan'			=>$this->input->post('gelar_depan'),
						'gelar_belakang'		=>$this->input->post('gelar_belakang'),
						'golongan_darah'		=>$this->input->post('golongan_darah'),
						'jenis_kelamin'			=>$this->input->post('jenis_kelamin'),
						'agama'					=>$this->input->post('agama'),
						'tempat_lahir'			=>$this->input->post('tempat_lahir'),
						'tgl_lahir'				=>date('Y-m-d', strtotime($this->input->post('tgl_lahir'))),
						'alamat_domisili'		=>$this->input->post('alamat_domisili'),
						'rt'					=>$this->input->post('rt'),
						'rw'					=>$this->input->post('rw'),
						'rt'					=>$this->input->post('rt'),
						'id_kel'				=>$this->input->post('id_kel'),
						'id_kec'				=>$this->input->post('id_kec'),
						'id_kab'				=>$this->input->post('id_kab'),
						'id_prov'				=>$this->input->post('id_prov'),
						'kode_pos'				=>$this->input->post('kode_pos'),
						'telepon'				=>$this->input->post('telepon'),
						'no_ktp'				=>$this->input->post('no_ktp'),
						'email'					=>$this->input->post('email'),
						'tgl_aktif_bekerja'		=>$tgl_aktif_bekerja,
						'tgl_berhenti_bekerja'	=>date('Y-m-d', strtotime($this->input->post('tgl_berhenti_bekerja'))),
						'id_departemen'			=>(($this->input->post('departemen') == "null")?"":$this->input->post('departemen')),
						'id_cabang'				=>$id_cabang,
						'id_jabatan'			=>$this->input->post('jabatan'),
						'supervisi'				=>(($this->input->post('supervisi') == "null")?"":$this->input->post('supervisi')),
						'jatah_cuti'			=>$this->input->post('jatah_cuti'),
						'pendidikan_terakhir'	=>$this->input->post('pendidikan_terakhir'),
						'status'				=>$status,
						'id_company' 			=>$this->session->userdata('id_company'),
						'foto'					=>(($foto_profil== null)?"":$foto_profil));
		
		$data_akun = [];

		if(empty($id_karyawan)){
			$data_pribadi['id_karyawan'] = $id_company.preg_replace('/[^A-Za-z0-9\-]/', '', $no_induk_karyawan);

			$data_akun['id_user']      = $this->fungsi->getNextId('users','id_user',$id_company,4);
			$data_akun['username']     = $this->input->post('username');
			$data_akun['password']     = sha1(sha1(md5($this->input->post('password'))));
			$data_akun['id_karyawan']  = $id_company.preg_replace('/[^A-Za-z0-9\-]/', '', $no_induk_karyawan);;
			$data_akun['id_company']   = $id_company;
		}
	
		$this->Data_diri_m->save($data_pribadi, $data_akun, $id_karyawan, $level_user); 
	}

	public function delete(){
		$id = $this->input->post("id_karyawan");
		$this->Data_diri_m->delete($id);
	}
}
