<?php

defined('BASEPATH') OR exit('No direct script access allowed');
  
class Agent extends Admin_Controller {
  
	 public function __construct(){
	      parent::__construct();
		   if(user_type()!="admin"){
		     redirect('Dashboard', 'refresh');
		  }
	      $this->load->library('form_validation');
	      $this->load->model('Agent_model');
	 }
  
  
	  public function index()
	  {
		  $this->load->view('theme/agent/add_agent');
	  }
	  
	  public function manage()
	  { 
		  $data=array();
		  $data['table_data']=$this->Agent_model->get_all();
		  $this->load->view('theme/agent/manage_agent',$data);
	  }
	  
	  public function save($id="")
	  {
  
		$data=array();
		$data['first_name']=$this->input->post('first_name',false);
		$data['last_name']=$this->input->post('last_name',false);
		$data['username']=$this->input->post('username',false);
		$data['email']=$this->input->post('email',false);
		$data['password']=md5($this->input->post('password',false));
		$data['user_type']="agent";
		$data['status']=$this->input->post('status',false);
		$data['creation_date']=date("Y-m-d");
		
		
		  // Set up the form
		  $rules = $this->Agent_model->rules;
		  //For Edit Unset Some Data
		  if($id!=""){
		  if($this->input->post('password',false)==""){
		  unset($data['password']);
		  unset($rules['password']);
		  unset($data['username']);
		  unset($rules['username']);
		  }
		  
		  }
		  
		  $this->form_validation->set_rules($rules);
  
		  //Process The Form
		  //Insert
		  if($id==""){
		  if ($this->form_validation->run() == TRUE) {
			  if($this->db->insert("users",$data)){
			  $this->session->set_flashdata('success', 'Data Inserted Sucessfully');
			  redirect('Agent', 'refresh');
		  }else{
			  $this->session->set_flashdata('error', 'Saved Failed !');
			  redirect('Agent', 'refresh');
		  }
		  }else{
			  $this->load->view('theme/agent/add_agent');
		  }
		
		}else if($id!=""){
		  //Update
		  if ($this->form_validation->run() == TRUE) {
		     $this->db->where('id',$id);
		     if($this->db->update("users",$data)){
			  $this->session->set_flashdata('success', 'Data Updated Sucessfully');
			  redirect('Agent/edit/'.$id, 'refresh');
		      }else{
			  $this->session->set_flashdata('error', 'Update Failed !');
			  redirect('Agent/edit/'.$id, 'refresh');
		      }
		  }else{
			$data=array();
			$data['edit_data']=$this->Agent_model->get_by($id);
			$this->load->view('theme/agent/edit_agent',$data);
		  }
		
		}		
	  }
	  
	  public function edit($id=NULL){
	  if($id==NULL){
	    show_404();
	  }
	  $data=array();
	  $data['edit_data']=$this->Agent_model->get_by($id);
	  $this->load->view('theme/agent/edit_agent',$data);
	  
	  }
	  
	  public function remove($id=NULL)
	  {
	  $this->db->delete('users', array('id' => $id));
	  $this->session->set_flashdata('success', 'Remove Sucessfully');
	  redirect('Agent/manage', 'refresh');	
	  }
	  
	    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;
    }
  }