<?php
namespace App\Controllers\Admin;
use App\Controllers\BaseController;

use App\Models\PagesModel; // Import the model

class Pages extends BaseController {
	protected $session;
	protected $pagesmodel;
	
	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/userguide3/general/urls.html
	 */
	
	public function __construct() {
		$this->session = session();
		$this->pagesmodel = new PagesModel();
		if(!$this->session->get('username')){
			redirect("admin");
		}
	}
	public function index()
	{	
		$uri = $this->request->getUri();
		$path = $uri->getSegment(2);
		
		$heading="Pages";
		/* URI SEGMENT FOR AJAX */
		$ajax_url = $uri->getSegment(1).'/'.$uri->getSegment(2);
		/* URI SEGMENT FOR AJAX END */
		$data['heading']=$heading;
		$data['ajax_url']=$ajax_url;
		$data['base_packages_url']  = base_url() . $uri->getSegment(1).'/'.$path.'/getData';
		echo view($uri->getSegment(1).'/includes/header',$data);
		echo view($uri->getSegment(1).'/includes/sidebar');
		echo view($uri->getSegment(1).'/'.$path.'/page',$data);
		echo view($uri->getSegment(1).'/includes/footer');
		echo view($uri->getSegment(1).'/'.$path.'/scripts',$data);
		echo view($uri->getSegment(1).'/includes/script_validation');
	}
	public function getData()
    {
        return $this->pagesmodel->getData();
    }
	public function addForm()
	{   		
		$uri = $this->request->getUri();
		$id = $this->request->getPost('id');
		$path = $uri->getSegment(2);
		$data['edit']=$this->pagesmodel->editData($id);
		echo view($uri->getSegment(1).'/'.$path.'/addform',$data);
		echo view($uri->getSegment(1).'/includes/imagepreview');
	}
	public function addFormPage()
	{   		
		$uri = $this->request->getUri();
		$id = $uri->getSegment(4);
		$path = $uri->getSegment(2);
		$data['edit']=$this->pagesmodel->editData($id);
		$data['heading']=ucfirst($path);
		/* URI SEGMENT FOR AJAX */
		$ajax_url = $uri->getSegment(1).'/'.$uri->getSegment(2);
		/* URI SEGMENT FOR AJAX END */
		$data['ajax_url']=$ajax_url;
		echo view($uri->getSegment(1).'/includes/header',$data);
		echo view($uri->getSegment(1).'/includes/sidebar');
		echo view('admin/'.$path.'/addFormPage');
		echo view($uri->getSegment(1).'/includes/footer');
		echo view($uri->getSegment(1).'/'.$path.'/scripts',$data);
		echo view($uri->getSegment(1).'/includes/script_validation');
		echo view($uri->getSegment(1).'/includes/imagepreview');
	}
	public function addDataProcess()
	{   
		$id=$_POST['id'];
		unset($_POST['id']);
		//$page_name=strtolower($_POST['page_name']);
		$page_name=strtolower(str_replace(" ","",$_POST['page_name']));

		$dir_name="pages/".$page_name;
		mkdir($dir_name);
		mkdir($dir_name."/css");
		mkdir($dir_name."/js");
		mkdir($dir_name."/images");
		mkdir($dir_name."/video");
		if(empty($id)){
		 echo $status= $this->pagesmodel->addDataProcess($_POST);
		}else{
		 echo $status= $this->pagesmodel->updateDataProcess($_POST,$id);
		}
	}
	/* DATAPROCESS WITH IMAGE */
	public function addDataProcessImage()
	{   
		$id=$_POST['id'];
		$image=$this->uploadfilelibrary->uploadFile('image','uploads',1);
		$_POST['image']=$image;
		unset($_POST['id']);
		$_POST['createdOn']=date("Y-m-d H:i:s");
		$_POST['updatedOn']=date("Y-m-d H:i:s");
		if(empty($id)){
		 echo $status= $this->pagesmodel->addDataProcess($_POST);
		}else{
		$_POST['updatedOn']=date("Y-m-d H:i:s");
		/* IMAGE SECTION */ 	
		
		if(empty($image)){
		$_POST['image']=$_POST['oldImage'];
		$oldImage="";
		}else{
		$oldImage=$_POST['oldImage'];	
		}
		unset($_POST['oldImage']);
		/* IMAGE SECTION END */ 	
		echo $status= $this->pagesmodel->updateDataProcessImage($_POST,$id,$oldImage);
		}
		exit;
	}
	/* DATAPROCESS WITH IMAGE END */
	public function viewData()
	{	
		$uri = $this->request->getUri();
		$id = $this->request->getPost('id'); 
		$path = $uri->getSegment(2);
		$data['edit']=$this->pagesmodel->editData($id);
		echo view($uri->getSegment(1).'/'.$path.'/viewform',$data);
	}
	public function deleteAll()
	{	
		$id = $this->request->getPost('id');
		echo $this->pagesmodel->deleteAll($id);
	}
	public function deleteDataProcess()
	{   
		$id = $this->request->getPost('deleteId');
		echo $this->pagesmodel->deleteDataProcess($id);
		//echo $this->PagesModel->deleteDataProcessImage($id);
	}
	
	public function checkDuplicatePagename()
	{	
		$id = $this->request->getPost('id');
		$name = $this->request->getPost('name');
		if(empty($id)){
		$result = $this->pagesmodel->checkDuplicatePagename($name);
		}else{
		$result = $this->pagesmodel->checkDuplicatePagenameEdit($id,$name);
		}
		if($result > 0){ echo 1;}
		else{echo 0;}
	}

}
