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 ItemsUseView struct {
	BaseformView
}

func (this *ItemsUseView) CreateView(t *model.TestCase) fyne.CanvasObject {
	geziId := widget.NewEntry()
	geziId.PlaceHolder = "格子ID"
	this.form.AppendItem(widget.NewFormItem(common.APP_TESTCASE_FORM_ITEM_GEZIID, geziId))

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

	selectId := widget.NewEntry()
	selectId.PlaceHolder = "选择下标"
	this.form.AppendItem(widget.NewFormItem(common.APP_TESTCASE_FORM_ITEM_IDX, selectId))

	this.form.OnSubmit = func() {
		if err := service.GetPttService().SendToClient(
			t.MainType,
			t.SubType,
			&pb.ItemsUseItemReq{
				GridId: geziId.Text,				
				Amount: cast.ToUint32(amount.Text),
				Select: cast.ToInt32(selectId),
			},
		); err != nil {
			logrus.Error(err)
			return
		}

	}
	return this.form
}
