package user_feedback

import (
	"applet/app/db"
	"applet/app/e"
	"applet/app/md"
	"applet/app/svc"
	"applet/app/utils"
	"code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
	"code.fnuoos.com/EggPlanet/egg_models.git/src/model"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"github.com/tidwall/gjson"
	"strings"
	"time"
)

func List(c *gin.Context) {
	var req *md.UserFeedbackListReq
	if err := c.ShouldBindJSON(&req); err != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err)
		return
	}
	var resp md.UserFeedbackListResp
	noticeList := make([]md.UserFeedbackList, 0)
	NewUserFeedbackDb := implement.NewUserFeedbackDb(db.Db)
	uid := ""
	if req.Uid != "" {
		uid = req.Uid
	}
	if req.Phone != "" {
		uid = "-1"
		NewUserDb := implement.NewUserDb(db.Db)
		user, _ := NewUserDb.UserGetOneByParams(map[string]interface{}{
			"key":   "phone",
			"value": req.Phone,
		})
		if user != nil {
			uid = utils.Int64ToStr(user.Id)
		}
	}
	notice, total, _ := NewUserFeedbackDb.FindUserFeedbackAndTotal(req.Page, req.Limit, uid, req.Cid, "")
	resp.Total = total
	resp.SelectData = make([]map[string]interface{}, 0)
	NewUserFeedbackCateDb := implement.NewUserFeedbackCateDb(db.Db)
	pid, _ := NewUserFeedbackCateDb.FindUserFeedbackCate("1", "100")
	if pid != nil {
		for _, v := range *pid {
			tmp := map[string]interface{}{
				"name": v.Name, "value": utils.IntToStr(v.Id),
			}
			resp.SelectData = append(resp.SelectData, tmp)
		}
	}
	if notice != nil {
		platformMap := map[string]string{
			"android":   "安卓",
			"iOS":       "iOS",
			"wx_applet": "小程序",
		}
		for _, v := range *notice {
			v.Extra = strings.ToLower(v.Extra)
			platform := gjson.Get(v.Extra, "platform").String()
			img := make([]string, 0)
			json.Unmarshal([]byte(v.Img), &img)
			if v.Phone == "" {
				NewUserDb := implement.NewUserDb(db.Db)
				user, _ := NewUserDb.UserGetOneByParams(map[string]interface{}{
					"key":   "id",
					"value": v.Uid,
				})
				if user != nil {
					v.Phone = user.Phone
				}
			}
			tmp := md.UserFeedbackList{
				Id:       utils.IntToStr(v.Id),
				Phone:    v.Phone,
				Title:    v.Title,
				Platform: platformMap[platform],
				Version:  gjson.Get(v.Extra, "devicemodel").String() + " " + gjson.Get(v.Extra, "osversion").String() + " " + gjson.Get(v.Extra, "appversionname").String(),
				Type:     v.Type,
				Img:      img,
				Content:  v.Content,
				CreateAt: v.CreateAt.Format("2006-01-02 15:04:05"),
				State:    utils.IntToStr(v.State),
			}
			noticeList = append(noticeList, tmp)
		}
	}
	resp.List = noticeList
	e.OutSuc(c, resp, nil)
	return
}
func RecordList(c *gin.Context) {
	eg := db.Db
	var req md.UserFeedbackRecordListReq
	err := c.ShouldBindJSON(&req)
	if err != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err)
		return
	}
	NewUserFeedbackRecordDb := implement.NewUserFeedbackRecordDb(eg)
	article, _ := NewUserFeedbackRecordDb.FindUserFeedback(req.Page, "1000", req.Id)
	data := make([]md.UserFeedbackRecordListResp, 0)
	if article != nil {
		for _, v := range *article {
			tmp := md.UserFeedbackRecordListResp{
				Content:  v.Content,
				CreateAt: v.CreateAt.Format("2006-01-02"),
				Type:     "official",
			}
			if v.Uid > 0 {
				NewUserDb := implement.NewUserDb(eg)
				user, _ := NewUserDb.GetUser(int64(v.Uid))
				if user != nil {
					tmp.Nickname = user.Nickname
					tmp.HeadImg = svc.GetOssUrl(user.Avatar)
				}
				tmp.Type = "user"
			} else {
				tmp.Nickname = svc.GetSysCfgStr("app_name")
				tmp.HeadImg = svc.GetOssUrl(svc.GetSysCfgStr("app_logo"))
			}
			data = append(data, tmp)
		}
	}
	e.OutSuc(c, data, nil)
	return
}

func Say(c *gin.Context) {
	var req *md.UserFeedbackSayReq
	if err := c.ShouldBindJSON(&req); err != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err)
		return
	}
	data := model.UserFeedbackRecord{
		Lid:      utils.StrToInt(req.Id),
		Content:  req.Content,
		CreateAt: time.Now(),
	}
	db.Db.Insert(&data)
	e.OutSuc(c, "success", nil)
	return
}
func ChangeState(c *gin.Context) {
	var req *md.UserFeedbackChangeStateReq
	if err := c.ShouldBindJSON(&req); err != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err)
		return
	}
	NewUserFeedbackDb := implement.NewUserFeedbackDb(db.Db)
	notice, _ := NewUserFeedbackDb.GetUserFeedback(req.Id)
	if notice == nil {
		e.OutErr(c, 400, e.NewErr(400, "记录不存在"))
		return
	}
	notice.State = utils.StrToInt(req.State)
	db.Db.Where("id=?", notice.Id).Cols("state").Update(notice)
	e.OutSuc(c, "success", nil)
	return
}

func Del(c *gin.Context) {
	var req *md.UserFeedbackDelReq
	if err := c.ShouldBindJSON(&req); err != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err)
		return
	}
	db.Db.In("id", req.Id).Delete(&model.UserFeedback{})
	db.Db.In("lid", req.Id).Delete(&model.UserFeedbackRecord{})
	e.OutSuc(c, "success", nil)
	return
}