<?php

declare(strict_types=1);

ob_start();

$old = $old ?? [];

?>

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

                    <div class="mb-3">

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

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

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

                            <?php foreach ($branches as $branch): ?>

                                <option
                                    value="<?= (int) $branch['id'] ?>"
                                    <?= (int) ($old['branch_id'] ?? 0)
                                        === (int) $branch['id']
                                        ? 'selected'
                                        : '' ?>>

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

                                </option>

                            <?php endforeach; ?>

                        </select>

                    </div>

                    <div class="mb-3">

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

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

                    </div>

                    <div class="mb-4">

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

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

                    </div>

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

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

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

                    </div>

                </form>

            </div>
        </div>
    </div>
</div>

<?php

$content = ob_get_clean();

$title = 'Create Department';

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