<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Login_Model extends CI_Model {

    var $tablename = 'tbl_admin';

    function __construct() {
        // Call the Model constructor
        parent::__construct();
        $this->load->database();
    }

    /*     * ****** Purpose : Fuction Use For Authenticate user for access ************** */

    function check_login() {
        $username = $this->input->post('username');
        $password = $this->input->post('password');
        if ($username != '' && $password != '') {
            $this->db->where('username', $username);
            $this->db->where('password', md5($password));
            $this->db->where('status', 'active');
            $query = $this->db->get($this->tablename);

            if ($query->num_rows() > 0) {
                return $user_data = $query->row();
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public function checkEmail($email) {
        $this->db->select('username,id,email');
        $this->db->where('email', $email);
        $query = $this->db->get($this->tablename);
        $row = $query->row();
        return $row;
    }

    function generate_session($user_data) {
        $this->session->set_userdata('admin_id', $user_data->id);
        $this->session->set_userdata('admin_location_id', $user_data->province_id);
        $this->session->set_userdata('admin_email', $user_data->email);
        $this->session->set_userdata('admin_user_name', $user_data->username);
        return true;
    }

    public function forgotPassword() {
        $strnew = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
		$shuff = str_shuffle($strnew);
		$verification_code=substr($shuff,0,8) ;
                
//        $strnew = 'abcXdefgRhijklmnoYpqrstGuvwxJyz012345A6789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
//        $shuff = str_shuffle($strnew);
//        $newPassword = substr($shuff, 0, 8);
//        $this->session->set_userdata('NEWPASSWORD', $newPassword);
//          $this->db->set('password', md5($newPassword));
                
        $this->session->set_userdata('verification_code', $verification_code);  
        $this->db->set('verification_code', $verification_code);
        $this->db->where('email', $this->input->post('email'));
        $this->db->update($this->tablename);
        return $this->db->insert_id();
    }
    
     public function ResetPassword($token) {
        if ($this->input->post('new_password'))
            $this->db->set('password', md5($this->input->post('new_password')));
        $this->db->where('verification_code', $token);
        $this->db->update($this->tablename);
        return $this->db->insert_id();
    }

}