<?php
require_once __DIR__ . '/../library/auth.php'; 
$school_id = $_SESSION['school_id'];
allow_roles(['admin']);

// ======================================================
// FETCH SETTINGS
// ======================================================

$stmt = $pdo->prepare("
    SELECT *
    FROM school_settings
    WHERE school_id = ?
");

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

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


// ======================================================
// PAGE TITLE
// ======================================================

$page_title = 'School Profile Settings';

require_once __DIR__ . '/../include/header.php';
require_once __DIR__ . '/../include/sidebar.php';

?>

<div class="content-wrapper">

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

    <section class="content-header">

        <div class="container-fluid">

            <div class="row mb-2">

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

                    <h1>

                        <i class="fas fa-school text-primary"></i>
                        School Profile Settings

                    </h1>

                </div>


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

                    <a
                        href="index.php"
                        class="btn btn-info"
                    >

                        <i class="fas fa-eye"></i>
                        View Profile

                    </a>

                </div>

            </div>

        </div>

    </section>



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

    <section class="content">

        <div class="container-fluid">

            <?php if (isset($_GET['msg'])): ?>

                <div class="alert alert-success alert-dismissible fade show">

                    <?= htmlspecialchars($_GET['msg']) ?>

                    <button
                        type="button"
                        class="close"
                        data-dismiss="alert"
                    >

                        &times;

                    </button>

                </div>

            <?php endif; ?>



            <form
                action="process.php"
                method="POST"
                enctype="multipart/form-data"
            >

                <input
                    type="hidden"
                    name="existing_logo"
                    value="<?= htmlspecialchars($data['school_logo'] ?? '') ?>"
                >


                <div class="row">

                    <!-- ====================================================== -->
                    <!-- LEFT COLUMN -->
                    <!-- ====================================================== -->

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

                        <div class="card card-primary card-outline">

                            <div class="card-header">

                                <h3 class="card-title">

                                    <i class="fas fa-building"></i>
                                    School Information

                                </h3>

                            </div>



                            <div class="card-body">

                                <!-- SCHOOL NAME -->

                                <div class="form-group">

                                    <label>

                                        School Name

                                    </label>

                                    <input
                                        type="text"
                                        name="school_name"
                                        class="form-control"
                                        value="<?= htmlspecialchars($data['school_name'] ?? '') ?>"
                                        placeholder="Enter School Name"
                                        required
                                    >

                                </div>



                                <!-- ADDRESS -->

                                <div class="form-group">

                                    <label>

                                        School Address

                                    </label>

                                    <textarea
                                        name="address"
                                        rows="4"
                                        class="form-control"
                                        placeholder="Enter School Address"
                                    ><?= htmlspecialchars($data['school_address'] ?? '') ?></textarea>

                                </div>



                                <!-- PHONE -->

                                <div class="form-group">

                                    <label>

                                        Phone Number

                                    </label>

                                    <input
                                        type="text"
                                        name="phone"
                                        class="form-control"
                                        value="<?= htmlspecialchars($data['school_phone'] ?? '') ?>"
                                        placeholder="Enter Phone Number"
                                    >

                                </div>



                                <!-- EMAIL -->

                                <div class="form-group">

                                    <label>

                                        Email Address

                                    </label>

                                    <input
                                        type="email"
                                        name="email"
                                        class="form-control"
                                        value="<?= htmlspecialchars($data['school_email'] ?? '') ?>"
                                        placeholder="Enter Email Address"
                                    >

                                </div>



                                <!-- WEBSITE -->

                                <div class="form-group">

                                    <label>

                                        Website

                                    </label>

                                    <input
                                        type="text"
                                        name="website"
                                        class="form-control"
                                        value="<?= htmlspecialchars($data['school_website'] ?? '') ?>"
                                        placeholder="https://example.com"
                                    >

                                </div>

                            </div>

                        </div>

                    </div>



                    <!-- ====================================================== -->
                    <!-- RIGHT COLUMN -->
                    <!-- ====================================================== -->

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

                        <div class="card card-success card-outline">

                            <div class="card-header">

                                <h3 class="card-title">

                                    <i class="fas fa-cogs"></i>
                                    Academic & Branding

                                </h3>

                            </div>



                            <div class="card-body">

                                <!-- REG NO -->

                                <div class="form-group">

                                    <label>

                                        Registration Number

                                    </label>

                                    <input
                                        type="text"
                                        name="reg_no"
                                        class="form-control"
                                        value="<?= htmlspecialchars($data['school_reg_no'] ?? '') ?>"
                                        placeholder="Enter Registration Number"
                                    >

                                </div>



                                <!-- DISE CODE -->

                                <div class="form-group">

                                    <label>

                                        DISE Code

                                    </label>

                                    <input
                                        type="text"
                                        name="dise_code"
                                        class="form-control"
                                        value="<?= htmlspecialchars($data['dise_code'] ?? '') ?>"
                                        placeholder="Enter DISE Code"
                                    >

                                </div>



                                <!-- SESSION -->

                                <div class="form-group">

                                    <label>

                                        Current Session

                                    </label>

                                    <input
                                        type="text"
                                        name="session"
                                        class="form-control"
                                        value="<?= htmlspecialchars($data['current_session'] ?? '') ?>"
                                        placeholder="2025-26"
                                    >

                                </div>



                                <!-- LANGUAGE -->

                                <div class="form-group">

                                    <label>

                                        Language

                                    </label>

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

                                        <option value="en" <?= (($data['language'] ?? '') == 'en') ? 'selected' : '' ?>>

                                            English

                                        </option>

                                        <option value="hi" <?= (($data['language'] ?? '') == 'hi') ? 'selected' : '' ?>>

                                            Hindi

                                        </option>

                                    </select>

                                </div>



                                <!-- CURRENCY -->

                                <div class="form-group">

                                    <label>

                                        Currency

                                    </label>

                                    <input
                                        type="text"
                                        name="currency"
                                        class="form-control"
                                        value="<?= htmlspecialchars($data['currency'] ?? 'INR') ?>"
                                        placeholder="INR"
                                    >

                                </div>



                                <!-- RESULT PATTERN -->

                                <div class="form-group">

                                    <label>

                                        Result Pattern

                                    </label>

                                    <input
                                        type="text"
                                        name="result_pattern"
                                        class="form-control"
                                        value="<?= htmlspecialchars($data['result_pattern'] ?? '') ?>"
                                        placeholder="CBSE / State Board"
                                    >

                                </div>



                                <!-- LOGO -->

                                <div class="form-group">

                                    <label>

                                        School Logo

                                    </label>

                                    <input
                                        type="file"
                                        name="school_logo"
                                        class="form-control"
                                        accept=".jpg,.jpeg,.png,.webp"
                                    >

                                </div>



                                <!-- PREVIEW -->

                                <?php if (!empty($data['school_logo'])): ?>

                                    <div class="text-center mt-3">

                                        <img
                                            src="../uploads/school/<?= htmlspecialchars($data['school_logo']) ?>"
                                            alt="School Logo"
                                            class="img-thumbnail"
                                            style="
                                                width:120px;
                                                height:120px;
                                                object-fit:contain;
                                                padding:10px;
                                            "
                                        >

                                    </div>

                                <?php endif; ?>

                            </div>

                        </div>

                    </div>

                </div>



                <!-- ====================================================== -->
                <!-- SUBMIT BUTTON -->
                <!-- ====================================================== -->

                <div class="text-center mb-5">

                    <button
                        type="submit"
                        name="update_settings"
                        class="btn btn-primary btn-lg px-5"
                    >

                        <i class="fas fa-save"></i>
                        Save Settings

                    </button>

                </div>

            </form>

        </div>

    </section>

</div>

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