<?php

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

// ======================================================
// FETCH SCHOOL 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';

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

                    </h1>

                </div>


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

                    <a
                        href="addprofile.php"
                        class="btn btn-primary"
                    >

                        <i class="fas fa-edit"></i>
                        Edit Profile

                    </a>

                </div>

            </div>

        </div>

    </section>



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

    <section class="content">

        <div class="container-fluid">

            <!-- ALERT MESSAGE -->

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



            <?php if ($data): ?>

                <div class="row">

                    <!-- ====================================================== -->
                    <!-- LEFT SIDE -->
                    <!-- ====================================================== -->

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

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

                            <div class="card-body text-center">

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

                                    <img
                                        src="../uploads/school/<?= htmlspecialchars($data['school_logo']) ?>"
                                        alt="School Logo"
                                        class="img-fluid img-circle elevation-3 mb-3"
                                        style="
                                            width:140px;
                                            height:140px;
                                            object-fit:contain;
                                            background:#fff;
                                            padding:10px;
                                        "
                                    >

                                <?php else: ?>

                                    <div
                                        class="bg-light d-flex align-items-center justify-content-center mx-auto mb-3"
                                        style="
                                            width:140px;
                                            height:140px;
                                            border-radius:50%;
                                            font-size:50px;
                                        "
                                    >

                                        <i class="fas fa-school text-muted"></i>

                                    </div>

                                <?php endif; ?>



                                <h3 class="profile-username text-center">

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

                                </h3>

                                <p class="text-muted text-center">

                                    <?= htmlspecialchars($data['current_session']) ?>

                                </p>



                                <ul class="list-group list-group-unbordered mb-3">

                                    <li class="list-group-item">

                                        <b>

                                            Language

                                        </b>

                                        <span class="float-right">

                                            <?= htmlspecialchars($data['language']) ?>

                                        </span>

                                    </li>


                                    <li class="list-group-item">

                                        <b>

                                            Currency

                                        </b>

                                        <span class="float-right">

                                            <?= htmlspecialchars($data['currency']) ?>

                                        </span>

                                    </li>


                                    <li class="list-group-item">

                                        <b>

                                            Result Pattern

                                        </b>

                                        <span class="float-right">

                                            <?= htmlspecialchars($data['result_pattern']) ?>

                                        </span>

                                    </li>

                                </ul>

                            </div>

                        </div>

                    </div>



                    <!-- ====================================================== -->
                    <!-- RIGHT SIDE -->
                    <!-- ====================================================== -->

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

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

                            <div class="card-header">

                                <h3 class="card-title">

                                    <i class="fas fa-info-circle"></i>
                                    School Details

                                </h3>

                            </div>



                            <div class="card-body">

                                <div class="row">

                                    <!-- SCHOOL NAME -->

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

                                        <label class="font-weight-bold text-primary">

                                            School Name

                                        </label>

                                        <div class="border rounded p-3 bg-light">

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

                                        </div>

                                    </div>



                                    <!-- PHONE -->

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

                                        <label class="font-weight-bold text-primary">

                                            Phone Number

                                        </label>

                                        <div class="border rounded p-3 bg-light">

                                            <?= htmlspecialchars($data['school_phone']) ?>

                                        </div>

                                    </div>



                                    <!-- EMAIL -->

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

                                        <label class="font-weight-bold text-primary">

                                            Email Address

                                        </label>

                                        <div class="border rounded p-3 bg-light">

                                            <?= htmlspecialchars($data['school_email']) ?>

                                        </div>

                                    </div>



                                    <!-- WEBSITE -->

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

                                        <label class="font-weight-bold text-primary">

                                            Website

                                        </label>

                                        <div class="border rounded p-3 bg-light">

                                            <?= htmlspecialchars($data['school_website']) ?>

                                        </div>

                                    </div>



                                    <!-- REGISTRATION -->

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

                                        <label class="font-weight-bold text-primary">

                                            Registration Number

                                        </label>

                                        <div class="border rounded p-3 bg-light">

                                            <?= htmlspecialchars($data['school_reg_no']) ?>

                                        </div>

                                    </div>



                                    <!-- DISE -->

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

                                        <label class="font-weight-bold text-primary">

                                            DISE Code

                                        </label>

                                        <div class="border rounded p-3 bg-light">

                                            <?= htmlspecialchars($data['dise_code']) ?>

                                        </div>

                                    </div>



                                    <!-- ADDRESS -->

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

                                        <label class="font-weight-bold text-primary">

                                            School Address

                                        </label>

                                        <div class="border rounded p-3 bg-light">

                                            <?= nl2br(htmlspecialchars($data['school_address'])) ?>

                                        </div>

                                    </div>



                                    <!-- UPDATED -->

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

                                        <label class="font-weight-bold text-primary">

                                            Last Updated

                                        </label>

                                        <div class="border rounded p-3 bg-light">

                                            <?= date('d M Y h:i A', strtotime($data['updated_at'])) ?>

                                        </div>

                                    </div>

                                </div>

                            </div>

                        </div>

                    </div>

                </div>

            <?php else: ?>

                <!-- ====================================================== -->
                <!-- EMPTY STATE -->
                <!-- ====================================================== -->

                <div class="card card-warning">

                    <div class="card-body text-center p-5">

                        <i
                            class="fas fa-school text-warning mb-4"
                            style="font-size:70px;"
                        ></i>

                        <h3>

                            No School Profile Found

                        </h3>

                        <p class="text-muted">

                            Please add your school profile settings first.

                        </p>

                        <a
                            href="addprofile.php"
                            class="btn btn-primary btn-lg mt-3"
                        >

                            <i class="fas fa-plus-circle"></i>
                            Add School Profile

                        </a>

                    </div>

                </div>

            <?php endif; ?>

        </div>

    </section>

</div>

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