package router

import (
	"demo/data/dao"
	"demo/data/domain"
	"demo/share"
	"demo/util/templatefunc"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"net/http"
	"strconv"
)

func TestRouth(engine *gin.RouterGroup) {
	user := engine.Group("/test")
	{
		PushRouter(user, "POST", "/order/pay", OrderSubmit)
		PushRouter(user, "POST", "/test", DataTest)

		PushRouter(user, "GET", "/home", Home)
		PushRouter(user, "GET", "/topic", Topic)
		PushRouter(user, "GET", "/detail", Detail)
		PushRouter(user, "GET", "/search", SearchPage)
		PushRouter(user, "GET", "/info/:infoName", InfoPage)
	}

}

func OrderSubmit(c *gin.Context) {
	id := GetUserIdByToken(c)
	if id == 0 {
		return
	}
	data := share.GetJsonAnyParam(c)
	orderId, _ := data("orderId")
	err := dao.OrderPaySuccess(cast.ToInt64(orderId), id)
	if err != nil {
		c.JSON(200, CreateResultError(401, err.Error()))
		return
	}
	c.JSON(200, CreateResult())
}

func DataTest(c *gin.Context) {
	body := make(map[string]interface{})
	c.ShouldBindJSON(&body)
	fmt.Println("router:", body)
	fmt.Println("router:", c.Request.Body)
	c.JSON(200, CreateResultData("test"))
}

func Home(c *gin.Context) {
	m := make(map[string]interface{})
	//首页宣传信息
	data, err := dao.GetAdviceData()
	if err != nil {
		c.JSON(200, CreateResultError(401, "advice查询失败"))
		return
	}
	marshal, err := json.Marshal(data)
	if err != nil {
		fmt.Println(err)
		c.JSON(200, CreateResultError(401, err.Error()))
		return
	}
	json.Unmarshal(marshal, &m)

	//首页主题信息
	list, err := dao.GetTopicList()
	if err != nil {
		c.JSON(200, CreateResultError(401, "主题列表查询失败"))
		return
	}
	m2 := map[string]interface{}{
		"list": list,
	}
	//结构体转map
	d2m, err := json.Marshal(m2)
	if err != nil {
		fmt.Println(err)
		c.JSON(200, CreateResultError(401, err.Error()))
		return
	}
	json.Unmarshal(d2m, &m)

	fmt.Println("data:", m)
	c.HTML(http.StatusOK, "index.html", m)
}

func Topic(c *gin.Context) {
	topicId := c.Query("topicId")
	m := make(map[string]interface{})
	list, err := dao.GetTopicPageDataListById(cast.ToInt64(topicId))
	if err != nil {
		c.JSON(200, CreateResultError(401, "主题列表查询失败"))
		return
	}
	dm, err := json.Marshal(list)
	if err != nil {
		fmt.Println(err)
		c.JSON(200, CreateResultError(401, "json转换失败"))
		return
	}
	json.Unmarshal(dm, &m)
	fmt.Println("data:", m)
	c.HTML(http.StatusOK, "topic.html", m)
}

func Detail(c *gin.Context) {
	skuId := c.Query("skuId")
	m := make(map[string]interface{})
	//标签
	tagList, err := dao.GetGoodsTagList(domain.GoodsTag{}, 1, 10000)
	if err != nil {
		c.JSON(200, CreateResultError(401, "文章标签查询失败"))
		return
	}
	tagMap := map[string]interface{}{
		"tags": tagList.List,
	}
	tagString, err := json.Marshal(tagMap)
	if err != nil {
		fmt.Println(err)
		c.JSON(200, CreateResultError(401, "json转换失败"))
		return
	}
	json.Unmarshal(tagString, &m)
	//文章内容
	vo, err := dao.SelectSkuInfoBySkuId(cast.ToInt64(skuId))
	if err != nil {
		c.JSON(200, CreateResultError(401, "文章查询失败"))
		return
	}
	dm, err := json.Marshal(vo)
	if err != nil {
		fmt.Println(err)
		c.JSON(200, CreateResultError(401, "json转换失败"))
		return
	}
	json.Unmarshal(dm, &m)

	fmt.Println("data:", m)
	c.HTML(http.StatusOK, "detail.html", m)
}

func InfoPage(c *gin.Context) {
	infoName := c.Param("infoName")
	content := templatefunc.InfoPageContentMap[infoName]
	m := make(map[string]interface{})
	if content.InfoName != "" {
		dm, err := json.Marshal(content)
		if err != nil {
			fmt.Println(err)
			c.JSON(200, CreateResultError(401, "json转换失败"))
			return
		}
		json.Unmarshal(dm, &m)
		c.HTML(http.StatusOK, "info.html", m)
		return
	}
	c.JSON(200, CreateResultError(401, "页面不存在"))

}

func SearchPage(c *gin.Context) {
	key := c.Query("key")
	pageNum := c.Query("pageNum")
	pageSize := c.Query("pageSize")

	fmt.Println("key:", key, "num:", pageNum, "size:", pageSize)
	m := make(map[string]interface{})
	arr, count, err := dao.GetSkuByKey2(key, cast.ToInt(pageNum), cast.ToInt(pageSize))
	if err != nil {
		c.JSON(200, CreateResultError(401, "未查询到结果"))
		return
	}
	arrM := map[string]interface{}{
		"list": arr,
	}
	list, err := json.Marshal(arrM)
	fmt.Println("list:", string(list))
	json.Unmarshal(list, &m)
	m["count"] = count
	fmt.Println("map:", m)
	c.HTML(http.StatusOK, "search.html", m)
}

func GetUserId(c *gin.Context) int64 {
	header := c.GetHeader("auth-sign")
	if header == "" {
		return 0
	}
	claims, err := share.ParseToken(header)
	if err != nil {
		return 0
	}
	i, err := strconv.ParseInt(claims["iss"].(string), 10, 32)
	return i
}
