<?php namespace App\Admin\Actions; use Dcat\Admin\Actions\Response; use App\Admin\Forms\MerchantStoreForms; use App\Models\Good; use Dcat\Admin\Grid\RowAction; use Dcat\Admin\Traits\HasPermissions; use Illuminate\Contracts\Auth\Authenticatable; //use Illuminate\Database\Eloquent\Model; use Illuminate\Http\Request; use Dcat\Admin\Widgets\Modal; use App\Models\GoodSku; use App\Models\MerchantGoodSku; class MerchantStore extends RowAction { protected $type = 0; protected $title = ''; protected $goods_id = 0; /** * 接收参数 * @type 1:增加库存 2:减少库存 */ public function __construct($type = 0, $title = '', $goods_id = 0) { $this->type = $type; $this->goods_id = $goods_id; parent::__construct($title); $this->title = $title; } /** * @return string */ //protected $title = '<i class="feather icon-stop-circle"> 增加库存 </i>'; /** * 按钮文本 * * @return string|void */ public function title() { return '<i class="feather icon-stop-circle"></i> ' . $this->title . $this->type . ''; } /** * Handle the action request. * * @param Request $request * * @return Response */ public function handle(Request $request) { $id = $this->getKey() ?? 0; $type = $request->get('type'); return $this->response()->success('成功')->refresh(); } /** * 渲染模态框. * @return Modal */ public function render() { $mgs_id = $this->getKey() ?? 0; $goodObj = Good::where("id", $this->goods_id)->first(); //获取商品规格信息 if ($this->type == 1) { $dataList = GoodSku::where("goods_id", $this->goods_id)->get(); } else { $dataList = MerchantGoodSku::where("mgs_id", $mgs_id)->get(); foreach ($dataList as $kk => $vv) { $skuObj = GoodSku::find($vv['attr_id']); $dataList[$kk]['attr_val'] = $skuObj->attr_val; } } $dataArr = $dataList ? $dataList->toArray() : []; $html = ''; foreach ($dataArr as $kk => $vv) { $html .= '<tr>'; if ($this->type == 2) { $html .= '<input type="hidden" name="sskuid[]" value="' . $vv['id'] . '">'; } $html .= '<input type="hidden" name="goods_id" value="' . $this->goods_id . '">'; $html .= '<input type="hidden" name="mgs_id" value="' . $mgs_id . '">'; $html .= '<input type="hidden" name="typeid" value="' . $this->type . '">'; $html .= '<td style="text-align:center;vertical-align: middle;">' . $goodObj->goods_name . '<input type="hidden" name="attr_id[]" value="' . $vv['id'] . '"></td>'; $html .= '<td style="text-align:center;vertical-align: middle;">' . $vv['attr_val'] . '</td>'; $html .= '<td ><input type="text" name="stock[]" value="" class="form-control field_remark _normal_" placeholder=""></td>'; $html .= '<td style="text-align:center;vertical-align: middle;">' . $vv['stock'] . '</td>'; $html .= '</tr>'; } // 这里直接创建一个modal框 model的内容由工具表单提供,这里也需要创建一个工具表单才行 return Modal::make() ->lg() ->title($this->title) ->button("$this->title") // ->body('<form action="' . $actionUrl . '" method="post" class="form-horizontal" accept-charset="UTF-8" pjax-container="1" > // <div class="row"> // <div class="col-md-12"> // <table class="table"> // <thead> // <tr> // <th width="20%" style="text-align:center;">商品名称</th> // <th width="20%" style="text-align:center;">商品规格</th> // <th width="20%" style="text-align:center;">增加库存</th> // <th width="20%" style="text-align:center;">剩余库存</th> // </tr> // </thead> // <tbody> // <!-- 表格数据 --> // <!--<tr> // <td style="text-align:center;vertical-align: middle;">数据1</td> // <td style="text-align:center;vertical-align: middle;">蓝色、大号</td> // <td ><input type="text" name="remark" value="" class="form-control field_remark _normal_" placeholder=""></td> // <td style="text-align:center;vertical-align: middle;">123</td> // </tr>--> // ' . $html . ' // <!-- 更多行数据 --> // </tbody> // </table> // </div> // <div class="col-md-8"><button type="submit" class="btn btn-primary pull-right"><i class="feather icon-save"></i> 提交</button></div> // </div> // </form> // '); ->body(MerchantStoreForms::make(['id' => $this->type])) ->onShown( <<<js $("#storeTitle").html('{$this->title}'); $("#tabletr").html('"+ $html +"'); js ); //->button("<button class='btn btn-sm btn-primary'>$this->title</button>"); // 这个button就是对应上面的按钮 } /** * @return string|array|void */ public function confirm() { // return ['Confirm?', 'contents']; } /** * @param Model|Authenticatable|HasPermissions|null $user * * @return bool */ protected function authorize($user): bool { return true; } /** * @return array */ protected function parameters() { return []; } }