<?php

Class Contractarea extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->model('cost_center');
		$this->load->model('contractareas');
		$this->load->library('session');
		if( $this->session->userdata('type') == "" )
		{
			$this->session->set_flashdata('error_msg','Not Login Yet');
			redirect('user/index');
		}
	}

	function index()
	{
		if(isset($_POST['costcenterfilter']) && !empty($_POST['costcenterfilter']) && $_POST['costcenterfilter']!="all"){

			$this->session->set_userdata('costcenterfilter',$_POST['costcenterfilter']);
			$data['costcenter'] = $this->cost_center->cc_list();
			$get_list = $this->contractareas->get_filter_contractarea($_POST['costcenterfilter']);
			$get_list_populate_selectbox = $this->contractareas->get_all();
			for( $i=0; $i<count($get_list); $i++ )
			{
				$this->cost_center->costcenterid = $get_list[$i]['parent'];
				$get_list[$i]['costcenter'] = $this->cost_center->get_list();
				$this->contractareas->contractarea_id = $get_list[$i]['lindex'];
				$get_list[$i]['office_count'] = count($this->contractareas->get_contract_office());
				$data['list_costcenter'] = $get_list;
			}
			$this->load->view('contractarea/contract_area', $data);
			
		}else{
			$this->session->set_userdata('costcenterfilter','');
			$data['costcenter'] = $this->cost_center->cc_list();
			$get_list = $this->contractareas->get_all();
			//debug($get_list);
			for( $i=0; $i<count($get_list); $i++ )
			{
				$this->cost_center->costcenterid = $get_list[$i]['parent'];
				$get_list[$i]['costcenter'] = $this->cost_center->get_list();
				
				$this->contractareas->contractarea_id = $get_list[$i]['lindex'];
				$office_count = count($this->contractareas->get_contract_office());
				//debug($office_count,1);
				if($office_count)
				{
					$get_list[$i]['office_count'] = $office_count;
				}
				else
				{
					$get_list[$i]['office_count'] = '0';
				}
				
				
				$data['list_costcenter'] = $get_list;
			}
			//debug($data);
			$this->load->view('contractarea/contract_area', $data);
		}
	}
	

	function check_email()
	{
		$this->contractareas->email = $this->input->post('email_address');
		$check_contract_email = $this->contractareas->check_contract_email();
	}

	function add_contractAreaOffice()
	{
		$this->contractareas->costcenterid = $this->input->post('costcenter', true);
		$this->contractareas->contract_area = $this->input->post('contract_area', true);
		echo $contractAreaOffices = $this->contractareas->add_contractArea();

	}

	function edit_contractArea()
	{
		$this->contractareas->costcenter = $this->input->post('costcenter');
		$this->contractareas->contract_area = $this->input->post('contract_area');
		$this->contractareas->contractarea_id = $this->input->post('contractarea_id');
		echo $EditcontractAreaOffices = $this->contractareas->EditcontractAreaOffices();
	}

	function change_status()
	{
		$this->contractareas->contractarea_id = $this->input->post('id', true);
		$this->contractareas->status = $this->input->post('status', true);
		echo $change_status_contractarea = $this->contractareas->change_status_contractarea();
	}
	
}