<?php namespace App\Models\Front;
use App\Models\Project\Zebra_Mptt;
class CommonModel{
	protected $db;
	protected $session; 
	protected $project;
	
    public function __construct(){
        $this->db = \Config\Database::connect();
		$this->session = session();
		$this->project = config('Config\\Project');
    }
	
	public function get_table_builder($table_name){
		return $this->db->table($table_name);
	}


	public function get_list($table,$pk_column,$pk_value){
		$builder = $this->db->table($table);
		$query = $builder->where($pk_column,$pk_value)
		            ->where('is_deleted','No')
					->get();
		return $query->getResultArray();
	}
	
	public function insert_record($table,$data){
		$builder = $this->db->table($table);
		$builder->insert($data);
		return $this->db->insertID();
	}
	
	public function get_config_dtl(){
		$builder = $this->db->table('config');
		$query = $builder->where('config_id',1)
				->get();
		return $query->getRowArray();
	}
	function get_company_detail($company_id){
		$builder = $this->db->table('company');
		$query = $builder->where('company_id',$company_id)
					->get();
		return $query->getRowArray();
	}
	public function send_sms($mobile_no, $msg, $template_id=""){
		$config = $this->get_config_dtl();
		$url = $config['sms_url']."&".
			   $config['sms_mobile_no_field']."=".$mobile_no."&".
			   $config['sms_msg_field']."=".rawurlencode($msg);
		if($template_id!=""){
			$url .= "&templateid=".$template_id;
		}
		//echo $url;
		//exit();
		$ch = curl_init($url); 
 	 	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
	  	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
	  	$curl_scraped_page = curl_exec($ch); 
	  	curl_close($ch);
	}
	
	public function get_email_object($from_email="",$from_name=""){
		$email = \Config\Services::email();
		$config['protocol'] = 'sendmail';
		$config['mailPath'] = '/usr/sbin/sendmail';
		$config['charset']  = 'utf-8';
		$config['wordWrap'] = false;
		$config['mailType'] = 'html';
		$email->initialize($config);
		$from_email = $from_email ?: 'noreply@bosssecurity.in';
		$from_name = $from_name ?: $this->project->project_name;
		$email->setFrom($from_email, $from_name);
		return $email;
	}
	
	function getOneRow($table,$where){		
		$query=$this->db->query("select * from $table $where limit 1");
		return $query->getRowArray();
	}
	
	function get_site_menu_tree(){	
		$config = $this->get_config_dtl();
		$mptt = new Zebra_Mptt($this->db->connID,'site_menu','site_menu_id','title','lft','rgt','parent_site_menu_id');
		return $mptt->get_active_node_tree(1,false);		
	}
	
	function is_language_supported($lang_code){
		$builder = $this->db->table('language');
		$query = $builder->selectCount('language_id','no_of_rec')
			->where('status','Active')
			->where('is_deleted','No')
			->where('code',$lang_code)
			->get();
		$result = $query->getRowArray();
		if($result['no_of_rec']){
			return true;
		}
		return false;
	}
	public function get_status_options($default="",$only_default=false){
		$default = $default ?: 'Active';
		$opt_str = $only_default ? '' : '<option value="">Select</option>';
		$opt_arr = ['Active', 'Inactive'];
		foreach($opt_arr as $opt_val){
			if($opt_val==$default){
				$opt_str .= '<option value="'.$opt_val.'" selected>'.$opt_val.'</option>';
			}else if( ! $only_default){
				$opt_str .= '<option value="'.$opt_val.'">'.$opt_val.'</option>';
			}
		}
		return $opt_str;
	}
	
	public function get_product_menu(){
		$builder = $this->db->table('products AS p');
		$builder->join('products_ln AS pl',"p.product_id=pl.product_id AND pl.language_code='".$this->project->default_lang_code."'","inner");
		$query = $builder->select('p.product_id,p.product_url,pl.title')
			->where('p.status','Active')
			->where('p.is_deleted','No')
			->orderBy('pl.title')
			->get();
		return $query->getResultArray();
	}
	public function get_country_list(){
		$builder = $this->db->table('country AS c');
		$query = $builder->select('c.country_id,c.name')
			->where('c.status','Active')
			->where('c.is_deleted','No')
			->orderBy('c.country_id')
			->get();
		return $query->getResultArray();
	}
	public function get_city_list($country_id){
		$builder = $this->db->table('city AS c');
		$query = $builder->select('c.city_id,c.name')
			->where('c.status','Active')
			->where('c.is_deleted','No')
			->where('c.country_id',$country_id)
			->orderBy('c.city_id')
			->get();
		return $query->getResultArray();
	}

	
	
