package formview

import (
	"go_dreamfactory/cmd/v2/lib/common"
	"go_dreamfactory/cmd/v2/model"
	"go_dreamfactory/cmd/v2/service"
	"go_dreamfactory/pb"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/widget"
	"github.com/sirupsen/logrus"
	"github.com/spf13/cast"
)

type ShopBuyView struct {
	BaseformView
}

func (this *ShopBuyView) CreateView(t *model.TestCase) fyne.CanvasObject {
	shopType := widget.NewEntry()
	shopType.PlaceHolder = "商店类型"
	this.form.AppendItem(widget.NewFormItem(common.APP_TESTCASE_FORM_ITEM_GEZIID, shopType))

	goodsId := widget.NewEntry()
	goodsId.PlaceHolder = "商品ID"
	this.form.AppendItem(widget.NewFormItem(common.APP_TESTCASE_FORM_ITEM_GOODSID, goodsId))

	amount := widget.NewEntry()
	amount.PlaceHolder = "购买数量"
	this.form.AppendItem(widget.NewFormItem(common.APP_TESTCASE_FORM_LABEL_NUM, amount))

	this.form.OnSubmit = func() {
		if err := service.GetPttService().SendToClient(t.MainType, t.SubType, &pb.ShopBuyReq{
			ShopType: pb.ShopType(cast.ToInt32(shopType.Text)),
			GoodsId:  cast.ToInt32(goodsId.Text),
			BuyNum:   cast.ToInt32(amount.Text),
		}); err != nil {
			logrus.Error(err)
		}
	}
	return this.form
}
