<?php
// /public_html/admin/plans.php
session_start();
require '../config/db_connect.php';

// 1. Admin Check
if (!isset($_SESSION['user_id'])) { header("Location: ../login.php"); exit(); }
$user_id = $_SESSION['user_id'];
$check = $conn->query("SELECT role FROM users WHERE id = '$user_id'")->fetch_assoc();
if (($check['role'] ?? '') !== 'admin') { die("Access Denied"); }

// 2. Handle Update
$msg = "";
if (isset($_POST['update_plan'])) {
    $price = floatval($_POST['price']);
    $scan_limit = intval($_POST['scan_limit']);
    $qr_limit = intval($_POST['qr_limit']);
    $plan_id = intval($_POST['plan_id']);

    $stmt = $conn->prepare("UPDATE plans SET price = ?, scan_limit = ?, qr_limit = ? WHERE id = ?");
    $stmt->bind_param("diii", $price, $scan_limit, $qr_limit, $plan_id);
    if ($stmt->execute()) {
        $msg = "Plan updated successfully!";
    } else {
        $msg = "Error updating plan.";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Manage Pricing - Admin</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css">
</head>
<body class="bg-light">
    
    <?php include 'includes/navbar.php'; ?>

    <div class="container mt-5">
        <h2 class="mb-4">Pricing Plans</h2>
        
        <?php if(!empty($msg)) echo "<div class='alert alert-success'>$msg</div>"; ?>

        <div class="row">
            <?php
            $plans = $conn->query("SELECT * FROM plans");
            while($plan = $plans->fetch_assoc()):
            ?>
            <div class="col-md-6">
                <div class="card shadow-sm mb-3">
                    <div class="card-header fw-bold bg-white">
                        <?php echo htmlspecialchars($plan['name']); ?> Plan
                    </div>
                    <div class="card-body">
                        <form method="POST">
                            <input type="hidden" name="plan_id" value="<?php echo $plan['id']; ?>">
                            
                            <div class="mb-3">
                                <label class="form-label">Price (₹)</label>
                                <input type="number" step="0.01" name="price" class="form-control" value="<?php echo $plan['price']; ?>" required>
                            </div>
                            
                            <div class="mb-3">
                                <label class="form-label">Scan Limit (Per Month)</label>
                                <input type="number" name="scan_limit" class="form-control" value="<?php echo $plan['scan_limit']; ?>" required>
                                <div class="form-text">0 = Unlimited (Use high number like 1000000000)</div>
                            </div>
                            
                            <div class="mb-3">
                                <label class="form-label">QR Limit (Max Active)</label>
                                <input type="number" name="qr_limit" class="form-control" value="<?php echo $plan['qr_limit']; ?>" required>
                            </div>
                            
                            <button type="submit" name="update_plan" class="btn btn-primary w-100">Update <?php echo htmlspecialchars($plan['name']); ?></button>
                        </form>
                    </div>
                </div>
            </div>
            <?php endwhile; ?>
        </div>
    </div>

    <?php include 'includes/footer.php'; ?>