<?php

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

class Forgetpassword extends OWN_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     * 	- or -  
     * 		http://example.com/index.php/welcome/index
     * 	- or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    function __construct() {
        parent::__construct();
        //$this->admin_login_check();
        $this->load->model('userregistrationmodel/userregistrationmodel', 'UREGModel');
    }

    public function getUsername()
    {
        $this->load->helper(array('url','form'));
        $this->load->library('form_validation');
        $this->form_validation->set_rules('admin_level','User Type','trim|xss_clean');
        $this->form_validation->set_rules('employeeId','Employee Id','trim|xss_clean');
        if($this->form_validation->run()==FALSE)
          {
            //echo "2";
            echo '{"status": "1", "admin_level": "' . form_error('admin_level') . '", "employeeId": "' . form_error('employeeId') . '"}';
          }
          else
          {
                $admin_level=$this->input->post('admin_level',true);
                $employeeId=$this->input->post('employeeId',true);
                $data= array('admin_level' => $admin_level,
                                'employeeId'=>$employeeId 
                    );
                if(!empty($data)){
                    $userName=$this->UREGModel->getUsernameById($data);
                    echo json_encode($userName);
                }
            }
        
    }

    public function change(){
        $this->load->helper(array('url','form'));
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username','User Name','trim|xss_clean');
        $this->form_validation->set_rules('varifypassword','Varify Password','trim|xss_clean');
        if($this->form_validation->run()==FALSE)
          {
            //echo "3";
            echo '{"status": "1", "username": "' . form_error('username') . '", "varifypassword": "' . form_error('varifypassword') . '"}';
          }
          else
          {
                $username=$this->input->post('username',true);
                $varifypassword=$this->input->post('varifypassword',true);
                $data= array('userName' => $username,
                                'password'=>sha1(md5($varifypassword)) 
                    );
                if(!empty($data)){
                    $execution=$this->UREGModel->changePassword($data);
                    //echo $execution;
                       if(!empty($execution)){
                           $message['message']="Password changed";
                           $this->session->set_userdata($message);
                          //echo "2";
                      }
                }
            }
    }
}