<?php

declare(strict_types=1);

ob_start();

$old = $old ?? [];

?>

<h2 class="mb-4">Create Payam</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/payams/create">

                    <div class="mb-3">

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

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

                            <option value="">
                                Select State / Area
                            </option>

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

                                <option
                                    value="<?= (int) $state['id'] ?>">

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

                                </option>

                            <?php endforeach; ?>

                        </select>

                    </div>

                    <div class="mb-3">

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

                        <select
                            name="county_id"
                            id="county_id"
                            class="form-select"
                            required>

                            <option value="">
                                Select County
                            </option>

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

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

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

                                </option>

                            <?php endforeach; ?>

                        </select>

                    </div>

                    <div class="mb-3">

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

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

                    </div>

                    <div class="mb-4">

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

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

                    </div>

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

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

                        <button
                            type="submit"
                            class="btn btn-primary">
                            Create Payam
                        </button>

                    </div>

                </form>

            </div>

        </div>

    </div>

</div>

<script>
document.addEventListener('DOMContentLoaded', function () {
    const stateSelect = document.getElementById('state_id');
    const countySelect = document.getElementById('county_id');
    const options = Array.from(countySelect.options);

    function filterCounties() {
        const stateId = stateSelect.value;

        countySelect.value = '';

        options.forEach(function (option) {
            if (option.value === '') {
                option.hidden = false;
                return;
            }

            option.hidden =
                option.dataset.stateId !== stateId;
        });
    }

    stateSelect.addEventListener(
        'change',
        filterCounties
    );
});
</script>

<?php

$content = ob_get_clean();

$title = 'Create Payam';

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