columnId();
$grid->column('chest.name', '宝箱名称')->link(function () {
return admin_url('game-items/' . $this->chest_id);
});
$grid->column('cost_type', '消耗类型')->display(function ($value) {
return CHEST_COST_TYPE::getAll()[$value] ?? '未知';
});
$grid->column('cost_id', '消耗ID')->as(function ($value) {
if ($this->cost_type == CHEST_COST_TYPE::ITEM->value) {
if ($this->costItem) {
$itemUrl = admin_url('game-items-items/' . $value);
return "{$this->costItem->name} (ID: {$value})";
}
return $value;
}
return $value;
})->unescape();
$grid->column('cost_quantity', '消耗数量');
$grid->column('is_active', '是否激活')->switch();
$grid->column('created_at', '创建时间');
$grid->column('updated_at', '更新时间');
// 筛选器
$grid->filter(function (Grid\Filter $filter) {
$helper = new FilterHelper($filter, $this);
$helper->equal('id', 'ID');
$helper->equalSelectModelChestItem('chest_id', '宝箱ID');
$filter->equal('cost_type', '消耗类型')->select(CHEST_COST_TYPE::getAll());
$filter->equal('is_active', '是否激活')->radio([
1 => '是',
0 => '否',
]);
});
// 批量操作
$grid->batchActions(function (Grid\Tools\BatchActions $batch) {
$batch->add(new \App\Module\GameItems\AdminControllers\Actions\BatchDeactivateAction());
$batch->add(new BatchActivateAction());
});
// 工具栏
$grid->tools(function (Grid\Tools $tools) {
$tools->append(new \App\Module\GameItems\AdminControllers\Actions\CopyToAnotherChestAction());
});
});
}
/**
* 详情页
*
* @param mixed $id
* @return Show
*/
protected function detail($id)
{
return Show::make($id, new ItemChestOpenCostRepository(['chest', 'costItem']), function (Show $show) {
$helper = new ShowHelper($show, $this);
$helper->field('id', 'ID');
$show->field('chest.name', '宝箱名称');
$helper->field('chest_id', '宝箱ID');
$show->field('cost_type', '消耗类型')->as(function ($value) {
return CHEST_COST_TYPE::getAll()[$value] ?? '未知';
});
$helper->field('cost_id', '消耗ID');
// 根据消耗类型显示不同的关联信息
$show->field('costItem.name', '消耗物品名称')->unescape()->as(function ($name) {
if ($this->cost_type == CHEST_COST_TYPE::ITEM->value && $name) {
$itemUrl = admin_url('game-items/items/' . $this->cost_id);
return "{$name} "
. ""
. " 查看物品详情";
}
return '-';
});
$helper->field('cost_quantity', '消耗数量');
$show->field('is_active', '是否激活')->as(function ($value) {
return $value ? '是' : '否';
});
$helper->field('created_at', '创建时间');
$helper->field('updated_at', '更新时间');
});
}
/**
* 表单
*
* @return Form
*/
protected function form()
{
return Form::make(new ItemChestOpenCostRepository(), function (Form $form) {
$helper = new FormHelper($form, $this);
// 只显示宝箱类型的物品
$chests = Item::where('type', 3)->pluck('name', 'id');
$helper->selectModelChestItem('chest_id', '宝箱');
$form->select('cost_type', '消耗类型')->options(CHEST_COST_TYPE::getAll())->required();
// 根据消耗类型显示不同的选择器
$form->number('cost_id', '消耗ID')->required();
$helper->number('cost_quantity', '消耗数量')->min(1)->default(1)->required();
$form->switch('is_active', '是否激活')->default(1);
// 保存前回调
$form->saving(function (Form $form) {
// 验证消耗ID
if ($form->cost_type == CHEST_COST_TYPE::ITEM->value) {
$item = Item::find($form->cost_id);
if (!$item) {
return $form->response()->error('无效的物品ID');
}
}
});
});
}
}