column('id', 'ID')->sortable();
$grid->column('name', '赛季名称');
$grid->column('start_time', '开始时间')->sortable();
$grid->column('end_time', '结束时间')->sortable();
$grid->column('boss_power', 'Boss战力')->sortable();
$grid->column('status', '状态')->display(function ($value) {
$statuses = [
PetBattleSeason::STATUS_NOT_STARTED => '未开始',
PetBattleSeason::STATUS_IN_PROGRESS => '进行中',
PetBattleSeason::STATUS_ENDED => '已结束',
];
return $statuses[$value] ?? '未知';
})->label();
$grid->column('created_at', '创建时间');
$grid->column('updated_at', '更新时间');
// 添加行操作
$grid->actions(function (Grid\Displayers\Actions $actions) {
// 添加查看队伍按钮
$actions->append('查看队伍');
});
// 筛选
$grid->filter(function ($filter) {
$helper = new FilterHelper($filter, $this);
$helper->equal('id', 'ID');
$filter->like('name', '赛季名称');
$filter->equal('status', '状态')->select([
PetBattleSeason::STATUS_NOT_STARTED => '未开始',
PetBattleSeason::STATUS_IN_PROGRESS => '进行中',
PetBattleSeason::STATUS_ENDED => '已结束',
]);
$filter->between('start_time', '开始时间')->datetime();
$filter->between('end_time', '结束时间')->datetime();
});
// 默认按开始时间倒序排序
$grid->model()->orderBy('start_time', 'desc');
return $grid;
});
}
/**
* 详情页
*
* @param mixed $id
* @return Show
*/
protected function detail($id)
{
return Show::make($id, new PetBattleSeasonRepository(['teams']), function (Show $show) {
$helper = new ShowHelper($show, $this);
$helper->field('id', 'ID');
$show->field('name', '赛季名称');
$show->field('start_time', '开始时间');
$show->field('end_time', '结束时间');
$show->field('boss_power', 'Boss战力');
$show->field('reward_pool', '奖池配置')->json();
$show->field('status', '状态')->as(function ($value) {
$statuses = [
PetBattleSeason::STATUS_NOT_STARTED => '未开始',
PetBattleSeason::STATUS_IN_PROGRESS => '进行中',
PetBattleSeason::STATUS_ENDED => '已结束',
];
return $statuses[$value] ?? '未知';
});
$show->field('created_at', '创建时间');
$show->field('updated_at', '更新时间');
// 显示赛季队伍
$show->teams('参赛队伍', function ($teams) {
$teams->resource('/admin/pet-battle-teams');
$teams->id('ID');
$teams->name('队伍名称');
$teams->leader_id('队长ID');
$teams->total_power('队伍总战力');
$teams->member_count('成员数量');
$teams->created_at('创建时间');
});
return $show;
});
}
/**
* 表单
*
* @return Form
*/
protected function form()
{
return Form::make(new PetBattleSeasonRepository(), function (Form $form) {
$helper = new FormHelper($form, $this);
$form->display('id', 'ID');
$form->text('name', '赛季名称')
->required()
->help('赛季的名称,如"第一赛季"、"松狮争霸赛2023春季"等');
$form->datetime('start_time', '开始时间')
->required();
$form->datetime('end_time', '结束时间')
->required();
$form->number('boss_power', 'Boss战力')
->min(1000)
->default(10000)
->required()
->help('Boss的战力值,决定了挑战的难度');
$helper->embedsCats('reward_pool', '奖池配置')
->help('赛季奖池配置,JSON格式');
$form->select('status', '状态')
->options([
PetBattleSeason::STATUS_NOT_STARTED => '未开始',
PetBattleSeason::STATUS_IN_PROGRESS => '进行中',
PetBattleSeason::STATUS_ENDED => '已结束',
])
->default(PetBattleSeason::STATUS_NOT_STARTED)
->required();
$form->display('created_at', '创建时间');
$form->display('updated_at', '更新时间');
// 保存前回调
$form->saving(function (Form $form) {
// 验证开始时间必须早于结束时间
if ($form->start_time && $form->end_time) {
$startTime = strtotime($form->start_time);
$endTime = strtotime($form->end_time);
if ($startTime >= $endTime) {
return $form->error('开始时间必须早于结束时间');
}
}
});
return $form;
});
}
}