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

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

class Header extends BaseController {
	protected $session;
	protected $headermodel;
	
	/**
	 * 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->headermodel = new HeaderModel();
		if(!$this->session->get('username')){
		header('Location: '.base_url().'admin');
		exit();
		}
	}
	public function index()
	{	
		
		$uri = $this->request->getUri();
		$path = $uri->getSegment(2);
		
		$heading="Header";
		/* 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->headermodel->getData();
    }
	public function addForm()
	{   		
		$uri = $this->request->getUri();
		$id = $this->request->getPost('id');
		$path = $uri->getSegment(2);
		$data['pages']=$this->headermodel->getPages();
		$data['headerdetails']=$this->headermodel->getHeaderDetails($id);
		$data['headerimagedetails']=$this->headermodel->getHeaderImagedetails($id);
		$data['edit']=$this->headermodel->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->headermodel->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']));
		$header_title=$_POST['header_title'];
		$meta_content=$_POST['meta_content'];
		$external_css=$_POST['external_css'];
		$header_content=$_POST['header_content'];
		$favicon_content=$_POST['favicon_content'];
		$cannonical_tag=$_POST['cannonical_tag'];
		$social_media_tag=$_POST['social_media_tag'];
		$json_script=$_POST['json_script'];
		$author_image_attribute=$_POST['author_image_attribute'];
		
		$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,"favicon_content"=>$favicon_content,"cannonical_tag"=>$cannonical_tag,"social_media_tag"=>$social_media_tag,"json_script"=>$json_script,"author_image_attribute"=>$author_image_attribute,);
			}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,"favicon_content"=>$favicon_content,"cannonical_tag"=>$cannonical_tag,"social_media_tag"=>$social_media_tag,"json_script"=>$json_script,"author_image_attribute"=>$author_image_attribute,);
			}
			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()
	{	
		$uri = $this->request->getUri();
		$id = $this->request->getPost('id'); 
		$path = $uri->getSegment(2);
		$data['edit']=$this->headermodel->editData($id);
		echo view($uri->getSegment(1).'/'.$path.'/viewform',$data);
	}
	public function deleteAll()
	{	
		$id = $this->request->getPost('id');
		echo $this->headermodel->deleteAll($id);
	}
	public function deleteDataProcess()
	{   
		$id = $this->request->getPost('deleteId');
		//echo $this->headermodel->deleteDataProcess($id);
		echo $this->headermodel->deleteDataProcessImage($id);
	}
	public function deleteCSS()
	{   
		$id = $this->request->getPost('id');
		//echo $this->HeaderModel->deleteDataProcess($id);
		echo $this->headermodel->deleteCSS($id);
	}
	
	public function checkDuplicatePagename()
	{	
		$id = $this->request->getPost('id');
		$name = $this->request->getPost('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->request->getPost('id');
		//echo $this->ContentModel->deleteDataProcess($id);
		echo $this->headermodel->deleteImage($id);
	}
	

}
