<?php

declare(strict_types=1);

ob_start();

?>

<h2 class="mb-4">Edit County</h2>

<div class="row">

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

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

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

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

                    <div class="alert alert-danger">

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

                    </div>

                <?php endif; ?>

                <form
                    method="POST"
                    action="/public/counties/edit">

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

                    <div class="mb-3">

                        <label class="form-label">
                            State / Administrative Area
                        </label>

                        <select
                            name="state_id"
                            class="form-select"
                            required>

                            <?php foreach (
                                $states as $state
                            ): ?>

                                <option
                                    value="<?= (int) $state['id'] ?>"
                                    <?= (int) ($county['state_id'] ?? 0)
                                        === (int) $state['id']
                                        ? 'selected'
                                        : '' ?>>

                                    <?= htmlspecialchars(
                                        $state['state_name']
                                        . ' — '
                                        . (
                                            $state['administrative_type']
                                            === 'ADMINISTRATIVE_AREA'
                                                ? 'Administrative Area'
                                                : 'State'
                                        ),
                                        ENT_QUOTES,
                                        'UTF-8'
                                    ) ?>

                                </option>

                            <?php endforeach; ?>

                        </select>

                    </div>

                    <div class="mb-3">

                        <label class="form-label">
                            County Code
                        </label>

                        <input
                            type="text"
                            name="county_code"
                            class="form-control"
                            maxlength="30"
                            value="<?= htmlspecialchars(
                                (string) (
                                    $county['county_code'] ?? ''
                                ),
                                ENT_QUOTES,
                                'UTF-8'
                            ) ?>"
                            required>

                    </div>

                    <div class="mb-3">

                        <label class="form-label">
                            County Name
                        </label>

                        <input
                            type="text"
                            name="county_name"
                            class="form-control"
                            maxlength="150"
                            value="<?= htmlspecialchars(
                                (string) (
                                    $county['county_name'] ?? ''
                                ),
                                ENT_QUOTES,
                                'UTF-8'
                            ) ?>"
                            required>

                    </div>

                    <div class="mb-4">

                        <div class="form-check form-switch">

                            <input
                                type="checkbox"
                                class="form-check-input"
                                name="status"
                                id="status"
                                value="1"
                                <?= (int) (
                                    $county['status'] ?? 0
                                ) === 1
                                    ? 'checked'
                                    : '' ?>>

                            <label
                                class="form-check-label"
                                for="status">

                                Active County

                            </label>

                        </div>

                    </div>

                    <div class="d-flex justify-content-between">

                        <a
                            href="/public/counties"
                            class="btn btn-light">
                            Cancel
                        </a>

                        <button
                            type="submit"
                            class="btn btn-primary">
                            Save Changes
                        </button>

                    </div>

                </form>

            </div>

        </div>

    </div>

</div>

<?php

$content = ob_get_clean();

$title = 'Edit County';

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