<?php

session_start();

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

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

// allow_super_admin();

$page_title = 'Manage Plan Features';

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

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

/*
|--------------------------------------------------------------------------
| VALIDATE PLAN
|--------------------------------------------------------------------------
*/

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

if (!$plan_id) {

    die('Invalid Plan ID');
}

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

$stmt = $pdo->prepare("

    SELECT *

    FROM super_admin_subscription_plans

    WHERE id = ?

");

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

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

if (!$plan) {

    die('Plan not found.');
}

/*
|--------------------------------------------------------------------------
| FEATURE LIST
|--------------------------------------------------------------------------
*/

$available_features = [

    'student_management'   => 'Student Management',
    'attendance'           => 'Attendance',
    'fees_management'      => 'Fees Management',
    'salary_management'    => 'Salary Management',
    'id_cards'             => 'ID Cards',
    'advanced_reports'     => 'Advanced Reports',
    'global_search'        => 'Global Search',
    'transport_reports'    => 'Transport Reports',
    'inventory_management' => 'Inventory Management',
    'library_management'   => 'Library Management',
    'website'              => 'School Website',
    'priority_support'     => 'Priority Support',
    'data_entry_support'   => 'Data Entry Support'

];

/*
|--------------------------------------------------------------------------
| SAVE FEATURES
|--------------------------------------------------------------------------
*/

$success = '';

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

    /*
    |--------------------------------------------------------------------------
    | DELETE OLD FEATURES
    |--------------------------------------------------------------------------
    */

    $delete_stmt = $pdo->prepare("

        DELETE FROM super_admin_feature_permissions

        WHERE plan_id = ?

    ");

    $delete_stmt->execute([$plan_id]);

    /*
    |--------------------------------------------------------------------------
    | INSERT NEW FEATURES
    |--------------------------------------------------------------------------
    */

    $selected_features = $_POST['features'] ?? [];

    foreach ($available_features as $feature_key => $feature_name) {

        $is_enabled = in_array($feature_key, $selected_features) ? 1 : 0;

        $insert_stmt = $pdo->prepare("

            INSERT INTO super_admin_feature_permissions (

                plan_id,
                feature_key,
                is_enabled

            ) VALUES (

                ?, ?, ?

            )

        ");

        $insert_stmt->execute([

            $plan_id,
            $feature_key,
            $is_enabled

        ]);
    }

    $success = 'Feature permissions updated successfully.';
}

/*
|--------------------------------------------------------------------------
| FETCH ENABLED FEATURES
|--------------------------------------------------------------------------
*/

$feature_stmt = $pdo->prepare("

    SELECT feature_key

    FROM super_admin_feature_permissions

    WHERE plan_id = ?
    AND is_enabled = 1

");

$feature_stmt->execute([$plan_id]);

$enabled_features = $feature_stmt->fetchAll(PDO::FETCH_COLUMN);

?>

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

                    Manage Features

                </h1>

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

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

                    Plan Permissions

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

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

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

                        <?php if($success): ?>

                            <div class="alert alert-success">

                                <?= $success ?>

                            </div>

                        <?php endif; ?>

                        <form method="POST">

                            <div class="row">

                                <?php foreach($available_features as $key => $label): ?>

                                    <div class="col-md-4 mb-3">

                                        <div class="feature-card">

                                            <div class="custom-control custom-switch">

                                                <input
                                                    type="checkbox"
                                                    class="custom-control-input"
                                                    id="<?= $key ?>"
                                                    name="features[]"
                                                    value="<?= $key ?>"
                                                    <?= in_array($key, $enabled_features) ? 'checked' : '' ?>
                                                >

                                                <label
                                                    class="custom-control-label"
                                                    for="<?= $key ?>"
                                                >

                                                    <?= $label ?>

                                                </label>

                                            </div>

                                        </div>

                                    </div>

                                <?php endforeach; ?>

                            </div>

                            <!-- SUBMIT -->

                            <div class="text-right mt-4">

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

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

                                    Save Permissions

                                </button>

                            </div>

                        </form>

                    </div>

                </div>

            </div>

        </div>

    </div>

</section>

<style>

.card {

    border-radius: 18px;
}

.feature-card {

    background: #f8f9fa;
    border-radius: 14px;
    padding: 18px;
    transition: 0.3s ease;
    border: 1px solid #eee;
}

.feature-card:hover {

    background: #ffffff;
}

.custom-control-label {

    font-weight: 600;
    cursor: pointer;
}

.btn {

    border-radius: 10px;
}

</style>

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