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

class Header 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('HeaderModel');
		if(!$this->session->userdata('username')){
			redirect("admin");
		}
	}
	public function index()
	{	
		$path = $this->uri->segment(2);
		$heading="Header";
		/* 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->HeaderModel->getData();
    }
	public function addForm()
	{   		
		$id = $this->input->post('id');
		$path = $this->uri->segment(2);
		$data['pages']=$this->HeaderModel->getPages();
		$data['headerdetails']=$this->HeaderModel->getHeaderDetails($id);
		$data['headerimagedetails']=$this->HeaderModel->getHeaderImagedetails($id);
		$data['edit']=$this->HeaderModel->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->HeaderModel->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']));
		$header_title=$_POST['header_title'];
		$meta_content=$_POST['meta_content'];
		$external_css=$_POST['external_css'];
		$header_content=$_POST['header_content'];
		
		$i=0;
		$favicons = '';
		$tmp_name=array();
			if (isset($_FILES['favicon'])) {
				$tmp_name=$_FILES['favicon']['tmp_name'][$i];
				//foreach ($_FILES['hotelfile']['tmp_name'] as $key => $tmp_name) {
					if ($_FILES['favicon']['error'][$i] == UPLOAD_ERR_OK) {
						// Generate a unique file name
						$uniqueName = basename($_FILES['favicon']['name'][$i]);
						$filePath = "pages/".$page_slug."/images/" . $uniqueName;
						if (move_uploaded_file($tmp_name, $filePath)) {
							$favicons .= $filePath . ',';
						} else {
							echo "Failed to move file: " . $_FILES['favicon']['name'][$i] . "<br>";
						}
					} else {
						echo "Error uploading Favicon: " . $_FILES['favicon']['error'][$i] . "<br>";
					}
				//} 
				$favicons = rtrim($favicons, ',');
			
			}else{
				$favicons="";
			}
		    /* IMAGE SECTION */ 	
			if(empty($favicons)){
			//$_POST['image']=$_POST['oldImage'];
			$oldImage="";
			$data=array("page"=>$page,"page_slug"=>$page_slug,"header_title"=>$header_title,"meta_content"=>$meta_content,"external_css"=>$external_css,"header_content"=>$header_content);
			}else{
			$oldImage=$_POST['oldImage'];
			$data=array("page"=>$page,"page_slug"=>$page_slug,"header_title"=>$header_title,"meta_content"=>$meta_content,"external_css"=>$external_css,"header_content"=>$header_content,"favicon"=>$favicons,);
			}
			unset($_POST['oldImage']);
			/* IMAGE SECTION END */
			
			//$data=array("page"=>$page,"page_slug"=>$page_slug,"header_title"=>$header_title,"meta_content"=>$meta_content,"external_css"=>$external_css,"favicon"=>$favicons,);
		if(!isset($_FILES['css'])){
			$_FILES['css']=array();
		}
		if(!isset($_FILES['image'])){
			$_FILES['image']=array();
		}
		if(empty($id)){
		 //echo $status= $this->HeaderModel->addDataProcess($data);
		 echo $status= $this->HeaderModel->addDataProcess($data,$_FILES['css'],$_FILES['image'],$page_slug);
		}else{
		 echo $status= $this->HeaderModel->updateDataProcessImage($data,$id,$oldImage,$_FILES['css'],$_FILES['image'],$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->HeaderModel->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->HeaderModel->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->HeaderModel->editData($id);
		$this->load->view($this->uri->segment(1).'/'.$path.'/viewform',$data);
	}
	public function deleteAll()
	{	
		$id = $this->input->post('id');
		echo $this->HeaderModel->deleteAll($id);
	}
	public function deleteDataProcess()
	{   
		$id = $this->input->post('deleteId');
		//echo $this->HeaderModel->deleteDataProcess($id);
		echo $this->HeaderModel->deleteDataProcessImage($id);
	}
	public function deleteCSS()
	{   
		$id = $this->input->post('id');
		//echo $this->HeaderModel->deleteDataProcess($id);
		echo $this->HeaderModel->deleteCSS($id);
	}
	
	public function checkDuplicatePagename()
	{	
		$id = $this->input->post('id');
		$name = $this->input->post('name');
		if(empty($id)){
		$result = $this->HeaderModel->checkDuplicatePagename($name);
		}else{
		$result = $this->HeaderModel->checkDuplicatePagenameEdit($id,$name);
		}
		if($result > 0){ echo 1;}
		else{echo 0;}
	}
	public function deleteImage()
	{   
		$id = $this->input->post('id');
		//echo $this->ContentModel->deleteDataProcess($id);
		echo $this->HeaderModel->deleteImage($id);
	}
}
