<?php namespace App\Models\Admin;

use App\Models\Project\DatatableModel;

class CountryModel extends AdminModel{
	private $table;
	
    public function __construct(){
		parent::__construct();
		$this->table = "country";
    }
	
	public function get_datatable(){
		$dtObj = new DatatableModel($this->db);
		$dtObj->set_table($this->table);
		// Set Datatable columns 
		$dtObj->set_column('country_id', 'desc|sort|search', 'RecordID');
		$dtObj->set_column('name','sort|search');
		$dtObj->set_column('photo', 'sort|search');
		$dtObj->set_column('status', 'sort|search');
		$builder = $dtObj->get_builder();
		$builder->where('is_deleted','No');
	    return $dtObj->get_data($builder);
	}
	
	public function get($country_id){
		$builder = $this->db->table($this->table);
		$query = $builder->where('country_id',$country_id)->get();
		return $query->getRowArray();
	}
	
	public function insert($data){
		 
		$this->add_insert_log($data);
		$builder = $this->db->table($this->table);
		$builder->insert($data);
	}
	
	public function update($data, $country_id){
		$this->add_update_log($data);
		$builder = $this->db->table($this->table);
		$builder->where('country_id',$country_id);
		$builder->update($data);
	}
	
	public function delete($country_id){
		$data['is_deleted'] = "Yes";
		$this->add_update_log($data);
		$builder = $this->db->table($this->table);
		$builder->where('country_id',$country_id)
				->update($data);
	}
	
	public function is_unique_title($title,$country_id){
		$builder = $this->db->table($this->table);
		$builder->where('is_deleted','No')
				->where('title',$title);
		if($country_id){
			$builder->where('country_id!=',$country_id);
		}
		$query = $builder->get();
		if($query->getRowArray()){
			$response = "FALSE|The Name already exist!";
		}else{
			$response = "TRUE|";
		}
		return $response;
	}
}