<?php

namespace App\Controllers\Admin;

use App\Controllers\BaseController;

class Komentar extends BaseController
{
    protected $db;

    public function __construct()
    {
        $this->db = \Config\Database::connect();
    }

    public function index()
    {
        // Mengambil data komentar beserta judul beritanya
        $builder = $this->db->table('komentar k');
        $builder->select('k.*, b.judul as judul_berita');
        $builder->join('berita b', 'b.id_berita = k.id_berita');
        $builder->orderBy('k.created_at', 'DESC');
        
        $data = [
            'title'    => 'Manajemen Komentar Berita',
            'komentar' => $builder->get()->getResultArray()
        ];

        return view('admin/komentar/index', $data);
    }

    public function setujui($id)
    {
        $this->db->table('komentar')->where('id_komentar', $id)->update(['status' => 'publish']);
        return redirect()->back()->with('success', 'Komentar berhasil diterbitkan ke website.');
    }

    public function hapus($id)
    {
        $this->db->table('komentar')->where('id_komentar', $id)->delete();
        return redirect()->back()->with('success', 'Komentar berhasil dihapus.');
    }
}