<?php

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

class Profile extends Admin_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
		$this->load->model('Agent_model');
    }


    public function index()
    {
        $this->load->view('theme/profile/my_profile');
    }
	
	public function change_password()
    {
        $this->load->view('theme/profile/change_password');
    }
	
	   public function update_password()
      {
	   if($this->input->post('new-password',false)==$this->input->post('repeat-password',false)){
        $data=array();
		$data['password']=md5($this->input->post('new-password',false));
		$this->form_validation->set_rules('new-password', 'Password', 'trim|required|min_length[5]|max_length[12]');
		if ($this->form_validation->run() == TRUE)
		{
			$this->db->where('id',$this->session->userdata("id"));
			$this->db->update("users",$data);
			$this->session->set_flashdata('success', 'Password Changed Sucessfully');
			redirect('Profile/change_password', 'refresh');
		}
		else
		{
			$this->load->view('theme/profile/change_password');
		}
		
		}else{
		   $this->session->set_flashdata('error', 'New Password And Repeat Password Is Mismatch !');
		   redirect('Profile/change_password', 'refresh');
		}
      }
	
	  public function update(){
	    $data=array();
		$data['first_name']=$this->input->post('first_name',false);
		$data['last_name']=$this->input->post('last_name',false);
		$data['email']=$this->input->post('email',false);
		
		  // Set up the form
		  $rules = $this->Agent_model->rules;
		  unset($rules['username']);
		  unset($rules['password']);	  
		  $this->form_validation->set_rules($rules);
		  
		 if ($this->form_validation->run() == TRUE) {
		     $this->db->where('id',$this->session->userdata("id"));
		     if($this->db->update("users",$data)){
			  //Update Session
			  $this->session->set_userdata('first_name', $data['first_name']);
			  $this->session->set_userdata('last_name', $data['last_name']);
			  $this->session->set_userdata('email', $data['email']); 
			 
			  $this->session->set_flashdata('success', 'Profile Updated Sucessfully');
			  redirect('Profile', 'refresh');
		      }else{
			  $this->session->set_flashdata('error', 'Update Failed !');
			  redirect('Profile', 'refresh');
		      }
		  }else{
			$this->load->view('theme/profile/my_profile');
		  }  
	
	  }
	  
	      public function _unique_email ($str)
    {
        // Do NOT validate if email already exists
        // UNLESS it's the email for the current user

		$last = $this->uri->total_segments();
        $id = $this->uri->segment($last);
        $this->db->where('email', $this->input->post('email'));
        !$id || $this->db->where('id !=', $id);
        $user = $this->db->get('users')->result();

        if (count($user)) {
            $this->form_validation->set_message('_unique_email', '%s Should be unique');
            return FALSE;
        }

        return TRUE;
    }


      
}