<?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 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');
        }
	
	}
}