<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/* 	
 * 	Author 	: Tagor
 * 	Tanggal : 02-10-2014	
 * 	Revisi 	: 
 */

class Region extends CI_Controller {

    // inisialisasi manual
    public $menu_id = 186;

    public function __construct() {
        parent::__construct();
        $this->load->model('m_region');
        $this->load->helper('message_helper');
        $this->load->helper('status');
        $this->load->helper('custom_string_helper');
		$this->load->model('login/m_login');
	
		$is_login = $this->m_login->get_session_id($this->session->userdata('session_id'),$this->session->userdata('username'));
		if($is_login){

		}else{
			$username       = $this->session->userdata('username');
			$session_id     = $this->session->userdata('session_id');
			$this->m_login->delete_session($username,$session_id);
			$this->session->sess_destroy();

			$uri_func = $this->uri->segment(3);
			if(($uri_func === FALSE) || ($uri_func =='add') || ($uri_func =='edit')){
				redirect(base_url() . 'login', 'refresh');
			}else if(in_array($uri_func,array('approve_data','approved_mgr','approved_dir','approve','approve_transsaction','reverse_data','reversed_data','reverse_transsaction','save','save_data','insert','update','update_data','suspend','suspend_data','suspend_transsaction','suspend_mgr','suspend_dir'))){
				$result['status']   = 'session';
				$result['message']  = get_notification('session_destroy',0);
				echo json_encode($result);
				die();
			}
		}
    }

    //Function Index
    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');

                    // set data
                    $data['menu_id'] = $this->menu_id;

