package formview

import (
	"go_dreamfactory/cmd/v2/model"
	"go_dreamfactory/cmd/v2/service"
	"go_dreamfactory/pb"
	"strings"

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

type SysFuncListView struct {
	BaseformView
}

func (this *SysFuncListView) CreateView(t *model.TestCase) fyne.CanvasObject {
	keyEntry := widget.NewEntry()
	this.form.AppendItem(widget.NewFormItem("Key", keyEntry))
	this.form.OnSubmit = func() {
		s := strings.SplitN(keyEntry.Text, ",", 2)
		req := &pb.SysFuncGetListReq{}
		if s[0] == "" {
			req.Keys = []string{}
		} else {
			req.Keys = s
		}
		if err := service.GetPttService().SendToClient(
			t.MainType,
			t.SubType,
			req,
		); err != nil {
			logrus.Error(err)
			return
		}
		logrus.WithFields(logrus.Fields{"mainType": t.MainType, "subType": t.SubType, "params": req}).Info("功能开启列表")
	}

	newBtn := widget.NewButton("新的", func() {
		if err := service.GetPttService().SendToClient(
			t.MainType,
			"funcgetlist",
			&pb.SysFuncGetListReq{Keys: []string{"pavilion_kick"}},
		); err != nil {
			logrus.Error(err)
			return
		}
	})

	c := container.NewBorder(newBtn, nil, nil, nil, this.form)
	return c
}
