<?php
namespace App\Controllers;
use App\Models\FrontpageModel; // Import the model

class Page extends BaseController {

	/**
	 * 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->helper(array('url'));
		//$this->load->model('FrontpageModel');
	} */
	public function index()
	{
		$frontpagemodel = new FrontpageModel();
		$uri = $this->request->getUri();
		$page_slug = $uri->getSegment(1); // 'users'
		
		if($page_slug == ""){
			$page_slug = "home";
		}
		/* HEADER DATA */
		$getHeaderData=$frontpagemodel->getHeaderData($page_slug);
		//echo "<pre>";print_r($getHeaderData);exit;
		if(!empty($getHeaderData)){
		$header_id=$getHeaderData[0]->id;
		$data['csss']=$frontpagemodel->getHeaderDetailsData($header_id);
		$data['title']=$getHeaderData[0]->header_title;
		$data['meta_content']=$getHeaderData[0]->meta_content;
		$data['favicon']=base_url().$getHeaderData[0]->favicon;
		$data['external_css']=$getHeaderData[0]->external_css;
		$data['header_content']=$getHeaderData[0]->header_content;
		$data['favicon_content']=$getHeaderData[0]->favicon_content;
		$data['cannonical_tag']=$getHeaderData[0]->cannonical_tag;
		$data['social_media_tag']=$getHeaderData[0]->social_media_tag;
		$data['json_script']=$getHeaderData[0]->json_script;
		$data['author_image_attribute']=$getHeaderData[0]->author_image_attribute;
		}else{
		$data['csss']=array();
		$data['title']="";
		$data['meta_content']="";
		$data['favicon']="";
		$data['external_css']="";
		$data['header_content']="";
		$data['favicon_content']="";
		$data['cannonical_tag']="";
		$data['social_media_tag']="";
		$data['json_script']="";
		$data['author_image_attribute']="";
		}
		/* FOOTER DATA */
		$getFooterData=$frontpagemodel->getFooterData($page_slug);
		if(!empty($getFooterData)){
		$footer_id=$getFooterData[0]->id;
		$data['jss']=$frontpagemodel->getFooterDetailsData($footer_id);
		$data['footer_content']=$getFooterData[0]->footer_content;
		$data['external_js']=$getFooterData[0]->external_js;
		//echo "<pre>";print_r($data['css']);exit;
		}else{
		$data['jss']=array();
		$data['footer_content']="";
		$data['external_js']="";
		}	
		/* PAGE DATA */
		$getPageData=$frontpagemodel->getPageData($page_slug);
		if(!empty($getPageData)){
		$page_id=$getPageData[0]->id;
		$data['images']=$frontpagemodel->getPageDetailsData($page_id);
		$data['page_content']=$getPageData[0]->page_content;
		//echo "<pre>";print_r($data['page_content']);exit;
		}else{
		$data['images']=array();
		$data['page_content']="";
		}	
		/* MENU DATA */
		$getMenuData=$frontpagemodel->getMenuData();
		$data['menu']=$getMenuData;
		//echo "<pre>";print_r($getMenuData);exit;

		echo view('page/header',$data);
		echo view('page/content',$data);
		echo view('page/footer',$data);
	}
}
