<?php
class User_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }

    /*
        Author      : Naresh Lakhara
        Date        : 25-05-2022
        Description : Ajax Through List Display
    */
    public function manage_user_ajax() {

        $in = $_POST ? $_POST : array();        
        
        $q = "SELECT user_id, name, email, mobile_number, status, is_verify FROM zo_tbl_user as zo_u  WHERE 1 = 1 ";

        if (@$in['search']['value']) {
           $q .= ' AND (zo_u.name LIKE "%' . trim(@$in['search']['value']) . '%" 
           OR zo_u.email LIKE "%' . trim(@$in['search']['value']) . '%" OR
            zo_u.mobile_number LIKE "%' . trim(@$in['search']['value']) . '%")';
        } 

        if(!empty(@$in['search_data']['user_id'])) {
            $q .= " AND user_id = '".$in['search_data']['user_id']."'";
        }

        $q .= " ORDER BY {$in['order'][0]['column']} {$in['order'][0]['dir']} ";

        $query = $this->db->query($q);
        $total = $query->num_rows();

        $qury  = $q . " LIMIT {$in['start']}, {$in['length']}";

        $exe_q = $this->db->query($qury);
        $data   = $exe_q->result_array();

        $final_array = array();

        foreach ($data as $key => $data_value) {
                
            $final_array[] = array(
                "user_id"       => @$data_value['user_id'],
                "name"          => @$data_value['name'],
                "email"         => @$data_value['email'],
                "mobile_number" => @$data_value['mobile_number'],
                "status"        => @$data_value['status'],
                "is_verify"     => @$data_value['is_verify']
            );
        }
        echo json_encode(array(
           "data"            => $final_array,
           "recordsTotal"    => $total ? $total : 0,
           "recordsFiltered" => $total ? $total : 0,
       ));
       exit;
    }

    /*
        Author      : Naresh Lakhara
        Date        : 09-06-2022
        Description : Validate Email
    */
    public function validate_email($email, $user_id ) {

        $q = "SELECT user_id FROM zo_tbl_user WHERE email LIKE '$email' ";

        if(!empty($user_id) && isset($user_id)) {
            $q .= " AND user_id != '$user_id'";
        }

        $query = $this->db->query($q);
        $total = $query->num_rows();

        if($total > 0) {
            return 1;
        } else {
            return 0;
        }
    }

     /*
        Author      : Naresh Lakhara
        Date        : 09-06-2022
        Description : Edit
    */
    public function edit($user_id ) {

        $q = "SELECT * FROM zo_tbl_user  WHERE user_id = '$user_id' ";
    
        $query = $this->db->query($q);
        $total = $query->num_rows();

        if($total > 0) {
            return $query->row_array();
        } else {
            return 0;
        }
    }
}