<?php

declare(strict_types=1);

ob_start();

?>

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

    <div>
        <h2 class="mb-1">County Management</h2>

        <p class="text-muted mb-0">
            Manage counties under States and Administrative Areas.
        </p>
    </div>

    <a
        href="/public/counties/create"
        class="btn btn-primary">

        <i class="bi bi-plus-lg me-1"></i>
        Add County

    </a>

</div>

<?php if (!empty($created)): ?>
    <div class="alert alert-success">
        County created successfully.
    </div>
<?php endif; ?>

<?php if (!empty($updated)): ?>
    <div class="alert alert-success">
        County updated successfully.
    </div>
<?php endif; ?>

<?php if (!empty($statusChanged)): ?>
    <div class="alert alert-success">
        County status updated successfully.
    </div>
<?php endif; ?>

<?php if (!empty($error)): ?>

    <div class="alert alert-danger">

        <?= htmlspecialchars(
            (string) $error,
            ENT_QUOTES,
            'UTF-8'
        ) ?>

    </div>

<?php endif; ?>

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

    <div class="card-body">

        <div class="table-responsive">

            <table class="table table-hover align-middle">

                <thead>
                    <tr>
                        <th>ID</th>
                        <th>State / Area</th>
                        <th>Type</th>
                        <th>County Code</th>
                        <th>County Name</th>
                        <th>Status</th>
                        <th class="text-end">
                            Actions
                        </th>
                    </tr>
                </thead>

                <tbody>

                <?php if (empty($counties)): ?>

                    <tr>
                        <td
                            colspan="7"
                            class="text-center text-muted py-4">

                            No counties found.

                        </td>
                    </tr>

                <?php else: ?>

                    <?php foreach (
                        $counties as $county
                    ): ?>

                        <tr>

                            <td>
                                <?= (int) $county['id'] ?>
                            </td>

                            <td>
                                <?= htmlspecialchars(
                                    (string) $county['state_name'],
                                    ENT_QUOTES,
                                    'UTF-8'
                                ) ?>
                            </td>

                            <td>
                                <?= $county['administrative_type']
                                    === 'ADMINISTRATIVE_AREA'
                                    ? 'Administrative Area'
                                    : 'State' ?>
                            </td>

                            <td>
                                <?= htmlspecialchars(
                                    (string) $county['county_code'],
                                    ENT_QUOTES,
                                    'UTF-8'
                                ) ?>
                            </td>

                            <td>
                                <strong>
                                    <?= htmlspecialchars(
                                        (string) $county['county_name'],
                                        ENT_QUOTES,
                                        'UTF-8'
                                    ) ?>
                                </strong>
                            </td>

                            <td>

                                <?php if (
                                    (int) $county['status'] === 1
                                ): ?>

                                    <span class="badge text-bg-success">
                                        Active
                                    </span>

                                <?php else: ?>

                                    <span class="badge text-bg-secondary">
                                        Inactive
                                    </span>

                                <?php endif; ?>

                            </td>

                            <td class="text-end">

                                <div class="btn-group">

                                    <a
                                        href="/public/counties/edit?id=<?= (int) $county['id'] ?>"
                                        class="btn btn-sm btn-outline-primary">

                                        <i class="bi bi-pencil"></i>

                                    </a>

                                    <form
                                        method="POST"
                                        action="/public/counties/toggle-status">

                                        <input
                                            type="hidden"
                                            name="id"
                                            value="<?= (int) $county['id'] ?>">

                                        <button
                                            type="submit"
                                            class="btn btn-sm <?= (int) $county['status'] === 1
                                                ? 'btn-outline-danger'
                                                : 'btn-outline-success' ?>">

                                            <i class="bi <?= (int) $county['status'] === 1
                                                ? 'bi-x-circle'
                                                : 'bi-check-circle' ?>">
                                            </i>

                                        </button>

                                    </form>

                                </div>

                            </td>

                        </tr>

                    <?php endforeach; ?>

                <?php endif; ?>

                </tbody>

            </table>

        </div>

    </div>

</div>

<?php

$content = ob_get_clean();

$title = 'County Management';

include APP_PATH
    . '/Modules/Administration/Views/layouts/admin.php';