                    // get link from tbl_menu 
                    $data['menu_label'] = $this->m_menu->get_menulabel($this->menu_id);
                    $data['menu_link'] = $this->m_menu->get_menulink($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('others/region/v_region', $data);
                    $this->load->view('v_footer');
                }
            } else {
                redirect(base_url() . 'error', 'refresh');
            }
        } else {
            redirect(base_url() . 'login', 'refresh');
        }
    }

    //Function Menampilkan Data Pada Data Grid View
    function ajax() {
        $result = $this->m_region->get_all();
        foreach ($result as $row) {
            $TransStatus = get_status_master($row->TransStatus);

            $hasil[] = array(
                'RegionCode' => $row->RegionCode,
                'RegionName' => $row->RegionName,
                'RegionDesc' => $row->RegionDesc,
                'TransStatus' => $TransStatus
            );
        }
        if (!empty($hasil)) {
            echo json_encode($hasil);
        } else {
            $emptyVar = array();
            echo json_encode($emptyVar);
        }
    }

    //Functiom Add Form
    function add() {
        if ($this->session->userdata("login") == TRUE) {
            $group_menu = $this->session->userdata('group_menu');
            /*
              untuk di index/list = get_menu_by_group,
              untuk di add = get_menu_by_group_add
              untuk di edit = get_menu_by_group_edit
             */
            $arr_group_id = $this->m_menu->get_menu_by_group_add($group_menu, $this->menu_id);
            //--------------------------end-----------------------------------------------------------
            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 {
                    /*
                      modul untuk akses add masing masing modul berebda
                      jangan lupa copy $data nya (jika dia bener langsung ke add)
                     */
                    //untuk ambil data dari session dan hak akses
                    $group_menu = $this->session->userdata('group_menu');

                    // get link from tbl_menu 
                    $data['menu_label'] = $this->m_menu->get_menulabel($this->menu_id);
                    $data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);

                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id, $group_menu);
                    // Create RegionCode
                    $data['RegionCode'] = $this->m_region->create_new_id();

                    $this->load->view('v_header');
                    $this->load->view('others/region/v_new_region', $data);
                    $this->load->view('v_footer');
                }
            } else {
                redirect(base_url() . 'error', 'refresh');
            }
        } else {
            redirect(base_url() . 'login', 'refresh');
        }
    }

    //Functiom Edit Form
    function edit() {
        if ($this->session->userdata("login") == TRUE) {
            $group_menu = $this->session->userdata('group_menu');
            /*
              untuk di index/list = get_menu_by_group,
              untuk di add = get_menu_by_group_add
              untuk di edit = get_menu_by_group_edit
             */
            $arr_group_id = $this->m_menu->get_menu_by_group_edit($group_menu, $this->menu_id);
            //--------------------------end-----------------------------------------------------------
            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 {
                    /*
                      modul untuk akses add masing masing modul berebda
                      jangan lupa copy $data nya (jika dia bener langsung ke add)
                     */
                    //untuk ambil data dari session dan hak akses
                    $group_menu = $this->session->userdata('group_menu');

                    // inisialisasi additional parameter
                    $id = $this->uri->segment(4);

                    // reset session 
                    $this->session->unset_userdata('checking');

                    // set data
                    $data['detail'] = $this->m_region->get_by_id($id);

                    // get from title tbl_menu 
                    $data['menu_label'] = $this->m_menu->get_menulabel($this->menu_id);

                    // get link from tbl_menu 
                    $data['menu_link'] = $this->m_menu->get_menulink($this->menu_id);

                    //panggil model hak akses untuk autoriti
                    $data['group_user'] = $this->m_menu->group_user_autority($this->menu_id, $group_menu);

                    // set for view
                    $this->load->view('v_header');
                    $this->load->view('others/region/v_edit_region', $data);
                    $this->load->view('v_footer');
                }
            } else {
                redirect(base_url() . 'error', 'refresh');
            }
        } else {
            redirect(base_url() . 'login', 'refresh');
        }
    }

    //Function insert
    function insert() {
        try {
            $txt_regioncode = $this->input->post('txt_regioncode');
//            $chk_suspend		= $this->input->post('chk_suspend');//di insert NEW ga ada suspend
            $txt_regionname = $this->input->post('txt_regionname');
            $txa_description = $this->input->post('txa_description');
            $dataSend = [];
            $dataSend['data_region'] = [];

            // cek apakah kode sudah digunakan
            if ($this->m_region->check_code_exist($txt_regioncode) == TRUE) {
                // set nilai untuk ke form
                $result['status'] = false;
                $result['message'] = 'Region Code telah digunakan';
            } else {
                // inisialisasi array
                $dataSend['data_region'] = array(
                    'RegionCode' => $txt_regioncode,
                    'RegionName' => $txt_regionname,
                    'RegionDesc' => $txa_description,
                    'TransStatus' => 1,
                    'LastUser' => $this->session->userdata("username")
                );
                //insert
//                    $hasil = $this->m_region->insert($data);
                $rstDataSend = $this->m_region->datasend_region_insert($dataSend);
                if ($rstDataSend) {
                    $result['status'] = true;
                    $result['message'] = get_notification('insert', 1);
                } else {
                    $result['status'] = false;
                    $result['message'] = get_notification('insert', 0);
                }
                echo json_encode($result);
            }
        } catch (Exception $e) {
            $result['status'] = false;
            $result['message'] = $e->getMessage();
            echo json_encode($result);
        }
    }

    function insert_LAMA() {
        $txt_regioncode = $this->input->post('txt_regioncode');
        $chk_suspend = $this->input->post('chk_suspend');
        $txt_regionname = $this->input->post('txt_regionname');
        $txa_description = $this->input->post('txa_description');

        // cek apakah kode sudah digunakan
        if ($this->m_region->check_code_exist($txt_regioncode) == TRUE) {
            // set nilai untuk ke form
            $result['status'] = false;
            $result['message'] = 'Region Code telah digunakan';
        } else {
            // inisialisasi array
            $data = array(
                'RegionCode' => $txt_regioncode,
                'RegionName' => $txt_regionname,
                'RegionDesc' => $txa_description,
                'TransStatus' => ($chk_suspend == 3) ? $chk_suspend : 0,
                'LastUser' => $this->session->userdata("username")
            );

            //insert
            $hasil = $this->m_region->insert($data);
            if ($hasil == 0) {
                $result['status'] = true;
                $result['message'] = get_notification('insert', $hasil);
            } else {
                $result['status'] = false;
                $result['message'] = get_notification('insert', $hasil);
            }
        }
        echo json_encode($result);
    }

    //function update
    // Function Update
    public function update() {
        try {
            $id = $this->input->post('id');
            $txt_regioncode = $this->input->post('txt_regioncode');
            $chk_suspend = $this->input->post('chk_suspend');
            $txt_regionname = $this->input->post('txt_regionname');
            $txa_description = $this->input->post('txa_description');
            $dataSend = [];
            $dataSend['data_region'] = [];
            $dataSend['idcode'] = $id;

            // inisialisasi array
            $dataSend['data_region'] = array(
                'RegionCode' => $txt_regioncode,
                'RegionName' => $txt_regionname,
                'RegionDesc' => $txa_description,
                'TransStatus' => ($chk_suspend == 3) ? $chk_suspend : 1,
                'LastUser' => $this->session->userdata("username")
            );
            //update
//            $hasil = $this->m_region->update($data, $id);            
            $rstDataSend = $this->m_region->datasend_region_update($dataSend);
            if ($rstDataSend) {
                $result['status'] = true;
                $result['message'] = get_notification('update', 1);
            } else {
                $result['status'] = false;
                $result['message'] = get_notification('update', 0);
            }
            echo json_encode($result);
        } catch (Exception $e) {
            $result['status'] = false;
            $result['message'] = $e->getMessage();
            echo json_encode($result);
        }
    }

    public function update_LAMA() {
        $id = $this->input->post('id');
        $txt_regioncode = $this->input->post('txt_regioncode');
        $chk_suspend = $this->input->post('chk_suspend');
        $txt_regionname = $this->input->post('txt_regionname');
        $txa_description = $this->input->post('txa_description');

        // inisialisasi array
        $data = array(
            'RegionCode' => $txt_regioncode,
            'RegionName' => $txt_regionname,
            'RegionDesc' => $txa_description,
            'TransStatus' => ($chk_suspend == 3) ? $chk_suspend : 0,
            'LastUser' => $this->session->userdata("username")
        );

        //update
        $hasil = $this->m_region->update($data, $id);
        if ($hasil == 0) {
            $result['status'] = true;
            $result['message'] = get_notification('update', $hasil);
        } else {
            $result['status'] = false;
            $result['message'] = get_notification('update', $hasil);
        }
        echo json_encode($result);
    }

    //Function  delete
    function delete() {
        try {
            // inisialisasi additional parameter
            $id = $this->input->post('id');
            $dataSend = [];
            $dataSend['data_region'] = [];
            $dataSend['idcode'] = $id;

            // inisialisasi array
            $dataSend['data_region'] = array(
                'TransStatus' => 3,
                'LastUser' => $this->session->userdata("username")
            );
            //delete from table
//            $hasil	= $this->m_region->delete($id);            
            $rstDataSend = $this->m_region->datasend_region_suspend($dataSend);
            if ($rstDataSend) {
                $result['status'] = true;
                $result['message'] = get_notification('delete', 1);
            } else {
                $result['status'] = false;
                $result['message'] = get_notification('delete', 0);
            }
            echo json_encode($result);
        } catch (Exception $e) {
            $result['status'] = false;
            $result['message'] = $e->getMessage();
            echo json_encode($result);
        }
    }

    function delete_LAMA() {
        // inisialisasi additional parameter
        $id = $this->input->post('id');

        //delete from table
        $hasil = $this->m_region->delete($id);
        if ($hasil == 0) {
            $result['status'] = true;
            $result['message'] = get_notification('Hapus', $hasil);
        } else {
            $result['status'] = false;
            $result['message'] = get_notification('Hapus', $hasil);
        }
        echo json_encode($result);
    }

    function suspend_data() {
        try {
            // inisialisasi additional parameter
            $id = $this->input->post('id');
            $dataSend = [];
            $dataSend['data_region'] = [];
            $dataSend['idcode'] = $id;

            // inisialisasi array
            $dataSend['data_region'] = array(
                'TransStatus' => 3,
                'LastUser' => $this->session->userdata("username")
            );
            //delete from table
//            $hasil	= $this->m_region->delete($id);            
            $rstDataSend = $this->m_region->datasend_region_suspend($dataSend);
            if ($rstDataSend) {
                $result['status'] = true;
                $result['message'] = get_notification('suspend', 1) . " jozzzz";
            } else {
                $result['status'] = false;
                $result['message'] = get_notification('suspend', 0);
            }
            echo json_encode($result);
        } catch (Exception $e) {
            $result['status'] = false;
            $result['message'] = $e->getMessage();
            echo json_encode($result);
        }
    }

    //Function view print data
    public function to_preview($obj_string = null) {
        // pengecekan otoritas
        //if($this->session->userdata("checking") == "") {
        //redirect(base_url() . 'custom/cek/'.$this->menu_id.'/'.$this->menu_model.'/'.$this->to_preview, 'refresh');	
        //}
        // set data
//        $data['list'] = $this->m_region->get_all();

        $data['menu_id']        = $this->menu_id;
        $data['menu_label']     = $this->m_menu->get_menulabel($this->menu_id);
        $data['menu_link']      = $this->m_menu->get_menulink($this->menu_id);
        
        $data_filter            = arrayParamInUrlToStr($obj_string);
        $data['list']           = $this->m_region->get_all_to_print($data_filter);
        
        // set for view
        $this->load->view('v_header');
        $this->load->view('others/region/v_print_region', $data);
        $this->load->view('v_footer');
    }

}
