<?php namespace App\Controllers\Admin;

class Product_category extends AdminController
{
	private $mObj;
	private $write_path;
	 
	public function __construct(){		
        $this->mObj = model('App\Models\Admin\Product_categoryModel');
		$this->menu_id =26;
		$this->write_path = ROOTPATH . 'public/uploads/';
    }
	
	public function index(){
		$this->allowed('is_list');
		$data['footer_includes'] = ['product_category_list_js'];		
		echo $this->view_inner_template('product_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 Product Category";
		$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('product_category'));
			}
		}
		$data['footer_includes'] = ['product_category_add_edit_js'];	
		echo $this->view_inner_template('product_category_add_edit',$data);
	}
	
	public function edit($product_category_id){
		$this->allowed('is_edit');
		$data['form_heading'] = "Edit Product Category";
		$data['action'] = "edit";
		$data['language_list'] = $this->cmObj->get_language_list();
		if($this->request->getMethod() === 'post'){
			$rules = $this->get_validation_rules($data['language_list'],$product_category_id);

			if ($this->validate($rules)){ 
				$data_in = $this->get_column_values();
				$this->mObj->update($data_in,$product_category_id);
				$this->save_language_data($data['language_list'],$product_category_id);
				$this->remove_prev_file();
				$this->notify_success('Record Updated Successfully');
				return redirect()->to(get_admin_path('product_category'));
			}
		}else{
			$data['update_data'] = $this->mObj->get($data['language_list'],$product_category_id);
		}
		$data['footer_includes'] = ['product_category_add_edit_js'];	
		echo $this->view_inner_template('product_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($product_category_id){
		$this->allowed('is_edit');
		$this->mObj->delete($product_category_id);
		$this->notify_success('Record Deleted Successfully');
		return redirect()->to(get_admin_path('product_category'));
	}
	
	private function get_column_values(){
		$data['product_category_url'] = $this->request->getPost('product_category_url');				
		$data['status'] = $this->request->getPost('status');
		$data['show_on_home_page'] = $this->request->getPost('show_on_home_page');
		$this->save_uploaded_file($data);
// add extra to get title and category
		//$data['product_category_id'] = $this->request->getPost('product_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['product_category_url'] = ['label'=>'Url', 'rules' => 'trim|required|pr_callback['.$this->mObj->is_unique_product_category_url($this->request->getPost('product_category_url'),$id).']'];
		$rules['status'] = ['label'=>'Status', 'rules' => 'trim|required'];
		$rules['show_on_home_page'] = ['label'=>'Show on home page', 'rules' => 'trim|required'];

		if($this->request->getFile('photo_file')->getName()){
			$rules['photo_file'] = ['label'=>'Image', 'rules'=>'uploaded[photo_file]|is_image[photo_file]']; //|max_size[company_logo,5048]|max_dims[company_logo,300,200]
		}
		if($this->request->getFile('banner_file')->getName()){
			$rules['banner_file'] = ['label'=>'Banner', 'rules'=>'uploaded[banner_file]|is_image[banner_file]']; //|max_size[company_logo,5048]|max_dims[company_logo,300,200]
		}
		return $rules;
	}
	
	private function save_uploaded_file(&$data){
	 	$file = $this->request->getFile('photo_file');
	 	if($file->getName()){
	 		$file->move(ROOTPATH . 'public/uploads/',$file->getRandomName());
	 		$data['photo_file'] = $file->getName();
	 	}
		$banner = $this->request->getFile('banner_file');
	 	if($banner->getName()){
	 		$banner->move(ROOTPATH . 'public/uploads/',$banner->getRandomName());
	 		$data['banner_file'] = $banner->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('product_category_ln_id_'.$language['code'])){
					$this->mObj->update_language_data($data,$this->request->getPost('product_category_ln_id_'.$language['code']));
				}else{
					$data['product_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'));
		}
	}
}