<?php namespace App\Controllers\Admin;

class Photo_gallery extends AdminController
{
	private $mObj;
	
	public function __construct(){		
        $this->mObj = model('App\Models\Admin\Photo_galleryModel');
		$this->menu_id =11;
		$this->write_path = ROOTPATH . 'public/uploads/';
    }
	
	public function index(){
		$this->allowed('is_list');
		$data['footer_includes'] = ['photo_gallery_list_js'];		
		echo $this->view_inner_template('photo_gallery_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['action'] = "add";
		$data['photo_gallery_id'] = '';		
		$data['form_heading'] = "Add Photo Gallery";
		if($this->request->getMethod() === 'post'){
			$rules = $this->get_validation_rules();
			// echo '<pre>';
			// print_r($this->validate($rules));
			// exit();
			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('photo_gallery'));
			}
		}
		$data['footer_includes'] = ['photo_gallery_add_edit_js'];	
		echo $this->view_inner_template('photo_gallery_add_edit',$data);
	}
	
	public function edit($photo_gallery_id){
		$this->allowed('is_edit');
		$data['form_heading'] = "Edit Photo Gallery";
		$data['action'] = "edit";
		$data['photo_gallery_id'] = $photo_gallery_id;
		if($this->request->getMethod() === 'post'){
			$rules = $this->get_validation_rules($photo_gallery_id);
			if ($this->validate($rules)) { 
				$data_in = $this->get_column_values();
				$this->mObj->update($data_in,$photo_gallery_id);
				$this->remove_prev_file($data_in);
				$this->notify_success('Record Updated Successfully');
				return redirect()->to(get_admin_path('photo_gallery'));
			}
		}else{
			$data['update_data'] = $this->mObj->get($photo_gallery_id);
		}
		
		$data['footer_includes'] = ['photo_gallery_add_edit_js'];	
		echo $this->view_inner_template('photo_gallery_add_edit',$data);
	}
	private function remove_prev_file($data_in){
		if(isset($data_in['image']) && 
			$data_in['image']!="" && 
			$this->request->getPost('uploaded_photo')!="" &&
			$data_in['image']!=$this->request->getPost('uploaded_photo')){
			unlink($this->write_path.$this->request->getPost('uploaded_photo'));
		}
	}

	public function delete($photo_gallery_id){
		$this->allowed('is_edit');
		$this->mObj->delete($photo_gallery_id);
		$this->notify_success('Record Deleted Successfully');
		return redirect()->to(get_admin_path('photo_gallery'));
	}
	
	private function get_column_values(){
		$data['photo_album_id'] = $this->request->getPost('photo_album_id');				
		$data['title'] = $this->request->getPost('title');				
		$data['status'] = $this->request->getPost('status');
		$this->save_uploaded_file($data);
		return $data;
	}
	
	private function get_validation_rules($photo_gallery_id=""){
		$rules['title'] = ['label'=>'title', 'rules' => 'trim|required'];
		$rules['photo_album_id'] = ['label'=>'Photo Album ID', 'rules' => 'trim|required'];
		$rules['status'] = ['label'=>'Status', 'rules' => 'trim|required'];
		$imageFile = $this->request->getFile('image');
		if ($imageFile) {
			$rules['image'] = [
				'label' => 'Image',
				'rules' => 'uploaded[image]|max_size[image,2048]|is_image[image]|max_dims[image,900,675]'
			];
		}
		return $rules;
	}
	
	private function save_uploaded_file(&$data){
		$file = $this->request->getFile('image');
		if($file && $file->isValid() && !$file->hasMoved()){
			$file->move(ROOTPATH . 'public/uploads/',$file->getRandomName());
			$data['image'] = $file->getName();
		}
	}
}