<?php
namespace App\Admin\Controllers;
use App\ServiceType;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;
class ServiceTypeController extends Controller
{
use HasResourceActions;
public function index(Content $content)
{
return $content
->header('服务类型')
->description('列表')
->body($this->grid());
}
public function show($id, Content $content)
{
return $content
->header('服务类型')
->description('详情')
->body($this->detail($id));
}
public function edit($id, Content $content)
{
return $content
->header('编辑')
->description('服务类型')
->body($this->form()->edit($id));
}
public function create(Content $content)
{
return $content
->header('创建')
->description('服务类型')
->body($this->form());
}
protected function grid()
{
$grid = new Grid(new ServiceType);
$grid->id('Id');
$grid->name('名称');
$grid->status('状态')->switch( [
'on' => ['value' => 1, 'text' => '打开', 'color' => 'primary'],
'off' => ['value' => 0, 'text' => '关闭', 'color' => 'default'],
]);
$grid->pay_type('支付方式')->using($this->payTypes());
$grid->column('价格')->display(function (){
if ($this->pay_type == 'price') {
return $this->price .'元';
}
if($this->pay_type == 'hour'){
return $this->hour_price. '元/小时';
}
});
$grid->created_at('创建时间');
$grid->updated_at('更新时间');
return $grid;
}
protected function detail($id)
{
$show = new Show(ServiceType::findOrFail($id));
$show->id('Id');
$show->status('状态')->using([0 => '禁用', 1=>'启用']);
$show->name('名称');
$show->options('配置');
$show->created_at('创建时间');
$show->updated_at('更新时间');
return $show;
}
protected function form()
{
$form = new Form(new ServiceType);
$form->switch('status', '状态');
$form->radio('pay_type','收费方式')->options($this->payTypes())->default('price')->rules('required');
$form->decimal('price', '价格')->rules('required')->default(0);
$form->decimal('hour_price', '价格(元/小时)')->rules('required')->default(0);
$form->text('name', '名称')->rules('required');
$form->table('options','表单', function ($table) {
$table->text('名称');
$table->select('类型')->options(['text'=>'文本', 'radio'=> '单选', 'textarea' => '多文本', 'select' => '下拉选', 'file' => '文件']);
$table->text('配置');
$table->text('验证');
})->rules('required');
return $form;
}
protected function payTypes(){
return ['free' => '免费', 'price' => '直接购买','hour'=> '按时收费'];
}
}