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

class SaleAgentSetup extends CI_Controller {
	
	public function __construct(){
		parent::__construct();
		$this->load->model('menu');
	}

	public function index() {
		
	}
	
	//Created by Farhana
	public function newAgent()
	{
	  if($this->input->post('mysubmit')){
		  if($this->input->post('ID')==NULL){
			  $newagent = array(
			  	'ID'		=> $this->input->post('ID'), 
			  	'companyID'		=> element('compID', $this->session->userdata('logged_in')), 
			  	'agentCode'		=> $this->input->post('agentCode'),
			  	'agentName'		=> $this->input->post('agentName'),
				'position'		=> $this->input->post('position'),
				'telOffice'		=> $this->input->post('telOffice'),
				'telHp'		=> $this->input->post('telHp'),
				'email'		=> $this->input->post('email'),
				'commisionRate'		=> $this->input->post('commisionRate'),
				'remarks'		=> $this->input->post('remarks'),
	);
			$this->load->model('SaleAgent/agent_model');
		  	$insert_agent = $this->agent_model->agent_insert($newagent);
			if($insert_agent){
				echo '<script>alert("Insert Data Success..");
				window.location.replace("agentlist");
				</script>';
			}else{
				echo '<script>alert("Insert Data Failed.");
				history.go(-1);</script>';
			}
		  }
		  else{
			$ID = $this->input->post('ID');
		  	$newagent = array(
				'ID'		=> $this->input->post('ID'), 
			  	'companyID'		=> element('compID', $this->session->userdata('logged_in')), 
			  	'agentCode'		=> $this->input->post('agentCode'),
			  	'agentName'		=> $this->input->post('agentName'),
				'position'		=> $this->input->post('position'),
				'telOffice'		=> $this->input->post('telOffice'),
				'telHp'		=> $this->input->post('telHp'),
				'email'		=> $this->input->post('email'),
				'commisionRate'		=> $this->input->post('commisionRate'),
				'remarks'		=> $this->input->post('remarks'),
	);
			$this->load->model('SaleAgent/agent_model');
			$insert_agent = $this->agent_model->agent_edit($newagent, $ID);
			if($insert_agent){
				echo '<script>alert("Update Success..");
					window.location.replace("agentlist");
					</script>';
			}else{
				echo '<script>alert("Update Failed.");
				history.go(-1);</script>';
			}
		  }
		   } else {     
			$ID = isset($_REQUEST['ID'])?$_REQUEST['ID']:"";
			$this->lang->load("agent",$this->session->userdata('language'));
			$this->load->library('layouts');
			$this->load->helper('form');
			$this->load->model('SaleAgent/agent_model');
	
			$this->layouts->add_includes('js/validationengine/languages/jquery.validationEngine-'.$this->session->userdata('language').'.js')
			->add_includes('js/validationengine/jquery.validationEngine.js');
			
			if($ID >= '0'){
				$dataum = $this->agent_model->agent_id($ID);
				echo $this->db->last_query();
				$data['agentmaster'] = $dataum[0];
				$this->layouts->view('agent/add_new', array('latest' => 'sidebar/latest'), $data);
			}else{
				$data['agentmaster'] = '';
				$this->layouts->view('agent/add_new', array('latest' => 'sidebar/latest'), $data);
			}
						
			}
	}
		public function agent_delete(){
			$this->load->model('SaleAgent/agent_model');
		  	$deleteuser = array('is_delete' => '1');
			$ID = $this->input->post('ID');
		  	$delete_user = $this->agent_model->agent_delete($deleteuser, $ID);
			if($delete_user){
				return TRUE;
			}else{
				return FALSE;
			}
	}

	public function agent_edit(){
			$this->load->model('SaleAgent/agent_model');
		  	$updateuser = array('is_delete' => '1');
			$ID = $this->input->post('ID');
		  	$updateuser = $this->agent_model->agent_edit($updateuser,$ID);
			if($updateuser){
				return TRUE;
			}else{
				return FALSE;
			}
	}
	public function agentList(){
		$this->load->library('layouts');
		$this->load->model('SaleAgent/agent_model');
		$data['datatbls'] = $this->agent_model->agent_all();
		$this->lang->load("agent",$this->session->userdata('language'));
		$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->layouts->view('agent/list', array('latest' => 'sidebar/latest'), $data);
	}
	
	public function agentPrint($mode = false){
		$this->load->library('layouts');
		$this->load->helper('form');
		$this->load->helper('menu');
		$this->load->model('company/company_info_model');
		$this->load->model('SaleAgent/agent_model');
		
		$this->lang->load("agent",$this->session->userdata('language'));
		
		$data['datatbls'] = $this->agent_model->agent_all();
            $headerData['rptTitle'] = $this->lang->line("rptTitle");
			$bodyData['rptDataDetail'] = $data['datatbls'];
			$bodyData['tableHeader'] = array($this->lang->line("agentName"), $this->lang->line("position"), $this->lang->line("telOffice"), $this->lang->line("telHp"), $this->lang->line("email"), $this->lang->line("commisionRate")); 
			$bodyData['selectFields'] = array("agentName","position","telOffice","telHp","email","commisionRate");
			$bodyData['tableFooter'] = array ("");
			
			$this->load->view('layouts/report/header', $headerData);
			$this->load->view('layouts/report/body', $bodyData);
			$this->load->view('layouts/report/footer','');
		if($mode){
			$this->load->helper('excel');
			excel($bodyData);
		}
	}
	
	// by issa // is used to refresh combo
	public function refreshAgent(){
		//for ajax call 
		$this->load->model('SaleAgent/Agent_model');
		$agent = $this->Agent_model->agent_all();
		
		//json_encode ($this->project_model->project_all());
		
		$JsonRecords = '{"records":[';
		$n = sizeof($agent);
		$i= 0;
		if($agent)
		 foreach($agent as $row){
			$JsonRecords .= '{';
			$JsonRecords .= '"agentName":"' . $row->agentName.'","id":"'.$row->ID.'"';
			$JsonRecords .= '}';
			if ($i < $n - 1)
				$JsonRecords .= ',';
			$i++;
		}
		$JsonRecords .= ']}';

		//$resul= '{"supplier":['. json_encode($this->supplier_model->suppliermaster_all()).']}';
		 echo $JsonRecords;
	}
	
}



?>