<?php namespace App\Controllers\Admin;

class Testimonial extends AdminController
{
	private $mObj;
	private $write_path; 
	
	public function __construct(){		
        $this->mObj = model('App\Models\Admin\TestimonialModel');
		$this->menu_id = 8;
		$this->write_path = ROOTPATH . 'public/uploads/';		
    }
	
	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";
		$data['action'] = "add";
		$data['language_list'] = $this->cmObj->get_language_list();
		
		if($this->request->getMethod() === 'post'){
			$rules = $this->get_validation_rules($data['language_list']);
			if($this->validate($rules)) { 
				$data_in = $this->get_column_values();
				$id = $this->mObj->insert($data_in);
				$this->save_language_data($data['language_list'],$id);
				$this->notify_success('Record Added Successfully');
				return redirect()->to(get_admin_path('testimonial'));
			}
		}
		//$data['footer_includes'] = ['testimonial_add_edit_js'];	
		echo $this->view_inner_template('testimonial_add_edit',$data);
	}
	
	public function edit($testimonial_id){
		$this->allowed('is_edit');
		$data['form_heading'] = "Edit Testimonial";
		$data['action'] = "edit";
		$data['language_list'] = $this->cmObj->get_language_list();
		
		if($this->request->getMethod() === 'post'){
			$rules = $this->get_validation_rules($data['language_list'],$testimonial_id);
			if ($this->validate($rules)) { 
				$data_in = $this->get_column_values();
				$this->mObj->update($data_in,$testimonial_id);
				$this->save_language_data($data['language_list'],$testimonial_id);
				$this->remove_prev_file($data_in);
				$this->notify_success('Record Updated Successfully');
				return redirect()->to(get_admin_path('testimonial'));
			}
		}else{
			$data['update_data'] = $this->mObj->get($data['language_list'],$testimonial_id);
		}
		//$data['footer_includes'] = ['testimonial_add_edit_js'];	
		echo $this->view_inner_template('testimonial_add_edit',$data);
	}
	
	private function remove_prev_file($data_in){
		if(isset($data_in['photo_image']) && 
			$data_in['photo_image']!="" && 
			$this->request->getPost('uploaded_photo_image')!="" &&
			$data_in['photo_image']!=$this->request->getPost('uploaded_photo_image')){
			unlink($this->write_path.$this->request->getPost('uploaded_photo_image'));
		}
	}
	
	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 get_column_values(){
		//$data['testimonial_url'] = $this->request->getPost('testimonial_url');
		$data['status'] = $this->request->getPost('status');
		$this->save_uploaded_file($data);
		return $data;
	}
	// .' '.$this->request->getPost('publish_time')));
	private function get_validation_rules($language_list,$record_id=""){
		if($language_list){
			foreach($language_list as $language){
				$rules['name_'.$language['code']] = ['label'=>'Name', 'rules' => 'trim|required'];
				$rules['description_'.$language['code']] = ['label'=>'Description', 'rules' => 'trim|required'];
			}
		}
		//$rules['testimonial_url'] = ['label'=>'News URL', 'rules' => 'trim|required|pr_callback['.$this->mObj->is_unique_testimonial_url($this->request->getPost('testimonial_url'),$record_id).']'];
		$rules['status'] = ['label'=>'Status', 'rules' => 'trim|required'];

		return $rules;
	}
	
	public function is_valid_datetime_range($start_dt,$end_dt){
		if(is_valid_datetime_range($start_dt,$end_dt)){
			$response = "TRUE|";			
		}else{
			$response = "FALSE|The End Date Time must be greater than Start Date Time";
		}
		return $response;
	}
	
	private function save_uploaded_file(&$data){
		$file = $this->request->getFile('photo_image');
		if($file->getName()){
			$file->move(ROOTPATH . 'public/uploads/',$file->getRandomName());
			$data['photo_image'] = $file->getName();
		}
	}
	
	public function save_language_data($language_list,$id=""){
		if($language_list){			
			foreach($language_list as $language){
				$data = [];
				$data['name'] = $this->request->getPost('name_'.$language['code']);
				$data['city'] = $this->request->getPost('city_'.$language['code']);
				$data['description'] = $this->request->getPost('description_'.$language['code']);
				$data['language_code'] = $language['code'];
				if($this->request->getPost('testimonial_ln_id_'.$language['code'])){
					$this->mObj->update_language_data($data,$this->request->getPost('testimonial_ln_id_'.$language['code']));
				}else{
					$data['testimonial_id'] = $id;
					$this->mObj->insert_language_data($data);
				}
			}
		}
	}
}