<?php

namespace App\Controllers\user;

use App\Models\UserModel;

use App\Models\KontakModel;
use App\Models\ProfileModel;
use App\Models\BeritaModel;
use App\Controllers\BaseController;


class NewsController extends BaseController
{
    public function index()
    {



        $userModel = new UserModel();

        $kontakModel = new KontakModel();
        $beritaModel = new BeritaModel();
        $profileModel = new ProfileModel();


        // Ambil data
        $user = $userModel->findAll();
        $profileData = $profileModel->first();
        $kontakData = $kontakModel->first();
        $beritaData = $beritaModel->orderBy('id_berita', 'DESC')->findAll();
        // Tampilkan view dengan data profile dan user
        return view('user/news/index', [
            'profileData' => $profileData,
            'kontakData' => $kontakData,
            'beritaData' => $beritaData,
            'user' => $user,


        ]);
    }
    public function detail($id)
    {
        if (empty($id) || !is_numeric($id)) {
            session()->setFlashdata('error', 'Data tidak valid');
            return redirect()->to(base_url('news'))->with('error', 'ID tidak valid.');
        }
        $userModel = new UserModel();
        $kontakModel = new KontakModel();
        $beritaModel = new BeritaModel();
        $profileModel = new ProfileModel();

        // Ambil data guru berdasarkan ID
        $user = $userModel->findAll();
        $profileData = $profileModel->first();
        $kontakData = $kontakModel->first();
        $beritaData = $beritaModel->find($id);

        if (!$beritaData) {
            session()->setFlashdata('error', 'Data tidak ditemukan');
            return redirect()->to(base_url('news'))->with('error', 'Berita tidak ditemukan.');
        }



        return view('user/news/detail', [
            'profileData' => $profileData,
            'kontakData' => $kontakData,
            'beritaData' => $beritaData,
            'user' => $user,
        ]);
    }
}