<?php

namespace App\Models;

use CodeIgniter\Model;

class UserModel extends Model
{
    protected $db;        
    protected $users                        = 'users';
    protected $role_permission              = 'role_permission';
    protected $order                        = 'order';
    

    function count_all_users_by_role($role)
    {
        return $this->db->table($this->users)
        ->where('user_role', $role)
        ->get()
        ->getNumRows();
    }

    function get_all_stop_by_pin_code($pin_code)
    {
        return $this->db->table($this->users)
        ->where('user_role', 'Stock Point')
        ->like('user_pin_code', $pin_code)
        ->select('user_id, user_stock_point_name')
        ->get()
        ->getResult();
    }

    function count_all_delivery_boy_by_shop_id($id)
    {
        return $this->db->table($this->users)
        ->where('user_role', 'Delivery Boy')
        ->where('user_added_by', $id)
        ->get()
        ->getNumRows();
    }

    //Get All Role Permission
    public function get_role_permission()
    {
        return $this->db->table($this->role_permission)->get()->getResult();
    }

    public function get_role_permission_for_role_table()
    {
        return $this->db->table($this->role_permission)
        ->where('is_designation', 0)
        ->get()
        ->getResult();
    }

    //Get All Role Permission
    public function get_all_designation_role_permission()
    {
        return $this->db->table($this->role_permission)
        ->where('is_designation', 1)
        ->get()
        ->getResult();
    }


    public function get_info()
    {
        $query = $this->db->query('SELECT * FROM role_permission');
        $option =[];
        foreach ($query->getFieldNames() as $field) 
        {
            $option[] = $field;
        }
        return $option;
    }
    //Add new Role
    public function  add_new_role_permission($data)
    {
        return $this->db->table($this->role_permission)->insert($data);
    }

    //Get All Role Permission Detaild By ID
    public function get_role_permission_detaild_by_id($id)
    {
        return $this->db->table($this->role_permission)
       ->where(['id'=> $id])
       ->get()
       ->getRow();
    }

    public function update_role_permission($data, $id)
    {
        return $this->db->table($this->role_permission)
       ->where(['id'=> $id])
       ->update($data);
    }

    public function delete_role_permission($id)
    {
        return $this->db->table($this->role_permission)
       ->where(['id'=> $id])
       ->delete();
    }
    public function check_role_name_status($name)
    {
        return $this->db->table($this->role_permission)
       ->where('role_name', $name)
       ->get()
       ->getNumRows();
    }

    function get_all_user()
    {
        return $this->db->table($this->users)
        ->where('user_role !=', 'Admin')
        ->get()
        ->getResult();
    }

    //Add New User
    public function add_new_user($data)
    {
        return $this->db->table($this->users)->insert($data);
    }

    public function get_user_by_id($id)
    {
        return $this->db->table($this->users)
       ->where('user_id', $id)
       ->select('*')
       ->get()
       ->getRow();
    }

    public function get_all_teacher()
    {
        return $this->db->table($this->users)
        ->select('user_id, user_name')
        ->where('user_is_teacher', 1)
        ->get()
        ->getResult();
    }


    public function update_user($data, $id)
    {
        return $this->db->table($this->users)
       ->where(['user_id'=> $id])
       ->update($data);
    }

    public function delete_user($id)
    {
        return $this->db->table($this->users)
       ->where(['user_id'=> $id])
       ->delete();
    }

    public function get_vol_last_code()
    {
       $app_setting =  $this->db->table('application_setting')
        ->where('id', 1)
        ->get()
        ->getRow();

        $app_name = explode(" ", $app_setting->application_name);
        $a_name = mb_substr($app_name[0], 0, 1).mb_substr($app_name[1], 0, 1);


        $result = $this->db->table($this->users)
        ->selectMax('user_stock_point_code')
        ->where('user_role', "Volunteer")
        ->get()
        ->getResult();
        $order_no = $result[0]->user_stock_point_code;
        if ($order_no !='') {
            $cd = ltrim($order_no, $a_name.'VOL');
            $rr = $cd + 1;
            $order_no = $a_name."VOL".$rr;
        }else{
            $order_no = $a_name.'VOL101';
        }
        return $order_no;
    }

    function get_user_by_email_id_for_password($email)
    {
        return  $this->db->table($this->users)
        ->select('user_email, user_id, user_name')
        ->where('user_role !=', 'Admin')
        ->where('user_email', $email)
        ->get()
        ->getRow();
    }


    function update_user_password($data, $id)
    {
        return $this->db->table($this->users)
       ->where('user_id', $id)
       ->update($data);
    }

   
    function submit_join_us_form($data)
    {
        return $this->db->table('users')
        ->insert($data);
    }

    function all_volunteer_report()
    {
        return $this->db->table('users')
        ->where('user_role', 'Volunteer')
        ->get()
        ->getResult();
    }

    function get_user_by_details($table, $data)
    {
        return $this->db->table('users')
        ->where($table, $data)
        ->get()
        ->getRow();
    }

    function all_subscribers_report()
    {
        return $this->db->table('users')
        ->where('user_role', 'Subscriber')
        ->get()
        ->getResult();
    }

    function get_user_email_by_user_id($id)
    {
        return $this->db->table('users')
        ->select('user_email')
        ->where('user_id', $id)
        ->get()
        ->getRow();
    }

    public function view_all_member()
    {
        return $this->db->table('users')
        ->where('user_role', 'Member')
        ->get()
        ->getResult();
    }

    public function view_all_member_for_own($id)
    {
        return $this->db->table('users')
        ->where('user_added_by', $id)
        ->where('user_role', 'Member')
        ->get()
        ->getResult();
    }

    public function add_member($data)
    {
        return $this->db->table('users')
        ->insert($data);
    }

    public function delete_member($code)
    {
        return $this->db->table('users')
        ->where('user_rand_code', $code)
        ->delete();
    }

    public function get_user_by_code($code)
    {
        return $this->db->table('users')
        ->where('user_rand_code', $code)
        ->get()
        ->getRow();
    }

    public function update_member($data, $code)
    {
        return $this->db->table('users')
        ->where('user_rand_code', $code)
        ->update($data);
    }

    public function get_all_employee()
    {
        return $this->db->table('users')
        ->where('is_employee', 1)
        ->get()
        ->getResult();
    }

    public function get_all_employee_by_added_by($id)
    {
        return $this->db->table('users')
        ->where('is_employee', 1)
        ->where('user_added_by', $id)
        ->get()
        ->getResult();
    }

    function get_all_employee_role_for_admin()
    {
        return $this->db->table('role_permission')
        ->where('role_name !=', 'Admin')
        ->where('is_designation', 0)
        ->select('id, role_name')
        ->get()
        ->getResult();
    }

    public function check_email_avaliblity($email)
    {
        return $this->db->table('users')
       ->where('user_email', $email)
       ->get()
       ->getNumRows();
    }
    public function check_mobile_avaliblity($mobile)
    {
        return $this->db->table('users')
       ->where('user_mobile', $mobile)
       ->get()
       ->getNumRows();
    }

    public function check_mobile_avaliblity_for_student_add($mobile)
    {
        return $this->db->table('users')
       ->where('user_mobile', $mobile)
       ->get()
       ->getRow();
    }


   
}
