<?php

namespace App\Filament\Resources\MenuPermissionResource\Pages;
use Illuminate\Database\Eloquent\Builder;

use App\Filament\Resources\MenuPermissionResource;
use Filament\Actions;
use Filament\Resources\Pages\CreateRecord;

class CreateMenuPermission extends CreateRecord
{
    protected static string $resource = MenuPermissionResource::class;

    protected function mutateFormDataBeforeCreate(array $data): array
    {
        // 如果没有提供排序，默认使用当前用户的最大排序+1
        if (empty($data['sort_order'])) {
            $maxOrder = \App\Models\MenuPermission::where('user_id', $data['user_id'])
                ->max('sort_order');
            $data['sort_order'] = ($maxOrder ?? -1) + 1;
        }

        return $data;
    }
}
