<?php

namespace App\Controllers\Front;

use CodeIgniter\Controller;
use App\Controllers\BaseController;


class Home extends BaseController
{
    public function initController($request, $response, $logger)
    {
        parent::initController($request, $response, $logger);
        
    }
 

    public function index()
    {
        $title = 'UGA REDD+ Registry Data Management System (DMS)';
        $description = 'UGA REDD+ Registry Data Management System (DMS), the official platform for tracking and managing REDD+ projects and emission reductions in Uganda. This system serves as a vital tool to ensure transparency, prevent double counting, and facilitate results-based payments for forest conservation efforts.';
        $keywords = 'Uganda REDD+, REDD Registry, Carbon Credits, Forest Conservation, Emission Reductions, Data Management System, DMS, Climate Change, UN-REDD, Deforestation Monitoring, NFMS, Carbon Finance';

        $post = $this->postsModel->showSinglePost(2);
        $postId = $post->id; // Dams post ID

        //Getting Hits & Saving them.
        $id = $post->id;
        $current_hits = $post->hits;
        $new_hits = $current_hits + 1;
        $this->postsModel->update($id, ['hits'=>$new_hits]);

        // Get blocks ordered
        $blocks = $this->postblocksModel
            ->where('post_id', $postId)
            ->orderBy('ordering', 'ASC')
            ->findAll();

        // Group by block_type for easier access
        $grouped = [];
        foreach ($blocks as $block) {
            $grouped[$block['block_type']][] = $block;
        }

        // For blocks with children
        $stats = isset($grouped['stats'][0]) 
            ? $this->postblockitemsModel->where('block_id', $grouped['stats'][0]['id'])->findAll()
            : [];

        $cards = isset($grouped['cards'][0]) 
            ? $this->postblockitemsModel->where('block_id', $grouped['cards'][0]['id'])->findAll()
            : [];
        $keyAreas = isset($grouped['cards'][1]) 
            ? $this->postblockitemsModel->where('block_id', $grouped['cards'][1]['id'])->findAll()
            : [];

        $gallery = isset($grouped['gallery'][0]) 
            ? $this->postblockitemsModel->where('block_id', $grouped['gallery'][0]['id'])->findAll()
            : [];

        // Pass to view
        $data = [
            'title'             => $title,
            'description'       => $description,
            'keywords'          => $keywords,
            'post'              => $post,
            'blocks'            => $grouped,
            'stats'             => $stats,
            'cards'             => $cards,
            'keyAreas'          => $keyAreas,
            'gallery'           => $gallery
        ];
        
        //$data['partners'] = $this->partnersModel->getAll();
        log_hit($this->request, $this); //logs hits on this website page.
        return view('website/pages/home', $data);
    }

    //About Us Page
	public function aboutUsPage()
	{	
		$post = $this->postsModel->showSinglePost(3);
        $postId = $post->id; // Dams post ID

        //Meta info for pages
        $title = $post->title.'UGA REDD+ Registry: Data Management System (DMS) ';
        $description = $post->metadesc;
        $keywords = $post->metakey;

        //Getting Hits & Saving them.
        $id = $post->id;
        $current_hits = $post->hits;
        $new_hits = $current_hits + 1;
        $this->postsModel->update($id, ['hits'=>$new_hits]);

        // Get blocks ordered
        $blocks = $this->postblocksModel
            ->where('post_id', $postId)
            ->orderBy('ordering', 'ASC')
            ->findAll();

        // Group by block_type for easier access
        $grouped = [];
        foreach ($blocks as $block) {
            $grouped[$block['block_type']][] = $block;
        }

        // For blocks with children
        $stats = isset($grouped['stats'][0]) 
            ? $this->postblockitemsModel->where('block_id', $grouped['stats'][0]['id'])->findAll()
            : [];

        $cards = isset($grouped['cards'][0]) 
            ? $this->postblockitemsModel->where('block_id', $grouped['cards'][0]['id'])->findAll()
            : [];
        $keyAreas = isset($grouped['cards'][1]) 
            ? $this->postblockitemsModel->where('block_id', $grouped['cards'][1]['id'])->findAll()
            : [];

        $gallery = isset($grouped['gallery'][0]) 
            ? $this->postblockitemsModel->where('block_id', $grouped['gallery'][0]['id'])->findAll()
            : [];

        // Pass to view
        $data = [
            'title'             => $title,
            'description'       => $description,
            'keywords'          => $keywords,
            'post'              => $post,
            'blocks'            => $grouped,
            'stats'             => $stats,
            'cards'             => $cards,
            'keyAreas'          => $keyAreas,
            'gallery'           => $gallery,
        ];
        return view('website/pages/about_template', $data);
	}
	
