<?php

namespace App\Livewire\Forms;

use App\Models\Room;
use App\Models\Branch;
use App\Enums\RoomType;

use Livewire\Component;
use Illuminate\Database\Eloquent\Collection;


class RoomGrid extends Component
{
    protected $listeners = ['openModal','closeModal'];//,'refreshRooms' => '$refresh'

    public bool $showModal = false;
    public int $branch_id = 0;
    public $branchName="";
    public array $roomTypes;
    

    public function mount()
    {
        $this->roomTypes = ['all' => '全部'] + collect(RoomType::cases())->mapWithKeys(fn($e) => [$e->value => $e->labels()])->toArray();
    }
    public function openModal($branch_id = null)
    { 
        $this->branch_id = $branch_id;
        $branch = Branch::find($branch_id);
        $this->branchName = Branch::find($branch_id)?->name ?? '';
        $this->showModal = true;
    }
    public function closeModal(){
        $this->showModal = false;
        $this->branch_id = 0;
    }

    public function render()
    {
        $rooms = Room::where('branch_id', $this->branch_id)->get();
        // 取得樓層
        $floors = $rooms->pluck('floor')->unique()->sort()->values()->toArray();

        return view('livewire.forms.room-grid',['rooms' =>$rooms,'floors' =>$floors]);
    }
}
