<?php namespace App\Controllers\Admin;

class Download extends AdminController
{
	private $mObj;
	
	public function __construct(){		
        $this->mObj = model('App\Models\Admin\DownloadModel');
		$this->menu_id = 21;
    }
	
	public function index(){
		$this->allowed('is_list');
		$data['footer_includes'] = ['download_list_js'];		
		echo $this->view_inner_template('download_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 Download";
		$data['language_list'] = $this->cmObj->get_language_list();
		if($this->request->getMethod() === 'post'){
			$rules = $this->get_validation_rules();
			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('download'));
			}
		}
		$data['footer_includes'] = ['download_add_edit_js'];	
		echo $this->view_inner_template('download_add_edit',$data);
	}
	
	public function edit($download_id){
		$this->allowed('is_edit');
		$data['form_heading'] = "Edit Download";
		$data['language_list'] = $this->cmObj->get_language_list();
		if($this->request->getMethod() === 'post'){
			$rules = $this->get_validation_rules($download_id);
			if ($this->validate($rules)) { 
				$data_in = $this->get_column_values();				
				$this->mObj->update($data_in,$download_id);
				$this->save_language_data($data['language_list'],$download_id);
				$this->notify_success('Record Updated Successfully');
				return redirect()->to(get_admin_path('download'));
			}
		}else{
			$data['update_data'] = $this->mObj->get($data['language_list'],$download_id);
		}
		$data['footer_includes'] = ['download_add_edit_js'];	
		echo $this->view_inner_template('download_add_edit',$data);
	}
	
	public function delete($download_id){
		$this->allowed('is_edit');
		$this->mObj->delete($download_id);
		$this->notify_success('Record Deleted Successfully');
		return redirect()->to(get_admin_path('download'));
	}
		
	private function get_column_values(){
		$data['upload_type'] = $this->request->getPost('upload_type');
		$data['icon_id'] = $this->request->getPost('icon_id');
		$data['download_category_id'] = $this->request->getPost('download_category_id');
		$data['document_file'] = $this->request->getPost('document_file');
		$data['icon_file'] = $this->request->getPost('icon_file');
		$data['status'] = $this->request->getPost('status');
		return $data;
	}
	
	private function get_validation_rules($download_id=""){
		$rules['status'] = ['label'=>'Status', 'rules' => 'trim|required'];
		$rules['document_file'] = ['label'=>'File', 'rules' => 'trim|required'];
		$rules['upload_type'] = ['label'=>'Upload Type', 'rules' => 'trim|required'];
		$rules['icon_id'] = ['label'=>'Icon', 'rules' => 'trim|required'];
		//$rules['icon_file'] = ['label'=>'Icon File', 'rules' => 'trim|required'];
		$rules['download_category_id'] = ['label'=>'Download Category', 'rules' => 'trim|required'];
		return $rules;
	}
	
	public function document_upload(){
		$file = $this->request->getFile('file');
		if($file->getName()){
			$file->move(ROOTPATH . 'public/download/',$file->getRandomName());
			$data['filename'] = $file->getName();
			$data['file_error'] = "";
		}else{
			$data['filename'] = "";
			$data['file_error'] = "Error while upload";
		}
		echo json_encode($data);
	}
	
	public function icon_upload(){
		$file = $this->request->getFile('file');
		if($file->getName()){
			$file->move(ROOTPATH . 'public/uploads/',$file->getRandomName());
			$data['filename'] = $file->getName();
			$data['file_error'] = "";
		}else{
			$data['filename'] = "";
			$data['file_error'] = "Error while upload";
		}
		echo json_encode($data);
	}
	
	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['language_code'] = $language['code'];
				if($this->request->getPost('download_ln_id_'.$language['code'])){
					$this->mObj->update_language_data($data,$this->request->getPost('download_ln_id_'.$language['code']));
				}else{
					$data['download_id'] = $id;
					$this->mObj->insert_language_data($data);
				}
			}
		}
	}
}