package controllers

import (
	"encoding/json"
	"hongze/hz_crm_api/models"
	"hongze/hz_crm_api/models/company_todo"
	"hongze/hz_crm_api/services"
	"hongze/hz_crm_api/utils"
	"time"
)

// CompanyTodoController 客户任务列表
type CompanyTodoController struct {
	BaseAuthController
}

// Add
// @Title 新增任务
// @Description 新增任务接口
// @Param	request	body company_todo.CompanyTodoAddReq true "type json string"
// @Success 200 新增成功
// @router /company_todo/add [post]
func (this *CompanyTodoController) Add() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req company_todo.CompanyTodoAddReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.CompanyId <= 0 {
		br.Msg = "请选择客户"
		br.ErrMsg = "客户ID不能为空"
		return
	}
	if req.Description == "" {
		br.Msg = "任务描述不能为空"
		return
	}

	if req.EndTime == "" {
		br.Msg = "请输入截止日期"
		return
	}

	endTime, err := time.ParseInLocation(utils.FormatDate, req.EndTime, time.Local)
	if err != nil {
		br.Msg = "截止日期格式错误"
		return
	}

	description := utils.TrimStr(req.Description)

	productId := services.GetProductId(this.SysUser.RoleTypeCode)
	if productId == 0 {
		if this.SysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
			productId = req.ProductId
		} else {
			br.Msg = "账号角色异常，不允许添加任务"
			return
		}
	}

	// 新增任务
	errMsg, err := services.AddCompanyTodo(req.CompanyId, productId, description, endTime, this.SysUser)
	if err != nil {
		br.Msg = errMsg
		br.ErrMsg = "添加任务失败,err:" + err.Error()
		return
	}

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

// EditList
// @Title 获取客户待操作任务列表
// @Description 获取客户待操作任务列表接口
// @Param   CompanyId   query   int  true       "客户id"
// @Success 200 {object} []company_todo.CompanyTodo
// @router /company_todo/edit_list [get]
func (this *CompanyTodoController) EditList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	companyId, _ := this.GetInt("CompanyId")
	if companyId <= 0 {
		br.Msg = "请选择客户"
		return
	}
	productId := services.GetProductId(this.SysUser.RoleTypeCode)
	if productId == 0 && this.SysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
		br.Msg = "账号角色异常"
		return
	}

	// 获取进行中的任务信息
	todoList, err := company_todo.GetDoingCompanyTodoList(companyId, productId)
	if err != nil {
		br.Msg = "获取任务信息失败"
		br.ErrMsg = "获取任务信息失败,err:" + err.Error()
		return
	}
	var list []*company_todo.CompanyTodoListItem
	for _, v := range todoList {
		temp := &company_todo.CompanyTodoListItem{
			CompanyTodo: v,
		}
		temp.EndTimeStr = temp.EndTime.Format(utils.FormatDate)
		if temp.EndTimeStr == "0001-01-01" {
			temp.EndTimeStr = ""
		}
		list = append(list, temp)
	}

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

// List
// @Title 任务列表
// @Description 客户任务列表接口
// @Param   CompanyId   query   int  true       "客户id"
// @Success 200 {object} []company_todo.CompanyTodo
// @router /company_todo/list [get]
func (this *CompanyTodoController) List() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	companyId, _ := this.GetInt("CompanyId")
	if companyId <= 0 {
		br.Msg = "请选择客户"
		return
	}
	productId := services.GetProductId(this.SysUser.RoleTypeCode)
	if productId == 0 && this.SysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
		br.Msg = "账号角色异常"
		return
	}

	// 获取任务信息
	todos, err := company_todo.GetCompanyTodoList(companyId, productId)
	if err != nil {
		br.Msg = "获取任务信息失败"
		br.ErrMsg = "获取任务信息失败,err:" + err.Error()
		return
	}
	var list []*company_todo.CompanyTodoListItem
	for _, v := range todos {
		temp := &company_todo.CompanyTodoListItem{
			CompanyTodo: v,
		}
		temp.EndTimeStr = temp.EndTime.Format(utils.FormatDate)
		if temp.EndTimeStr == "0001-01-01" {
			temp.EndTimeStr = ""
		}
		list = append(list, temp)
	}

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

// Edit
// @Title 修改分组
// @Description  修改分组接口
// @Param	request	body system.SysTeamEditReq true "type json string"
// @Success 200 修改成功
// @router /company_todo/edit [post]
func (this *CompanyTodoController) Edit() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req company_todo.CompanyTodoEditReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.Id <= 0 {
		br.Msg = "请选择客户"
		br.ErrMsg = "客户ID不能为空"
		return
	}
	if req.Description == "" {
		br.Msg = "任务描述不能为空"
		return
	}
	if req.EndTime == "" {
		br.Msg = "请输入截止日期"
		return
	}
	endTime, err := time.ParseInLocation(utils.FormatDate, req.EndTime, time.Local)
	if err != nil {
		br.Msg = "截止日期格式错误"
		return
	}

	description := utils.TrimStr(req.Description)

	errMsg, err := services.EditCompanyTodo(req.Id, description, endTime, this.SysUser)
	if err != nil {
		br.Msg = errMsg
		br.ErrMsg = errMsg + "，Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "保存成功"
}

// Approve
// @Title 审批任务
// @Description 审批任务接口
// @Param	request	body company_todo.ApproveCompanyTodoReq true "type json string"
// @Success 200 审批通过
// @router /company_todo/approve [post]
func (this *CompanyTodoController) Approve() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req company_todo.ApproveCompanyTodoReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.Id <= 0 {
		br.Msg = "参数错误"
		br.ErrMsg = "参数错误,任务编号必传 "
		return
	}
	errMsg, err := services.ApproveCompanyTodo(req.Id, req.Remark, this.SysUser)
	if err != nil {
		br.Msg = errMsg
		br.ErrMsg = errMsg + "，Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "审核完成"
}
