<?php
$boxOptions = ['top_a' => t('care.box_top_left'),'top_b' => t('care.box_top_center'),'top_c' => t('care.box_top_right'),'top_ab' => t('care.box_top_left_double'),'top_bc' => t('care.box_top_right_double'),'top_all' => t('care.box_top_full'),'bottom_a' => t('care.box_bottom_left'),'bottom_b' => t('care.box_bottom_right'),'bottom_all' => t('care.box_bottom_full')];
$roomsByCode = [];
foreach ($rooms as $room) {
    $roomsByCode[$room['code']] = $room;
}
?>
<form method="post" action="/care-round/setup/save" class="card">
    <div class="card-header d-flex justify-content-between align-items-center">
        <div>
            <h3 class="card-title mb-1"><?= h(t('care.configure_boxes')) ?></h3>
            <div class="text-muted small"><?= h(t('care.setup_help')) ?></div>
        </div>
        <div class="d-flex gap-2"><a class="btn btn-outline-primary" href="/settings/rooms">🚪
                <?= h(t('settings.rooms')) ?></a><a class="btn" href="/care-round"><?= h(t('care.back_round')) ?></a>
        </div>
    </div>
    <div class="card-body">
        <div class="row g-3">
            <?php foreach (['care' => ($roomsByCode['care']['name'] ?? t('care.medical_room')),'quarantine' => ($roomsByCode['quarantine']['name'] ?? t('care.quarantine_room'))] as $room => $label): if (!isset($layouts[$room])) {
                continue;
            }$layout = $layouts[$room]; ?>
            <div class="col-md-6">
                <div class="card card-sm h-100">
                    <div class="card-header">
                        <h4 class="card-title"><?= h($label) ?></h4>
                    </div>
                    <div class="card-body row g-2">
                        <div class="col-6"><label class="form-label"><?= h(t('care.three_top_boxes')) ?></label><select
                                class="form-select" name="<?= h($room) ?>_top">
                                <option value="split" <?= $layout['top_layout'] === 'split' ? 'selected' : '' ?>>
                                    <?= h(t('care.three_separate')) ?></option>
                                <option value="left2" <?= $layout['top_layout'] === 'left2' ? 'selected' : '' ?>>
                                    <?= h(t('care.double_left')) ?></option>
                                <option value="right2" <?= $layout['top_layout'] === 'right2' ? 'selected' : '' ?>>
                                    <?= h(t('care.double_right')) ?></option>
                                <option value="merged" <?= $layout['top_layout'] === 'merged' ? 'selected' : '' ?>>
                                    <?= h(t('care.one_large')) ?></option>
                            </select></div>
                        <div class="col-6"><label class="form-label"><?= h(t('care.two_bottom_boxes')) ?></label><select
                                class="form-select" name="<?= h($room) ?>_bottom">
                                <option value="split" <?= $layout['bottom_layout'] === 'split' ? 'selected' : '' ?>>
                                    <?= h(t('care.two_separate')) ?></option>
                                <option value="merged" <?= $layout['bottom_layout'] === 'merged' ? 'selected' : '' ?>>
                                    <?= h(t('care.one_large')) ?></option>
                            </select></div>
                    </div>
                </div>
            </div><?php endforeach; ?>
        </div>
        <h3 class="mt-4"><?= h(t('care.current_assignment')) ?></h3>
        <div class="table-responsive">
            <table class="table table-vcenter">
                <thead>
                    <tr>
                        <th><?= h(t('common.animal')) ?></th>
                        <th><?= h(t('care.room')) ?></th>
                        <th><?= h(t('care.box')) ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($animals as $animal):$currentRoom = ShelterRoomService::resolveAnimal($animal, $roomsByCode); ?>
                    <tr>
                        <td class="fw-bold"><?= h($animal['name']) ?></td>
                        <td><select class="form-select care-room-select" name="room[<?= (int)$animal['id'] ?>]"
                                data-animal="<?= (int)$animal['id'] ?>"><?php foreach ($rooms as $room): ?><option
                                    value="<?= h($room['code']) ?>"
                                    <?= $currentRoom === $room['code'] ? 'selected' : '' ?>><?= h($room['name']) ?>
                                </option><?php endforeach; ?></select></td>
                        <td><select class="form-select care-box-select" name="box[<?= (int)$animal['id'] ?>]"
                                data-animal="<?= (int)$animal['id'] ?>">
                                <option value=""><?= h(t('care.box_unassigned')) ?></option><?php foreach ($rooms as $room):$codes = array_column($room['boxes'], 'code');
                                    $legacy = in_array($room['code'], ['care','quarantine'], true) && !array_diff($codes, ['top_a','top_b','top_c','bottom_a','bottom_b']) && !array_diff(['top_a','top_b','top_c','bottom_a','bottom_b'], $codes);
                                    $options = $legacy ? $boxOptions : array_column($room['boxes'], 'name', 'code');
                                    foreach ($options as $key => $label): ?><option value="<?= h($key) ?>" data-room="<?= h($room['code']) ?>"
                                    <?= $currentRoom === $room['code'] && ($animal['care_box_key'] ?? '') === $key ? 'selected' : '' ?>>
                                    <?= h($label) ?></option><?php endforeach;endforeach; ?>
                            </select></td>
                    </tr><?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <div class="mt-3"><label class="form-label"><?= h(t('care.assignment_reason')) ?></label><input
                class="form-control" name="location_change_reason" maxlength="255"
                placeholder="<?= h(t('care.assignment_placeholder')) ?>"></div>
    </div>
    <div class="card-footer text-end"><button class="btn btn-primary"><?= h(t('care.save_setup')) ?></button></div>
</form>
<script>
    document.querySelectorAll('.care-room-select').forEach(room => {
        const sync = () => {
            const box = document.querySelector(`.care-box-select[data-animal="${room.dataset.animal}"]`);
            if (!box) return;
            let visibleSelected = false;
            box.querySelectorAll('option[data-room]').forEach(option => {
                option.hidden = option.dataset.room !== room.value;
                if (!option.hidden && option.selected) visibleSelected = true;
            });
            if (!visibleSelected) box.value = '';
            box.disabled = !Array.from(box.options).some(option => option.dataset.room === room.value && !
                option.hidden);
        };
        room.addEventListener('change', sync);
        sync();
    });

</script>
