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

class Resetpassword extends CI_Controller {
    function __construct()
 	{
	    parent::__construct();
		$this->load->helper('url');
		$this->load->helper('form');
		$this->load->library('session');
		$this->load->model('common');
		$this->load->library(array('form_validation','template'));
	}
	public function index()   
	{
		$this->template->template_render('resetpassword'); 
	}
	public function updatepassword()
	{
		$password="123456";
		$adminId = "lhZEYNiPlme1EMwKnYU3EkXfiW62";
		$firebase = $this->firebase->init();
		$atuh = $firebase->createAuth();
		$updatedUser = $atuh->changeUserPassword($adminId, $password);

			if($updatedUser) {
				echo "Password updated successfully!. <a href='".base_url()."dashboard'>Dashboard</a>";
		} else {
				echo "Error in resetting password. <a href='".base_url()."resetpassword'>Reset Password</a>";
		}
	}
	public function resetpasswordsave()
	{
		$adminId = $this->session->userdata['session_data']['userId'];
		
		$password = $this->input->post('password');
		$cnfpassword = $this->input->post('cnfpassword');
		if($password != $cnfpassword) {
			$this->session->set_flashdata('warningmessage', 'Error: Both password fields should match.');
			redirect(base_url().'resetpassword');
		}

		$firebase = $this->firebase->init();
		$atuh = $firebase->createAuth();

		$updatedUser = $atuh->changeUserPassword($adminId, $password);

		$data = [
            'password' => trim($this->input->post('password')),
        ];

        $resp = $this->common->updatedatamodel('Users', $adminId, $data);

        if($resp) {
        	$this->session->set_flashdata('successmessage', 'Password updated successfully!.');
        	redirect(base_url().'dashboard');
        } else {
        	$this->session->set_flashdata('warningmessage', 'Error in resetting password.');
			redirect(base_url().'resetpassword');
        }
	
	}
}