<?php
ob_start(); // Mulai output buffering di awal script


require_once '../../../config/database.php';
require_once '../../../config/auth.php';
require_once '../../../includes/header.php';
require_once '../../../includes/sidebar.php';

requireRole('super_admin');

$db = getDB();
$error = '';
$socket = null;

$id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT) ?: 0;
if (!$id) {
    header('Location: index.php');
    exit;
}

// Get socket data
$stmt = $db->prepare("SELECT * FROM socket_types WHERE id = ?");
$stmt->execute([$id]);
$socket = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$socket) {
    header('Location: index.php');
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = $_POST['name'] ?? '';
    $manufacturer = $_POST['manufacturer'] ?? '';
    $generation = $_POST['generation'] ?? '';
    
    if (empty($name) || empty($manufacturer)) {
        $error = 'Name and manufacturer are required!';
    } else {
        try {
            $stmt = $db->prepare("UPDATE socket_types SET name = ?, manufacturer = ?, generation = ? WHERE id = ?");
            $stmt->execute([$name, $manufacturer, $generation, $id]);
            
            $_SESSION['success'] = 'Socket type updated successfully!';
            header('Location: index.php');
            exit;
        } catch (PDOException $e) {
            $error = 'Failed to update socket type. Make sure the socket name is unique.';
            // Logging the actual error for debugging
            error_log('Socket update error: ' . $e->getMessage());
        }
    }
}
?>

<!-- Content Wrapper -->
<div class="content-wrapper">
    <!-- Content Header -->
    <section class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1>Edit Socket Type</h1>
                </div>
            </div>
        </div>
    </section>

    <!-- Main content -->
    <section class="content">
        <div class="container-fluid">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">Edit Socket Type</h3>
                </div>
                <div class="card-body">
                    <?php if ($error): ?>
                        <div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
                    <?php endif; ?>

                    <form method="POST">
                        <div class="form-group">
                            <label for="name">Socket Name</label>
                            <input type="text" class="form-control" id="name" name="name" required
                                   value="<?php echo htmlspecialchars($socket['name']); ?>">
                        </div>
                        
                        <div class="form-group">
                            <label for="manufacturer">Manufacturer</label>
                            <select class="form-control" id="manufacturer" name="manufacturer" required>
                                <option value="">Select Manufacturer</option>
                                <option value="AMD" <?php echo $socket['manufacturer'] === 'AMD' ? 'selected' : ''; ?>>AMD</option>
                                <option value="Intel" <?php echo $socket['manufacturer'] === 'Intel' ? 'selected' : ''; ?>>Intel</option>
                            </select>
                        </div>
                        
                        <div class="form-group">
                            <label for="generation">Generation</label>
                            <input type="text" class="form-control" id="generation" name="generation"
                                   value="<?php echo htmlspecialchars($socket['generation']); ?>"
                                   placeholder="e.g., Ryzen 1000-5000 series">
                        </div>

                        <button type="submit" class="btn btn-primary">Update</button>
                        <a href="index.php" class="btn btn-secondary">Cancel</a>
                    </form>
                </div>
            </div>
        </div>
    </section>
</div>

<?php 
require_once '../../../includes/footer.php';
ob_end_flush(); // Akhiri dan kirim output buffering
?>