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

class Change_password extends CI_Controller {
	
	/*
		Author     : Naresh Lakhara
		Date       : 11-04-2022
		Description: Change Passowrd
    */	
	public function index()
	{

		$this->form_validation->set_rules('password', 'New Password', 'required');
		$this->form_validation->set_rules('confirmPassword', 'Confirm Password', 'required');
		$user_id = $this->uri->segment(2);
				
		if ($this->form_validation->run() == FALSE)
		{	
			$data['user_id'] = $user_id;
		    $this->load->view('change_password',$data);

		} else {

			$password        = $this->input->post('password');
			$confirmPassword =  $this->input->post('confirmPassword');

			if($password == $confirmPassword) {

				$field      = array('user_id','expiry_date_time');
				$where_con  = array('user_id' => base64_decode($user_id));
				$check_expiry_time = $this->Common_model->single_record_fetch($field, 'zo_tbl_user', $where_con);

				if(empty($check_expiry_time)) {

					$this->session->set_flashdata('error', 'Your link is expired.');
					redirect("change_password/$user_id");

				} elseif(date("Y-m-d H:i:s") > $check_expiry_time['expiry_date_time']) {

					$this->session->set_flashdata('error', 'Your link is expired.');
					redirect("change_password/$user_id");

				} else {

					$up_data     = array('expiry_date_time' => "", 'password' => md5($password));
					$where_con   = array('user_id' => $check_expiry_time['user_id']);                                     
					$this->Common_model->update_by_where_array('zo_tbl_user', $where_con, $up_data );

					$this->session->set_flashdata('message', 'Password reset successfully.');
					redirect("change_password/$user_id");
				}

			} else {

				$this->session->set_flashdata('error', 'Password does not match.');
				redirect("change_password/$user_id");
			}
		}
	}

	/*
		Author     : Naresh Lakhara
		Date       : 11-04-2022
		Description: Email Confirmation 
    */
	public function email_confirmation() {

		$user_id = base64_decode($this->uri->segment(2)) ;

		$field              = array('user_id','is_verify','status');
		$where_con          = array('user_id' => $user_id);
		$verification_check = $this->Common_model->single_record_fetch($field, 'zo_tbl_user', $where_con);
		
		if($verification_check['is_verify'] == '0') {

			$up_data     = array('is_verify' => '1', 'status' => '1');
			$where_con   = array('user_id' => $user_id);
			$result      = $this->Common_model->update_by_where_array('zo_tbl_user', $where_con, $up_data );

			if(!empty($result) && isset($result)) {
				$data['message'] = 'Your account verified successfully.';
				$data['color']   = '#008000b8';	
			} else {
				$data['message'] = 'Your account is not verified, due to some technical issue, please try again later.';
				$data['color']   = 'red';	
			}

		} else {

			$data['message'] = 'Your account is already verified.';
			$data['color']   = '#008000b8';
		}

		$this->load->view('email_verify', $data);
	}
}