	public function get_location_options($default="",$only_default=false){
		$builder = $this->db->table('client');
		$query = $builder->select('city')
			->orderBy('city','ASC')
			->groupBy('city')
			->get();
		$opt_list = $query->getResultArray();
		$opt_str = $only_default ? '' : '<option value=""> -- Select Location -- </option>';
		if($opt_list){
			foreach($opt_list as $opt_dtl){
				if($opt_dtl['city']==$default){
					$opt_str .= '<option value="'.$opt_dtl['city'].'" selected>'.$opt_dtl['city'].'</option>';
				}else{
					$opt_str .= '<option value="'.$opt_dtl['city'].'">'.$opt_dtl['city'].'</option>';
				}
			}
		}
		return $opt_str;
	}
	
	public function get_property_type_options($default="",$only_default=false){
		$opt_str = $only_default ? '' : '<option value=""> -- Select Property Type -- </option>';
		// Get  list
		$builder = $this->db->table('property_type');
		$query = $builder->select('property_type_id, title')
				->orderBy('title','ASC')
				->where('is_deleted','No')
				->where('status','Active')
				->get();
		$opt_arr = $query->getResultArray();
		if($opt_arr){
			foreach($opt_arr as $opt_dtl){
				if($opt_dtl['property_type_id']==$default){
					$opt_str .= '<option value="'.$opt_dtl['property_type_id'].'" selected>'.$opt_dtl['title'].'</option>';
				}else if( ! $only_default){
					$opt_str .= '<option value="'.$opt_dtl['property_type_id'].'">'.$opt_dtl['title'].'</option>';
				}
			}
		}
		return $opt_str;
	}
	
	public function get_post_options($default="",$only_default=false){
		$opt_str = $only_default ? '' : '<option value="" disabled selected>---Select Post---</option>';
		$opt_arr = ['Legal Officer','Legal Officer'];
		foreach($opt_arr as $opt_val){
			if($opt_val==$default){
				$opt_str .= '<option value="'.$opt_val.'" selected>'.$opt_val.'</option>';
			}else if( ! $only_default){
				$opt_str .= '<option value="'.$opt_val.'">'.$opt_val.'</option>';
			}
		}
		return $opt_str;
	}
	
	public function get_product_category_options($default="",$only_default=false){
		//echo $default;
		$builder = $this->db->table('product_category AS mt');
		$query = $builder->join('product_category_ln AS ln',"mt.product_category_id=ln.product_category_id AND ln.language_code='".$this->project->default_lang_code."'","inner")
				->select('mt.product_category_id,ln.title')
				->orderBy('ln.title','ASC')
				->where('mt.is_deleted','No')
				->where('mt.status','Active')
				->get();

		$opt_arr = $query->getResultArray();
		$opt_str = $only_default ? '' : '<option value="">-- Select Category --</option>';
	
		if ($opt_arr) {
			foreach($opt_arr as $opt_val){
				if($opt_val['product_category_id']==$default){
					$opt_str .= '<option value="'.$opt_val['product_category_id'].'" selected="selected">'.$opt_val['title'].'</option>';
				}else if( ! $only_default){
					$opt_str .= '<option value="'.$opt_val['product_category_id'].'">'.$opt_val['title'].'</option>';
				}
			}
		}
		return $opt_str;
	}
	
