package eta_trial

import (
	"encoding/json"
	"eta/eta_api/models"
	"eta/eta_api/models/eta_trial"
	etaTrialService "eta/eta_api/services/eta_trial"
	"eta/eta_api/utils"
	"time"
)

// QuestionnairePopUp
// @Title 获取问卷调查弹窗信息
// @Description 获取问卷调查弹窗信息
// @Success 200 {object} response.TryOutCompanyListResp
// @router /questionnaire/popup [get]
func (this *EtaTrialController) QuestionnairePopUp() {
	br := new(models.BaseResponse).Init()
	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	// 仅试用平台用
	if utils.BusinessCode != utils.BusinessCodeSandbox {
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
		return
	}

	var resp eta_trial.QuestionnairePopupResp

	var userReq etaTrialService.GetEtaTrialUserReq
	userReq.Mobile = sysUser.Mobile
	item, e := etaTrialService.GetEtaTrialUser(userReq)
	if e != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取ETA试用客户失败, GetEtaTrialUser Err:" + e.Error()
		return
	}
	if item.EtaTrialId <= 0 {
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
		return
	}

	fillItem, e := eta_trial.GetQuestionnaireFillRecordCountByMobile(sysUser.Mobile)
	if e != nil && e.Error() != utils.ErrNoRow() {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败失败，GetQuestionnaireFillRecordCountByMobile Err:" + e.Error()
		return
	}
	if fillItem == nil {
		//没填过
		if item.ModifyTime.AddDate(0, 0, 6).Before(time.Now()) {
			//弹窗
			resp.IsPopup = 1
		}
	} else {
		//填过，判断是否超过了14天
		if fillItem.IsFill == 2 && fillItem.CreateTime.AddDate(0, 0, 13).Before(time.Now()) {
			//弹窗
			resp.IsPopup = 1
		} else if fillItem.IsFill == 1 {
			//显示图标但不弹窗
			resp.IsShow = 1
		}
	}

	questionnaireList, e := etaTrialService.GetEtaTrialQuestionnaireList()
	if e != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取问卷调查列表失败, GetEtaTrialQuestionnaireList Err: " + e.Error()
		return
	}
	resp.Question = questionnaireList

	br.Data = resp
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
}

// QuestionnaireCommit
// @Title 提交问卷
// @Description 提交问卷
// @Success 200 {object} response.TryOutCompanyListResp
// @router /questionnaire/commit [post]
func (this *EtaTrialController) QuestionnaireCommit() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}

	var req eta_trial.EtaTrialQuestionnaireReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	var userReq etaTrialService.GetEtaTrialUserReq
	userReq.Mobile = sysUser.Mobile
	admin, e := etaTrialService.GetEtaTrialUser(userReq)
	if e != nil {
		br.Msg = "操作失败"
		br.ErrMsg = "获取ETA试用客户失败, GetEtaTrialUser Err:" + e.Error()
		return
	}
	if admin.EtaTrialId <= 0 {
		br.Ret = 200
		br.Success = true
		br.Msg = "操作成功"
		return
	}

	if req.IsFill == 0 {
		// 稍后再填
		fillItem := &eta_trial.QuestionnaireFillRecord{
			Mobile:     admin.Mobile,
			IsFill:     1,
			CreateTime: time.Now(),
			ModifyTime: time.Now(),
		}
		err := eta_trial.AddQuestionnaireFillRecord(fillItem)
		if err != nil {
			br.Msg = "新增填写记录失败"
			br.ErrMsg = "新增填写记录失败， Err:" + err.Error()
			return
		}
	} else {
		// 已填写
		list := make([]eta_trial.EtaTrialQuestionnaireRecord, 0)
		for _, q := range req.List {
			v := eta_trial.EtaTrialQuestionnaireRecord{
				UserName:        admin.UserName,
				CompanyName:     admin.CompanyName,
				Position:        admin.Position,
				Options:         q.Options,
				Mobile:          admin.Mobile,
				Type:            q.Type,
				QuestionnaireId: q.QuestionnaireId,
				CreateTime:      time.Now(),
			}
			list = append(list, v)
		}

		var commitReq etaTrialService.EtaTrialQuestionnaireCommitReq
		commitReq.List = list
		commitRes, e := etaTrialService.EtaTrialQuestionnaireCommit(commitReq)
		if e != nil {
			br.Msg = "操作失败"
			br.ErrMsg = "新增问卷记录失败, EtaTrialQuestionnaireCommit Err: " + e.Error()
			return
		}
		if !commitRes {
			br.Msg = "操作失败"
			br.ErrMsg = "新增问卷记录失败" + e.Error()
			return
		}

		fillItem := &eta_trial.QuestionnaireFillRecord{
			Mobile:     admin.Mobile,
			IsFill:     2,
			CreateTime: time.Now(),
			ModifyTime: time.Now(),
		}
		err := eta_trial.AddQuestionnaireFillRecord(fillItem)
		if err != nil {
			br.Msg = "新增填写记录失败"
			br.ErrMsg = "新增填写记录失败， Err:" + err.Error()
			return
		}
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "提交成功"
}
