<?php

$pageTitle = "General Settings";

require_once '../includes/auth.php';

require_login();

$user = current_user();

$successMessage='';
$errorMessage='';

/*
|--------------------------------------------------------------------------
| FLASH MESSAGE
|--------------------------------------------------------------------------
*/

if(!empty($_SESSION['success_message'])){

    $successMessage=$_SESSION['success_message'];

    unset($_SESSION['success_message']);

}

if(!empty($_SESSION['error_message'])){

    $errorMessage=$_SESSION['error_message'];

    unset($_SESSION['error_message']);

}

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

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

    try{

        $systemName=trim($_POST['system_name']);

        $organization=trim($_POST['organization']);

        $systemEmail=trim($_POST['system_email']);

        $supportPhone=trim($_POST['support_phone']);

        $address=trim($_POST['address']);

        $currentBatch=trim($_POST['current_batch']);

        $currentStream=trim($_POST['current_stream']);

        $currentServiceYear=$_POST['current_service_year'];

        $attendanceRadius=(int)$_POST['attendance_radius'];

        $allowRegistration=$_POST['allow_registration'];

        $stmt=$db->prepare("

        UPDATE system_settings

        SET

        system_name=?,

        organization=?,

        system_email=?,

        support_phone=?,

        address=?,

        current_batch=?,

        current_stream=?,

        current_service_year=?,

        attendance_radius=?,

        allow_registration=?

        WHERE id=1

        ");

        $stmt->execute([

            $systemName,

            $organization,

            $systemEmail,

            $supportPhone,

            $address,

            $currentBatch,

            $currentStream,

            $currentServiceYear,

            $attendanceRadius,

            $allowRegistration

        ]);

        $_SESSION['success_message']="Settings updated successfully.";

        header("Location: general-settings.php");

        exit;

    }

    catch(Throwable $e){

        $_SESSION['error_message']=$e->getMessage();

        header("Location: general-settings.php");

        exit;

    }

}

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

$stmt=$db->query("

SELECT *

FROM system_settings

LIMIT 1

");

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

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

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

?>

<main class="dashboard-main">

    <?php require_once '../includes/topbar.php'; ?>

    <div class="dashboard-main-body">

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

            <div>

                <h6 class="fw-semibold mb-0">

                    <?= $pageTitle ?>

                </h6>

                <p class="text-neutral-500">

                    <?= APP_SHORT_NAME ?>

                    >

                    Settings

                    >

                    General Settings

                </p>

            </div>

        </div>

        <?php if($successMessage): ?>

        <div class="alert alert-success">

            <?= $successMessage ?>

        </div>

        <?php endif; ?>

        <?php if($errorMessage): ?>

        <div class="alert alert-danger">

            <?= $errorMessage ?>

        </div>

        <?php endif; ?>

        <div class="card">

            <div class="card-header">

                <h5>

                    General Settings

                </h5>

            </div>

            <div class="card-body">

                <form method="POST">

                    <div class="row">

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

                            <label class="form-label">

                                System Name

                            </label>

                            <input type="text" name="system_name" class="form-control"
                                value="<?= htmlspecialchars($settings['system_name']) ?>">

                        </div>

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

                            <label class="form-label">

                                Organization

                            </label>

                            <input type="text" name="organization" class="form-control"
                                value="<?= htmlspecialchars($settings['organization']) ?>">

                        </div>

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

                            <label class="form-label">

                                System Email

                            </label>

                            <input type="email" name="system_email" class="form-control"
                                value="<?= htmlspecialchars($settings['system_email']) ?>">

                        </div>

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

                            <label class="form-label">

                                Support Phone

                            </label>

                            <input type="text" name="support_phone" class="form-control"
                                value="<?= htmlspecialchars($settings['support_phone']) ?>">

                        </div>

                        <div class="col-12 mb-3">

                            <label class="form-label">

                                Address

                            </label>

                            <textarea name="address" rows="3"
                                class="form-control"><?= htmlspecialchars($settings['address']) ?></textarea>

                        </div>

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

                            <label class="form-label">

                                Current Batch

                            </label>

                            <select name="current_batch" class="form-select">

                                <option value="A" <?= ($settings['current_batch']=='A') ? 'selected' : '' ?>>

                                    Batch A

                                </option>

                                <option value="B" <?= ($settings['current_batch']=='B') ? 'selected' : '' ?>>

                                    Batch B

                                </option>

                                <option value="C" <?= ($settings['current_batch']=='C') ? 'selected' : '' ?>>

                                    Batch C

                                </option>

                            </select>

                        </div>

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

                            <label class="form-label">

                                Current Stream

                            </label>

                            <select name="current_stream" class="form-select">

                                <option value="I" <?= ($settings['current_stream']=='I') ? 'selected' : '' ?>>

                                    Stream I

                                </option>

                                <option value="II" <?= ($settings['current_stream']=='II') ? 'selected' : '' ?>>

                                    Stream II

                                </option>

                            </select>

                        </div>

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

                            <label class="form-label">

                                Current Service Year

                            </label>

                            <input type="number" min="2024" max="2100" name="current_service_year" class="form-control"
                                value="<?= htmlspecialchars($settings['current_service_year']) ?>">

                        </div>

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

                            <label class="form-label">

                                Attendance Radius (Meters)

                            </label>

                            <input type="number" min="1" name="attendance_radius" class="form-control"
                                value="<?= htmlspecialchars($settings['attendance_radius']) ?>">

                        </div>

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

                            <label class="form-label">

                                Allow Registration

                            </label>

                            <select name="allow_registration" class="form-select">

                                <option value="yes" <?= ($settings['allow_registration']=='yes') ? 'selected' : '' ?>>

                                    Yes

                                </option>

                                <option value="no" <?= ($settings['allow_registration']=='no') ? 'selected' : '' ?>>

                                    No

                                </option>

                            </select>

                        </div>

                        <hr>

                        <h5 class="mb-3">

                            System Files

                        </h5>

                        <div class="row">

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

                                <label class="form-label">

                                    Current Logo

                                </label>

                                <div class="border rounded p-3 text-center">

                                    <?php if(!empty($settings['logo'])): ?>

                                    <img src="../uploads/settings/<?= htmlspecialchars($settings['logo']) ?>"
                                        style="max-height:100px;">

                                    <?php else: ?>

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

                                        No Logo Uploaded

                                    </p>

                                    <?php endif; ?>

                                </div>

                            </div>

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

                                <label class="form-label">

                                    Current Favicon

                                </label>

                                <div class="border rounded p-3 text-center">

                                    <?php if(!empty($settings['favicon'])): ?>

                                    <img src="../uploads/settings/<?= htmlspecialchars($settings['favicon']) ?>"
                                        style="max-height:70px;">

                                    <?php else: ?>

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

                                        No Favicon Uploaded

                                    </p>

                                    <?php endif; ?>

                                </div>

                            </div>

                        </div>

                        <div class="mt-4">

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

                                <i class="ri-save-line"></i>

                                Save Settings

                            </button>

                        </div>

                </form>

            </div>

        </div>

    </div>

    <?php require_once "../includes/footer.php"; ?>