112 lines
3.5 KiB
PHP
112 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Controllers;
|
|
|
|
use App\Admin\Renderable\CouponRangeTable;
|
|
use App\Admin\Renderable\ProductSkuSimpleTable;
|
|
use App\Admin\Repositories\CouponRange;
|
|
use App\Models\Coupon;
|
|
use App\Models\ProductCategory;
|
|
use App\Models\ProductSku;
|
|
use Dcat\Admin\Form;
|
|
use Dcat\Admin\Grid;
|
|
use Dcat\Admin\Http\Controllers\AdminController;
|
|
use Dcat\Admin\Show;
|
|
use Illuminate\Support\Facades\Request;
|
|
|
|
class CouponRangeController extends AdminController
|
|
{
|
|
/**
|
|
* Make a grid builder.
|
|
*
|
|
* @return Grid
|
|
*/
|
|
protected function grid()
|
|
{
|
|
return CouponRangeTable::grid();
|
|
}
|
|
|
|
/**
|
|
* Make a show builder.
|
|
*
|
|
* @param mixed $id
|
|
*
|
|
* @return Show
|
|
*/
|
|
protected function detail($id)
|
|
{
|
|
return Show::make($id, new CouponRange(), function (Show $show) {
|
|
$show->field('id');
|
|
$show->field('coupon_id');
|
|
$show->field('type');
|
|
$show->field('ranges');
|
|
$show->field('created_at');
|
|
$show->field('updated_at');
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a form builder.
|
|
*
|
|
* @return Form
|
|
*/
|
|
protected function form()
|
|
{
|
|
return Form::make(new CouponRange(), function (Form $form) {
|
|
$couponId = Request::get('coupon_id', 0);
|
|
$form->display('id');
|
|
if ($couponId) {
|
|
$form->select('coupon_id_show', __('coupon-range.fields.coupon_id'))->options(Coupon::all()->pluck('name', 'id'))->value($couponId)->disable();
|
|
$form->hidden('coupon_id')->value($couponId);
|
|
} else {
|
|
$form->select('coupon_id')->options(Coupon::all()->pluck('name', 'id'))->required();
|
|
}
|
|
|
|
$form->radio('type')
|
|
->when(1, function (Form $form) {
|
|
$form->tree('ranges1')
|
|
->nodes(ProductCategory::get()->toArray()) // 设置所有节点
|
|
->setTitleColumn('name')
|
|
->customFormat(function ($v) {
|
|
if ($this->model()->type == 1) {
|
|
$v = $this->model()->ranges;
|
|
} else {
|
|
$v = [];
|
|
}
|
|
return $v;
|
|
})->expand(false);
|
|
})
|
|
->when(2, function (Form $form) {
|
|
$form->multipleSelectTable('ranges2')
|
|
->from(ProductSkuSimpleTable::make())
|
|
->model(ProductSku::class, 'id', 'name')
|
|
->customFormat(function ($v) {
|
|
if ($this->model()->type == 2) {
|
|
$v = $this->model()->ranges;
|
|
} else {
|
|
$v = [];
|
|
}
|
|
return $v;
|
|
});
|
|
})
|
|
->options([
|
|
1=>'商品分类',
|
|
2=>'SKU商品',
|
|
])->default(1);
|
|
$form->hidden('ranges');
|
|
$form->ignore(['coupon_id_show']);
|
|
|
|
$form->saving(function (Form $form) {
|
|
$ranges = 'ranges'.$form->type;
|
|
$form->ranges = $form->$ranges;
|
|
|
|
$form->deleteInput('ranges1');
|
|
$form->deleteInput('ranges2');
|
|
});
|
|
|
|
$form->display('created_at');
|
|
$form->display('updated_at');
|
|
});
|
|
}
|
|
}
|