<?php 

defined('BASEPATH') or exit ('Direct script access not allowed');

// require_once APPPATH . "core/My_Admin.php";

class Changepassword extends CI_Controller{

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

    public function index(){

        if($_GET['i']){

            $check_token = $this->cm->search_single_where([

                'token' => $_GET['i']
                
            ], 'user_forgot_password');
            if(empty($check_token)){

                redirect(site_url() . '/invalid-request');
                
            }
            
            if($check_token['valid_to'] < date('Y-m-d H:i:s')){
                
                redirect(site_url() . '/invalid-request');

            }

            if($check_token['used'] == '1' ){    
                
                redirect(site_url() . '/invalid-request');

            }

            if($this->input->post()){

                $this->cm->update_where([
                
                    'password' => md5($_POST['password'])
                    
                ], ['id' => $check_token['user_id']], 'users');
                

                $this->cm->update_where([
                
                    'used' => '1'
                    
                ], ['id' => $check_token['id']], 'user_forgot_password');


                redirect(site_url() . '/change-password-success');


            }


            $this->load->view('changepassword');
            
        }else{
            
            redirect(site_url() . '/invalid-request');
            
        }
        
        
        
    }
    
    public function invalid_request(){
        
        $this->load->view('invalidrequest');

    }
    
    public function change_password_success(){

        $this->load->view('changepasswordsuccess');

    }

}