138 lines
4.7 KiB
PHP
138 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Controllers;
|
|
|
|
use App\Admin\Renderable\ShippingRuleTable;
|
|
use App\Admin\Repositories\ShippingRule;
|
|
use App\Models\ShippingRule as ShippingRuleModel;
|
|
use App\Models\ShippingTemplate;
|
|
use App\Models\Zone;
|
|
use Dcat\Admin\Form;
|
|
use Dcat\Admin\Grid;
|
|
use Dcat\Admin\Http\Controllers\AdminController;
|
|
use Dcat\Admin\Show;
|
|
use Illuminate\Support\Facades\Request;
|
|
|
|
class ShippingRuleController extends AdminController
|
|
{
|
|
/**
|
|
* Make a grid builder.
|
|
*
|
|
* @return Grid
|
|
*/
|
|
protected function grid()
|
|
{
|
|
return ShippingRuleTable::grid();
|
|
}
|
|
|
|
/**
|
|
* Make a show builder.
|
|
*
|
|
* @param mixed $id
|
|
*
|
|
* @return Show
|
|
*/
|
|
protected function detail($id)
|
|
{
|
|
return Show::make($id, new ShippingRule(), function (Show $show) {
|
|
$show->field('id');
|
|
$show->field('template_id');
|
|
$show->field('type');
|
|
$show->field('info');
|
|
$show->field('remarks');
|
|
$show->field('created_at');
|
|
$show->field('updated_at');
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a form builder.
|
|
*
|
|
* @return Form
|
|
*/
|
|
protected function form()
|
|
{
|
|
// $builder = ShippingRule::with('zones');
|
|
$builder = new ShippingRule();
|
|
return Form::make($builder, function (Form $form) {
|
|
$templateId = Request::get('template_id', 0);
|
|
$form->display('id');
|
|
if ($templateId) {
|
|
$form->select('template_id_show', __('shipping-rule.fields.template_id'))->options(ShippingTemplate::all()->pluck('name', 'id'))->value($templateId)->disable();
|
|
$form->hidden('template_id')->value($templateId);
|
|
} else {
|
|
$form->select('template_id')->options(ShippingTemplate::all()->pluck('name', 'id'))->required();
|
|
}
|
|
$form->text('remarks');
|
|
$form->tree('zones')
|
|
->nodes(Zone::get()->toArray()) // 设置所有节点
|
|
->setTitleColumn('name')
|
|
// ->customFormat(function ($v) {
|
|
// if (!$v) {
|
|
// return [];
|
|
// }
|
|
// return array_column($v, 'id');
|
|
// })
|
|
->expand(false)->required();
|
|
$form->radio('type')
|
|
->when(1, function (Form $form) {
|
|
$form->embeds('info1', function ($form) {
|
|
$form->text('threshold')->rules('required_if:type,1', ['required_if'=>'请填写包邮门槛']);
|
|
})->customFormat(function () {
|
|
if ($this->model()->type == '1') {
|
|
return $this->model()->info;
|
|
} else {
|
|
return [];
|
|
}
|
|
});
|
|
})
|
|
->when(2, function (Form $form) {
|
|
$form->embeds('info2', function ($form) {
|
|
$form->number('first_weight')->min(1)->rules('required_if:type,2', ['required_if'=>'请填写计费首重'])->default(2);
|
|
$form->currency('first_w_amount')->rules('required_if:type,2', ['required_if'=>'请填写首重价格'])->symbol('¥');
|
|
$form->number('continue_weight')->min(1)->rules('required_if:type,2', ['required_if'=>'请填写计费续重'])->default(1);
|
|
$form->currency('continue_w_amount')->rules('required_if:type,2', ['required_if'=>'请填写续重价格'])->symbol('¥');
|
|
})->customFormat(function () {
|
|
if ($this->model()->type == '2') {
|
|
return $this->model()->info;
|
|
} else {
|
|
return [];
|
|
}
|
|
});
|
|
})
|
|
->options([
|
|
1 => '包邮',
|
|
2 => '计重',
|
|
])->default(1);
|
|
|
|
$form->hidden('info');
|
|
$form->ignore(['template_id_show']);
|
|
|
|
$form->saving(function (Form $form) {
|
|
$info = 'info'.$form->type;
|
|
$form->info = json_encode($form->$info);
|
|
|
|
$form->deleteInput('info1');
|
|
$form->deleteInput('info2');
|
|
});
|
|
|
|
$form->display('created_at');
|
|
$form->display('updated_at');
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 重构删除
|
|
*
|
|
* @param [type] $id
|
|
* @return void
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
$rule = ShippingRuleModel::findOrFail($id);
|
|
// $rule->zones()->detach();
|
|
|
|
return parent::destroy($id);
|
|
}
|
|
}
|