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

class Subkategori extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('mo_subkategori');
		$config = array('module' => 'subkategori','roleid'=> $this->session->userdata('sRoleId'));
		$this->load->library('rolemodule',$config);
    }

	public function index()
    {
		$this->rolemodule->view_page();
		$data = array(
			'app_title'=>'Data Sub Kategori',
			'app_desc' =>'it all Sub Kategori'
		);
		
        $data['content']	= 'vw_subkategori';
        $this->load->view('webadmin/main', $data);
    }

	public function fnSubkategoriDataJson()
	{
		$button = '';
		if($this->rolemodule->check_button('edit'))
		{
			$button .= '&nbsp;&nbsp;<a href="#" onclick="fnEdit($1)" class="tip" title="Edit"><i class="glyphicon glyphicon-edit"></i></a>&nbsp;&nbsp;';
		}

		if($this->rolemodule->check_button('delete'))
		{
			$button .= '&nbsp;&nbsp;<a href="#" onclick="fnDelete($1)" class="tip" title="Hapus"><i class="text-red glyphicon glyphicon-trash"></i></a>&nbsp;&nbsp;';
		}

		$this->load->library('datatables');
		$this->datatables->select('f_subkategori_id,f_kategori_name,f_subkategori_name,f_subkategori_desc,f_subkategori_active');
		$this->datatables->join('t_kategori b','b.f_kategori_id=a.f_kategori_id');
		$this->datatables->from('t_subkategori a');
		$this->datatables->add_column('Aksi',$button,'f_subkategori_id');
		
		echo $this->datatables->generate();		
	}

	public function fnSubkategoriDataId()
	{
		$id 	= $this->input->get_post('id');
		$data	= $this->mo_subkategori->getDataId($id);
		echo json_encode($data);		
	}

	//Untuk Form Terpisah
	public function fnSubkategoriAdd()
    {
		$this->rolemodule->check_function('add');
		$data = array(
			'app_title'=>'Form Sub Kategori',
			'app_desc' =>'it all Form Sub Kategori',
			'form_header'	=>'<i class="fa fa-table"></i> Add&nbsp;&nbsp;',
			'fAct'			=>'Add',
		);
		
        $data['content']	= 'vw_subkategori_add';
        $this->load->view('webadmin/main', $data);
    }

	public function fnSubkategoriSave()
	{
		$this->rolemodule->check_function('add');
		if($this->mo_subkategori->saveData())
		{
			$this->session->set_userdata('SUCMSG','Sukses Tambah !!!');
			echo json_encode(array('msg'=>true));
		}
		else
		{
			echo json_encode(array('msg'=>false));
		}		
	}

	//Untuk Form Terpisah
	public function fnSubkategoriEdit($id)
    {
		$this->rolemodule->check_function('edit');
		$data = array(
			'app_title'=>'Form Sub Kategori',
			'app_desc' =>'it all Form Sub Kategori',
			'form_header'	=>'<i class="fa fa-edit"></i> Edit&nbsp;&nbsp;',
			'fAct'			=>'Edit',
			'fId'			=>$id,
		);
		
        $data['content']	= 'vw_subkategori_add';
        $this->load->view('webadmin/main', $data);
    }

	public function fnSubkategoriUpdate()
	{
		$this->rolemodule->check_function('edit');
		$id 	= $this->input->get_post('id');
		if($this->mo_subkategori->updateData($id))
		{
			$this->session->set_userdata('SUCMSG','Sukses Edit !!!');
			echo json_encode(array('msg'=>true));
		}
		else
		{
			echo json_encode(array('msg'=>false));
		}		
	}

	public function fnSubkategoriDelete()
	{
		$this->rolemodule->check_function('delete');
		$id 	= $this->input->get_post('id');
		if($this->mo_subkategori->deleteData($id))
		{
			echo 'TRUE';
		}
		else
		{
			echo 'FALSE';
		}		
	}

	public function fnSubkategoriComboData()
	{
		$json['result'] = $this->mo_subkategori->remoteComboData();
		echo json_encode($json);
	}

	public function fnKategoriComboData()
	{
		$json['result'] = $this->mo_subkategori->remoteComboDataKat();
		echo json_encode($json);
	}

	public function fnSubkategoriExcel()
    {
        $this->load->helper('exportexcel');
        $namaFile = "t_subkategori.xls";
        $judul = "t_subkategori";
        $tablehead = 0;
        $tablebody = 1;
        $nourut = 1;
        //penulisan header
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header("Content-Disposition: attachment;filename=" . $namaFile . "");
        header("Content-Transfer-Encoding: binary ");

        xlsBOF();

        $kolomhead = 0;
        xlsWriteLabel($tablehead, $kolomhead++, "No");
		xlsWriteLabel($tablehead, $kolomhead++, "F Sub Kategori Id");
		xlsWriteLabel($tablehead, $kolomhead++, "F Kategori Id");
		xlsWriteLabel($tablehead, $kolomhead++, "F Sub Kategori Name");
		xlsWriteLabel($tablehead, $kolomhead++, "F Sub Kategori Desc");
		xlsWriteLabel($tablehead, $kolomhead++, "F Sub Kategori Active");
		xlsWriteLabel($tablehead, $kolomhead++, "F Created On");
		xlsWriteLabel($tablehead, $kolomhead++, "F Created By");
		xlsWriteLabel($tablehead, $kolomhead++, "F Updated On");
		xlsWriteLabel($tablehead, $kolomhead++, "F Updated By");

		foreach ($this->mo_subkategori->fnGetData() as $data) {
            $kolombody = 0;

            //ubah xlsWriteLabel menjadi xlsWriteNumber untuk kolom numeric
            xlsWriteNumber($tablebody, $kolombody++, $nourut);
			xlsWriteNumber($tablebody, $kolombody++, $data->f_subkategori_id);
			xlsWriteNumber($tablebody, $kolombody++, $data->f_kategori_id);
			xlsWriteLabel($tablebody, $kolombody++, $data->f_subkategori_name);
			xlsWriteLabel($tablebody, $kolombody++, $data->f_subkategori_desc);
			xlsWriteNumber($tablebody, $kolombody++, $data->f_subkategori_active);
			xlsWriteLabel($tablebody, $kolombody++, $data->f_created_on);
			xlsWriteNumber($tablebody, $kolombody++, $data->f_created_by);
			xlsWriteLabel($tablebody, $kolombody++, $data->f_updated_on);
			xlsWriteNumber($tablebody, $kolombody++, $data->f_updated_by);

			$tablebody++;
			$nourut++;
        }

        xlsEOF();
        exit();
	}

	public function fnSubkategoriWord()
    {
        header("Content-type: application/vnd.ms-word");
        header("Content-Disposition: attachment;Filename=t_subkategori.doc");

        $data = array(
            't_subkategori_data' => $this->mo_subkategori->fnGetData(),
            'start' => 0
        );
        
        $this->load->view('vw_subkategori_doc',$data);
    }

}

/* End of file Subkategori.php */
/* Location: ./application/modules/Subkategori/controllers/Subkategori.php */ 
