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

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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = $_POST['name'] ?? '';
    $price = $_POST['price'] ?? 0;
    $capacity = $_POST['capacity'] ?? 0;
    $type = $_POST['type'] ?? 0;
    
    // Validasi input
    if (empty($name)) {
        $error = 'Nama RAM harus diisi!';
    } elseif (!is_numeric($price) || $price <= 0) {
        $error = 'Harga harus berupa angka positif!';
    } elseif (!is_numeric($capacity) || $capacity < 1 || $capacity > 10) {
        $error = 'Kapasitas harus antara 1-10!';
    } elseif (!is_numeric($type) || $type < 0 || $type > 5) {
        $error = 'Type harus antara 0-1!';
    } else {
        try {
            $db = getDB();
            $stmt = $db->prepare("INSERT INTO ram (name, price, capacity, type) VALUES (?, ?, ?, ?)");
            $stmt->execute([$name, $price, $capacity, $type]);
            $success = 'Data RAM berhasil ditambahkan!';
            
            // Redirect setelah 1 detik
            
        } catch (PDOException $e) {
            $error = 'Terjadi kesalahan saat menyimpan 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>Tambah RAM</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 Tambah RAM</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 RAM</label>
                            <input type="text" class="form-control" id="name" name="name" required>
                        </div>
                        <div class="form-group">
                            <label for="price">Harga</label>
                            <input type="number" class="form-control" id="price" name="price" min="0" required>
                        </div>
                        <div class="form-group">
                            <label for="capacity">Kapasitas (1-10)</label>
                            <input type="number" class="form-control" id="capacity" name="capacity" 
                                   min="1" max="10" step="0.1" required>
                                   <small class="form-text text-muted">
                                1 = Kapasitas Terendah, 10 = Kapasitas Tertinggi
                            </small>
                        </div>
                        <div class="form-group">
                            <label for="type">Type (0-1)</label>
                            <input type="number" class="form-control" id="type" name="type" 
                                   min="0" max="5" step="0.1" required>
                                   <small class="form-text text-muted">
                                0 = DDR3, 0,5 = DDR4, 1=DDR5
                            </small>
                        </div>
                        <button type="submit" class="btn btn-primary">Simpan</button>
                        <a href="index.php" class="btn btn-secondary">Kembali</a>
                    </form>
                </div>
            </div>
        </div>
    </section>
</div>

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