<?php
require_once __DIR__ . '/navbar.php';
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}
$role = $_SESSION['role'] ?? 'other';
$db_path = __DIR__ . '/../config/db.php';
$pdo = require $db_path;
$formats = $pdo->query('SELECT * FROM formats')->fetchAll();
?><!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>OOH Management</title>
    <link rel="icon" type="image/x-icon" href="../ooh_project/favicon.ico">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
      body, html, .container, .card, .modal-content, .table, .form-control, .btn, .modal-title, .fw-bold, .text-primary, .text-light {
        font-family: 'Arial Unicode MS', Arial, Helvetica, sans-serif !important;
      }
    </style>
</head>
<body>
<div class="container mt-5">
    <h2>Strategic Planning</h2>
    <?php if ($role === 'admin'): ?>
    <form method="post" action="../handler/format_handler.php" class="mb-4">
        <input type="hidden" name="action" value="add">
        <div class="row mb-2">
            <div class="col">
                <input type="text" name="supplier" class="form-control" placeholder="Supplier" required>
            </div>
            <div class="col">
                <input type="text" name="type" class="form-control" placeholder="Type" required>
            </div>
            <div class="col">
                <input type="text" name="format" class="form-control" placeholder="Format" required>
            </div>
        </div>
        <button type="submit" class="btn btn-primary">Add Format</button>
    </form>
    <?php endif; ?>

    <?php if ($role === 'admin' && isset($_GET['edit'])):
        $edit_id = intval($_GET['edit']);
        $edit_stmt = $pdo->prepare('SELECT * FROM formats WHERE id = ?');
        $edit_stmt->execute([$edit_id]);
        $edit_format = $edit_stmt->fetch();
        if ($edit_format): ?>
    <form method="post" action="../handler/format_handler.php" class="mb-4">
        <input type="hidden" name="action" value="edit">
        <input type="hidden" name="id" value="<?= $edit_format['id'] ?>">
        <div class="row mb-2">
            <div class="col">
                <input type="text" name="supplier" class="form-control" value="<?= htmlspecialchars($edit_format['supplier']) ?>" required>
            </div>
            <div class="col">
                <input type="text" name="type" class="form-control" value="<?= htmlspecialchars($edit_format['type']) ?>" required>
            </div>
            <div class="col">
                <input type="text" name="format" class="form-control" value="<?= htmlspecialchars($edit_format['format']) ?>" required>
            </div>
        </div>
        <button type="submit" class="btn btn-success">Update Format</button>
        <a href="strategic_planning.php" class="btn btn-secondary">Cancel</a>
    </form>
    <?php endif; endif; ?>
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>ID</th>
                <th>Supplier</th>
                <th>Type</th>
                <th>Format</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($formats as $f): ?>
            <tr>
                <td><?= $f['id'] ?></td>
                <td><?= htmlspecialchars($f['supplier']) ?></td>
                <td><?= htmlspecialchars($f['type']) ?></td>
                <td><?= htmlspecialchars($f['format']) ?></td>
                <td>
                    <?php if ($role === 'admin'): ?>
                    <a href="strategic_planning.php?edit=<?= $f['id'] ?>" class="btn btn-sm btn-info">Edit</a>
                    <form method="post" action="../handler/format_handler.php" style="display:inline-block">
                        <input type="hidden" name="action" value="delete">
                        <input type="hidden" name="id" value="<?= $f['id'] ?>">
                        <button type="submit" class="btn btn-danger btn-sm">Delete</button>
                    </form>
                    <?php endif; ?>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>
</body>
</html>
