<?php

namespace App\Livewire\Forms;

use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\Auth;

use Livewire\Component;
use WireUi\Traits\WireUiActions;

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

class RoomForm extends Component
{
    use WireUiActions;
    
    protected $listeners = ['openModal','closeModal', 'deleteRoom'];

    public bool $canCreate;
    public bool $canEdit;
    public bool $canDelect;

    public bool $showModal = false;
    public ?int $roomId = null;

    public array $typeOptions =[];
    
    public array $fields = [
        'floor' =>'',
        'type' =>'',
        'name' =>''
    ];
    

    public function mount()
    {
        $this->typeOptions = collect(RoomType::cases())->map(fn ($type) => [
            'name' => $type->labels(),
            'value' => $type->value,
        ])->toArray();
        $this->canCreate = Auth::user()?->can('room-edit') ?? false;
        $this->canEdit = Auth::user()?->can('room-edit') ?? false;
        $this->canDelect = Auth::user()?->can('room-delete') ?? false;
    }

    public function openModal($id = null)
    {
        $this->resetFields();

        if ($id) {
            $room = Room::findOrFail($id);
            $this->roomId = $room->id;
            $this->fields = $room->only(array_keys($this->fields));
        }
    
        $this->showModal = true;
    }

    public function closeModal()
    {
        $this->resetFields();
        $this->showModal = false;
    }
    
    public function save()
    {
        $description ="無權修改";
        if ($this->roomId) {
            if ($this->canEdit) {
                $room = Room::findOrFail($this->roomId);
                $room->update($this->fields);
                $description='分店已更新';
            }
        } else {
            if ($this->canCreate) {
                $room = Room::create([
                    'floor' => $this->fields['floor'],
                    'type' => $this->fields['type'],
                    'name' => $this->fields['name'],
                ]);
                $description='分店已新增';
            }
        }
        $this->notification()->send([
            'icon' => 'success',
            'title' => '成功',
            'description' => $description,
        ]);
        $this->resetFields();
        $this->showModal = false;
        $this->dispatch('pg:eventRefresh-room-table');
    }
    
    public function deleteRoom($id)
    {
        if ($this->canDelect) {
            Room::findOrFail($id)->delete();
            $this->notification()->send([
                'icon' => 'success',
                'title' => '成功',
                'description' => '分店已刪除',
            ]);
            
            $this->dispatch('pg:eventRefresh-room-table');
        }
    }

    public function resetFields()
    {
        foreach ($this->fields as $key => $value) {
            if ($key == 'enable') {
                $this->fields[$key] = true;
            } else {
                $this->fields[$key] = '';
            }
        }
        $this->branchId = null;
    }

    public function render()
    {
        return view('livewire.forms.room-form');
    }
}
