<?php require __DIR__ . '/settings_nav.php'; ?>
<?php if ($saved): ?><div class="alert alert-success"><?= h(t('rooms.saved')) ?></div><?php endif; ?>
<?php if ($error): ?><div class="alert alert-danger"><?= h($error) ?></div><?php endif; ?>
<form method="post" action="/settings/rooms/save"><input type="hidden" name="csrf" value="<?= h(Auth::csrf()) ?>">
    <div class="alert alert-info"><?= h(t('rooms.help')) ?></div>
    <div class="row row-cards">
        <?php foreach ($rooms as $room):$boxNames = implode("\n", array_column(array_filter($room['boxes'], static fn ($box) => (int)$box['active'] === 1), 'name')); ?>
        <div class="col-lg-6">
            <div class="card h-100">
                <div class="card-status-top" style="background:<?= h($room['color']) ?>"></div>
                <div class="card-header">
                    <div>
                        <h3 class="card-title"><?= h($room['name']) ?></h3>
                        <div class="text-muted small"><code><?= h($room['code']) ?></code></div>
                    </div><label class="form-check form-switch ms-auto"><input class="form-check-input" type="checkbox"
                            name="rooms[<?= (int)$room['id'] ?>][active]" <?= $room['active'] ? 'checked' : '' ?>><span
                            class="form-check-label"><?= h(t('common.active')) ?></span></label>
                </div>
                <div class="card-body row g-3">
                    <div class="col-md-8"><label class="form-label"><?= h(t('common.name')) ?></label><input
                            class="form-control" name="rooms[<?= (int)$room['id'] ?>][name]"
                            value="<?= h($room['name']) ?>" required></div>
                    <div class="col-md-4"><label class="form-label"><?= h(t('rooms.color')) ?></label><input
                            class="form-control form-control-color w-100" type="color"
                            name="rooms[<?= (int)$room['id'] ?>][color]" value="<?= h($room['color']) ?>"></div>
                    <div class="col-md-4"><label class="form-label"><?= h(t('rooms.type')) ?></label><select
                            class="form-select" name="rooms[<?= (int)$room['id'] ?>][room_type]">
                            <option value="collective" <?= $room['room_type'] === 'collective' ? 'selected' : '' ?>>
                                <?= h(t('rooms.collective')) ?></option>
                            <option value="boxes" <?= $room['room_type'] === 'boxes' ? 'selected' : '' ?>>
                                <?= h(t('rooms.with_boxes')) ?></option>
                        </select></div>
                    <div class="col-md-4"><label class="form-label"><?= h(t('animal.status')) ?></label><select
                            class="form-select"
                            name="rooms[<?= (int)$room['id'] ?>][status_code]"><?php foreach (['refuge' => t('status.refuge'),'soin' => t('status.soin'),'quarantaine' => t('status.quarantaine')] as $key => $label): ?>
                            <option value="<?= h($key) ?>" <?= $room['status_code'] === $key ? 'selected' : '' ?>>
                                <?= h($label) ?></option><?php endforeach; ?></select></div>
                    <div class="col-md-4"><label class="form-label"><?= h(t('rooms.order')) ?></label><input
                            class="form-control" type="number" min="0" name="rooms[<?= (int)$room['id'] ?>][sort_order]"
                            value="<?= (int)$room['sort_order'] ?>"></div>
                    <div class="col-12"><label class="form-label"><?= h(t('rooms.boxes')) ?></label><textarea
                            class="form-control" rows="5" name="rooms[<?= (int)$room['id'] ?>][boxes]"
                            placeholder="<?= h(t('rooms.one_box_per_line')) ?>"><?= h($boxNames) ?></textarea>
                        <div class="form-hint"><?= h(t('rooms.boxes_help')) ?></div>
                    </div>
                    <div class="col-12"><label class="form-check"><input class="form-check-input" type="checkbox"
                                name="rooms[<?= (int)$room['id'] ?>][bulk_validation]"
                                <?= $room['bulk_validation'] ? 'checked' : '' ?>><span
                                class="form-check-label"><?= h(t('rooms.bulk_validation')) ?></span></label></div>
                </div>
            </div>
        </div><?php endforeach; ?>
        <div class="col-12">
            <div class="card border-primary">
                <div class="card-header">
                    <h3 class="card-title"><?= h(t('rooms.add')) ?></h3>
                </div>
                <div class="card-body row g-3">
                    <div class="col-md-4"><label class="form-label"><?= h(t('common.name')) ?></label><input
                            class="form-control" name="new_room[name]"></div>
                    <div class="col-md-3"><label class="form-label"><?= h(t('rooms.type')) ?></label><select
                            class="form-select" name="new_room[room_type]">
                            <option value="collective"><?= h(t('rooms.collective')) ?></option>
                            <option value="boxes"><?= h(t('rooms.with_boxes')) ?></option>
                        </select></div>
                    <div class="col-md-3"><label class="form-label"><?= h(t('animal.status')) ?></label><select
                            class="form-select" name="new_room[status_code]">
                            <option value="refuge"><?= h(t('status.refuge')) ?></option>
                            <option value="soin"><?= h(t('status.soin')) ?></option>
                            <option value="quarantaine"><?= h(t('status.quarantaine')) ?></option>
                        </select></div>
                    <div class="col-md-2"><label class="form-label"><?= h(t('rooms.color')) ?></label><input
                            class="form-control form-control-color w-100" type="color" name="new_room[color]"
                            value="#6c757d"></div>
                    <div class="col-12"><label class="form-label"><?= h(t('rooms.boxes')) ?></label><textarea
                            class="form-control" name="new_room[boxes]" rows="3"
                            placeholder="<?= h(t('rooms.one_box_per_line')) ?>"></textarea></div>
                    <div class="col-12"><label class="form-check"><input class="form-check-input" type="checkbox"
                                name="new_room[bulk_validation]"><span
                                class="form-check-label"><?= h(t('rooms.bulk_validation')) ?></span></label></div>
                </div>
            </div>
        </div>
    </div>
    <div class="mt-4 text-end"><button class="btn btn-primary"><?= h(t('common.save')) ?></button></div>
</form>
