package controllers_join

import (
	"encoding/json"
	"net/http"
	"kkscrap-go/locals"
	models_themes "kkscrap-go/models/themes"
	models_join "kkscrap-go/models/join"

	"github.com/dabory/abango-rest"
	"github.com/labstack/echo"
)

type SlipSearchPageReq struct {
	QueryVars        locals.QueryVars
	SlipSearchVars   locals.SlipSearchVars
	SlipSearchFields locals.SlipSearchFields
	PageVars         locals.PageVars
}

func SlipSearchPage(c echo.Context) error {

	v := c.Get("receiver").(SlipSearchPageReq)
	y := c.Get("abango").(abango.Controller)

	slip := v.QueryVars.QueryName
	if slip == "eyetest" {
		vRet := &struct {
			models_themes.EyetestSearchPageRet
		}{}

		vRet.QueryVars = v.QueryVars
		vRet.SlipSearchVars = v.SlipSearchVars
		vRet.SlipSearchFields = v.SlipSearchFields
		vRet.PageVars = v.PageVars

		// fmt.Println(y.Gtb.UserId)

		if err := vRet.GetEyetestSearchPage(&y); err != nil {
			return c.String(607, err.Error())
		}

		ret, _ := json.Marshal(vRet)
		return c.JSONBlob(http.StatusOK, ret)

	} else {

		vRet := &struct {
			models_join.SlipSearchPageRet
		}{}

		vRet.QueryVars = v.QueryVars
		vRet.SlipSearchVars = v.SlipSearchVars
		vRet.SlipSearchFields = v.SlipSearchFields
		vRet.PageVars = v.PageVars

		// fmt.Println(y.Gtb.UserId)

		if err := vRet.GetSlipSearchPage(&y); err != nil {
			return c.String(607, err.Error())
		}

		ret, _ := json.Marshal(vRet)
		return c.JSONBlob(http.StatusOK, ret)
		// return c.String(709, e.LogStr("234adfq3fae", "QueryName Not Found in SlipSearchBook Controller for '"+slip+"'"))
	}
}
