<?php

namespace App\Filament\Resources\GroupResource\RelationManagers;

use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class UsersRelationManager extends RelationManager
{
    protected static string $relationship = 'users';

    protected static ?string $title = '分组成员';

    protected static ?string $modelLabel = '用户';

    public function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('name')
                    ->label('用户名称')
                    ->required()
                    ->maxLength(255),
                Forms\Components\TextInput::make('email')
                    ->label('邮箱')
                    ->email()
                    ->required()
                    ->maxLength(255),
            ]);
    }

    public function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('name')
            ->columns([
                Tables\Columns\TextColumn::make('id')
                    ->label('ID')
                    ->sortable(),
                Tables\Columns\TextColumn::make('name')
                    ->label('用户名称')
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('email')
                    ->label('邮箱')
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('created_at')
                    ->label('加入时间')
                    ->dateTime('Y-m-d H:i:s')
                    ->sortable(),
            ])
            ->filters([
                //
            ])
            ->headerActions([
                Tables\Actions\AttachAction::make()
                    ->label('添加成员')
                    ->preloadRecordSelect()
                    ->modalHeading('添加分组成员')
                    ->modalSubmitActionLabel('添加')
                    ->modalCancelActionLabel('取消'),
            ])
            ->actions([
                Tables\Actions\DetachAction::make()
                    ->label('移除')
                    ->modalHeading('移除分组成员')
                    ->modalDescription('确定要将此用户从分组中移除吗？')
                    ->modalSubmitActionLabel('确认移除')
                    ->modalCancelActionLabel('取消'),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DetachBulkAction::make()
                        ->label('批量移除')
                        ->modalHeading('批量移除分组成员')
                        ->modalDescription('确定要将选中的用户从分组中移除吗？')
                        ->modalSubmitActionLabel('确认移除')
                        ->modalCancelActionLabel('取消'),
                ]),
            ]);
    }
}
