<?php namespace App\Controllers\Admin;

class Clients extends AdminController
{
	private $mObj;
	
	public function __construct(){		
        $this->mObj = model('App\Models\Admin\ClientsModel');
		$this->menu_id = 20;
    }
	
	public function index(){
		$this->allowed('is_list');
		$data['footer_includes'] = ['clients_list_js'];		
		echo $this->view_inner_template('clients_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'] = "Clients Add";
		
		if($this->request->getMethod() === 'post'){
			$rules = $this->get_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('clients'));
			}
		}
		$data['action'] = "add";
		//$data['footer_includes'] = ['clients_add_edit_js'];	
		echo $this->view_inner_template('clients_add_edit',$data);
	}
	
	public function edit($client_id){
		$this->allowed('is_edit');
		$data['form_heading'] = "Clients Edit";
		if($this->request->getMethod() === 'post'){
			$rules = $this->get_validation_rules($client_id);
			if ($this->validate($rules)) { 
				$data_in = $this->get_column_values();
				$this->mObj->update($data_in,$client_id);
				$this->notify_success('Record Updated Successfully');
				return redirect()->to(get_admin_path('clients'));
			}
		}else{
			$data['update_data'] = $this->mObj->get($client_id);
		}
		$data['action'] = "edit";
		$data['client_id'] = $client_id;
		//$data['footer_includes'] = ['clients_add_edit_js'];	
		echo $this->view_inner_template('clients_add_edit',$data);
	}
	
	public function delete($client_id){
		$this->allowed('is_edit');
		$this->mObj->delete($client_id);
		$this->notify_success('Record Deleted Successfully');
		return redirect()->to(get_admin_path('clients'));
	}
	
	private function save_uploaded_file(&$data){
		$file = $this->request->getFile('logo');
		if($file->getName()){
			$file->move(ROOTPATH . 'public/uploads/',$file->getRandomName());
			$data['logo'] = $file->getName();
		}
	}
	
	private function get_column_values(){
		$data['title'] = $this->request->getPost('title');
		$data['property_type_id'] = $this->request->getPost('property_type_id');
		$data['city'] = $this->request->getPost('city');
		$data['status'] = $this->request->getPost('status');
		$this->save_uploaded_file($data);
		return $data;
	}
	
	private function get_validation_rules($blog_id=""){
		$rules['title'] = ['label'=>'Title', 'rules' => 'trim|required'];
		$rules['status'] = ['label'=>'Status', 'rules' => 'trim|required'];
		$rules['property_type_id'] = ['label'=>'Property Type', 'rules' => 'trim|required'];
		$rules['city'] = ['label'=>'City', 'rules' => 'trim|required'];
		$rules['status'] = ['label'=>'Status', 'rules' => 'trim|required'];
		return $rules;
	}
	
	
	
	
}