<?php namespace App\Controllers\Admin;

class Category extends AdminController
{
	private $mObj;
	private $write_path; 
	
	public function __construct(){		
        $this->mObj = model('App\Models\Admin\CategoryModel');
		$this->menu_id = 20;
		$this->write_path = ROOTPATH . 'public/uploads/';		
    }
	
	public function index(){
		$this->allowed('is_list');
		$data['footer_includes'] = ['category_list_js'];		
		echo $this->view_inner_template('category_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 Category";
		$data['action'] = "add";
		if($this->request->getMethod() === 'post'){
			$rules = $this->get_validation_rules();
			if($this->validate($rules)) { 
				$data_in = $this->get_column_values();
				$id = $this->mObj->insert($data_in);
				$this->notify_success('Record Added Successfully');
				return redirect()->to(get_admin_path('category'));
			}
		}
		$data['footer_includes'] = ['category_add_edit_js'];	
		echo $this->view_inner_template('category_add_edit',$data);
	}
	
	public function edit($category_id){
		$this->allowed('is_edit');
		$data['form_heading'] = "Edit Category";
		$data['action'] = "edit";
		
		
		if($this->request->getMethod() === 'post'){
			$rules = $this->get_validation_rules($category_id);
			if ($this->validate($rules)) { 
				$data_in = $this->get_column_values();
				$this->mObj->update($data_in,$category_id);
				$this->remove_prev_file($data_in);
				$this->notify_success('Record Updated Successfully');
				return redirect()->to(get_admin_path('category'));
			}
		}else{
			$data['update_data'] = $this->mObj->get($category_id);
		}
		$data['footer_includes'] = ['category_add_edit_js'];	
		echo $this->view_inner_template('category_add_edit',$data);
	}
	
	
	
	public function delete($category_id){
		$this->allowed('is_edit');
		$this->mObj->delete($category_id);
		$this->notify_success('Record Deleted Successfully');
		return redirect()->to(get_admin_path('category'));
	}
	
	private function get_column_values(){
		$data['name'] = $this->request->getPost('name');
		$data['status'] = $this->request->getPost('status');
		$this->save_uploaded_file($data);
		return $data;
	}
	private function get_validation_rules($record_id=""){
		$rules['name'] = ['label'=>'Name', 'rules' => 'trim|required'];
// 		$rules['url'] = ['label'=>'City URL', 'rules' => 'trim|required|pr_callback['.$this->mObj->is_unique_category_url($this->request->getPost('url'),$record_id).']'];
		$rules['status'] = ['label'=>'Status', 'rules' => 'trim|required'];
		if ($this->request->getFile('badge')->getName()) {
            $rules['badge'] = [
                'label' => 'badge',
                'rules' => 'uploaded[badge]|is_image[badge]|mime_in[badge,image/jpeg,image/jpg]|max_size[badge,2048]',
                'errors' => [
                    'max_size' => 'The badge image must not exceed 2MB.',
                    'mime_in' => 'Only JPG images are allowed.'
                ]
            ];
        }



		return $rules;
	}


	private function save_uploaded_file(&$data){
		$file = $this->request->getFile('badge');
		if($file->getName()){
			$file->move(ROOTPATH . 'public/uploads/',$file->getRandomName());
			$data['badge'] = $file->getName();
		}
		
	}

	private function remove_prev_file($data_in){
		if(isset($data_in['badge']) && 
			$data_in['badge']!="" && 
			$this->request->getPost('uploaded_photo_image')!="" &&
			$data_in['badge']!=$this->request->getPost('uploaded_photo_image') && 
			file_exists($this->write_path.$this->request->getPost('uploaded_photo_image'))){
			unlink($this->write_path.$this->request->getPost('uploaded_photo_image'));
		}
	}
	
	
}

