package services

import (
	"errors"
	"eta/eta_mobile/models"
	"eta/eta_mobile/models/system"
	"eta/eta_mobile/utils"
	"strconv"
	"time"
)

const (
	TodoButtonColorDoing     = "red"
	TodoButtonColorDefault   = "gray"
	TodoButtonColorCompleted = "green"
)

// GetEnglishCompanyTodoAuthRole 获取可操作英文客户TODO的角色列表
func GetEnglishCompanyTodoAuthRole() []string {
	return []string{
		utils.ROLE_TYPE_CODE_ADMIN,           // 管理员
		utils.ROLE_TYPE_CODE_FICC_ADMIN,      // FICC管理员
		utils.ROLE_TYPE_CODE_FICC_DEPARTMENT, // FICC部门经理
		utils.ROLE_TYPE_CODE_FICC_GROUP,      // FICC销售主管
		utils.ROLE_TYPE_CODE_FICC_TEAM,       // FICC销售组长
		utils.ROLE_TYPE_CODE_FICC_SELLER,     // FICC销售
		utils.ROLE_TYPE_CODE_FICC_RESEARCHR,  // ficc研究员
	}
}

// CheckEnglishCompanyTodoAuth 校验当前用户对客户的操作(新增/编辑)权限
func CheckEnglishCompanyTodoAuth(sysUser *system.Admin, sellerInfo *system.Admin) (ok bool, errMsg string, err error) {
	// 是否有权限添加/编辑任务
	roleCode := sysUser.RoleTypeCode
	authList := GetEnglishCompanyTodoAuthRole()
	if !utils.InArrayByStr(authList, roleCode) {
		errMsg = "账号角色异常，不允许操作"
		return
	}

	// 销售仅能操作自己的客户
	if roleCode == utils.ROLE_TYPE_CODE_FICC_SELLER && sellerInfo.AdminId != sysUser.AdminId {
		errMsg = "该客户不是当前账号所属，不允许操作"
		return
	}

	// 判断销售主管、组长权限, 其余管理员角色不做限制
	switch roleCode {
	case utils.ROLE_TYPE_CODE_FICC_GROUP:
		same, e := CheckAdminIsSameBigGroup(sysUser, sellerInfo)
		if e != nil {
			errMsg = "保存失败"
			err = errors.New("判断主管与客户所属销售是否属于同一大组失败, Err: " + e.Error())
			return
		}
		if !same {
			errMsg = "该客户销售不是当前账号所属组员，不允许操作"
			return
		}
	case utils.ROLE_TYPE_CODE_FICC_TEAM:
		if sellerInfo.GroupId != sysUser.GroupId {
			errMsg = "该客户销售不是当前账号所属组员，不允许操作"
			return
		}
	}
	ok = true
	return
}

// GetEnglishCompanyListTodoMap 获取英文客户列表TODO相关信息
func GetEnglishCompanyListTodoMap(companyList []*models.EnglishCompanyListItem, sysUser *system.Admin) (todoMap map[int]*models.EnglishCompanyListTodo) {
	todoMap = make(map[int]*models.EnglishCompanyListTodo, 0)
	if len(companyList) == 0 || sysUser == nil {
		return
	}
	nowTime := time.Now().Local()
	todayTime := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, time.Local)
	for i := range companyList {
		c := companyList[i]
		v := new(models.EnglishCompanyListTodo)
		v.TodoButtonColor = TodoButtonColorDefault
		v.HiddenConfirm = true // 默认隐藏完成按钮
		if c.TodoStatusStr != models.EnglishCompanyTodoStatusNull {
			// 已完成
			if c.TodoStatusStr == models.EnglishCompanyTodoStatusCompleted {
				v.TodoButtonColor = TodoButtonColorCompleted
			}
			// 进行中
			if c.TodoStatusStr == models.EnglishCompanyTodoStatusDoing {
				v.TodoStatus = true
				v.TodoButtonColor = TodoButtonColorDoing
				//v.TodoEndTime = td.EndTime
				v.TodoEndTimeStr = c.TodoEndTime.Format(utils.FormatDate)
				// 截止目前还剩余的天数
				todoEndTime := time.Date(c.TodoEndTime.Year(), c.TodoEndTime.Month(), c.TodoEndTime.Day(), 0, 0, 0, 0, time.Local)
				deadline := int(todoEndTime.Sub(todayTime).Hours() / 24)
				v.Deadline = strconv.Itoa(deadline)
			}
			// 根据当前角色判断按钮权限
			switch sysUser.RoleTypeCode {
			case utils.ROLE_TYPE_CODE_ADMIN, utils.ROLE_TYPE_CODE_FICC_ADMIN, utils.ROLE_TYPE_CODE_FICC_DEPARTMENT, utils.ROLE_TYPE_CODE_FICC_GROUP, utils.ROLE_TYPE_CODE_FICC_RESEARCHR:
				v.HiddenConfirm = false
				if c.TodoStatusStr == models.EnglishCompanyTodoStatusDoing {
					v.CanConfirm = true
				}
			case utils.ROLE_TYPE_CODE_FICC_TEAM:
				// 组长不是自己的客户则展示完成按钮
				if c.SellerId != sysUser.AdminId {
					v.HiddenConfirm = false
				}
				if c.TodoStatusStr == models.EnglishCompanyTodoStatusDoing {
					v.CanConfirm = true
				}
			}
		}
		todoMap[c.CompanyId] = v
	}
	return
}
