<?php

Class Contractareas extends CI_Model
{
	private $mysql;
	private $table;
	private $table2;
	public $costcenterid;
	public $contractarea_id;
	public $contract_area;
	public $email_address;
	public $contact_number;
	public $password;
	public $office_name;
	public $admin_name;
	public $status;
	
	function __construct()
	{
		parent::__construct();
		$this->mysql = $this->load->database('mysql',true);
		$this->table = 'newcontractareas';
		$this->table2 = 'newoffices';
	}

	function get_all()
	{
		return $query = $this->mysql->select('*')->from($this->table)->get()->result_array();
	}
	
	function get_filter_contractarea($id){
		
		return $query = $this->mysql->select('*')->from($this->table)->where('parent',$id)->get()->result_array();
	}

	function add_contractArea()
	{
		$data['description'] = $this->contract_area;
		$data['parent'] = $this->costcenterid;
		$data['create_date'] = date('Y-m-d H:i:s');
		//debug($data,1);
		$this->mysql->insert($this->table, $data);
		$insert_id = $this->mysql->insert_id();

		/*for( $i=0; $i<count( $this->email_address ); $i++ )
		{
			$data2['contractarea_id'] = $insert_id;
			$data2['email'] = $this->email_address[$i];
			$data2['office_name'] = $this->office_name[$i];
			$data2['admin_name'] = $this->admin_name[$i];
			$data2['contact_no'] = $this->contact_number[$i];
			$data2['password'] = $this->password[$i];
			$data2['create_date'] = date('Y-m-d H:i:s');
			$this->mysql->insert($this->table2, $data2);
		}
		$insert_id2 = $this->mysql->insert_id();*/
		if($insert_id > 0)
		{
			return "done";
		}
		else
		{
			return $this->db->error();
		}
	}

	function EditcontractAreaOffices()
	{
		$data['description'] = $this->contract_area;
		$query = $this->mysql->where([ 'lindex' => $this->contractarea_id ])->update('newcontractareas', $data);
		if($query)
		{
			echo  'update';
		}
		else
		{
			$this->mysql->error();
		}
	}

	function get_contract_list()
	{
		return $this->mysql->select('*')->from('newcontractareas')->where([ 'parent' => $this->costcenterid ])->get()->result_array();

	}

	function get_contract_office()
	{
		return $this->mysql->select('*')->from('newoffices')->where(['parent' => $this->contractarea_id ])->get()->result_array();
		//echo $this->mysql->last_query();	
	}

	function change_status_contractarea()
	{
		$data['status'] = $this->status;
		$query = $this->mysql->where(['lindex' => $this->contractarea_id])->update('newcontractareas', $data);
		if($query)
		{
			echo 'done';
		}
		else
		{
			return $this->mysql->error();
		}
	}

}