<?php namespace App\Controllers\Admin;

class Dealer extends AdminController
{
	private $mObj;
	
	public function __construct(){		
        $this->mObj = model('App\Models\Admin\DealerModel');
		$this->menu_id = 25;
    }
	
	public function index(){
		$this->allowed('is_list');
		$data['footer_includes'] = ['dealer_list_js'];		
		echo $this->view_inner_template('dealer_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'] = "Add Dealer";
		
		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('dealer'));
			}
		}
		$data['footer_includes'] = ['dealer_add_edit_js'];	
		echo $this->view_inner_template('dealer_add_edit',$data);
	}
	
	public function edit($dealer_id){
		$this->allowed('is_edit');
		$data['form_heading'] = "Edit Dealer";
		if($this->request->getMethod() === 'post'){
			$rules = $this->get_validation_rules($dealer_id);
			if ($this->validate($rules)) { 
				$data_in = $this->get_column_values();				
				$this->mObj->update($data_in,$dealer_id);
				$this->notify_success('Record Updated Successfully');
				return redirect()->to(get_admin_path('dealer'));
			}
		}else{
			$data['update_data'] = $this->mObj->get($dealer_id);
		}
		$data['footer_includes'] = ['dealer_add_edit_js'];	
		echo $this->view_inner_template('dealer_add_edit',$data);
	}
	
	public function delete($dealer_id){
		$this->allowed('is_edit');
		$this->mObj->delete($dealer_id);
		$this->notify_success('Record Deleted Successfully');
		return redirect()->to(get_admin_path('dealer'));
	}
		
	private function get_column_values(){
		$data['title'] = $this->request->getPost('title');
		$data['upload_type'] = $this->request->getPost('upload_type');
		$data['icon_id'] = $this->request->getPost('icon_id');
		$data['document_file'] = $this->request->getPost('document_file');
		$data['status'] = $this->request->getPost('status');
		return $data;
	}
	
	private function get_validation_rules($dealer_id=""){
		$rules['title'] = ['label'=>'Title', 'rules' => 'trim|required'];
		$rules['status'] = ['label'=>'Status', 'rules' => 'trim|required'];
		$rules['document_file'] = ['label'=>'File', 'rules' => 'trim|required'];
		$rules['upload_type'] = ['label'=>'Upload Type', 'rules' => 'trim|required'];
		$rules['icon_id'] = ['label'=>'Icon', 'rules' => 'trim|required'];
		return $rules;
	}
	
	public function document_upload(){
		$file = $this->request->getFile('file');
		if($file->getName()){
			$file->move(ROOTPATH . 'public/download/',$file->getRandomName());
			$data['filename'] = $file->getName();
			$data['file_error'] = "";
		}else{
			$data['filename'] = "";
			$data['file_error'] = "Error while upload";
		}
		echo json_encode($data);
	}
}