<?php

namespace App\Livewire\Forms;

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

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

use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;

class RoleForm extends Component
{
    use WireUiActions;

    protected $listeners = ['openCreateRoleModal','openEditRoleModal', 'deleteRole'];

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

    public $showCreateModal=false;
    public ?int $roleId = null;
    public $name = '';
    public $permissions = []; // 所有權限清單
    public $selectedPermissions = []; // 表單中選到的權限

   

    public function mount()
    {
        $this->permissions = Permission::all();
        $this->canCreate = Auth::user()?->can('role-edit') ?? false;
        $this->canEdit = Auth::user()?->can('role-edit') ?? false;
        $this->canDelect = Auth::user()?->can('role-delete') ?? false;
    }

    public function openCreateRoleModal()
    {
        $this->resetFields();
        $this->showCreateModal = true;
    }

    public function openEditRoleModal($id)
    {
        $role = Role::findOrFail($id);
        $this->roleId = $role->id;
        $this->name = $role->name;
        $this->selectedPermissions = $role->permissions()->pluck('id')->toArray();
        $this->showCreateModal = true;
    }
    
    public function save()
    {
        $this->validate([
            'name' => 'required|string|max:255',
            'selectedPermissions' => 'array',
        ]);
        
        if ($this->roleId) {
            if ($this->canEdit) {
                $role = Role::findOrFail($this->roleId);
                $role->update(['name' => $this->name]);
                $role->syncPermissions($this->selectedPermissions);
                $this->notification()->send([
                    'icon' => 'success',
                    'title' => '成功',
                    'description' => '角色已更新',
                ]);
            }
        } else {
            if ($this->canCreate) {
                $role = Role::create(['name' => $this->name]);
                $role->syncPermissions($this->selectedPermissions);
                $this->notification()->send([
                    'icon' => 'success',
                    'title' => '成功',
                    'description' => '角色已新增',
                ]);
            }
        }

        $this->resetFields();
        $this->showCreateModal = false;
        $this->dispatch('pg:eventRefresh-role-table');
    }

    public function deleteRole($id)
    {
        if ($this->canDelect) {
            Role::findOrFail($id)->delete();
            $this->notification()->send([
                'icon' => 'success',
                'title' => '成功',
                'description' => '角色已刪除',
            ]);
            $this->dispatch('pg:eventRefresh-role-table');
        }
    }

    public function resetFields()
    {
        $this->name = '';
        $this->selectedPermissions = [];
        $this->roleId = null;
    }

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