<?php

declare(strict_types=1);

ob_start();

?>

<h2 class="mb-4">Edit Branch</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(
                            $error,
                            ENT_QUOTES,
                            'UTF-8'
                        ) ?>

                    </div>

                <?php endif; ?>

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

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

                    <div class="mb-3">

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

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

                            <?php foreach (
                                $organizations as $organization
                            ): ?>

                                <option
                                    value="<?= (int) $organization['id'] ?>"
                                    <?= (int) $branch['organization_id']
                                        === (int) $organization['id']
                                        ? 'selected'
                                        : '' ?>>

                                    <?= htmlspecialchars(
                                        $organization['organization_name'],
                                        ENT_QUOTES,
                                        'UTF-8'
                                    ) ?>

                                </option>

                            <?php endforeach; ?>

                        </select>

                    </div>

                    <div class="mb-3">

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

                        <input
                            type="text"
                            name="branch_name"
                            class="form-control"
                            value="<?= htmlspecialchars(
                                $branch['branch_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="active"
                                id="active"
                                value="1"
                                <?= (int) $branch['active'] === 1
                                    ? 'checked'
                                    : '' ?>>

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

                                Active Branch

                            </label>

                        </div>

                    </div>

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

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

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