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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = $_POST['name'] ?? '';
    $price = $_POST['price'] ?? 0;
    $type = $_POST['type'] ?? 0;
    $capacity = $_POST['capacity'] ?? 0;
    
    if (empty($name)) {
        $error = 'Nama storage harus diisi!';
    } elseif (!is_numeric($price) || $price <= 0) {
        $error = 'Harga harus berupa angka positif!';
    } elseif (!is_numeric($type) || $type < 0 || $type > 1) {
        $error = 'Type harus antara 0-1!';
    } elseif (!is_numeric($capacity) || $capacity < 1 || $capacity > 10) {
        $error = 'Capacity harus antara 1-10!';
    } else {
        try {
            $stmt = $db->prepare("INSERT INTO storage (name, price, type, capacity) VALUES (?, ?, ?, ?)");
            $stmt->execute([$name, $price, $type, $capacity]);
            
            $success = 'Data storage berhasil ditambahkan!';
            
            // Redirect setelah 1 detik
            header("refresh:1;url=index.php");
        } 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 Storage</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 Storage</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 Storage</label>
                            <input type="text" class="form-control" id="name" name="name" 
                                   value="<?php echo htmlspecialchars($_POST['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 htmlspecialchars($_POST['price'] ?? ''); ?>" min="0" required>
                        </div>

                        <div class="form-group">
                            <label for="type">Storage Type (0-1)</label>
                            <input type="number" class="form-control" id="type" name="type" 
                                   value="<?php echo htmlspecialchars($_POST['type'] ?? ''); ?>"
                                   min="0" max="1" step="0.1" required>
                            <small class="form-text text-muted">
                                0 = HDD, 0.5 = SSD SATA, 1 = SSD NVMe
                            </small>
                        </div>

                        <div class="form-group">
                            <label for="capacity">Capacity Rating (1-10)</label>
                            <input type="number" class="form-control" id="capacity" name="capacity" 
                                   value="<?php echo htmlspecialchars($_POST['capacity'] ?? ''); ?>"
                                   min="1" max="10" step="0.1" required>
                            <small class="form-text text-muted">
                                1 = Low (<256GB), 5 = Medium (256GB-1TB), 10 = High (>1TB)
                            </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'; ?>