<?php

session_start();

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

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

// allow_super_admin();

$page_title = 'Assign Subscription';

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

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

/*
|--------------------------------------------------------------------------
| FETCH SCHOOLS
|--------------------------------------------------------------------------
*/

$school_stmt = $pdo->prepare("

    SELECT
        id,
        name

    FROM schools

    WHERE status = 'active'

    ORDER BY name ASC

");

$school_stmt->execute();

$schools = $school_stmt->fetchAll(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| FETCH PLANS
|--------------------------------------------------------------------------
*/

$plan_stmt = $pdo->prepare("

    SELECT *

    FROM super_admin_subscription_plans

    WHERE status = 'Active'

    ORDER BY price ASC

");

$plan_stmt->execute();

$plans = $plan_stmt->fetchAll(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| SAVE SUBSCRIPTION
|--------------------------------------------------------------------------
*/

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

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

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

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

    $start_date = $_POST['start_date'] ?? date('Y-m-d');

    $expiry_date = $_POST['expiry_date'] ?? '';

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

    $payment_status = $_POST['payment_status'] ?? 'Pending';

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

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

    if (!$school_id || !$plan_id || !$expiry_date) {

        $error = 'Please fill all required fields.';

    } else {

        $insert_stmt = $pdo->prepare("

            INSERT INTO super_admin_school_subscriptions (

                school_id,
                plan_id,
                start_date,
                expiry_date,
                status,
                payment_status,
                amount,
                notes

            ) VALUES (

                ?, ?, ?, ?, ?, ?, ?, ?

            )

        ");

        $insert = $insert_stmt->execute([

            $school_id,
            $plan_id,
            $start_date,
            $expiry_date,
            $status,
            $payment_status,
            $amount,
            $notes

        ]);

        if ($insert) {

            $success = 'Subscription assigned successfully.';
        }
    }
}

?>

<!-- =========================================================
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">

                    Assign Subscription

                </h1>

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

                    Assign SaaS plan to school

                </p>

            </div>

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

                <a
                    href="subscriptions.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-9">

                <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">

                                <!-- SCHOOL -->

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

                                    <div class="form-group">

                                        <label>

                                            Select School

                                        </label>

                                        <select
                                            name="school_id"
                                            class="form-control"
                                            required
                                        >

                                            <option value="">

                                                Select School

                                            </option>

                                            <?php foreach($schools as $school): ?>

                                                <option
                                                    value="<?= $school['id'] ?>"
                                                >

                                                    <?= htmlspecialchars($school['name']) ?>

                                                </option>

                                            <?php endforeach; ?>

                                        </select>

                                    </div>

                                </div>

                                <!-- PLAN -->

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

                                    <div class="form-group">

                                        <label>

                                            Select Plan

                                        </label>

                                        <select
                                            name="plan_id"
                                            class="form-control"
                                            required
                                        >

                                            <option value="">

                                                Select Plan

                                            </option>

                                            <?php foreach($plans as $plan): ?>

                                                <option
                                                    value="<?= $plan['id'] ?>"
                                                >

                                                    <?= htmlspecialchars($plan['plan_name']) ?>

                                                    -
                                                    ₹<?= number_format($plan['price'], 0) ?>

                                                </option>

                                            <?php endforeach; ?>

                                        </select>

                                    </div>

                                </div>

                                <!-- START DATE -->

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

                                    <div class="form-group">

                                        <label>

                                            Start Date

                                        </label>

                                        <input
                                            type="date"
                                            name="start_date"
                                            class="form-control"
                                            value="<?= date('Y-m-d') ?>"
                                            required
                                        >

                                    </div>

                                </div>

                                <!-- EXPIRY DATE -->

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

                                    <div class="form-group">

                                        <label>

                                            Expiry Date

                                        </label>

                                        <input
                                            type="date"
                                            name="expiry_date"
                                            class="form-control"
                                            required
                                        >

                                    </div>

                                </div>

                                <!-- STATUS -->

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

                                    <div class="form-group">

                                        <label>

                                            Subscription Status

                                        </label>

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

                                            <option value="Trial">

                                                Trial

                                            </option>

                                            <option value="Active">

                                                Active

                                            </option>

                                            <option value="Expired">

                                                Expired

                                            </option>

                                            <option value="Suspended">

                                                Suspended

                                            </option>

                                        </select>

                                    </div>

                                </div>

                                <!-- PAYMENT STATUS -->

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

                                    <div class="form-group">

                                        <label>

                                            Payment Status

                                        </label>

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

                                            <option value="Pending">

                                                Pending

                                            </option>

                                            <option value="Paid">

                                                Paid

                                            </option>

                                            <option value="Partial">

                                                Partial

                                            </option>

                                        </select>

                                    </div>

                                </div>

                                <!-- AMOUNT -->

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

                                    <div class="form-group">

                                        <label>

                                            Amount

                                        </label>

                                        <input
                                            type="number"
                                            step="0.01"
                                            name="amount"
                                            class="form-control"
                                            placeholder="6500"
                                        >

                                    </div>

                                </div>

                                <!-- NOTES -->

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

                                    <div class="form-group">

                                        <label>

                                            Notes

                                        </label>

                                        <textarea
                                            name="notes"
                                            class="form-control"
                                            rows="4"
                                            placeholder="Optional notes..."
                                        ></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>

                                    Assign Subscription

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