<?php namespace App\Models\Admin;

use App\Models\Project\DatatableModel;

class UsersModel extends AdminModel{
	private $table;
	
    public function __construct(){
		parent::__construct();
		$this->table = "user";
    }
	
	public function get_datatable(){
		$dtObj = new DatatableModel($this->db);
		$dtObj->set_table($this->table);
		// Set Datatable columns 
		$dtObj->set_column('user_id', 'desc|sort|search', 'RecordID');
		$dtObj->set_column('user_type', 'sort|search');
		$dtObj->set_column('name', 'sort|search');
		$dtObj->set_column('mobile_no', 'sort|search');
		$dtObj->set_column('email_id', 'sort|search');
		$dtObj->set_column('status', 'sort|search');
		$builder = $dtObj->get_builder();
		$builder->where('is_deleted','No')
				->where('user_type!=','Developer');
		return $dtObj->get_data($builder);
	}
	
	public function get($user_id){
		$builder = $this->db->table($this->table);
		$query = $builder->where('user_id',$user_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, $user_id){
		$this->add_update_log($data);
		$builder = $this->db->table($this->table);
		$builder->where('user_id',$user_id);
		$builder->update($data);
	}
	
	public function delete($user_id){
		$data['is_deleted'] = "Yes";
		$this->add_update_log($data);
		$builder = $this->db->table($this->table);
		$builder->where('user_id',$user_id)
				->update($data);
	}
	
	public function is_unique_username($username,$user_id){
		$builder = $this->db->table($this->table);
		$builder->where('username',$username)
				->where('is_deleted','No');
		if($user_id){
			$builder->where('user_id!=',$user_id);
		}
		$query = $builder->get();
		if($query->getRowArray()){
			$response = "FALSE|The Username already exist!";
		}else{
			$response = "TRUE|";
		}
		return $response;
	}
}