package controllers

import (
	"encoding/json"
	"eta/eta_mobile/models"
	"eta/eta_mobile/services"
	"eta/eta_mobile/utils"
)

// ChartPermissionController 品种列表
type ChartPermissionController struct {
	BaseAuthController
}

// List
// @Title 品种列表
// @Description 品种列表
// @Param   Keyword  query  string  false  "关键词"
// @Success 200 Ret=200 操作成功
// @router /list [get]
func (this *ChartPermissionController) List() {
	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
	}
	cond := ` and product_id=1`
	pars := make([]interface{}, 0)
	list, e := services.GetChartPermissionList(cond, pars)
	if e != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取品种列表失败, Err: " + e.Error()
		return
	}

	// 品种树
	resp := make([]*models.ChartPermissionItem, 0)
	childMap := make(map[int][]*models.ChartPermissionItem)

	for _, v := range list {
		t := new(models.ChartPermissionItem)
		t.PermissionId = v.ChartPermissionId
		t.PermissionName = v.PermissionName
		t.ParentId = v.ParentId
		t.IsPublic = v.IsPublic
		t.Enabled = v.Enabled
		t.Sort = v.Sort
		t.CreateTime = v.CreatedTime.Format(utils.FormatDateTime)
		t.Child = make([]*models.ChartPermissionItem, 0)
		if v.ParentId == 0 {
			resp = append(resp, t)
			continue
		}
		if v.ParentId > 0 {
			if childMap[v.ParentId] == nil {
				childMap[v.ParentId] = make([]*models.ChartPermissionItem, 0)
			}
			childMap[v.ParentId] = append(childMap[v.ParentId], t)
		}
	}
	for _, r := range resp {
		r.Child = childMap[r.PermissionId]
	}

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

// Add
// @Title 新增品种列表
// @Description 新增品种列表
// @Param  request body models.PermissionAddReq true "type json string"
// @Success 200 Ret=200 操作成功
// @router /add [post]
func (this *ChartPermissionController) Add() {
	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
	}
	var req models.PermissionAddReq
	if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + e.Error()
		return
	}
	if req.PermissionName == "" {
		br.Msg = "请输入品种名称"
		return
	}
	e, msg := services.AddChartPermission(req)
	if e != nil {
		br.Msg = msg
		br.ErrMsg = "新增品种失败, Err: " + e.Error()
		return
	}

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

// Edit
// @Title 编辑品种
// @Description 编辑品种
// @Param	request	body models.PermissionEditReq true "type json string"
// @Success 200 Ret=200 操作成功
// @router /edit [post]
func (this *ChartPermissionController) Edit() {
	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
	}
	var req models.PermissionEditReq
	if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + e.Error()
		return
	}
	if req.PermissionId <= 0 {
		br.Msg = "请选择要编辑的品种"
		return
	}

	if req.PermissionName == "" {
		br.Msg = "请输入品种名称"
		return
	}

	e, msg := services.EditChartPermission(req)
	if e != nil {
		br.Msg = msg
		br.ErrMsg = "编辑品种失败, Err: " + e.Error()
		return
	}

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

// Move
// @Title 移动品种
// @Description 移动品种
// @Param	request	body models.PermissionMoveReq true "type json string"
// @Success 200 Ret=200 操作成功
// @router /move [post]
func (this *ChartPermissionController) Move() {
	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
	}
	var req models.PermissionMoveReq
	// todo 限制修改一级品种
	if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + e.Error()
		return
	}

	if req.PermissionId == 0 {
		br.Msg = "请选择要移动的品种"
		return
	}
	e, msg := services.MoveChartPermission(req)
	if e != nil {
		br.Msg = msg
		br.ErrMsg = "移动品种失败, Err: " + e.Error()
		return
	}

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

// SetEnabled
// @Title 启用/禁用品种
// @Description 启用/禁用品种
// @Param	request	body models.PermissionEnabledReq true "type json string"
// @Success 200 Ret=200 操作成功
// @router /enabled/set [post]
func (this *ChartPermissionController) SetEnabled() {
	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
	}
	var req models.PermissionEnabledReq
	if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + e.Error()
		return
	}
	if req.PermissionId <= 0 {
		br.Msg = "请选择要编辑的品种"
		return
	}
	if req.Enabled != 0 && req.Enabled != 1 {
		br.Msg = "请选择正确的启用禁用状态"
		return
	}

	e, msg := services.SetEnabledChartPermission(req)
	if e != nil {
		br.Msg = msg
		br.ErrMsg = "编辑品种失败, Err: " + e.Error()
		return
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
}
