package svc

import (
	"applet/app/db"
	"applet/app/db/model"
	"applet/app/e"
	"applet/app/svc"
	"applet/app/utils"
	"applet/app/utils/qrcode"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"strings"
)

func QrcodePayStyle(c *gin.Context) {
	eg := svc.MasterDb(c)
	user := svc.GetUser(c)
	m, _ := SysModDataBySkipIdentifier(c, "pub.flutter.community_store_qrcode_style")
	bytes := utils.MarshalJSONCamelCase2JsonSnakeCase(m.Data)
	m.Data = string(bytes)
	fmt.Println(m.Data)
	store := db.GetStoreIdEg(eg, utils.IntToStr(user.Info.Uid))
	if store != nil {
		m.Data = strings.ReplaceAll(m.Data, "{店铺名}", store.Name)
		m.Data = strings.ReplaceAll(m.Data, "{店铺编号}", utils.IntToStr(store.Uid))
	}
	var res = make(map[string]interface{})
	json.Unmarshal([]byte(m.Data), &res)
	e.OutSuc(c, res, nil)
	return
}

// 获取mod data by SkipIdentifier
func SysModDataBySkipIdentifier(c *gin.Context, name string) (*model.SysModule, error) {
	mod, err := db.SysModFindBySkipIdentifier(c, db.DBs[c.GetString("mid")], name)
	if err != nil {
		return nil, err
	}
	return mod, nil
}
func QrcodeBase(c *gin.Context) {
	user := svc.GetUser(c)
	qrcodeData := db.GetStorePayQrcodeByUid(svc.MasterDb(c), user.Info.Uid)
	eg := svc.MasterDb(c)
	m, _ := SysModDataBySkipIdentifier(c, "pub.flutter.community_store_qrcode_style")
	bytes := utils.MarshalJSONCamelCase2JsonSnakeCase(m.Data)
	m.Data = string(bytes)
	fmt.Println(m.Data)
	store := db.GetStoreIdEg(eg, utils.IntToStr(user.Info.Uid))
	if store != nil {
		m.Data = strings.ReplaceAll(m.Data, "{店铺名}", store.Name)
		m.Data = strings.ReplaceAll(m.Data, "{店铺编号}", utils.IntToStr(store.Uid))
	}
	css := make(map[string]interface{})
	json.Unmarshal([]byte(m.Data), &css)
	res := map[string]interface{}{
		"pay_url":    "",
		"pay_qrcode": "",
		"css":        css,
	}
	if qrcodeData != nil {
		h5Domain := svc.GetWebSiteDomainInfo(c, "wap")
		content := h5Domain + "/#/zy-cake-single-order/pages/scan-result/scan-pay-result?qrcode_id=" + utils.IntToStr(qrcodeData.Id)
		res["pay_url"] = content
		res["pay_qrcode"] = strings.ReplaceAll(qrcode.GetPNGBase64(content), "\u0000", "")
	}
	e.OutSuc(c, res, nil)
	return
}
func Qrcode(c *gin.Context) {
	var arg map[string]string
	if err := c.ShouldBindJSON(&arg); err != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err)
		return
	}
	user := svc.GetUser(c)
	arg["uid"] = utils.IntToStr(user.Info.Uid)
	arg["store_type"] = "1"
	cate, total := db.GetStoreQrcode(svc.MasterDb(c), arg)
	list := make([]map[string]string, 0)
	if cate != nil {
		h5Domain := svc.GetWebSiteDomainInfo(c, "wap")
		for _, v := range *cate {
			tmp := map[string]string{
				"id":        utils.IntToStr(v.Id),
				"table_num": v.TableNum,
				"name":      v.Name,
				"is_use":    utils.IntToStr(v.IsUse),
				"qrcode":    "",
				"url":       "",
			}
			content := h5Domain + "/#/zy-cake-single-order/pages/scan-result/scan-buy-result?qrcode_id=" + utils.AnyToString(v.Id)
			tmp["qrcode"] = strings.ReplaceAll(qrcode.GetPNGBase64(content), "\u0000", "")
			tmp["url"] = content
			list = append(list, tmp)
		}
	}
	res := map[string]interface{}{
		"list": list, "total": total,
	}
	e.OutSuc(c, res, nil)
	return
}
func QrcodeSave(c *gin.Context) {
	var arg map[string]string
	if err := c.ShouldBindJSON(&arg); err != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err)
		return
	}
	user := svc.GetUser(c)
	if arg["id"] != "" {
		data := db.GetStoreQrcodeById(svc.MasterDb(c), arg["id"])
		data.TableNum = arg["table_num"]
		data.Name = arg["name"]
		data.IsUse = utils.StrToInt(arg["is_use"])
		svc.MasterDb(c).Where("id=?", data.Id).Update(data)
	} else {
		data := &model.CommunityTeamStoreQrcode{
			Uid:      user.Info.Uid,
			TableNum: arg["table_num"],
			IsUse:    utils.StrToInt(arg["is_use"]),
			Name:     arg["name"],
		}
		svc.MasterDb(c).Insert(data)
	}
	e.OutSuc(c, "success", nil)
	return
}
func QrcodeDel(c *gin.Context) {
	var arg map[string]string
	if err := c.ShouldBindJSON(&arg); err != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err)
		return
	}
	svc.MasterDb(c).In("id", strings.Split(arg["ids"], ",")).Delete(&model.CommunityTeamStoreQrcode{})
	e.OutSuc(c, "success", nil)
	return
}
