<?php namespace App\Controllers\Admin;

class Users extends AdminController
{
	private $mObj;
	
	public function __construct(){		
        $this->mObj = model('App\Models\Admin\UsersModel');
		$this->menu_id = 4;
    }
	
	public function index(){
		$this->allowed('is_list');
		$data['footer_includes'] = ['users_list_js'];		
		echo $this->view_inner_template('users_list',$data);
	}
	
	public function ajax_list(){
		$this->allowed('is_list');
		$result = $this->mObj->get_datatable();
		$output = array(
			"meta" => $result['meta'],
            "data" => $result['data']
        );
        
        // Output to JSON format
        echo json_encode($output);
	}
	
	public function add(){
		$this->allowed('is_add');
		$data['form_heading'] = "User Add";
		
		if($this->request->getMethod() === 'post'){
			$rules = $this->get_user_validation_rules();
			if ($this->validate($rules)) { 
				$data_in = $this->get_column_values();
				$this->mObj->insert($data_in);
				$this->notify_success('Record Added Successfully');
				return redirect()->to(get_admin_path('users'));
			}
		}
		echo $this->view_inner_template('users_add_edit',$data);
	}
	
	public function edit($user_id){
		$this->allowed('is_edit');
		$data['form_heading'] = "User Edit";
		if($this->request->getMethod() === 'post'){
			$rules = $this->get_user_validation_rules($user_id);
			if ($this->validate($rules)) { 
				$data_in = $this->get_column_values();
				$this->mObj->update($data_in,$user_id);
				$this->notify_success('Record Updated Successfully');
				return redirect()->to(get_admin_path('users'));
			}
		}else{
			$data['update_data'] = $this->mObj->get($user_id);
		}
		echo $this->view_inner_template('users_add_edit',$data);
	}
	
	public function delete($user_id){
		$this->allowed('is_edit');
		$this->mObj->delete($user_id);
		$this->notify_success('Record Deleted Successfully');
		return redirect()->to(get_admin_path('users'));
	}
	
	private function get_column_values(){
		$data['name'] = $this->request->getPost('name');
		$data['user_type'] = $this->request->getPost('user_type');
		$data['mobile_no'] = $this->request->getPost('mobile_no');
		$data['email_id'] = $this->request->getPost('email_id');
		$data['username'] = $this->request->getPost('username');
		$data['password'] = md5($this->request->getPost('password'));
		$data['c_password'] = $this->request->getPost('password');
		return $data;
	}
	
	private function get_user_validation_rules($user_id=""){
		$rules['name'] = ['label'=>'Name', 'rules' => 'trim|required'];
		$rules['user_type'] = ['label'=>'Type', 'rules' => 'trim|required'];
		$rules['mobile_no'] = ['label'=>'Mobile No.', 'rules' => 'trim|required|numeric|exact_length[10]'];
		$rules['email_id'] = ['label'=>'Email ID', 'rules' => 'trim|required|valid_email'];
		$rules['username'] = ['label'=>'Username', 'rules' => 'trim|required|alpha_numeric|min_length[4]|pr_callback['.$this->mObj->is_unique_username($this->request->getPost('username'),$user_id).']'];
		$rules['password'] = ['label'=>'Password', 'rules' => 'trim|required|alpha_numeric|min_length[6]|max_length[12]'];
		return $rules;
	}
	
	public function profile(){
		$data['form_heading'] = "User Profile";
		if($this->request->getMethod() === 'post'){
			$rules['password'] = ['label'=>'Password', 'rules' => 'trim|required|alpha_numeric|min_length[6]|max_length[12]'];
			if ($this->validate($rules)) { 
				$data_in['password'] = md5($this->request->getPost('password'));
				$data_in['c_password'] = $this->request->getPost('password');
				$this->mObj->update($data_in,$this->session->get($this->project->session_admin_user_id));
				$this->notify_success('Record Updated Successfully');
				return redirect()->to(get_admin_path('users/profile'));
			}
		}else{
			$data['update_data'] = $this->mObj->get($this->session->get($this->project->session_admin_user_id));
		}
		echo $this->view_inner_template('users_profile',$data);
	}
}