<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Menu extends CI_Controller {

	/**
	 * 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() {
		parent::__construct();
		$this->load->library(array('session'));
		$this->load->helper(array('url'));
		$this->load->model('MenuModel');
		if(!$this->session->userdata('username')){
			redirect("admin");
		}
	}
	public function index()
	{	
		$path = $this->uri->segment(2);
		$heading="Menu";
		/* URI SEGMENT FOR AJAX */
		$ajax_url = $this->uri->segment(1).'/'.$this->uri->segment(2);
		/* URI SEGMENT FOR AJAX END */
		$data['heading']=$heading;
		$data['ajax_url']=$ajax_url;
		$data['base_packages_url']  = base_url() . $this->uri->segment(1).'/'.$path.'/getData';
		$this->load->view($this->uri->segment(1).'/includes/header',$data);
		$this->load->view($this->uri->segment(1).'/includes/sidebar');
		$this->load->view($this->uri->segment(1).'/'.$path.'/page',$data);
		$this->load->view($this->uri->segment(1).'/includes/footer');
		$this->load->view($this->uri->segment(1).'/'.$path.'/scripts',$data);
		$this->load->view($this->uri->segment(1).'/includes/script_validation');
	}
	public function getData()
    {
        return $this->MenuModel->getData();
    }
	public function addForm()
	{   		
		$id = $this->input->post('id');
		$path = $this->uri->segment(2);
		$data['pages']=$this->MenuModel->getPages();
		$data['edit']=$this->MenuModel->editData($id);
		$this->load->view($this->uri->segment(1).'/'.$path.'/addform',$data);
		$this->load->view($this->uri->segment(1).'/includes/imagepreview');
	}
	public function addFormPage()
	{   		
		$id = $this->uri->segment(4);
		$path = $this->uri->segment(2);
		$data['edit']=$this->MenuModel->editData($id);
		$data['heading']=ucfirst($path);
		/* URI SEGMENT FOR AJAX */
		$ajax_url = $this->uri->segment(1).'/'.$this->uri->segment(2);
		/* URI SEGMENT FOR AJAX END */
		$data['ajax_url']=$ajax_url;
		$this->load->view($this->uri->segment(1).'/includes/header',$data);
		$this->load->view($this->uri->segment(1).'/includes/sidebar');
		$this->load->view('admin/'.$path.'/addFormPage');
		$this->load->view($this->uri->segment(1).'/includes/footer');
		$this->load->view($this->uri->segment(1).'/'.$path.'/scripts',$data);
		$this->load->view($this->uri->segment(1).'/includes/script_validation');
		$this->load->view($this->uri->segment(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,$_FILES['js'],$page_slug);
		}
	}
	/* 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->input->post('id'); 
		$path = $this->uri->segment(2);
		$data['edit']=$this->MenuModel->editData($id);
		$this->load->view($this->uri->segment(1).'/'.$path.'/viewform',$data);
	}
	public function deleteAll()
	{	
		$id = $this->input->post('id');
		echo $this->MenuModel->deleteAll($id);
	}
	public function deleteDataProcess()
	{   
		$id = $this->input->post('deleteId');
		echo $this->MenuModel->deleteDataProcess($id);
		//echo $this->MenuModel->deleteDataProcessImage($id);
	}
	public function deleteJS()
	{   
		$id = $this->input->post('id');
		//echo $this->MenuModel->deleteDataProcess($id);
		echo $this->MenuModel->deleteJS($id);
	}
	
	public function checkDuplicatePagename()
	{	
		$id = $this->input->post('id');
		$name = $this->input->post('name');
		if(empty($id)){
		$result = $this->MenuModel->checkDuplicatePagename($name);
		}else{
		$result = $this->MenuModel->checkDuplicatePagenameEdit($id,$name);
		}
		if($result > 0){ echo 1;}
		else{echo 0;}
	}

}