	public function get_category_product_options($cat_id="",$default="",$only_default=false){
		//echo $default;
		$opt_arr = [];
		if($cat_id){
			$builder = $this->db->table('products AS mt');
			$query = $builder->join('products_ln AS ln',"mt.product_id=ln.product_id AND ln.language_code='".$this->project->default_lang_code."'","inner")
					->select('mt.product_id,ln.title')
					->orderBy('ln.title','ASC')
					->where('mt.is_deleted','No')
					->where('mt.status','Active')
					->where('mt.product_category_id',$cat_id)					
					->get();
			$opt_arr = $query->getResultArray();
		}
		$opt_str = $only_default ? '' : '<option value="">-- Select Product --</option>';
	
		if ($opt_arr) {
			foreach($opt_arr as $opt_val){
				if($opt_val['product_id']==$default){
					$opt_str .= '<option value="'.$opt_val['product_id'].'" selected="selected">'.$opt_val['title'].'</option>';
				}else if( ! $only_default){
					$opt_str .= '<option value="'.$opt_val['product_id'].'">'.$opt_val['title'].'</option>';
				}
			}
		}
		return $opt_str;
	}
	public function get_product_sub_category_options($default="",$only_default=false){
		$opt_str = $only_default ? '' : '<option value="">---Select Product Category---</option>';
		// Get list
		$builder = $this->db->table('product_sub_category');
		$query = $builder->select('product_sub_category_id, name')
				->orderBy('name','ASC')
				->where('is_deleted','No')
				->where('status','Active')
				->get();
		$opt_arr = $query->getResultArray();
		if($opt_arr){
			foreach($opt_arr as $opt_dtl){
				if($opt_dtl['product_sub_category_id']==$default){
					$opt_str .= '<option value="'.$opt_dtl['product_sub_category_id'].'" selected>'.$opt_dtl['name'].'</option>';
				}else if( ! $only_default){
					$opt_str .= '<option value="'.$opt_dtl['product_sub_category_id'].'">'.$opt_dtl['name'].'</option>';
				}
			}
		}
		return $opt_str;
	}
	public function get_news_list($language_code){
		$builder =  $this->db->table('news AS mt');
		$builder->join('news_ln AS ln',"mt.news_id=ln.news_id AND ln.language_code='".$language_code."'","inner");
		$query = $builder->select('mt.news_id,mt.news_url,mt.photo_image,mt.publish_date,mt.publisher_name,ln.title')
			->where('mt.status','Active')
			->where('mt.is_deleted','No')
			->where('mt.start_date_time<=',date('Y-m-d H:i:s'))
			->where('mt.end_date_time>=',date('Y-m-d H:i:s'))
			->get();
		return $query->getResultArray();
	} 

	
	public function get_state_options($default="",$only_default=false){
		$opt_str = $only_default ? '' : '<option value="">Select</option>';
		// Get state list
		$opt_arr = [];
			$builder = $this->db->table('franchise AS mt');
			$query = $builder->join('state AS s',"mt.state_id=s.state_id","inner")
					->select('s.name,mt.state_id')
					->where('mt.is_deleted','No')
					->where('mt.status','Active')
					->get();
			$opt_arr = $query->getResultArray();
		
		if($opt_arr){
			foreach($opt_arr as $opt_dtl){
				if($opt_dtl['state_id']==$default){
					$opt_str .= '<option value="'.$opt_dtl['state_id'].'" selected>'.$opt_dtl['name'].'</option>';
				}else if( ! $only_default){
					$opt_str .= '<option value="'.$opt_dtl['state_id'].'">'.$opt_dtl['name'].'</option>';
				}
			}
		}
		return $opt_str;
	}
	public function get_state_district_options($state_id, $default="", $only_default=false){
		$opt_str = $only_default ? '' : '<option value="">Select</option>';
		// Get district list
		$opt_arr = $this->get_state_district_list($state_id,$default,$only_default);
		if($opt_arr){
			foreach($opt_arr as $opt_dtl){
				if($opt_dtl['district_id']==$default){
					$opt_str .= '<option value="'.$opt_dtl['district_id'].'" selected>'.$opt_dtl['name'].'</option>';
				}else if( ! $only_default){
					$opt_str .= '<option value="'.$opt_dtl['district_id'].'">'.$opt_dtl['name'].'</option>';
				}
			}
		}
		return $opt_str;
	}
	public function get_state_district_list($state_id,$default="",$only_default=false){
		$rec_list = [];
		// Get State Detail
		// get state district list
		if($state_id){
			$builder = $this->db->table('franchise AS f');
			$builder->join('district AS d',"f.district_id=d.district_id","inner")
			->select('d.district_id, d.name')
				->where('f.state_id',$state_id);
			$query = $builder->get();
			$rec_list = $query->getResultArray();
		}
		return $rec_list;
	}



	public function get_construction_type_options($default="",$only_default=false)
	{
		$opt_str = $only_default ? '' : '<option value="" disabled selected>---Select---</option>';
		$opt_arr = ['Industrial Buildings','Warehousing','Cold Storages','Showrooms','Site Offices & Cabins','Farmhouses','Parking & Garages','Agricultural Structures & Godowns'];
		foreach($opt_arr as $opt_val){
			if($opt_val==$default){
				$opt_str .= '<option value="'.$opt_val.'" selected>'.$opt_val.'</option>';
			}else if( ! $only_default){
				$opt_str .= '<option value="'.$opt_val.'">'.$opt_val.'</option>';
			}
		}
		return $opt_str;
	}
}	