<?php

namespace App\Controllers\user;

use App\Models\UserModel;

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


class ScheduleController extends BaseController
{
    public function index()
    {



        $userModel = new UserModel();

        $kontakModel = new KontakModel();
        $agendaModel = new AgendaModel();
        $profileModel = new ProfileModel();


        // Ambil data
        $user = $userModel->findAll();
        $profileData = $profileModel->first();
        $kontakData = $kontakModel->first();
        $agendaData = $agendaModel->orderBy('id_agenda', 'DESC')->findAll();
        // Tampilkan view dengan data profile dan user
        return view('user/schedule/index', [
            'profileData' => $profileData,
            'kontakData' => $kontakData,
            'agendaData' => $agendaData,
            'user' => $user,


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

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

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



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