<?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 casing
$stmt = $db->prepare("SELECT * FROM casing WHERE id = ?");
$stmt->execute([$id]);
$casing = $stmt->fetch();

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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = $_POST['name'] ?? '';
    $price = $_POST['price'] ?? 0;
    $build_quality = $_POST['build_quality'] ?? 0;
    $physical_condition = $_POST['physical_condition'] ?? 0;
    
    if (empty($name)) {
        $error = 'Nama casing harus diisi!';
    } elseif (!is_numeric($price) || $price <= 0) {
        $error = 'Harga harus berupa angka positif!';
    } elseif (!is_numeric($build_quality) || $build_quality < 1 || $build_quality > 10) {
        $error = 'Build Quality harus antara 1-10!';
    } elseif (!is_numeric($physical_condition) || $physical_condition < 0 || $physical_condition > 5) {
        $error = 'Physical Condition harus antara 0-5!';
    } else {
        try {
            $stmt = $db->prepare("UPDATE casing SET name = ?, price = ?, build_quality = ?, physical_condition = ? WHERE id = ?");
            $stmt->execute([$name, $price, $build_quality, $physical_condition, $id]);
            
            $success = 'Data casing 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 Casing</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 Casing</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 Casing</label>
                            <input type="text" class="form-control" id="name" name="name" 
                                   value="<?php echo htmlspecialchars($casing['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($casing['price']); ?>" min="0" required>
                        </div>

                        <div class="form-group">
                            <label for="build_quality">Build Quality (1-10)</label>
                            <input type="number" class="form-control" id="build_quality" name="build_quality" 
                                   value="<?php echo htmlspecialchars($casing['build_quality']); ?>"
                                   min="1" max="10" step="0.1" required>
                            <small class="form-text text-muted">
                                1 = Kualitas Terendah, 5 = Kualitas Sedang, 10 = Kualitas Terbaik
                            </small>
                        </div>

                        <div class="form-group">
                            <label for="physical_condition">Physical Condition (0-5)</label>
                            <input type="number" class="form-control" id="physical_condition" name="physical_condition" 
                                   value="<?php echo htmlspecialchars($casing['physical_condition']); ?>"
                                   min="0" max="5" step="0.1" required>
                            <small class="form-text text-muted">
                                0 = Rusak, 2.5 = Normal, 5 = Baik Sekali
                            </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'; ?>