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

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

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

	public function fnCustomersDataJson()
	{
		$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_customer_id,f_customer_name,f_customer_link,f_customer_active');
		$this->datatables->from('t_customers');
		$this->datatables->add_column('Aksi',$button,'f_customer_id');
		
		echo $this->datatables->generate();		
	}

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

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

	public function fnCustomersSave()
	{
		$this->rolemodule->check_function('add');
		if(!$this->mo_customers->cekLink())
		{
			echo json_encode(array('msg'=>false,'kode'=> 'ShortLink sudah digunakan'));
		}
		else
		{
			if($this->mo_customers->saveData())
			{
				$this->session->set_userdata('SUCMSG','Sukses Tambah !!!');
				echo json_encode(array('msg'=>true));
			}
			else
			{
				echo json_encode(array('msg'=>false,'kode'=> ' Terjadi kesalahan'));
			}
		}		
	}

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

	public function fnCustomersUpdate()
	{
		$this->rolemodule->check_function('edit');
		$id 	= $this->input->get_post('id');
		if(!$this->mo_customers->cekLink($id))
		{
			echo json_encode(array('msg'=>false,'kode'=> 'ShortLink sudah digunakan'));
		}
		else
		{
			if($this->mo_customers->updateData($id))
			{
				$this->session->set_userdata('SUCMSG','Sukses Edit !!!');
				echo json_encode(array('msg'=>true));
			}
			else
			{
				echo json_encode(array('msg'=>false,'kode'=> ' Terjadi kesalahan'));
			}
		}		
	}

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

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

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

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

        $data = array(
            't_customers_data' => $this->mo_customers->fnGetData(),
            'start' => 0
        );
        
        $this->load->view('vw_customers_doc',$data);
    }

	public function fnListCustomers()
	{
		$this->db->select('f_customer_id,f_customer_name',false);
		$this->db->like('f_customer_name',$this->input->get_post('query'), 'both');
		$this->db->limit(15);
		$sql = $this->db->get('t_customers');
		$data = array();
		if($sql->num_rows()>0)
		{
			foreach($sql->result() as $a)
			{
				$data[] = array(
					'id' => $a->f_customer_id,
					'label' => $a->f_customer_name.' (SUDAH ADA)',
				);
			}
			echo json_encode($data);
		}
		else
		{
			$data = array(
					'id' => 0,
					'label' => 'Belum Terdaftar',
				);
			echo json_encode(array($data));
		}
	}

	public function fnListShortLink()
	{
		$this->db->select('f_customer_id,f_customer_link',false);
		$this->db->like('f_customer_link',$this->input->get_post('query'), 'both');
		$this->db->limit(15);
		$sql = $this->db->get('t_customers');
		$data = array();
		if($sql->num_rows()>0)
		{
			foreach($sql->result() as $a)
			{
				$data[] = array(
					'id' => $a->f_customer_id,
					'label' => $a->f_customer_link.' (SUDAH ADA)',
				);
			}
			echo json_encode($data);
		}
		else
		{
			$data = array(
					'id' => 0,
					'label' => 'Belum Terdaftar',
				);
			echo json_encode(array($data));
		}
	}

}

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