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

class NewpasswordSetup extends OWN_Controller{
    //put your code here
     function __construct() {
        parent::__construct();
        $this->admin_login_check();
        $this->load->model('adminmodel/adminmodel','adminmodel');
        $this->load->model('landcustomerregistrationmodel/landcustomerregistrationmodel', 'CREGModel');
    }

    public function index()
    {
    	$admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !="")) 
        {
        	$this->load->view('common/header');
            $this->load->view('common/menu');
            $this->load->view('admin/changePassword/newpasswordEntry');
            $this->load->view('common/footer');
        }
        else
        {
        	redirect(base_url('admin','refresh'));
        }
    }

    public function change_password()
    {
    	$admin_id=$this->session->userdata('admin_id');
        if ((!empty($admin_id)) || ($admin_id !="")) 
        {
        	$this->load->helper(array('form', 'url'));
	        $this->load->library('form_validation');   
	        $config = array(
               array(
                     'field'   => 'data[courrentPassword]', 
                     'label'   => 'Current Password', 
                     'rules'   => 'required|xss_clean'
                  ),
                  array(
                     'field'   => 'data[newPassword]', 
                     'label'   => 'New Password', 
                     'rules'   => 'required'
                  ),
                  array(
                     'field'   => 'data[confirmPassword]', 
                     'label'   => 'Confirm Password', 
                     'rules'   => 'required'
                  )
              );
	        $this->form_validation->set_rules($config); 
	        if ($this->form_validation->run() == FALSE)
	        {
	            $this->load->view('common/header');
	            $this->load->view('common/menu');
	            $this->load->view('admin/changePassword/newpasswordEntry');
	            $this->load->view('common/footer');
	        }
	        else
	        {
	            $data= $this->input->post('data', TRUE);
	            if(!empty($data))
	            {
	            	 $data['admin_id']=$this->session->userdata('admin_id');
	            	 $data['userName']=$this->session->userdata('userName');
	            	 $data['admin_name']=$this->session->userdata('admin_name');
	            	 $data['admin_email']=$this->session->userdata('admin_email');
	            	 $data['admin_access']=$this->session->userdata('admin_access');
	            	 $data['employeeId']=$this->session->userdata('employeeId');
	            	 $data['admin_level']=$this->session->userdata('admin_level');
	            	 $checkpass=$this->adminmodel->checkpassword($data);
	            	 if(!empty($checkpass))
                	{
	                    if($data['newPassword']==$data['confirmPassword'])
	                    {
		                      $data['password']=$data['confirmPassword'];
		                      $execution=$this->adminmodel->updatePassword($data);
	                    }
	                    if(!empty($execution))
	                    {
	                      	$message['message']='Password Changed';
	                        $this->session->set_userdata($message);
	                        redirect(admin_Url()."/newpasswordsetup",'refresh');
	                    }
	                    else
	                    {
	                       // $encrypted=sha1(md5($data['newPassword']));
	                        
	                       // $errormessage['errormessage']='Password Not Changed '.$encrypted.'';
	                        $errormessage['errormessage']='Password Not Changed';
	                        $this->session->set_userdata($errormessage);
	                        redirect(admin_Url()."/newpasswordsetup",'refresh');
	                    }
                	}
                	else
                	{
	                    $errormessage['errormessage']='Your Courrent Password is wrong';
	                    $this->session->set_userdata($errormessage);
	                    redirect(admin_Url()."/newpasswordsetup",'refresh');
                	}
	            }
	            else
	            {
	            	$errormessage['errormessage']='Password Information Missing';
	            	$this->session->set_userdata($errormessage);
	            	redirect(admin_Url()."/newpasswordsetup",'refresh');
	            }
	        }
        }
        else
        {
        	redirect(base_url('admin','refresh'));
        }

    }


    public function changeCustomerPassword_form($id)
    {
        $admin_id=$this->session->userdata("admin_id");
        if(!empty($admin_id) || $admin_id!="")
        {
            //$data["customerInfo"]=$this->CREGModel->getcustomerById($id);
            //print_r($data);die();
            $data['id']=$id;
            if(!empty($data["id"]))
            {
                $this->load->view('common/header');
                $this->load->view('common/menu');
                $this->load->view('admin/changePassword/newCustomerPasswordEntry',$data);
                $this->load->view('common/footer');
            }
            else 
            {
                $errormessage['errormessage']='Invalid Customer Id';
                $this->session->set_userdata($errormessage);
                redirect(admin_url().'/landcustomerRegistration/ViewLandCustomer','refresh');
            }
        }
        else
        {
            //redirect(admin_Url()."/logout",'refresh');
            redirect(base_url('admin','refresh'));
        }
    }


    public function changeCustomer_password(){
      $admin_id=$this->session->userdata("admin_id");
      if(!empty($admin_id) || $admin_id!="")
      {
          $this->load->helper(array('url','form'));
          $this->load->library('form_validation');
          $this->form_validation->set_rules('data[user_name]','New UserName','trim|xss_clean');
          $this->form_validation->set_rules('data[conuser_name]','Varified UserName','trim|xss_clean');
          $this->form_validation->set_rules('data[newPassword]','New Password','trim|xss_clean');
          $this->form_validation->set_rules('data[confirmPassword]','Varified Password','trim|xss_clean');
          if($this->form_validation->run()==FALSE)
          {
            echo '{"status": "1", "user_name": "' . form_error('data[user_name]') . '", "conuser_name": "' . form_error('data[conuser_name]') . '", "newPassword" : "' . form_error('data[newPassword]') . '", "confirmPassword" : "' . form_error('data[confirmPassword]') . '" }';
          }
          else
          {
              $data=$this->input->post('data',TRUE);
              //print_r($data);
              if(!empty($data))
              {
                if(!empty($data['conuser_name'])){
                  $datax=array(
                      'id'=>$this->input->post('id',TRUE),
                      'user_name'=>$data['conuser_name'],
                      //'user_password'=>$data['confirmPassword']
                    );

                  $execution=$this->CREGModel->updateCustomerById($datax);}
                  if(!empty($data['confirmPassword'])){
                  $datax=array(
                      'id'=>$this->input->post('id',TRUE),
                      //'user_name'=>$data['conuser_name'],
                      'user_password'=>$data['confirmPassword']
                    );

                  $execution1=$this->CREGModel->updateCustomerById($datax);}
                  if(!empty($execution)||!empty($execution1))
                  {
                           $message['message']='Username or Password Changed';
                           $this->session->set_userdata($message);
                          // redirect(admin_Url()."/newpasswordsetup",'refresh');
                    echo "2";
                  }
                  else
                  {
                          $errormessage['errormessage']='Username or Password Not Changed';
                          $this->session->set_userdata($errormessage);
                          echo "3";
                          //redirect(admin_url().'/landcustomerRegistration/ViewLandCustomer','refresh');
                  }
              }
          }
      }
      else
      {
          redirect(base_url('admin','refresh'));
      }
      //$data=$this->input->post('data',TRUE);
        //echo json_encode($data);

    }
}