<?php

class loginController extends Apank_Controller
{
	public $baseLayout = 'login';
	
	public function __construct($config)
	{
		$this->getConfiguration($config);
	}
	
	public function index()
	{
		if($_POST){
		
			$error = $this->getValidation('login')->cekLogin();
			if($error == false){

				$data = $this->getModel('login')->findUser();

				if($this->em->getNumRows($data) == true){
				
					$this->getModel('spk')->setExpDateUnit();
				
					if(!empty($_POST['lastUrl'])){
						$lastUrl = 'http://'.str_ireplace('(!)', '&', str_ireplace('[!]', '/', $_POST['lastUrl']));
					}else{
						$lastUrl = 'index';
					}
					
					$this->setSession($this->em->getFetchObject($data));
					$this->data->page = $lastUrl; 
					$this->baseLayout = 'redirectLogin';
				}else{
					$this->getPlugin('Error')->getAlertError('UserName atau Password Salah!');
				}
			}
		}
		$this->view = 'none';
	}
	
	protected function setSession($data)
	{
		$mainDealer = $this->getModel('supplier')->getMainDealer();
	
		$_SESSION['loginHspm']['kode'] 	 = $data[0]->no_induk;
		$_SESSION['loginHspm']['nama'] 	 = $data[0]->namakaryawan;
		$_SESSION['loginHspm']['pass'] 	 = $data[0]->passwd;
		$_SESSION['loginHspm']['cabang'] = $data[0]->kodecabang;
		$_SESSION['loginHspm']['namacabang'] = $data[0]->namacabang;
		$_SESSION['loginHspm']['kdMain'] = $mainDealer->kodesupp;
		$_SESSION['loginHspm']['nmMain'] = $mainDealer->namasupp;
		$_SESSION['loginHspm']['nmcbg']  = $data[0]->namacabang;
		$_SESSION['loginHspm']['alamat'] = $data[0]->alamatcabang;
		$_SESSION['loginHspm']['telp']   = $data[0]->telpcabang;
		$_SESSION['loginHspm']['fax']    = $data[0]->fax;
		$_SESSION['loginHspm']['nmkota'] = $data[0]->namakota;
		$_SESSION['loginHspm']['kdkota'] = $data[0]->kotacabang;
		$_SESSION['loginHspm']['area'] 	 = $data[0]->area;
		$_SESSION['loginHspm']['jabatan']= $data[0]->kodejabatan;
		$_SESSION['loginHspm']['kacab']	 = $data[0]->badanhukum;
		$_SESSION['loginHspm']['emailKacab'] = $data[0]->emailkacab;
		$_SESSION['loginHspm']['warning'] = $data[0]->warning;
		$_SESSION['loginHspm']['kd_md']  = 'SIS01';
		$_SESSION['loginHspm']['nm_md']  = 'PT.SUZUKI INDOMOBIL SALES';
		$_SESSION['loginHspm']['kd_md_parts']  = '000100';
		$_SESSION['loginHspm']['nm_md_parts']  = 'PT SUZUKI INDOMOBIL MOTOR';
		$_SESSION['loginHspm']['kd_ho']  = '00';
		$_SESSION['loginHspm']['nm_ho']  = 'HEAD OFFICE';
		$_SESSION['loginHspm']['fad']  	 = 'KISBUN NAKHLI';
		$_SESSION['loginHspm']['gm']  	 = 'HANDAJANA BASUKI';
		$_SESSION['loginHspm']['emailFad']  	= 'hspm.fad@gmail.com';
		$_SESSION['loginHspm']['emailGm']  		= 'handajana.basuki@yahoo.co.id';
		$_SESSION['loginHspm']['pt_nama']  		= 'PT. HERO SAKTI PUTRA MOTOR';
		$_SESSION['loginHspm']['pt_alamat'] 	= 'Jl. Trunojoyo 21 Kepatihan Kaliwates Jember';
		$_SESSION['loginHspm']['pt_telp']  		= '0331-489760(Hunting)';
		$_SESSION['loginHspm']['pt_fax']  		= '0331-487517';
		$_SESSION['loginHspm']['pt_npwp']   	= '02.306.363.9-651.000';
		$_SESSION['loginHspm']['pt_pkp']   		= '02.306.363.9-651.000';
		$_SESSION['loginHspm']['pt_tgl_npwp']	= '2003-08-27';
		$_SESSION['loginHspm']['pt_tgl_pkp']	= '2003-08-27';
		$_SESSION['loginHspm']['pt_bank']   	= 'BCA Cabang Jember';
		$_SESSION['loginHspm']['pt_bank_an']	= 'PT. HERO SAKTI PUTRA MOTOR';
		$_SESSION['loginHspm']['pt_no_rek'] 	= '024.395379.8';
		$_SESSION['loginHspm']['ccd_1'] 	= 'DIEN JULIA';
		$_SESSION['loginHspm']['ccd_2'] 	= 'NITA';
		
	}
	
	public function logout()
	{
		session_destroy();
		header('location: '.$this->baseUrl.'login');
	}
}