model()->where(['bidding_user_id' => Admin::user()->id,'bidding_user_type' =>  DemandTraits::$col[1]]);
            $grid->column('id')->sortable();
            $grid->column('price');
            $grid->column('comment');
			$grid->column('demand.title','竞拍标题');
			$grid->column('demand.comment','竞拍内容')
				->display('查看')
				->modal('详情',function ($modal) {
					$modal->xl();
					return $this->demand->comment;
				});
			$grid->column('state','状态')->using(DemandTraits::$biddingState)->dot(
				[
					'yellow',
					'success',
					'danger',
				]
			);
			$grid->column('bidding','操作')
				->if(function (){
					return $this->state == 1 && $this->bidding_user_type == DemandTraits::$col[1] && empty($this->demand->demand_product_id);
				})
				->then(function (Grid\Column $column) {
					$column->append('添加中标产品');
				})
				->if(function (){
					return $this->state == 1 && $this->bidding_user_type == DemandTraits::$col[1] && !empty($this->demand->demand_product_id);
				})
				->then(function (Grid\Column $column) {
					$column->append('查看产品');
				});
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
			$grid->disableDeleteButton();
			$grid->disableEditButton();
			$grid->disableQuickEditButton();
			$grid->disableViewButton();
			$grid->disableCreateButton();
			$grid->disableActions();
			$grid->disableRowSelector();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }
    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new DemandBidding(), function (Show $show) {
            $show->field('id');
            $show->field('price');
            $show->field('comment');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new DemandBidding(), function (Form $form) {
			$form->disableEditingCheck();
			$form->disableCreatingCheck();
			$form->disableDeleteButton();
			$form->disableViewButton();
			$form->disableViewCheck();
			$form->display('id')->disable();
			if(request('is_bidding',0)) {
				$form->textarea('comment')->disable()->required();
				$form->selectTable('demand_product_id', '产品')
					->title('选择产品')
					->dialogWidth('50%;min-width:600px;') //不起作用
					->from(SelectProduct::make())
					->model(DemandProduct::class)
					->required();
			}else{
				$form->text('price')->required();
				$form->textarea('comment')->required();
			}
			$form->hidden('demand_id')->value(request('demand_id',0));
			$form->hidden('bidding_user_type')->disable();
			$form->hidden('bidding_user_id')->disable();
			$form->saving(function (Form $form) {
				$form->deleteInput('demand_product_id');
				//发布人身份
				$form->bidding_user_type = DemandTraits::$col[1];
				$form->bidding_user_id = Admin::user()->id;
			});
			$form->saving(function (Form $form) {
				if ($form->isCreating()) {
					$isBidding = \App\Models\DemandBidding::query()
						->where('demand_id',$form->demand_id)
						->where([
							'bidding_user_type' => DemandTraits::$col[1],
							'bidding_user_id' => Admin::user()->id
						])
						->exists();
					if ($isBidding) {
						Admin::exit('你已经竞标过了,无需重复参加');
					}
				}
			});
			$form->saved(function (Form $form) {
				$provinceId = Demand::query()->where('id',$this->demand_id)->value('province_id');
				if ($provinceId != Admin::user()->province_id) {
					$form->response()->error('竞标失败,只能竞标跟自己相同省份的的需求');
				}
				if($form->isEditing()) {
					$productId = request('demand_product_id', 0);
					if (!empty($productId)) {
						DB::beginTransaction();
						try {
							//处理订单
							//将产品绑给代理商
							$demand = Demand::find($form->model()->demand_id);
							$demand->demand_product_id = $productId;
							$demandProduct = DemandProduct::find($productId);
							$product = new Product();
							$product->supplier_id = $demandProduct->supplier_id;
							$product->category_id = $demandProduct->category_id;
							$product->title = $demandProduct->title;
							$product->price = $form->model()->price;
							$product->original_price = $form->model()->price;
							$product->pictures = $demandProduct->pictures;
							$product->stock = $demand->stock;
							$product->know = $demandProduct->know;
							$product->content = $demandProduct->content;
							$product->agent_id = $form->model()->bidding_user_id;
							$product->save();
							//处理需求
							$demand = Demand::find($form->model()->demand_id);
							$demand->demand_product_id = $productId;
							$demand->save();
							$demand->product_id = $product->id;
							$demand->save();
							DB::commit();
						} catch (\Exception $e) {
							Log::error('分配订单失败::' . $e->getTraceAsString());
							DB::rollBack();
							return $form->response()->error('分配订单失败,稍后重试或联系管理员!' . $e->getMessage());
						}
					}
				}
				$form->response()->success('操作成功')->redirect('demand_bidding');
			});
        });
    }
}