package cygx

import (
	"encoding/json"
	"github.com/rdlucklib/rdluck_tools/paging"
	"hongze/hz_crm_api/controllers"
	"hongze/hz_crm_api/models"
	"hongze/hz_crm_api/models/cygx"
	"hongze/hz_crm_api/utils"
	"strings"
)

// 访谈申请
type InterviewApplyController struct {
	controllers.BaseAuthController
}

// @Title 访谈申请列表
// @Description 访谈申请列表接口
// @Param   PageSize   query   int  true       "每页数据条数"
// @Param   CurrentIndex   query   int  true       "当前页页码,从1开始"
// @Param   KeyWord   query   string  true       "搜索关键词"
// @Success 200 {object} cygx.InterviewApplyListResp
// @router /interview/list [get]
func (this *InterviewApplyController) List() {
	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
	}
	pageSize, _ := this.GetInt("PageSize")
	currentIndex, _ := this.GetInt("CurrentIndex")
	keyWord := this.GetString("KeyWord")
	keyWord = strings.Trim(keyWord, " ")
	keyWord = strings.Replace(keyWord, "'", "", -1)

	roleTypeCode := sysUser.RoleTypeCode
	var condition string
	var pars []interface{}
	{
		if roleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
			if roleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
				condition += ` AND c.product_id=? `
				pars = append(pars, 1)
			} else if roleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN {
				condition += ` AND c.product_id=? `
				pars = append(pars, 2)
			} else {
				if sysUser.Authority <= 0 {
					condition += ` AND c.seller_id=?  `
					pars = append(pars, sysUser.AdminId)
				} else {
					if sysUser.Authority == 1 {
						condition += ` AND c.department_id=? `
						pars = append(pars, sysUser.DepartmentId)
					}
					if sysUser.Authority == 2 {
						condition += ` AND c.group_id=? `
						pars = append(pars, sysUser.GroupId)
					}
				}
			}
		}
	}

	var startSize int
	if pageSize <= 0 {
		pageSize = utils.PageSize20
	}
	if currentIndex <= 0 {
		currentIndex = 1
	}
	startSize = paging.StartIndex(currentIndex, pageSize)

	if keyWord != "" {
		condition += ` AND (b.mobile LIKE '%` + keyWord + `%' OR b.email LIKE '%` + keyWord + `%' OR b.real_name LIKE '%` + keyWord + `%') `
	}

	total, err := cygx.GetInterviewApplyListCount(condition, pars)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据总数失败,Err:" + err.Error()
		return
	}

	list, err := cygx.GetInterviewApplyList(condition, pars, startSize, pageSize)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败,Err:" + err.Error()
		return
	}

	if list == nil {
		list = make([]*cygx.CygxInterviewApply, 0)
	}
	resp := new(cygx.InterviewApplyListResp)
	page := paging.GetPaging(currentIndex, pageSize, total)
	resp.List = list
	resp.Paging = page

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

// @Title 访谈申请状态变更接口
// @Description 访谈申请状态变更接口(包含取消和状态更新)
// @Param	request	body cygx.InterviewApplyStatusReq true "type json string"
// @Success Ret=200 访谈申请取消成功
// @router /interview/status/modify [post]
func (this *InterviewApplyController) InterviewApplyCancel() {
	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 cygx.InterviewApplyStatusReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常!"
		br.ErrMsg = "参数解析失败,Err:" + err.Error()
		return
	}
	if req.InterviewApplyId <= 0 {
		br.Msg = "参数错误"
		br.ErrMsg = "参数错误,访谈申请id小于等于0"
		return
	}
	item, err := cygx.GetCygxInterviewApplyDetail(req.InterviewApplyId)
	if err != nil {
		br.Msg = "获取数据失败"
		br.ErrMsg = "获取数据失败,Err:" + err.Error()
		return
	}
	var status string
	if req.HandleType == 2 {
		if item.Status == "已取消" || item.Status == "已完成" {
			br.Msg = "当前状态为:" + item.Status + ",不可进行取消操作"
			br.ErrMsg = "当前状态为:" + item.Status + ",不可进行取消操作"
			return
		}
		status = "已取消"
	} else {
		if req.InterviewTime != "" {
			status = "待访谈"
		} else {
			status = "已完成"
		}
	}
	err = cygx.InterviewApplyStatusModify(req.InterviewApplyId, status, req.InterviewTime)
	if err != nil {
		br.Msg = "状态更新失败"
		br.ErrMsg = "状态更新失败,Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "状态更新成功"
}