<?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']);

$id = $_GET['id'] ?? 0;
$db = getDB();

// Ambil data VGA
$stmt = $db->prepare("SELECT * FROM vga_cards WHERE id = ?");
$stmt->execute([$id]);
$vga = $stmt->fetch();

if (!$vga) {
    die('Data VGA tidak ditemukan!');
}

$error = '';
$success = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = $_POST['name'] ?? '';
    $price = $_POST['price'] ?? 0;
    $vram_capacity = $_POST['vram_capacity'] ?? 0;
    $age = $_POST['age'] ?? 0;
    $wattage = $_POST['wattage'] ?? 0;
    
    // Validasi input
    if (empty($name)) {
        $error = 'Nama VGA harus diisi!';
    } elseif (!is_numeric($price) || $price <= 0) {
        $error = 'Harga harus berupa angka positif!';
    } elseif (!is_numeric($vram_capacity) || $vram_capacity < 1 || $vram_capacity > 10) {
        $error = 'VRAM Capacity harus antara 1-10!';
    } elseif (!is_numeric($age) || $age < 0 || $age > 5) {
        $error = 'Age harus antara 0-5!';
    } elseif (!is_numeric($wattage) || $wattage <= 0) {
        $error = 'Wattage harus berupa angka positif!';
    } else {
        try {
            $stmt = $db->prepare("UPDATE vga_cards SET name = ?, price = ?, vram_capacity = ?, age = ?, wattage = ? WHERE id = ?");
            $stmt->execute([$name, $price, $vram_capacity, $age, $wattage, $id]);
            $success = 'Data VGA berhasil diupdate!';
            
            // Redirect setelah 1 detik
            header("refresh:1;url=index.php");
        } catch (PDOException $e) {
            $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 VGA</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 VGA</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 VGA</label>
                            <input type="text" class="form-control" id="name" name="name" 
                                   value="<?php echo htmlspecialchars($vga['name']); ?>" required>
                        </div>
                        <div class="form-group">
                            <label for="price">Harga</label>
                            <input type="number" class="form-control" id="price" name="price" 
                                   value="<?php echo $vga['price']; ?>" min="0" required>
                        </div>
                        <div class="form-group">
                            <label for="vram_capacity">VRAM Capacity (1-10)</label>
                            <input type="number" class="form-control" id="vram_capacity" name="vram_capacity" 
                                   value="<?php echo $vga['vram_capacity']; ?>" min="1" max="10" step="0.1" required>
                            <small class="form-text text-muted">
                                1 = Kapasitas Rendah, 5 = Kapasitas Menengah, 10 = Kapasitas Tinggi
                            </small>
                        </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 $vga['age']; ?>" min="0" max="5" step="0.1" required>
                            <small class="form-text text-muted">
                                0 = Baru, 5 = Sangat Lama
                            </small>
                        </div>
                        <div class="form-group">
                            <label for="wattage">Power Requirement (Watt)</label>
                            <input type="number" class="form-control" id="wattage" name="wattage" 
                                   value="<?php echo $vga['wattage']; ?>" min="0" required>
                            <small class="form-text text-muted">
                                Masukkan kebutuhan daya VGA dalam Watt (contoh: 300)
                            </small>
                        </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'; ?>