<?php

session_start();

require_once __DIR__ . '/../../library/config.php';

require_once __DIR__ . '/../../library/super_admin_auth.php';

// allow_super_admin();

$page_title = 'Edit Subscription Plan';

require_once __DIR__ . '/../includes/header.php';

require_once __DIR__ . '/../includes/sidebar.php';

/*
|--------------------------------------------------------------------------
| VALIDATE ID
|--------------------------------------------------------------------------
*/

$id = (int)($_GET['id'] ?? 0);

if (!$id) {

    die('Invalid Plan ID');
}

/*
|--------------------------------------------------------------------------
| FETCH PLAN
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("

    SELECT *

    FROM super_admin_subscription_plans

    WHERE id = ?

");

$stmt->execute([$id]);

$plan = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$plan) {

    die('Plan not found.');
}

/*
|--------------------------------------------------------------------------
| UPDATE PLAN
|--------------------------------------------------------------------------
*/

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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $plan_name = trim($_POST['plan_name'] ?? '');

    $price = (float)($_POST['price'] ?? 0);

    $duration_months = (int)($_POST['duration_months'] ?? 12);

    $max_students = (int)($_POST['max_students'] ?? 0);

    $description = trim($_POST['description'] ?? '');

    $status = $_POST['status'] ?? 'Active';

    if ($plan_name == '') {

        $error = 'Plan name is required.';

    } else {

        $update_stmt = $pdo->prepare("

            UPDATE super_admin_subscription_plans

            SET

                plan_name = ?,
                price = ?,
                duration_months = ?,
                max_students = ?,
                description = ?,
                status = ?

            WHERE id = ?

        ");

        $updated = $update_stmt->execute([

            $plan_name,
            $price,
            $duration_months,
            $max_students,
            $description,
            $status,
            $id

        ]);

        if ($updated) {

            $success = 'Subscription plan updated successfully.';

            /*
            |--------------------------------------------------------------------------
            | REFRESH DATA
            |--------------------------------------------------------------------------
            */

            $stmt->execute([$id]);

            $plan = $stmt->fetch(PDO::FETCH_ASSOC);
        }
    }
}

?>

<!-- =========================================================
PAGE HEADER
========================================================= -->

<section class="content-header">

    <div class="container-fluid">

        <div class="row mb-3">

            <div class="col-sm-6">

                <h1 class="font-weight-bold">

                    Edit Subscription Plan

                </h1>

                <p class="text-muted mb-0">

                    Update SaaS pricing plan details

                </p>

            </div>

            <div class="col-sm-6 text-right">

                <a
                    href="plans.php"
                    class="btn btn-secondary"
                >

                    <i class="fas fa-arrow-left mr-1"></i>

                    Back

                </a>

            </div>

        </div>

    </div>

</section>

<!-- =========================================================
MAIN CONTENT
========================================================= -->

<section class="content">

    <div class="container-fluid">

        <div class="row justify-content-center">

            <div class="col-lg-8">

                <div class="card shadow-sm border-0">

                    <div class="card-body p-4">

                        <?php if($error): ?>

                            <div class="alert alert-danger">

                                <?= $error ?>

                            </div>

                        <?php endif; ?>

                        <?php if($success): ?>

                            <div class="alert alert-success">

                                <?= $success ?>

                            </div>

                        <?php endif; ?>

                        <form method="POST">

                            <div class="row">

                                <!-- PLAN NAME -->

                                <div class="col-md-6">

                                    <div class="form-group">

                                        <label>

                                            Plan Name

                                        </label>

                                        <input
                                            type="text"
                                            name="plan_name"
                                            class="form-control"
                                            value="<?= htmlspecialchars($plan['plan_name']) ?>"
                                            required
                                        >

                                    </div>

                                </div>

                                <!-- PRICE -->

                                <div class="col-md-6">

                                    <div class="form-group">

                                        <label>

                                            Price

                                        </label>

                                        <input
                                            type="number"
                                            name="price"
                                            class="form-control"
                                            value="<?= $plan['price'] ?>"
                                            required
                                        >

                                    </div>

                                </div>

                                <!-- DURATION -->

                                <div class="col-md-6">

                                    <div class="form-group">

                                        <label>

                                            Duration (Months)

                                        </label>

                                        <input
                                            type="number"
                                            name="duration_months"
                                            class="form-control"
                                            value="<?= $plan['duration_months'] ?>"
                                            required
                                        >

                                    </div>

                                </div>

                                <!-- MAX STUDENTS -->

                                <div class="col-md-6">

                                    <div class="form-group">

                                        <label>

                                            Max Students

                                        </label>

                                        <input
                                            type="number"
                                            name="max_students"
                                            class="form-control"
                                            value="<?= $plan['max_students'] ?>"
                                        >

                                    </div>

                                </div>

                                <!-- STATUS -->

                                <div class="col-md-6">

                                    <div class="form-group">

                                        <label>

                                            Status

                                        </label>

                                        <select
                                            name="status"
                                            class="form-control"
                                        >

                                            <option
                                                value="Active"
                                                <?= $plan['status'] == 'Active' ? 'selected' : '' ?>
                                            >

                                                Active

                                            </option>

                                            <option
                                                value="Inactive"
                                                <?= $plan['status'] == 'Inactive' ? 'selected' : '' ?>
                                            >

                                                Inactive

                                            </option>

                                        </select>

                                    </div>

                                </div>

                                <!-- DESCRIPTION -->

                                <div class="col-md-12">

                                    <div class="form-group">

                                        <label>

                                            Description

                                        </label>

                                        <textarea
                                            name="description"
                                            class="form-control"
                                            rows="4"
                                        ><?= htmlspecialchars($plan['description']) ?></textarea>

                                    </div>

                                </div>

                            </div>

                            <!-- SUBMIT -->

                            <div class="text-right">

                                <button
                                    type="submit"
                                    class="btn btn-primary px-4"
                                >

                                    <i class="fas fa-save mr-1"></i>

                                    Update Plan

                                </button>

                            </div>

                        </form>

                    </div>

                </div>

            </div>

        </div>

    </div>

</section>

<style>

.card {

    border-radius: 18px;
}

.form-control {

    border-radius: 10px;
    height: 45px;
}

textarea.form-control {

    height: auto;
}

.btn {

    border-radius: 10px;
}

label {

    font-weight: 600;
}

</style>

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