<?php namespace App\Controllers\Admin;

class Testimonial extends AdminController
{
	private $mObj;
	
	public function __construct(){		
        $this->mObj = model('App\Models\Admin\TestimonialModel');
		$this->menu_id = 16;
    }
	
	public function index(){
		$this->allowed('is_list');
		$data['footer_includes'] = ['testimonial_list_js'];		
		echo $this->view_inner_template('testimonial_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 Testimonial";
		
		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('testimonial'));
			}
		}
		echo $this->view_inner_template('testimonial_add_edit',$data);
	}
	
	public function edit($testimonial_id){
		$this->allowed('is_edit');
		$data['form_heading'] = "Edit Testimonial";
		if($this->request->getMethod() === 'post'){
			$rules = $this->get_validation_rules($testimonial_id);
			if ($this->validate($rules)) { 
				$data_in = $this->get_column_values();				
				$this->mObj->update($data_in,$testimonial_id);
				$this->notify_success('Record Updated Successfully');
				return redirect()->to(get_admin_path('testimonial'));
			}
		}else{
			$data['update_data'] = $this->mObj->get($testimonial_id);
		}
		//$data['footer_includes'] = ['testimonial_add_edit_js'];	
		echo $this->view_inner_template('testimonial_add_edit',$data);
	}
	
	public function delete($testimonial_id){
		$this->allowed('is_edit');
		$this->mObj->delete($testimonial_id);
		$this->notify_success('Record Deleted Successfully');
		return redirect()->to(get_admin_path('testimonial'));
	}
	
	private function save_uploaded_file(&$data){
		$file = $this->request->getFile('image');
		if($file->getName()){
			$file->move(ROOTPATH . 'public/uploads/',$file->getRandomName());
			$data['image'] = $file->getName();
		}
	}
	
	private function get_column_values(){
		$data['name'] = $this->request->getPost('name');
		$data['occupation'] = $this->request->getPost('occupation');
		$data['description'] = $this->request->getPost('description');
		$data['status'] = $this->request->getPost('status');
		$data['star_rating'] = $this->request->getPost('star_rating');
		$data['review'] = $this->request->getPost('review');
		$this->save_uploaded_file($data);
		return $data;
	}
	
	private function get_validation_rules($testimonial_id=""){
		$rules['name'] = ['label'=>'Name', 'rules' => 'trim|required'];
		$rules['occupation'] = ['label'=>'Occupation', 'rules' => 'trim'];
		$rules['description'] = ['label'=>'Description', 'rules' => 'trim'];
		$rules['status'] = ['label'=>'Status', 'rules' => 'trim|required'];
		$rules['review'] = ['label'=>'Review', 'rules' => 'trim|required'];
		$rules['star_rating'] = ['label'=>'Star Rating', 'rules' => 'trim|required|greater_than_equal_to[0]|less_than_equal_to[5]'];
		if($this->request->getFile('image')->getName()){
			$rules['image'] = ['label'=>'Image', 'rules'=>'uploaded[image]|max_size[image,1048]|is_image[image]|max_dims[image,90,90]'];
		}
		return $rules;
	}
}