package controllers

import (
	"LAPP_LF_MOM_BACKEND/utils"
	"LAPP_LF_MOM_BACKEND/web/middleware/jwts"
	"LAPP_LF_MOM_BACKEND/web/models"
	"LAPP_LF_MOM_BACKEND/web/supports"
	"github.com/kataras/iris/v12"
	"log"
	"time"
)

// @Summary 角色列表数据
// @Description Get JSON
// @Tags 角色/Role
// @Param roleName query string false "roleName"
// @Param status query string false "status"
// @Param roleKey query string false "roleKey"
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {object} app.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/rolelist [get]
// @Security
func GetRoleList(ctx iris.Context) {
	var data models.Roletab
	var err error
	var pageSize = 10
	var pageIndex = 1

	if size := ctx.URLParam("pageSize"); size != "" {
		pageSize = utils.ValueToInt(size,0)
	}

	if index := ctx.URLParam("pageIndex"); index != "" {
		pageIndex = utils.ValueToInt(index,0)
	}

	data.Rolename = ctx.URLParam("rolename")
	data.Status = ctx.URLParam("status")
	result, count, err := data.GetPage(pageSize, pageIndex)
	if err != nil{
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	res := make(map[string]interface{})
	res["data"] = result
	res["count"] = count
	res["pageIndex"] = pageIndex
	res["pageSize"] = pageSize
	supports.Ok(ctx, supports.OptionSuccess, res)
}
// @Summary 获取Role数据
// @Description 获取JSON
// @Tags 角色/Role
// @Param roleId path string false "roleId"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Success 200 {string} string "{"code": -1, "message": "抱歉未找到相关信息"}"
// @Router /api/v1/role [get]
// @Security Bearer
func GetRole(ctx iris.Context) {
	var Role models.Roletab
	roleId, _ := ctx.URLParamInt("roleId")
	Role.RoleId = roleId
	result, err := Role.Get()
	if err !=nil{
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	result.Clipped()
	var menuIds []int
	menuIds, err = Role.GetRoleMeunId()
	if err != nil{
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	result.MenuIds = menuIds
	supports.Ok(ctx, supports.OptionSuccess, result)
}

// @Summary 创建角色
// @Description 获取JSON
// @Tags 角色/Role
// @Accept  application/json
// @Product application/json
// @Param data body models.SysRole true "data"
// @Success 200 {string} string	"{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "添加失败"}"
// @Router /api/v1/role [post]
func InsertRole(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user,*user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	data := new(models.Roletab)
	data.Lastmodify = user.Userid
	data.Createtime = utils.TimeFormat(time.Now(),"yyyyMMddHHmmss")
	data.Lastmodifytime = utils.TimeFormat(time.Now(),"yyyyMMddHHmmss")
	if err := ctx.ReadJSON(data); err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "json解析错误", nil)
		return
	}
	id, err := data.Insert()
	data.RoleId = id
	if err != nil{
		supports.Error(ctx, iris.StatusBadRequest,"角色标识已经存在！", nil)
		return
	}
	var t models.RoleMenu
	_, err = t.Insert(id, data.MenuIds)
	if err != nil{
		supports.Error(ctx, iris.StatusBadRequest, "插入失败2", nil)
		return
	}
	supports.Ok(ctx, supports.OptionSuccess, data)
}

// @Summary 修改用户角色
// @Description 获取JSON
// @Tags 角色/Role
// @Accept  application/json
// @Product application/json
// @Param data body models.SysRole true "body"
// @Success 200 {string} string	"{"code": 200, "message": "修改成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "修改失败"}"
// @Router /api/v1/role [put]
func UpdateRole(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user,*user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	data := new(models.Roletab)
	data.Lastmodify = user.Userid
	if err := ctx.ReadJSON(data); err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	result, err := data.Update(data.RoleId)
	if err != nil{
		log.Println("角色更新失败！")
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	var t models.RoleMenu
	_, err = t.DeleteRoleMenu(data.RoleId)
	if err != nil{
		supports.Error(ctx, iris.StatusBadRequest, "添加失败1", nil)
		return
	}
	if !utils.ValueIsEmpty(data.MenuIds){
		_, err2 := t.Insert(data.RoleId, data.MenuIds)
		if err2 != nil{
			supports.Error(ctx, iris.StatusBadRequest, "添加失败2", nil)
			return
		}
	}
	supports.Ok(ctx, "修改成功", result)
}

// @Summary 删除用户角色
// @Description 删除数据
// @Tags 角色/Role
// @Param roleId path int true "roleId"
// @Success 200 {string} string	"{"code": 200, "message": "删除成功"}"
// @Success 200 {string} string	"{"code": -1, "message": "删除失败"}"
// @Router /api/v1/role/{roleId} [delete]
func DeleteRole(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user,*user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	Role:=new(models.Roletab)
	Role.Lastmodify = user.Userid

	roleId := ctx.Params().Get("roleId")

	IDS := utils.IdsStrToIdsIntGroup(roleId)
	//查询rolekey集合
	rolekeys ,err :=Role.GetRoles(IDS)
	if err != nil{
		supports.Error(ctx, iris.StatusBadRequest, "roleIds不存在", nil)
		return
	}
	_, err = Role.BatchDelete(IDS)
	if err != nil{
		supports.Error(ctx, iris.StatusBadRequest, "删除失败1", nil)
		return
	}
	var t models.RoleMenu
	_, err = t.BatchDeleteRoleMenu(IDS,rolekeys)
	if err != nil{
		supports.Error(ctx, iris.StatusBadRequest, "删除失败2", nil)
		return
	}
	supports.Ok(ctx, "删除成功", "")
}
