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)

	SurveyUser := model.SurveyUser{
		Id:              req.Id,
		Contacts:        req.Contacts,
		ContactsMobile:  req.ContactsMobile,
		Reference:       req.Reference,
		ReferenceMobile: req.ReferenceMobile,
		Status:          1,
	}

	service.UpdateSurveyUser(SurveyUser)

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

	response.OkWithMessage("修改成功", c)
	return
}

func CreateSurveyUserData(c *gin.Context) {
	var req request.CreateSurveyUserReq
	_ = c.ShouldBindJSON(&req)
	for _, v := range req.Data {
		v.SurveyUserId = req.Id
		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
	req.Status = 1

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

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

	f.MergeCell(fileName, "A1", "E1")
	f.SetCellValue(fileName, "A1", "留资信息")
	f.MergeCell(fileName, "F1", "P1")
	f.SetCellValue(fileName, "F1", "电销沟通结果")
	f.MergeCell(fileName, "Q1", "U1")
	f.SetCellValue(fileName, "Q1", "渠道信息")

	f.SetCellValue(fileName, "A2", "联系人")
	f.SetCellValue(fileName, "B2", "联系电话")
	f.SetCellValue(fileName, "C2", "留资时间")
	f.SetCellValue(fileName, "D2", "留资选配产品")
	f.SetCellValue(fileName, "E2", "备注")
	f.SetCellValue(fileName, "F2", "电话沟通")
	f.SetCellValue(fileName, "G2", "沟通日期")
	f.SetCellValue(fileName, "H2", "医院名称")
	f.SetCellValue(fileName, "I2", "医院状态")
	f.SetCellValue(fileName, "J2", "开业时间")
	f.SetCellValue(fileName, "K2", "所属省份")
	f.SetCellValue(fileName, "L2", "签单状态")
	f.SetCellValue(fileName, "M2", "转介绍渠道状态")
	f.SetCellValue(fileName, "N2", "签约金额")
	f.SetCellValue(fileName, "O2", "签约选配产品")
	f.SetCellValue(fileName, "P2", "电销人员")
	f.SetCellValue(fileName, "Q2", "转介绍人")
	f.SetCellValue(fileName, "R2", "转介绍人电话")
	f.SetCellValue(fileName, "S2", "渠道类型")
	f.SetCellValue(fileName, "T2", "渠道")
	f.SetCellValue(fileName, "U2", "渠道链接")

	for k, v := range list {

		surveyData := service.GetSurveyUserDataList(v.Id)

		optionData := ""
		for _, v2 := range surveyData {
			optionData += v2.OptionValue + " * " + strconv.Itoa(v2.OptionNum) + "; "
		}

		isReference := "转介绍留资"
		if v.Contacts == v.WorkerMobile {
			isReference = "直接留资"
		}

		moc := ""
		if v.MocId == 1 {
			moc = "短信"
		}

		url := global.GVA_CONFIG.Common.Url + "?" + v.Vcode

		a := strconv.Itoa(k + 3)
		f.SetCellValue(fileName, "A"+a, v.Contacts)
		f.SetCellValue(fileName, "B"+a, v.ContactsMobile)
		f.SetCellValue(fileName, "C"+a, v.CreateTime)
		f.SetCellValue(fileName, "D"+a, optionData)
		f.SetCellValue(fileName, "E"+a, "")
		f.SetCellValue(fileName, "F"+a, "")
		f.SetCellValue(fileName, "G"+a, "")
		f.SetCellValue(fileName, "H"+a, "")
		f.SetCellValue(fileName, "I"+a, "")
		f.SetCellValue(fileName, "J"+a, "")
		f.SetCellValue(fileName, "K"+a, "")
		f.SetCellValue(fileName, "L"+a, "")
		f.SetCellValue(fileName, "M"+a, "")
		f.SetCellValue(fileName, "N"+a, "")
		f.SetCellValue(fileName, "O"+a, "")
		f.SetCellValue(fileName, "P"+a, "")
		f.SetCellValue(fileName, "Q"+a, v.Reference)
		f.SetCellValue(fileName, "R"+a, v.ReferenceMobile)
		f.SetCellValue(fileName, "S"+a, isReference)
		f.SetCellValue(fileName, "T"+a, moc)
		f.SetCellValue(fileName, "U"+a, url)
	}

	// 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
}
