<?php

session_start();

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

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

// allow_super_admin();

$page_title = 'Renew Subscription';

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

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

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

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

if (!$id) {

    die('Invalid Subscription ID');
}

/*
|--------------------------------------------------------------------------
| FETCH SUBSCRIPTION
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("

    SELECT

        sss.*,
        s.name AS school_name,
        sp.plan_name,
        sp.duration_months

    FROM super_admin_school_subscriptions sss

    INNER JOIN schools s
    ON s.id = sss.school_id

    INNER JOIN super_admin_subscription_plans sp
    ON sp.id = sss.plan_id

    WHERE sss.id = ?

");

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

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

if (!$subscription) {

    die('Subscription not found.');
}

/*
|--------------------------------------------------------------------------
| RENEW SUBSCRIPTION
|--------------------------------------------------------------------------
*/

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

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

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

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

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

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

    /*
    |--------------------------------------------------------------------------
    | CALCULATE NEW EXPIRY
    |--------------------------------------------------------------------------
    */

    $current_expiry = $subscription['expiry_date'];

    $base_date = strtotime($current_expiry);

    if ($base_date < time()) {

        $base_date = time();
    }

    $new_expiry = date(

        'Y-m-d',

        strtotime("+$renew_months months", $base_date)

    );

    /*
    |--------------------------------------------------------------------------
    | UPDATE SUBSCRIPTION
    |--------------------------------------------------------------------------
    */

    $update_stmt = $pdo->prepare("

        UPDATE super_admin_school_subscriptions

        SET

            expiry_date = ?,
            amount = ?,
            payment_status = ?,
            status = 'Active',
            notes = ?

        WHERE id = ?

    ");

    $updated = $update_stmt->execute([

        $new_expiry,
        $amount,
        $payment_status,
        $notes,
        $id

    ]);

    if ($updated) {

        $success = 'Subscription renewed successfully.';

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

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

        $subscription = $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">

                    Renew Subscription

                </h1>

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

                    Renew school SaaS subscription

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

                        <!-- SUBSCRIPTION INFO -->

                        <div class="subscription-info mb-4">

                            <div class="row">

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

                                    <p>

                                        <strong>

                                            School:

                                        </strong>

                                        <?= htmlspecialchars($subscription['school_name']) ?>

                                    </p>

                                </div>

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

                                    <p>

                                        <strong>

                                            Plan:

                                        </strong>

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

                                    </p>

                                </div>

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

                                    <p>

                                        <strong>

                                            Current Expiry:

                                        </strong>

                                        <?= date('d M Y', strtotime($subscription['expiry_date'])) ?>

                                    </p>

                                </div>

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

                                    <p>

                                        <strong>

                                            Current Status:

                                        </strong>

                                        <?= htmlspecialchars($subscription['status']) ?>

                                    </p>

                                </div>

                            </div>

                        </div>

                        <!-- FORM -->

                        <form method="POST">

                            <div class="row">

                                <!-- RENEW MONTHS -->

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

                                    <div class="form-group">

                                        <label>

                                            Renew Duration (Months)

                                        </label>

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

                                    </div>

                                </div>

                                <!-- AMOUNT -->

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

                                    <div class="form-group">

                                        <label>

                                            Renewal Amount

                                        </label>

                                        <input
                                            type="number"
                                            step="0.01"
                                            name="amount"
                                            class="form-control"
                                            value="<?= $subscription['amount'] ?>"
                                            required
                                        >

                                    </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="Paid">

                                                Paid

                                            </option>

                                            <option value="Partial">

                                                Partial

                                            </option>

                                            <option value="Pending">

                                                Pending

                                            </option>

                                        </select>

                                    </div>

                                </div>

                                <!-- NOTES -->

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

                                    <div class="form-group">

                                        <label>

                                            Notes

                                        </label>

                                        <textarea
                                            name="notes"
                                            class="form-control"
                                            rows="4"
                                            placeholder="Renewal notes..."
                                        ></textarea>

                                    </div>

                                </div>

                            </div>

                            <!-- SUBMIT -->

                            <div class="text-right">

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

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

                                    Renew 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;
}

.subscription-info {

    background: #f8f9fa;
    border-radius: 14px;
    padding: 20px;
}

.btn {

    border-radius: 10px;
}

label {

    font-weight: 600;
}

</style>

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