<?php

declare(strict_types=1);

ob_start();

?>

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

    <div>
        <h2 class="mb-1">
            Edit Position
        </h2>

        <p class="text-muted mb-0">
            Update position information.
        </p>
    </div>

    <a
        href="/public/positions"
        class="btn btn-outline-secondary">

        Back

    </a>

</div>

<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/positions/edit">

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

                    <div class="mb-3">

                        <label class="form-label">
                            Organization / Branch / Department
                        </label>

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

                            <?php foreach (
                                $departments as $department
                            ): ?>

                                <option
                                    value="<?= (int) $department['id'] ?>"
                                    <?= (int) $position['department_id']
                                        === (int) $department['id']
                                        ? 'selected'
                                        : '' ?>>

                                    <?= htmlspecialchars(
                                        $department['organization_name']
                                        . ' — '
                                        . $department['branch_name']
                                        . ' — '
                                        . $department['department_name'],
                                        ENT_QUOTES,
                                        'UTF-8'
                                    ) ?>

                                </option>

                            <?php endforeach; ?>

                        </select>

                    </div>

                    <div class="mb-3">

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

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

                    </div>

                    <div class="mb-3">

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

                        <input
                            type="text"
                            name="position_name"
                            class="form-control"
                            maxlength="150"
                            value="<?= htmlspecialchars(
                                (string) $position['position_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) $position['status'] === 1
                                    ? 'checked'
                                    : '' ?>>

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

                                Active Position

                            </label>

                        </div>

                    </div>

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

                        <a
                            href="/public/positions"
                            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 Position';

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