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

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

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

	public function fnEventDataJson()
	{
		$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_event_id,f_customer_name,f_event_name,f_event_no,f_event_start,f_event_end,f_event_active');
		$this->datatables->join('t_customers b','b.f_customer_id=a.f_customer_id');
		$this->datatables->from('t_event a');
		$this->datatables->add_column('Aksi',$button,'f_event_id');
		
		echo $this->datatables->generate();		
	}

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

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

	public function fnEventSave()
	{
		$this->rolemodule->check_function('add');
		if($this->mo_event->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 fnEventEdit($id)
    {
		$this->rolemodule->check_function('edit');
		$data = array(
			'app_title'=>'Form Event',
			'app_desc' =>'it all Form Event',
			'form_header'	=>'<i class="fa fa-edit"></i> Edit&nbsp;&nbsp;',
			'fAct'			=>'Edit',
			'fId'			=>$id,
		);
		
        $data['content']	= 'vw_event_add';
        $this->load->view('webadmin/main', $data);
    }

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

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

	public function fnEventCekPeriode()
	{
		$id = $this->input->get_post('id');
		$this->db->select_max('f_event_no','urut');
		$sql = $this->db->get_where('t_event a',array('a.f_customer_id'=> $id));
		if($sql->num_rows()>0)
		{
			$no = $sql->row();
			echo $no->urut + 1;
		}
		else
		{
			echo 1;
		}
	}
	
	public function fnEventComboData()
	{
		$json['result'] = $this->mo_event->remoteComboData();
		echo json_encode($json);
	}

	public function fnEventExcel()
    {
        $this->load->helper('exportexcel');
        $namaFile = "t_event.xls";
        $judul = "t_event";
        $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 Event Id");
		xlsWriteLabel($tablehead, $kolomhead++, "F Customer Id");
		xlsWriteLabel($tablehead, $kolomhead++, "F Event Name");
		xlsWriteLabel($tablehead, $kolomhead++, "F Event No");
		xlsWriteLabel($tablehead, $kolomhead++, "F Event 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_event->fnGetData() as $data) {
            $kolombody = 0;

            //ubah xlsWriteLabel menjadi xlsWriteNumber untuk kolom numeric
            xlsWriteNumber($tablebody, $kolombody++, $nourut);
			xlsWriteNumber($tablebody, $kolombody++, $data->f_event_id);
			xlsWriteNumber($tablebody, $kolombody++, $data->f_customer_id);
			xlsWriteLabel($tablebody, $kolombody++, $data->f_event_name);
			xlsWriteNumber($tablebody, $kolombody++, $data->f_event_no);
			xlsWriteNumber($tablebody, $kolombody++, $data->f_event_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 fnEventWord()
    {
        header("Content-type: application/vnd.ms-word");
        header("Content-Disposition: attachment;Filename=t_event.doc");

        $data = array(
            't_event_data' => $this->mo_event->fnGetData(),
            'start' => 0
        );
        
        $this->load->view('vw_event_doc',$data);
    }

}

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