<?php namespace App\Controllers\Admin;

class Banners extends AdminController
{
	private $mObj;
	
	public function __construct(){		
        $this->mObj = model('App\Models\Admin\BannersModel');
		$this->menu_id = 6;
    }
	
	public function index(){
		$this->allowed('is_list');
		$data['footer_includes'] = ['banners_list_js'];		
		echo $this->view_inner_template('banners_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');
	    file_put_contents(WRITEPATH.'uploads/test.txt', 'testing');
		$data['form_heading'] = "Add Banners";
		
		if($this->request->getMethod() === 'post'){
			$rules = $this->get_validation_rules();
			if ($this->validate($rules)) { 
				$data_in = $this->get_column_values();				
				$this->mObj->insert($data_in);
				$this->notify_success('Record Added Successfully');
				return redirect()->to(get_admin_path('banners'));
			}
		}
		$data['footer_includes'] = ['banners_add_edit_js'];	
		echo $this->view_inner_template('banners_add_edit',$data);
	}
	
	private function get_new_sort_order($position_after_banner_id){
		if($position_after_banner_id==0){
			$this->mObj->increment_sort_order_greater_than(0);
			return 1;
		}else{
			$after_banner_dtl = $this->mObj->get($position_after_banner_id);
			// increment sort order of banners
			$this->mObj->increment_sort_order_greater_than($after_banner_dtl['sort_order']);
			return $after_banner_dtl['sort_order'] + 1;
		}
	}
	
	public function edit($banner_id){
		$this->allowed('is_edit');
		$data['form_heading'] = "Edit banner";
		if($this->request->getMethod() === 'post'){
			$rules = $this->get_validation_rules($banner_id);
			if ($this->validate($rules)) { 
				$data_in = $this->get_column_values();				
				$this->mObj->update($data_in,$banner_id);
				$this->notify_success('Record Updated Successfully');
				return redirect()->to(get_admin_path('banners'));
			}
		}else{
			$data['update_data'] = $this->mObj->get($banner_id);
		}
		$data['footer_includes'] = ['banners_add_edit_js'];	
		echo $this->view_inner_template('banners_add_edit',$data);
	}
	
	public function delete($banner_id){
		$this->allowed('is_edit');
		$this->mObj->delete($banner_id);
		$this->notify_success('Record Deleted Successfully');
		return redirect()->to(get_admin_path('banners'));
	}
	
	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();
		}
	}
	
	private function get_column_values(){
		$data['title'] = $this->request->getPost('title');
		$data['link'] = $this->request->getPost('link');
		$data['banner_type'] = $this->request->getPost('banner_type');
		$data['video_url'] = $this->request->getPost('video_url');
		$data['open_in_new_tab'] = $this->request->getPost('open_in_new_tab');
		$data['sort_order'] = $this->request->getPost('sort_order');
		$data['status'] = $this->request->getPost('status');
		//$data['position_after_banner_id'] = $this->request->getPost('position_after_banner_id');
		$this->save_uploaded_file($data);
		return $data;
	}
	
	private function get_validation_rules($banner_id=""){
		$rules['title'] = ['label'=>'Title', 'rules' => 'trim|required|pr_callback['.$this->mObj->is_unique_title($this->request->getPost('title'),$banner_id).']'];
		$rules['status'] = ['label'=>'Status', 'rules' => 'trim|required'];
		if($this->request->getFile('image')->getName()){
			$rules['image'] = ['label'=>'Image', 'rules'=>'uploaded[image]|max_size[image,2048]|is_image[image]|max_dims[image,1925,870]'];
		}
		return $rules;
	}
}