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

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

class Menu extends BaseController {
protected $session;
protected $menumodel;
	
	/**
	 * 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->menumodel = new MenuModel();
		if(!$this->session->get('username')){
		header('Location: '.base_url().'admin');
		exit();
		}
	}
	public function index()
	{	
		$uri = $this->request->getUri();
		$path = $uri->getSegment(2);
		$heading="Menu";
		/* 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->menumodel->getData();
    }
	public function addForm()
	{   		
		$uri = $this->request->getUri();
		$id = $this->request->getPost('id');
		$path = $uri->getSegment(2);
		$data['pages']=$this->menumodel->getPages();
		$data['edit']=$this->menumodel->editData($id);
		echo view($uri->getSegment(1).'/'.$path.'/addform',$data);
		echo view($uri->getSegment(1).'/includes/imagepreview');
	}
	public function addFormPage()
	{   		
		$id = $uri->getSegment(4);
		$path = $uri->getSegment(2);
		$data['edit']=$this->menumodel->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=$_POST['page'];
		$page_slug=strtolower(str_replace(" ","-",$_POST['page']));
		$menu_order=$_POST['menu_order'];
		
		$data=array("page_name"=>$page,"page_slug"=>$page_slug,"menu_order"=>$menu_order);
		if(empty($id)){
		echo $status= $this->menumodel->addDataProcess($data);
		}else{
		 echo $status= $this->menumodel->updateDataProcess($data,$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->menumodel->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->menumodel->updateDataProcessImage($_POST,$id,$oldImage);
		}
		exit;
	}
	/* DATAPROCESS WITH IMAGE END */
	public function viewData()
	{	
		$id = $this->request->getPost('id'); 
		$path = $uri->getSegment(2);
		$data['edit']=$this->menumodel->editData($id);
		echo view($uri->getSegment(1).'/'.$path.'/viewform',$data);
	}
	public function deleteAll()
	{	
		$id = $this->request->getPost('id');
		echo $this->menumodel->deleteAll($id);
	}
	public function deleteDataProcess()
	{   
		$id = $this->request->getPost('deleteId');
		echo $this->menumodel->deleteDataProcess($id);
		//echo $this->menumodel->deleteDataProcessImage($id);
	}
	public function deleteJS()
	{   
		$id = $this->request->getPost('id');
		//echo $this->menumodel->deleteDataProcess($id);
		echo $this->menumodel->deleteJS($id);
	}
	
	public function checkDuplicatePagename()
	{	
		$id = $this->request->getPost('id');
		$name = $this->request->getPost('name');
		if(empty($id)){
		$result = $this->menumodel->checkDuplicatePagename($name);
		}else{
		$result = $this->menumodel->checkDuplicatePagenameEdit($id,$name);
		}
		if($result > 0){ echo 1;}
		else{echo 0;}
	}

}
