<?php

session_start();

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

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

// allow_super_admin();



/*
|--------------------------------------------------------------------------
| SAVE SETTINGS
|--------------------------------------------------------------------------
*/

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

    /*
    |--------------------------------------------------------------------------
    | SETTINGS
    |--------------------------------------------------------------------------
    */

    $settings = [

        'subscription_expiry_alert' =>
            isset($_POST['subscription_expiry_alert']) ? 1 : 0,

        'trial_expiry_alert' =>
            isset($_POST['trial_expiry_alert']) ? 1 : 0,

        'new_school_alert' =>
            isset($_POST['new_school_alert']) ? 1 : 0,

        'renewal_alert' =>
            isset($_POST['renewal_alert']) ? 1 : 0,

        'payment_alert' =>
            isset($_POST['payment_alert']) ? 1 : 0,

    ];

    /*
    |--------------------------------------------------------------------------
    | SAVE SETTINGS
    |--------------------------------------------------------------------------
    */

    foreach ($settings as $key => $value) {

        $checkStmt = $pdo->prepare("

            SELECT id

            FROM super_admin_settings

            WHERE setting_key = ?

            LIMIT 1

        ");

        $checkStmt->execute([$key]);

        $exists = $checkStmt->fetch();

        if ($exists) {

            $updateStmt = $pdo->prepare("

                UPDATE super_admin_settings

                SET setting_value = ?

                WHERE setting_key = ?

            ");

            $updateStmt->execute([

                $value,
                $key

            ]);

        } else {

            $insertStmt = $pdo->prepare("

                INSERT INTO super_admin_settings (

                    setting_key,
                    setting_value

                ) VALUES (

                    ?, ?

                )

            ");

            $insertStmt->execute([

                $key,
                $value

            ]);
        }
    }

    $_SESSION['success'] =
        'Notification settings updated successfully.';

    header('Location: notification_settings.php');

    exit;
}

/*
|--------------------------------------------------------------------------
| FETCH SETTINGS
|--------------------------------------------------------------------------
*/

$settingsStmt = $pdo->query("

    SELECT *

    FROM super_admin_settings

");

$settingsData = $settingsStmt->fetchAll(PDO::FETCH_ASSOC);

$settings = [];

foreach ($settingsData as $setting) {

    $settings[$setting['setting_key']] =
        $setting['setting_value'];
}

$page_title = 'Notification Settings';

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

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

<!-- =========================================================
CONTENT HEADER
========================================================= -->

<section class="content-header">

    <div class="container-fluid">

        <div class="d-flex justify-content-between align-items-center mb-3">

            <div>

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

                    Notification Settings

                </h1>

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

                    Configure SaaS notification preferences

                </p>

            </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(isset($_SESSION['success'])): ?>

                            <div class="alert alert-success">

                                <?= $_SESSION['success']; ?>

                            </div>

                            <?php unset($_SESSION['success']); ?>

                        <?php endif; ?>

                        <form method="POST">

                            <!-- SUBSCRIPTION EXPIRY -->

                            <div class="setting-box">

                                <div>

                                    <h5>

                                        Subscription Expiry Alerts

                                    </h5>

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

                                        Notify when subscriptions are expiring

                                    </p>

                                </div>

                                <div>

                                    <input
                                        type="checkbox"
                                        name="subscription_expiry_alert"
                                        value="1"
                                        <?= !empty($settings['subscription_expiry_alert']) ? 'checked' : '' ?>
                                    >

                                </div>

                            </div>

                            <!-- TRIAL EXPIRY -->

                            <div class="setting-box">

                                <div>

                                    <h5>

                                        Trial Expiry Alerts

                                    </h5>

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

                                        Notify when trial plans are ending

                                    </p>

                                </div>

                                <div>

                                    <input
                                        type="checkbox"
                                        name="trial_expiry_alert"
                                        value="1"
                                        <?= !empty($settings['trial_expiry_alert']) ? 'checked' : '' ?>
                                    >

                                </div>

                            </div>

                            <!-- NEW SCHOOL -->

                            <div class="setting-box">

                                <div>

                                    <h5>

                                        New School Alerts

                                    </h5>

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

                                        Notify when a new school is created

                                    </p>

                                </div>

                                <div>

                                    <input
                                        type="checkbox"
                                        name="new_school_alert"
                                        value="1"
                                        <?= !empty($settings['new_school_alert']) ? 'checked' : '' ?>
                                    >

                                </div>

                            </div>

                            <!-- RENEWAL -->

                            <div class="setting-box">

                                <div>

                                    <h5>

                                        Renewal Alerts

                                    </h5>

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

                                        Notify when subscriptions are renewed

                                    </p>

                                </div>

                                <div>

                                    <input
                                        type="checkbox"
                                        name="renewal_alert"
                                        value="1"
                                        <?= !empty($settings['renewal_alert']) ? 'checked' : '' ?>
                                    >

                                </div>

                            </div>

                            <!-- PAYMENT -->

                            <div class="setting-box">

                                <div>

                                    <h5>

                                        Payment Alerts

                                    </h5>

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

                                        Notify for pending payments

                                    </p>

                                </div>

                                <div>

                                    <input
                                        type="checkbox"
                                        name="payment_alert"
                                        value="1"
                                        <?= !empty($settings['payment_alert']) ? 'checked' : '' ?>
                                    >

                                </div>

                            </div>

                            <!-- BUTTON -->

                            <div class="mt-4">

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

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

                                    Save Settings

                                </button>

                            </div>

                        </form>

                    </div>

                </div>

            </div>

        </div>

    </div>

</section>

<style>

.card {

    border-radius: 18px;
}

.setting-box {

    display: flex;
    justify-content: space-between;
    align-items: center;

    padding: 18px;
    margin-bottom: 15px;

    border-radius: 14px;

    background: #f8f9fa;
}

.btn {

    border-radius: 10px;
}

input[type="checkbox"] {

    width: 20px;
    height: 20px;
}

</style>

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