<?php

namespace App\Controllers\user;

use App\Models\UserModel;
use App\Models\BeritaModel;
use App\Models\GalleryModel;
use App\Models\KontakModel;
use App\Models\ProfileModel;
use App\Models\AgendaModel;
use App\Controllers\BaseController;


class SearchController extends BaseController
{
    public function index()
    {
        $keyword = $this->request->getVar('keyword');


        $userModel = new UserModel();

        $kontakModel = new KontakModel();
        $beritaModel = new BeritaModel();
        $galleryModel = new GalleryModel();
        $profileModel = new ProfileModel();
        $agendaModel = new AgendaModel();

        // Ambil data
        $user = $userModel->findAll();
        $profileData = $profileModel->first();
        $kontakData = $kontakModel->first();
        if (empty($keyword)) {
            return view('user/search/index', [
                'profileData' => $profileData,
                'kontakData' =>  $kontakData,
                'beritaHasil' => [],
                'agendaHasil' => [],
                'galleryHasil' => [],
                'user' => $user,
                'noData' => true,
            ]);
        }

        $beritaHasil = $beritaModel->db->table('berita')
            ->select('*')
            ->like('judul', $keyword)
            ->get()
            ->getResult();

        $galleryHasil = $galleryModel->db->table('galeri')
            ->select('*')
            ->like('judul', $keyword)
            ->get()
            ->getResult();

        $agendaHasil = $agendaModel->db->table('agenda')
            ->select('*')
            ->like('tema', $keyword)
            ->get()
            ->getResult();
        // Tampilkan view dengan data profile dan user
        return view('user/search/index', [
            'profileData' => $profileData,
            'kontakData' => $kontakData,
            'beritaHasil' => $beritaHasil,
            'agendaHasil' => $agendaHasil,
            'galleryHasil' => $galleryHasil,
            'user' => $user,
            'noData' => false,

        ]);
    }
}