<?php
require_once '../../../config/database.php';
require_once '../../../config/auth.php';
require_once '../../../includes/header.php';
require_once '../../../includes/sidebar.php';
requireRole(['admin', 'super_admin']);

$db = getDB();
$error = '';
$success = '';

$id = $_GET['id'] ?? 0;
if (!$id) {
    header('Location: index.php');
    exit;
}

// Ambil data socket
$sockets = $db->query("SELECT * FROM socket_types ORDER BY manufacturer, name")->fetchAll();

// Ambil data processor
$stmt = $db->prepare("SELECT p.*, pc.socket_id 
                     FROM processors p 
                     LEFT JOIN processor_compatibility pc ON pc.processor_id = p.id 
                     WHERE p.id = ?");
$stmt->execute([$id]);
$processor = $stmt->fetch();

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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = $_POST['name'] ?? '';
    $price = $_POST['price'] ?? 0;
    $performance = $_POST['performance'] ?? 0;
    $age = $_POST['age'] ?? 0;
    $socket_id = $_POST['socket_id'] ?? null;
    
    if (empty($name)) {
        $error = 'Nama processor harus diisi!';
    } elseif (!is_numeric($price) || $price <= 0) {
        $error = 'Harga harus berupa angka positif!';
    } elseif (!is_numeric($performance) || $performance < 1 || $performance > 10) {
        $error = 'Performance harus antara 1-10!';
    } elseif (!is_numeric($age) || $age < 0 || $age > 5) {
        $error = 'Age harus antara 0-5!';
    } elseif (empty($socket_id)) {
        $error = 'Socket harus dipilih!';
    } else {
        try {
            $db->beginTransaction();

            // Update processor
            $stmt = $db->prepare("UPDATE processors SET name = ?, price = ?, performance = ?, age = ? WHERE id = ?");
            $stmt->execute([$name, $price, $performance, $age, $id]);

            // Update socket compatibility
            $stmt = $db->prepare("
                INSERT INTO processor_compatibility (processor_id, socket_id) 
                VALUES (?, ?) 
                ON DUPLICATE KEY UPDATE socket_id = ?
            ");
            $stmt->execute([$id, $socket_id, $socket_id]);

            $db->commit();
            $success = 'Data processor berhasil diupdate!';
            
            // Redirect setelah 1 detik
           
        } catch (PDOException $e) {
            $db->rollBack();
            $error = 'Terjadi kesalahan saat mengupdate data!';
        }
    }
}
?>

<!-- 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 Processor</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">Form Edit Processor</h3>
                </div>
                <div class="card-body">
                    <?php if ($error): ?>
                        <div class="alert alert-danger"><?php echo $error; ?></div>
                    <?php endif; ?>
                    <?php if ($success): ?>
                        <div class="alert alert-success"><?php echo $success; ?></div>
                    <?php endif; ?>

                    <form method="POST">
                        <div class="form-group">
                            <label for="name">Nama Processor</label>
                            <input type="text" class="form-control" id="name" name="name" 
                                   value="<?php echo htmlspecialchars($processor['name']); ?>" required>
                        </div>

                        <div class="form-group">
                            <label for="socket_id">Socket</label>
                            <select class="form-control select2" id="socket_id" name="socket_id" required>
                                <option value="">Pilih Socket</option>
                                <?php foreach ($sockets as $socket): ?>
                                    <option value="<?php echo $socket['id']; ?>" 
                                            <?php echo ($processor['socket_id'] == $socket['id']) ? 'selected' : ''; ?>>
                                        <?php echo htmlspecialchars($socket['name'] . ' (' . $socket['manufacturer'] . ' - ' . $socket['generation'] . ')'); ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>

                        <div class="form-group">
                            <label for="price">Harga</label>
                            <input type="number" class="form-control" id="price" name="price" 
                                   value="<?php echo htmlspecialchars($processor['price']); ?>" min="0" required>
                        </div>

                        <div class="form-group">
                            <label for="performance">Performance (1-10)</label>
                            <input type="number" class="form-control" id="performance" name="performance" 
                                   value="<?php echo htmlspecialchars($processor['performance']); ?>"
                                   min="1" max="10" step="0.1" required>
                        </div>

                        <div class="form-group">
                            <label for="age">Age (0-5)</label>
                            <input type="number" class="form-control" id="age" name="age" 
                                   value="<?php echo htmlspecialchars($processor['age']); ?>"
                                   min="0" max="5" step="0.1" required>
                        </div>

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

<?php require_once '../../../includes/footer.php'; ?>
```