<?php
namespace App\Controllers;

use App\Core\Controller;
use App\Core\Database;

class CrudController extends Controller
{
    public function index()
    {
        $table = $_GET['table'] ?? '';
        if(!$table) die("Table missing");

        $db = Database::getInstance();
        $rows = $db->query("SELECT * FROM $table")->fetchAll();

        ob_start();
        require BASE_PATH.'/app/views/crud/index.php';
        $content = ob_get_clean();
        require BASE_PATH.'/app/views/layouts/admin_layout.php';
    }

    public function delete()
    {
        $table = $_GET['table'];
        $id = $_GET['id'];

        $db = Database::getInstance();
        $db->prepare("DELETE FROM $table WHERE id=?")->execute([$id]);

        header("Location: ".base_path("crud?table=$table"));
        exit;
    }
}
