<?php namespace App\Controllers\Admin;

class News_category extends AdminController
{
	private $mObj;
	private $write_path;
	 
	public function __construct(){		
        $this->mObj = model('App\Models\Admin\News_categoryModel');
		$this->menu_id =15;
		$this->write_path = ROOTPATH . 'public/uploads/';
    }
	
	public function index(){
		$this->allowed('is_list');
		$data['footer_includes'] = ['News_category_list_js'];		
		echo $this->view_inner_template('News_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'] = "News Category Add";
		$data['action'] = "add";
		$data['language_list'] = $this->cmObj->get_language_list();
		if($this->request->getMethod() === 'post'){
			$rules = $this->get_validation_rules($data['language_list']);
			// echo '<pre>';
			// print_r($this->request->getPost());
			// exit();
			if ($this->validate($rules)) { 
				$data_in = $this->get_column_values();
				$data_in['is_deleted'] = 'No';
				$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('News_category'));
			}
		}
		$data['footer_includes'] = ['News_category_add_edit_js'];	
		echo $this->view_inner_template('News_category_add_edit',$data);
	}
	
	public function edit($News_category_id){
		$this->allowed('is_edit');
		$data['form_heading'] = "News Category Edit";
		$data['action'] = "edit";
		$data['language_list'] = $this->cmObj->get_language_list();
		if($this->request->getMethod() === 'post'){
			$rules = $this->get_validation_rules($data['language_list'],$News_category_id);

			if ($this->validate($rules)){ 
				$data_in = $this->get_column_values();
				$this->mObj->update($data_in,$News_category_id);
				$this->save_language_data($data['language_list'],$News_category_id);
				$this->remove_prev_file();
				$this->notify_success('Record Updated Successfully');
				return redirect()->to(get_admin_path('News_category'));
			}
		}else{
			$data['update_data'] = $this->mObj->get($data['language_list'],$News_category_id);
		}
		$data['footer_includes'] = ['News_category_add_edit_js'];	
		echo $this->view_inner_template('News_category_add_edit',$data);
	}
	
	private function remove_prev_file(){
		if($this->request->getPost('photo_file')!=$this->request->getPost('prev_photo_file') && $this->request->getPost('prev_photo_file')!=""){
			unlink($this->write_path.$this->request->getPost('prev_photo_file'));
		}
	}
	
	public function delete($News_category_id){
		$this->allowed('is_edit');
		$this->mObj->delete($News_category_id);
		$this->notify_success('Record Deleted Successfully');
		return redirect()->to(get_admin_path('News_category'));
	}
	
	private function get_column_values(){
		$data['news_category_url'] = $this->request->getPost('news_category_url');				
		$data['status'] = $this->request->getPost('status');
		$data['photo_file'] = $this->request->getPost('photo_file');
// add extra to get title and category
		$data['news_category_id'] = $this->request->getPost('news_category_id');


		//$this->save_uploaded_file($data);
		return $data;
	}
	
	private function get_validation_rules($language_list, $id=""){
		if($language_list){
			foreach($language_list as $language){
				$rules['title_'.$language['code']] = ['label'=>'Title', 'rules' => 'trim|required|pr_callback['.$this->mObj->is_unique_title($this->request->getPost('title_'.$language['code']),$id).']'];
				$rules['meta_title_'.$language['code']] = ['label'=>'Meta Title', 'rules' => 'trim|required'];
				$rules['meta_keywords_'.$language['code']] = ['label'=>'Meta Keywords', 'rules' => 'trim|required'];
				$rules['meta_description_'.$language['code']] = ['label'=>'Meta Description', 'rules' => 'trim|required'];
			}
		}
		$rules['news_category_url'] = ['label'=>'Url', 'rules' => 'trim|required|pr_callback['.$this->mObj->is_unique_news_category_url($this->request->getPost('news_category_url'),$id).']'];
		$rules['status'] = ['label'=>'Status', 'rules' => 'trim|required'];
		$rules['photo_file'] = ['label'=>'Status', 'rules' => 'trim'];

		//if($this->request->getFile('image')->getName()){
		//	$rules['image'] = ['label'=>'Image', 'rules'=>'uploaded[image]|is_image[image]']; //|max_size[company_logo,5048]|max_dims[company_logo,300,200]
		//}
		return $rules;
	}
	
	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();
		}
	}
	
	public function save_language_data($language_list,$id=""){
		if($language_list){			
			foreach($language_list as $language){
				$data = [];
				$data['title'] = $this->request->getPost('title_'.$language['code']);
				$data['meta_title'] = $this->request->getPost('meta_title_'.$language['code']);
				$data['meta_keywords'] = $this->request->getPost('meta_keywords_'.$language['code']);
				$data['meta_description'] = $this->request->getPost('meta_description_'.$language['code']);
				$data['language_code'] = $language['code'];
				if($this->request->getPost('news_category_ln_id_'.$language['code'])){
					$this->mObj->update_language_data($data,$this->request->getPost('news_category_ln_id_'.$language['code']));
				}else{
					$data['news_category_id'] = $id;
					$this->mObj->insert_language_data($data);
				}
			}
		}
	}
	
	public function upload_image(){
	// 	//echo '<pre>';
	// 	//print_r($this->request->getPost());
				
		if($this->request->getPost('image'))
		{
			$data = $this->request->getPost('image');


			$image_array_1 = explode(";", $data);


			$image_array_2 = explode(",", $image_array_1[1]);
			$data = base64_decode($image_array_2[1]);
						
			$image_name = time() . '.jpg';

			file_put_contents($this->write_path.$image_name, $data);

			echo $image_name;
		}
		if($this->request->getPost('delete_image')!=$this->request->getPost('prev_photo_file')){
			unlink($this->write_path.$this->request->getPost('prev_photo_file'));
		}
	}
}