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

class Pengaturan_izin_m extends CI_Model {
    var $table = 'p_approval_izin';

    public function save($data,$id = null){
        if($id != null){
            $this->db->where(array('md5(id)'=>$id, 'id_company'=>$_SESSION['id_company']));
            $update = $this->db->update($this->table,$data);
            return  __response_update($update);
        }else{
            $nextId = $this->fungsi->getNextId($this->table,'id',$_SESSION['id_company'],3);
            $id_atasan_khusus = array('id' => $nextId);
            $save = $this->db->insert($this->table, array_merge($data,$id_atasan_khusus));
            return __response_save($save);
    
        }
        
    }

    public function delete($id=null){

        $this->db->where(array('md5(id)'=>$id));
        $delete =$this->db->delete($this->table);

        return  __response_delete($delete);
    
        
    }

    public function get_atasan($id = null, $search = null){
        $level = $this->db->select("level_user")->from("master_jabatan")->where("id_jabatan", $id)->get()->row()->level_user;
        //$this->db->query("SELECT level_user FROM master_jabatan WHERE id_jabatan = '$id'")->row()->level_user;
        $this->db->select('nama, id_jabatan');
        $this->db->like('nama', $search);
        $this->db->order_by('id_jabatan', 'ASC');
        $this->db->where('level_user', $level);
        $this->db->where('id_jabatan !=', $id);

        $result = $this->db->get('master_jabatan')->result();
        $json = [];
        //$json[] = ['id'=> 0, 'text'=>'Semua Cabang'];
        foreach ($result as $rows) {
            $json[] = ['id'=>$rows->id_jabatan, 'text'=>$rows->nama];
        }
        echo json_encode($json);
    }

}