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

/*	
 *	Author 	: Fachri
 *	Tanggal : 04-10-2016	
 *	
 */ 

class Sub_type extends CI_Controller {
	// inisialisasi manual
	public $menu_id = 406;
	public $code = "Sub Type ID";
	public $name = "Sub Type Name";
	
	public function __construct() {
        parent::__construct();
		$this->load->model("m_sub_type");
		if ($this->session->userdata("login") == TRUE) { 
            $group_menu             = $this->session->userdata('group_menu');
            $arr_group_id           = $this->m_menu->get_menu_by_group($group_menu, $this->menu_id);
            if(!empty($arr_group_id)){
				$data['available_menu'] = array_unique($arr_group_id);
				if(!in_array($this->menu_id, $data['available_menu'])) { 
					redirect(base_url() . 'error', 'refresh');	
					//$this->load->model('file/m_new_company');
				}
				else{
					$this->load->helper('message');
					$this->load->helper('regex');
					$this->load->helper('convert_date');
					
					//$this->load->model('file/m_new_company');		
				}
			}
			else{
				redirect(base_url() . 'error', 'refresh');
			}
        }
        else {
            redirect(base_url() . 'login', 'refresh');
        }
	}
	
	public function index() {
        if ($this->session->userdata("login") == TRUE) {
            $group_menu = $this->session->userdata('group_menu');
            $arr_group_id = $this->m_menu->get_menu_by_group($group_menu, $this->menu_id);
            if (!empty($arr_group_id)) {
                $data['available_menu'] = array_unique($arr_group_id);
                if (!in_array($this->menu_id, $data['available_menu'])) {
                    redirect(base_url() . 'error', 'refresh');
                } else {
                    //untuk ambil data dari session dan hak akses
                    $group_menu = $this->session->userdata('group_menu');
                    $data['menu_id'] = $this->menu_id;
                    $data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);
                    $data['menu_label'] = $this->m_menu->get_menulabel($this->menu_id);

                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id, $group_menu);
                  
                    $this->load->view('v_header');
                    $this->load->view('sub_type/v_sub_type', $data);
                    $this->load->view('v_footer');
                }
            } else {
                redirect(base_url() . 'error', 'refresh');
            }
        } else {
            redirect(base_url() . 'login', 'refresh');
        }
    }	
	
	public function sub_type_coa(){
        $filterRules = isset($_POST['filterRules']) ? ($_POST['filterRules']) : '';
		$field 		 = " "; 
		$value 		 = " "; 
		
		if (!empty($filterRules)){
			$filterRules = json_decode($filterRules);
			//print_r ($filterRules);
			foreach($filterRules as $rule){
				$field 	= $rule->field;
				$op 	= $rule->op;
				$value 	= $rule->value;
				
				if($field != " "){
					$result = $this->m_sub_type->get_sub_type_master($value,$field);
					//echo $this->db->last_query();
					echo json_encode($result);
				}
			}
		}
		
		if($field == " "){
			$result = $this->m_sub_type->get_sub_type_master_empty();
			echo json_encode($result);
		}   
    }
	
	function insert(){
		$selected_coa       = $this->input->post('detail');
		$coa_selected       = json_decode($selected_coa);
		
		$this->db->trans_begin();
		foreach ($coa_selected as $row){
			if($row->StatusSelected == "Yes"){
				$detail_subtype = array(
					'StatusSelected' => 1
				);
			}
			else if($row->StatusSelected == "No"){
				$detail_subtype = array(
					'StatusSelected' => 0
				);
			}
			
			$this->m_sub_type->update($detail_subtype, $row->AccountSubTypeID);
		}
		
		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			$result['status'] 		= FALSE;
			$result['message']  	= "Data Gagal Di Simpan";
		}
		else {
			$this->db->trans_commit();
			$result['status'] 		= TRUE;
			$result['message']  	= "Data Berhasil Di Simpan";
		}
		
		echo json_encode($result);
	}
}