	public function guidelinesPage()
	{	
		$post = $this->postsModel->showSinglePost(4);
        $postId = $post->id;

        //Meta info for pages
        $title = $post->title.'UGA REDD+ Registry: Data Management System (DMS) ';
        $description = $post->metadesc;
        $keywords = $post->metakey;

        //Getting Hits & Saving them.
        $id = $post->id;
        $current_hits = $post->hits;
        $new_hits = $current_hits + 1;
        $this->postsModel->update($id, ['hits'=>$new_hits]);

        // Get blocks ordered
        $blocks = $this->postblocksModel
            ->where('post_id', $postId)
            ->orderBy('ordering', 'ASC')
            ->findAll();

        // Group by block_type for easier access
        $grouped = [];
        foreach ($blocks as $block) {
            $grouped[$block['block_type']][] = $block;
        }

        // For blocks with children
        $stats = isset($grouped['stats'][0]) 
            ? $this->postblockitemsModel->where('block_id', $grouped['stats'][0]['id'])->findAll()
            : [];

        $cards = isset($grouped['cards'][0]) 
            ? $this->postblockitemsModel->where('block_id', $grouped['cards'][0]['id'])->findAll()
            : [];
        $keyAreas = isset($grouped['cards'][1]) 
            ? $this->postblockitemsModel->where('block_id', $grouped['cards'][1]['id'])->findAll()
            : [];

        $gallery = isset($grouped['gallery'][0]) 
            ? $this->postblockitemsModel->where('block_id', $grouped['gallery'][0]['id'])->findAll()
            : [];

        // Pass to view
        $data = [
            'title'             => $title,
            'description'       => $description,
            'keywords'          => $keywords,
            'post'              => $post,
            'blocks'            => $grouped,
            'stats'             => $stats,
            'cards'             => $cards,
            'keyAreas'          => $keyAreas,
            'gallery'           => $gallery,
        ];
        return view('website/pages/registration-guidelines', $data);
	}

    
    public function registerPage()
    {
        $post = $this->postsModel->showSinglePost(5);
        $postId = $post->id; // Dams post ID

        //Meta info for pages
        $title = $post->title.'UGA REDD+ Registry: Data Management System (DMS) ';
        $description = $post->metadesc;
        $keywords = $post->metakey;

        //Getting Hits & Saving them.
        $id = $post->id;
        $current_hits = $post->hits;
        $new_hits = $current_hits + 1;
        $this->postsModel->update($id, ['hits'=>$new_hits]);

        // Get blocks ordered
        $blocks = $this->postblocksModel
            ->where('post_id', $postId)
            ->orderBy('ordering', 'ASC')
            ->findAll();

        // Group by block_type for easier access
        $grouped = [];
        foreach ($blocks as $block) {
            $grouped[$block['block_type']][] = $block;
        }

        // For blocks with children
        $stats = isset($grouped['stats'][0]) 
            ? $this->postblockitemsModel->where('block_id', $grouped['stats'][0]['id'])->findAll()
            : [];

        $cards = isset($grouped['cards'][0]) 
            ? $this->postblockitemsModel->where('block_id', $grouped['cards'][0]['id'])->findAll()
            : [];
        $keyAreas = isset($grouped['cards'][1]) 
            ? $this->postblockitemsModel->where('block_id', $grouped['cards'][1]['id'])->findAll()
            : [];

        $gallery = isset($grouped['gallery'][0]) 
            ? $this->postblockitemsModel->where('block_id', $grouped['gallery'][0]['id'])->findAll()
            : [];

        // Pass to view
        $data = [
            'title'             => $title,
            'description'       => $description,
            'keywords'          => $keywords,
            'post'              => $post,
            'blocks'            => $grouped,
            'stats'             => $stats,
            'cards'             => $cards,
            'keyAreas'          => $keyAreas,
            'gallery'           => $gallery
        ];
        
        $data['activities']       = $this->activitiesModel->getAll();
        $data['carbon_pools']     = $this->carbonpoolsModel->getAll();
        $data['entity_types']     = $this->entitytypesModel->getAll();
        $data['methodologies']    = $this->methodologiesModel->getAll();
        $data['gases']            = $this->gasesModel->getAll();
        $data['forests']          = $this->forestsModel->getAll();
        $data['types']            = $this->typesModel->getAll();
        $data['statuses']         = $this->statusModel->getAll();
        $data['countries']        = $this->countryModel->getAll();
        $data['districts']        = $this->districtModel->findAll();
		$data['currencies']	      = $this->currencyModel->getAll();
        
        log_hit($this->request, $this); //logs hits on this website page.
        return view('website/pages/register', $data);
    }
    
