<?php
ob_start(); // Mulai output buffering di awal script

require_once '../../../config/database.php';
require_once '../../../config/auth.php';
require_once '../../../includes/header.php';
require_once '../../../includes/sidebar.php';
requireRole('super_admin');

$db = getDB();
$error = '';
$ram = null;

// Validasi dan filter ID
$id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT) ?: 0;
if (!$id) {
    header('Location: index.php');
    exit;
}

// Get RAM type data
$stmt = $db->prepare("SELECT * FROM ram_types WHERE id = ?");
$stmt->execute([$id]);
$ram = $stmt->fetch(PDO::FETCH_ASSOC);

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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Filter dan validasi input
    $name = trim($_POST['name'] ?? '');
    $speed_min = filter_input(INPUT_POST, 'speed_min', FILTER_VALIDATE_INT);
    $speed_max = filter_input(INPUT_POST, 'speed_max', FILTER_VALIDATE_INT);
    
    // Validasi input
    if (empty($name)) {
        $error = 'RAM Type Name is required!';
    } else if ($speed_min === false || $speed_max === false) {
        $error = 'Invalid speed values!';
    } else if ($speed_min > $speed_max) {
        $error = 'Minimum speed cannot be greater than maximum speed!';
    } else {
        try {
            $stmt = $db->prepare("UPDATE ram_types SET name = ?, speed_min = ?, speed_max = ? WHERE id = ?");
            $stmt->execute([$name, $speed_min, $speed_max, $id]);
            
            $_SESSION['success'] = 'RAM type updated successfully!';
            header('Location: index.php');
            exit;
        } catch (PDOException $e) {
            // Log the actual error for debugging
            error_log('RAM Type Update Error: ' . $e->getMessage());
            $error = 'Failed to update RAM type. Make sure the name is unique.';
        }
    }
}
?>
<!-- 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 RAM Type</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">Edit RAM Type</h3>
                </div>
                <div class="card-body">
                    <?php if ($error): ?>
                        <div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
                    <?php endif; ?>
                    <form method="POST">
                        <div class="form-group">
                            <label for="name">RAM Type Name</label>
                            <input type="text" class="form-control" id="name" name="name" required
                                   value="<?php echo htmlspecialchars($ram['name']); ?>">
                        </div>
                        
                        <div class="form-group">
                            <label for="speed_min">Minimum Speed (MHz)</label>
                            <input type="number" class="form-control" id="speed_min" name="speed_min" required
                                   min="0"
                                   value="<?php echo htmlspecialchars($ram['speed_min']); ?>">
                        </div>
                        
                        <div class="form-group">
                            <label for="speed_max">Maximum Speed (MHz)</label>
                            <input type="number" class="form-control" id="speed_max" name="speed_max" required
                                   min="0"
                                   value="<?php echo htmlspecialchars($ram['speed_max']); ?>">
                        </div>
                        <button type="submit" class="btn btn-primary">Update</button>
                        <a href="index.php" class="btn btn-secondary">Cancel</a>
                    </form>
                </div>
            </div>
        </div>
    </section>
</div>
<?php 
require_once '../../../includes/footer.php';
ob_end_flush(); // Akhiri dan kirim output buffering
?>