<?php
	defined('BASEPATH') OR exit('No direct script access allowed');

	class Admin_model extends My_Model{
		protected $_table_name = 'admin_users';
		protected $_order_by = 'id';
		public $rules = array(
			'email' => array(
					'field' => 'email', 
					'label' => 'Email', 
					'rules' => 'trim|required|valid_email|xss_clean'
			),

			'password' => array(
				'field' => 'password', 
				'label' => 'Password', 
				'rules' => 'trim|required'
			)
		);


		public $admin_user_rules = array(
			'email' => array(
				'field' => 'email', 
				'label' => 'Email', 
				'rules' => 'trim|required|valid_email|xss_clean'
			),

			'password' => array(
				'field' => 'password', 
				'label' => 'Password', 
				'rules' => 'trim'
			)
		);


		public function __construct(){
			parent::__construct();
		}

		public function get_new(){
			$admin_user = new stdClass();
			$admin_user->name = '';
			$admin_user->email = '';
			$admin_user->password = '';
			$admin_user->is_active = 1;
			$admin_user->role_id = '';

			return $admin_user;
		}

		public function login(){
			$user = $this->get_by(array(
				'email' => $this->input->post('email'),
				'password' => $this->hash($this->input->post('password'))
			), TRUE);

			//print_r($this->input->post('email'));die;

			if(!empty($user)){
				//Log in user
				$data = array(
					'name' => $user->name,
					'email' => $user->email,
					'id' => $user->id,
					'role_id' => $user->role_id,
					'loggedin' => TRUE
				);

				$this->session->set_userdata($data);

				return true;
			}

			return false;

		}

		public function logout(){
			$this->session->sess_destroy();
		}

		public function loggedin(){
			return (bool) $this->session->userdata('loggedin');
		}

		public function hash($string){
			return hash('sha512', $string . config_item('encryption_key'));
		}



		public function get_admin_sidebar(){

			$role_id = isset($_SESSION["role_id"]) ? $_SESSION["role_id"] : 0;

			$this->load->model('hc_modules_model', 'modules');
			$this->load->model('hc_role_rights_model', 'role_rights');

			$this->db->where(array("is_active" => 1));
			//$this->db->group_by("module_group_name");
			$this->db->order_by("sorting_order", "asc");
			$this->db->select("id, module_group_name, module_name, module_page_name, icon, module_unique_name");
			$b = $this->modules->get();

			$a = [];
			$seenModuleGroups = [];

			foreach ($b as $result) {
			    $moduleGroupName = $result->module_group_name;
			    
			    // Check if we've already seen this module_group_name
			    if (!in_array($moduleGroupName, $seenModuleGroups)) {
			        $a[] = $result;
			        $seenModuleGroups[] = $moduleGroupName;
			    }
			}

			foreach ($a as $key => $value) {
				$this->db->where(array("is_active" => 1, "module_group_name" => $value->module_group_name));
				$this->db->select("id, module_group_name, module_name, module_page_name, icon, module_unique_name");
				$m = $this->modules->get();
				if(COUNT($m) > 1){
					$a[$key]->sub_modules = $m;
				}else{
					$a[$key]->sub_modules = array();
				}
			}

			foreach ($a as $key => $value) {
				if(empty($value->sub_modules)){
					$role_right_row = $this->role_rights->get_by(array("module_id" => $value->id, "role_id" => $role_id), TRUE);
					if(isset($role_right_row->view_right) && $role_right_row->view_right == 1){
						$a[$key]->admin_rights = $role_right_row; 
					}else{
						unset($a[$key]);
					}
				}else{
					foreach ($value->sub_modules as $key1 => $value1) {
						$role_right_row = $this->role_rights->get_by(array("module_id" => $value1->id, "role_id" => $role_id), TRUE);
						if(isset($role_right_row->view_right) && $role_right_row->view_right == 1){
							$a[$key]->sub_modules[$key1]->admin_rights = $role_right_row;
						}else{
							unset($a[$key]->sub_modules[$key1]);
						}
					}

					if(empty($a[$key]->sub_modules)){
						unset($a[$key]);
					}

				}
			}


			return $a;

		}

		public function get_admin_rights($data){
			$role_id = isset($_SESSION["role_id"]) ? $_SESSION["role_id"] : 0;

			$this->load->model('hc_modules_model', 'modules');

			$this->db->join("hc_role_rights", "hc_role_rights.module_id = hc_modules.id AND hc_role_rights.role_id = ".$role_id);
			$this->db->select("hc_role_rights.*");
			if($data["access_right"] == "view_right"){
				$this->db->where("view_right = 1");
			}

			if($data["access_right"] == "create_right"){
				$this->db->where("create_right = 1");
			}

			if($data["access_right"] == "edit_right"){
				$this->db->where("edit_right = 1");
			}

			if($data["access_right"] == "delete_right"){
				$this->db->where("delete_right = 1");
			}
			$rights_row = $this->modules->get_by(array("module_unique_name" => $data['page_name']), TRUE);

			return $rights_row;
		}


		public function get_admin_users_search_count($search_params, $like_params){
			$this->db->join("hc_role", "hc_role.id = admin_users.role_id");

			if(!empty($like_params)){
				$this->db->group_start();
				foreach ($like_params as $key => $value) {
					$this->db->or_like($key, $value);
				}
				$this->db->group_end();
			}

			if(!empty($search_params)){
				$this->db->where($search_params);
			}
			

			return $this->db->count_all_results($this->_table_name);
		}


		public function get_admin_user($where){
			$this->db->order_by("created_at", "desc");
			$this->db->join("hc_role", "hc_role.id = admin_users.role_id");
			$this->db->select("admin_users.*, hc_role.role_name");
			return $this->admin_user->get_by($where, TRUE);
		}


	}
?>