    public function contactUs()
    {
        $post = $this->postsModel->showSinglePost(6);
        $postId = $post->id;

        //Meta info for pages
        $title = $post->title.'UGA REDD+ Registry: Data Management System (DMS) ';
        $description = $post->metadesc;
        $keywords = $post->metakey;

        //Getting Hits & Saving them.
        $id = $post->id;
        $current_hits = $post->hits;
        $new_hits = $current_hits + 1;
        $this->postsModel->update($id, ['hits'=>$new_hits]);

        // Get blocks ordered
        $blocks = $this->postblocksModel
            ->where('post_id', $postId)
            ->orderBy('ordering', 'ASC')
            ->findAll();

        // Group by block_type for easier access
        $grouped = [];
        foreach ($blocks as $block) {
            $grouped[$block['block_type']][] = $block;
        }

        // For blocks with children
        $stats = isset($grouped['stats'][0]) 
            ? $this->postblockitemsModel->where('block_id', $grouped['stats'][0]['id'])->findAll()
            : [];

        $cards = isset($grouped['cards'][0]) 
            ? $this->postblockitemsModel->where('block_id', $grouped['cards'][0]['id'])->findAll()
            : [];
        $keyAreas = isset($grouped['cards'][1]) 
            ? $this->postblockitemsModel->where('block_id', $grouped['cards'][1]['id'])->findAll()
            : [];

        $gallery = isset($grouped['gallery'][0]) 
            ? $this->postblockitemsModel->where('block_id', $grouped['gallery'][0]['id'])->findAll()
            : [];

        // Pass to view
        $data = [
            'title'             => $title,
            'description'       => $description,
            'keywords'          => $keywords,
            'post'              => $post,
            'blocks'            => $grouped,
            'stats'             => $stats,
            'cards'             => $cards,
            'keyAreas'          => $keyAreas,
            'gallery'           => $gallery,
        ];
        
        log_hit($this->request, $this);
        return view('website/pages/contact_template', $data);
    }
    
    public function policyPage($id = null)
    {
        //For Search Engine Optimization
        $seo = $this->postsModel->getSinglePost($id);
        $data['title'] = $seo->title.' - AgriMarket';
        $data['tagline']= "Uganda's agrobusiness marketplace";
        $data['description'] = $seo->metadesc;
        $data['keywords'] = $seo->metakey;
        $data['bcrumb'] = $seo->title;


        $data['lst']= $this->menusModel->CategoryList(1);
        $data['page']   = $this->postsModel->getSinglePost($id);
        log_hit($this->request, $this); //logs hits on this website page.

        return View('website/pages/policy', $data);
    }
}
