<?php
// Format CRUD handler
require_once __DIR__ . '/../config/db.php';

// Example: handle add/edit/delete based on $_POST['action']
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';
    if ($action === 'add') {
        $supplier = $_POST['supplier'] ?? '';
        $type = $_POST['type'] ?? '';
        $format = $_POST['format'] ?? '';
        if ($supplier && $type && $format) {
            $stmt = $pdo->prepare('INSERT INTO formats (supplier, type, format) VALUES (?, ?, ?)');
            $stmt->execute([$supplier, $type, $format]);
        }
        header('Location: ../modules/strategic_planning.php');
        exit;
    } elseif ($action === 'delete') {
        $id = $_POST['id'] ?? null;
        if ($id) {
            $stmt = $pdo->prepare('DELETE FROM formats WHERE id = ?');
            $stmt->execute([$id]);
        }
        header('Location: ../modules/strategic_planning.php');
        exit;
    } elseif ($action === 'edit') {
        $id = $_POST['id'] ?? null;
        $supplier = $_POST['supplier'] ?? '';
        $type = $_POST['type'] ?? '';
        $format = $_POST['format'] ?? '';
        if ($id && $supplier && $type && $format) {
            $stmt = $pdo->prepare('UPDATE formats SET supplier = ?, type = ?, format = ? WHERE id = ?');
            $stmt->execute([$supplier, $type, $format, $id]);
        }
        header('Location: ../modules/strategic_planning.php');
        exit;
    }
}
?>
