package controllers_join

import (
	"encoding/json"
	controllers_func "kkscrap-go/controllers/func"
	"kkscrap-go/locals"
	models_join "kkscrap-go/models/join"
	"net/http"

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

type ListType1PageReq struct {
	QueryVars     locals.QueryVars
	SetupVars     []locals.SetupBase
	ListType1Vars locals.ListType1Vars
	PageVars      locals.PageVars
}

func ListType1Page(c echo.Context) error {

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

	// vRet := &struct {
	// 	models_join.ListType1PageRet
	// }{}

	var vRet models_join.ListType1PageRet

	vRet.QueryVars = v.QueryVars
	vRet.SetupVars = v.SetupVars
	vRet.ListType1Vars = v.ListType1Vars
	vRet.PageVars = v.PageVars

	r := c.Request().URL.Path
	sqlDir := ""
	if r == "/list-type1-page" {
		sqlDir = "list"
	} else if r == "/search-type1-page" {
		sqlDir = "search"
	}

	if v.ListType1Vars.IsDownloadList == false {
		if err := vRet.GetListType1PagePlain(&y, sqlDir); err != nil {
			return c.String(607, err.Error())
		}
	} else if v.ListType1Vars.IsDownloadList == true {
		if err := vRet.GetListType1PageTmp(&y, sqlDir); err != nil {
			return c.String(607, err.Error())
		}
	}

	if len(vRet.SetupVars) != 0 {
		if err := controllers_func.SetupJsonArrayGet(&y, &vRet); err != nil {
			return c.String(607, err.Error())
		}
	}

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