package mobile

import (
	"fmt"
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
	"gin-vue-admin/model/response"
	"gin-vue-admin/service"
	"gin-vue-admin/utils"
	"github.com/gin-gonic/gin"
	"github.com/xuri/excelize/v2"
	"go.uber.org/zap"
	"strconv"
	"time"
)

func GetSurveyUserList(c *gin.Context) {
	var req request.GetSurveyUserListReq
	_ = c.ShouldBindJSON(&req)

	err, list, total := service.GetSurveyUserList(req)

	reply := request.GetSurveyUserListReply{}
	reply.List = list
	reply.Total = total

	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Any("err", err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithDetailed(reply, "获取成功", c)
	}
	return
}

func GetSurveyLogList(c *gin.Context) {
	var req request.GetSurveyLogListReq
	_ = c.ShouldBindJSON(&req)

	err, list, total := service.GetSurveyLogList(req)

	reply := request.GetSurveyLogListReply{}
	reply.List = list
	reply.Total = total

	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Any("err", err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithDetailed(reply, "获取成功", c)
	}
	return
}

func CreateSurveyUser(c *gin.Context) {
	var req request.CreateSurveyUserReq
	_ = c.ShouldBindJSON(&req)

	ipData, _ := service.GetIpaddr(c.ClientIP())

	SurveyUser := model.SurveyUser{
		SurveyId:        1,
		UserId:          req.UserId,
		MocId:           req.MocId,
		Contacts:        req.Contacts,
		ContactsMobile:  req.ContactsMobile,
		Reference:       req.Reference,
		ReferenceMobile: req.ReferenceMobile,
		CreateTime:      utils.NowTime(),
		Country:         ipData.Country,
		Area:            ipData.Area,
		Region:          ipData.Region,
		City:            ipData.City,
	}

	_, surveyUserId := service.CreateSurveyUser(SurveyUser)

	for _, v := range req.Data {
		v.SurveyUserId = surveyUserId
		service.CreateSurveyUserData(v)
	}

	response.OkWithMessage("创建成功", c)
	return
}

func CreateSurveyLog(c *gin.Context) {
	var req model.SurveyLog
	_ = c.ShouldBindJSON(&req)

	ipData, _ := service.GetIpaddr(c.ClientIP())
	//ipData, _ := service.GetIpaddr("125.34.219.138")
	//fmt.Println(ipData)

	req.Country = ipData.Country
	req.Area = ipData.Area
	req.Region = ipData.Region
	req.City = ipData.City

	req.CreateTime = utils.NowTime()
	if err := service.CreateSurveyLog(req); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Any("err", err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
	return
}

func ExportSurveyUserExcel(c *gin.Context) {
	var req request.GetSurveyUserListReq
	_ = c.ShouldBindQuery(&req)

	req.PageSize = 99999

	_, list, _ := service.GetSurveyUserList(req)

	fileName := "用户列表" + time.Now().Format("20060102")
	f := excelize.NewFile()
	// Create a new sheet.
	index := f.NewSheet(fileName)

	f.SetCellValue(fileName, "A1", "链接所属人")
	f.SetCellValue(fileName, "B1", "链接所属人电话")
	f.SetCellValue(fileName, "C1", "下单时间")
	f.SetCellValue(fileName, "D1", "开通天数")
	f.SetCellValue(fileName, "E1", "订单金额")
	f.SetCellValue(fileName, "F1", "支付金额")
	f.SetCellValue(fileName, "G1", "订单类型")
	f.SetCellValue(fileName, "H1", "备注")

	for k, v := range list {

		a := strconv.Itoa(k + 2)
		f.SetCellValue(fileName, "A"+a, v.WorkerName)
		f.SetCellValue(fileName, "B"+a, v.WorkerMobile)
		f.SetCellValue(fileName, "C"+a, v.WorkerMobile)
		f.SetCellValue(fileName, "D"+a, v.WorkerMobile)
		f.SetCellValue(fileName, "E"+a, v.WorkerMobile)
		f.SetCellValue(fileName, "F"+a, v.WorkerMobile)
		f.SetCellValue(fileName, "G"+a, v.WorkerMobile)
		f.SetCellValue(fileName, "H"+a, v.WorkerMobile)
	}

	// Set active sheet of the workbook.
	f.SetActiveSheet(index)
	// Save xlsx file by the given path.
	if err := f.SaveAs("./" + fileName + ".csv"); err != nil {
		fmt.Println(err)
		return
	}

	c.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", "./"+fileName+".csv")) //fmt.Sprintf("attachment; filename=%s", filename)对下载的文件重命名
	c.Writer.Header().Add("Content-Type", "application/octet-stream")
	c.File("./" + fileName + ".csv")
	return
}
