<?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 = '';

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("INSERT INTO socket_types (name, manufacturer, generation) VALUES (?, ?, ?)");
            $stmt->execute([$name, $manufacturer, $generation]);
            
            $_SESSION['success'] = 'Socket type added successfully!';
            
            // Redirect menggunakan header setelah eksekusi berhasil
            header('Location: index.php');
            exit;
        } catch (PDOException $e) {
            $error = 'Failed to add socket type. Make sure the socket name is unique.';
        }
    }
}
?>
<!-- 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>Add 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">Add New 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($_POST['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 (isset($_POST['manufacturer']) && $_POST['manufacturer'] === 'AMD') ? 'selected' : ''; ?>>AMD</option>
                                <option value="Intel" <?php echo (isset($_POST['manufacturer']) && $_POST['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($_POST['generation'] ?? ''); ?>"
                                   placeholder="e.g., Ryzen 1000-5000 series">
                        </div>
                        <button type="submit" class="btn btn-primary">Save</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
?>