<?php namespace App\Controllers\Admin;

class Products extends AdminController
{
	private $mObj;
	private $write_path;
	
	public function __construct(){		
        $this->mObj = model('App\Models\Admin\ProductsModel');
		$this->menu_id = 23;
		$this->write_path = ROOTPATH . 'public/uploads/';
    }
	
	public function index(){
		$this->allowed('is_list');
		$data['footer_includes'] = ['products_list_js'];		
		echo $this->view_inner_template('products_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'] = "Products Add";
		$data['action'] = "add";
		$data['product_id'] = '';
		$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('products'));
			}
		}
		$data['footer_includes'] = ['products_add_edit_js'];	
		echo $this->view_inner_template('products_add_edit',$data);
	}
	
	public function edit($product_id){
		$this->allowed('is_edit');
		$data['form_heading'] = "Products Edit";
		$data['action'] = "edit";
		$data['language_list'] = $this->cmObj->get_language_list();
		$data['product_id'] = $product_id;
		if($this->request->getMethod() === 'post'){
			$rules = $this->get_validation_rules($data['language_list'],$product_id);
			if ($this->validate($rules)) { 
				$data_in = $this->get_column_values();
				/*echo '<pre>';
				print_r($_FILES);
				print_r($data_in);
				exit();*/
				$this->mObj->update($data_in,$product_id);
				$this->save_language_data($data['language_list'],$product_id);
				$this->remove_prev_file($data_in);
				$this->notify_success('Record Updated Successfully');
				return redirect()->to(get_admin_path('products'));
			}
		}else{
			$data['update_data'] = $this->mObj->get($data['language_list'],$product_id);
		}
		$data['footer_includes'] = ['products_add_edit_js'];	
		echo $this->view_inner_template('products_add_edit',$data);
	}
	
	private function remove_prev_file($data_in){
		if(isset($data_in['banner_image']) && 
			$data_in['banner_image']!="" && 
			$data_in['banner_image']!=$this->request->getPost('uploaded_banner_image') && 
			$this->request->getPost('uploaded_banner_image') &&
			file_exists($this->write_path.$this->request->getPost('uploaded_banner_image'))){
			unlink($this->write_path.$this->request->getPost('uploaded_banner_image'));
		}
		if(isset($data_in['product_image_1']) && 
			$data_in['product_image_1']!="" && 
			$data_in['product_image_1']!=$this->request->getPost('uploaded_product_image_1') && 
			$this->request->getPost('uploaded_product_image_1') &&
			file_exists($this->write_path.$this->request->getPost('uploaded_product_image_1'))){
			unlink($this->write_path.$this->request->getPost('uploaded_product_image_1'));
		}
		if(isset($data_in['product_icon_img_1']) && 
			$data_in['product_icon_img_1']!="" && 
			$data_in['product_icon_img_1']!=$this->request->getPost('uploaded_product_icon_img_1') && 
			$this->request->getPost('uploaded_product_icon_img_1') &&
			file_exists($this->write_path.$this->request->getPost('uploaded_product_icon_img_1'))){
			unlink($this->write_path.$this->request->getPost('uploaded_product_icon_img_1'));
		}
		if(isset($data_in['product_image_2']) && 
			$data_in['product_image_2']!="" && 
			$data_in['product_image_2']!=$this->request->getPost('uploaded_product_image_2') && 
			$this->request->getPost('uploaded_product_image_2') &&
			file_exists($this->write_path.$this->request->getPost('uploaded_product_image_2'))){
			unlink($this->write_path.$this->request->getPost('uploaded_product_image_2'));
		}
		if(isset($data_in['product_icon_img_2']) && 
			$data_in['product_icon_img_2']!="" && 
			$data_in['product_icon_img_2']!=$this->request->getPost('uploaded_product_icon_img_2') && 
			$this->request->getPost('uploaded_product_icon_img_2') &&
			file_exists($this->write_path.$this->request->getPost('uploaded_product_icon_img_2'))){
			unlink($this->write_path.$this->request->getPost('uploaded_product_icon_img_2'));
		}
		if(isset($data_in['product_image_3']) && 
			$data_in['product_image_3']!="" && 
			$data_in['product_image_3']!=$this->request->getPost('uploaded_product_image_3') && 
			$this->request->getPost('uploaded_product_image_3') &&
			file_exists($this->write_path.$this->request->getPost('uploaded_product_image_3'))){
			unlink($this->write_path.$this->request->getPost('uploaded_product_image_3'));
		}
		if(isset($data_in['product_icon_img_3']) && 
			$data_in['product_icon_img_3']!="" && 
			$data_in['product_icon_img_3']!=$this->request->getPost('uploaded_product_icon_img_3') && 
			$this->request->getPost('uploaded_product_icon_img_3') &&
			file_exists($this->write_path.$this->request->getPost('uploaded_product_icon_img_3'))){
			unlink($this->write_path.$this->request->getPost('uploaded_product_icon_img_3'));
		}
		if(isset($data_in['product_image_4']) && 
			$data_in['product_image_4']!="" && 
			$data_in['product_image_4']!=$this->request->getPost('uploaded_product_image_4') && 
			$this->request->getPost('uploaded_product_image_4') &&
			file_exists($this->write_path.$this->request->getPost('uploaded_product_image_4'))){
			unlink($this->write_path.$this->request->getPost('uploaded_product_image_4'));
		}
		if(isset($data_in['product_icon_img_4']) && 
			$data_in['product_icon_img_4']!="" && 
			$data_in['product_icon_img_4']!=$this->request->getPost('uploaded_product_icon_img_4') && 
			$this->request->getPost('uploaded_product_icon_img_4') &&
			file_exists($this->write_path.$this->request->getPost('uploaded_product_icon_img_4'))){
			unlink($this->write_path.$this->request->getPost('uploaded_product_icon_img_4'));
		}
	}
	
	
	public function delete($products_id){
		$this->allowed('is_edit');
		$this->mObj->delete($products_id);
		$this->notify_success('Record Deleted Successfully');
		return redirect()->to(get_admin_path('products'));
	}
	
	public function delete_file($product_id,$fld_name){
		if($fld_name!=""){
			$data_up[$fld_name] = "";
			$this->mObj->update($data_up,$product_id);
			echo 'SUCCESS';
		}
 	}
	
	private function get_column_values(){
		$data['product_url'] = $this->request->getPost('product_url');
		$data['product_category_id'] = $this->request->getPost('product_category_id');
		$data['status'] = $this->request->getPost('status');
		$this->save_uploaded_file($data);
		return $data;
	}
	
	private function get_validation_rules($language_list,$record_id=""){
		if($language_list){
			foreach($language_list as $language){
				$rules['title_'.$language['code']] = ['label'=>'Title', 'rules' => 'trim|required'];
				$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['short_description_'.$language['code']] = ['label'=>'Short Description', 'rules' => 'trim|required'];
				$rules['description_'.$language['code']] = ['label'=>'Description', 'rules' => 'trim|required'];
			}
		}
		$rules['product_url'] = ['label'=>'Products URL', 'rules' => 'trim|required|pr_callback['.$this->mObj->is_unique_product_url($this->request->getPost('product_url'),$record_id).']'];
		$rules['status'] = ['label'=>'Status', 'rules' => 'trim|required'];
		$rules['product_category_id'] = ['label'=>'Category', 'rules' => 'trim|required'];
		return $rules;
	}
	
	private function save_uploaded_file(&$data){
		$file = $this->request->getFile('banner_image');
		$product_doc1_file = $this->request->getFile('product_doc_file');
		if($file->getName()){
			$file->move(ROOTPATH . 'public/uploads/',$file->getRandomName());
			$data['banner_image'] = $file->getName();			
		}
		if($product_doc1_file->getName()){
			$product_doc1_file->move(ROOTPATH . 'public/uploads/',$product_doc1_file->getRandomName());
			$data['product_doc_file'] = $product_doc1_file->getName();			
		}
		// Save Product Images
		for($pi=1;$pi<=5;$pi++){
			$prod_img = $this->request->getFile('product_image_'.$pi);
			if($prod_img->getName()){
				$prod_img->move(ROOTPATH . 'public/uploads/',$prod_img->getRandomName());
				$data['product_image_'.$pi] = $prod_img->getName();				
			}
			if($pi==1){
				$prod_icon = $this->request->getFile('product_icon_img_'.$pi);
				if($prod_icon->getName()){
					$prod_icon->move(ROOTPATH . 'public/uploads/',$prod_icon->getRandomName());
					$data['product_icon_img_'.$pi] = $prod_icon->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['short_description'] = $this->request->getPost('short_description_'.$language['code']);				
				$data['description'] = $this->request->getPost('description_'.$language['code']);
				$data['dose'] = $this->request->getPost('dose_'.$language['code']);
				$data['compatibility'] = $this->request->getPost('compatibility_'.$language['code']);
				$data['benefit'] = $this->request->getPost('benefit_'.$language['code']);
				$data['packing'] = $this->request->getPost('packing_'.$language['code']);
				$data['language_code'] = $language['code'];
				if($this->request->getPost('product_ln_id_'.$language['code'])){
					$this->mObj->update_language_data($data,$this->request->getPost('product_ln_id_'.$language['code']));
				}else{
					$data['product_id'] = $id;
					$this->mObj->insert_language_data($data);
				}
			}
		}
	}
}