package controllers

import (
	"github.com/astaxie/beego"
	// "log"
	"fmt"

	"box-gm/models"
)

type RoleController struct {
	beego.Controller
}

func (c *RoleController) Get() {
	userinfo := c.GetSession("user")
	if userinfo == nil {
		c.Ctx.Redirect(302, "/accountlogin")
		return
	}

	c.Data["username"] = userinfo.(*LoginInfo).Username
	c.Data["token"] = models.GetToken()

	userPermission := models.GetPermission(userinfo.(*LoginInfo).Username)
	if (userPermission & (1 << uint(models.ModelUser))) == 0 {
		c.Ctx.Redirect(302, "/index")
		return
	}
	roles, _ := models.GetRolelist()
	permission := models.GetPermissions()

	c.Data["permissions"] = permission
	c.Data["roles"] = roles
	// log.Printf("RoleController get Data[%v]", c.Data)
	c.TplName = "role.tpl"
}

func (c *RoleController) Post() {
	if !models.CheckToken(c.GetString("token")) {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": "操作失败，请重新刷新界面"}
		c.ServeJSON()
		return
	}

	userinfo := c.GetSession("user")
	if userinfo == nil {
		c.Ctx.Redirect(302, "/accountlogin")
		return
	}

	if !models.CheckPermission(userinfo.(*LoginInfo).Username, models.ModelUser) {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": "没有操作权限"}
		c.ServeJSON()
		return
	}

	r := new(models.Role)
	r.Name = c.GetString("name")
	i, _ := c.GetInt("permission")
	r.Permission = int64(i)
	
	// log.Printf("RoleController post Name[%s] Permission[%d]", r.Name, r.Permission)

	_, err := models.AddRole(r)
	if err != nil {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": err}
		c.ServeJSON()
		return
	}

	c.Data["json"] = &map[string]interface{}{"status": true, "info": "创建角色成功"}
	c.ServeJSON()

	go models.SaveLog(userinfo.(*LoginInfo).Username, "创建角色", &map[string]interface{}{"name": r.Name, "permission": i})
}

func (c *RoleController) Put() {
	if !models.CheckToken(c.GetString("token")) {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": "操作失败，请重新刷新界面"}
		c.ServeJSON()
		return
	}

	userinfo := c.GetSession("user")
	if userinfo == nil {
		c.Ctx.Redirect(302, "/accountlogin")
		return
	}

	if !models.CheckPermission(userinfo.(*LoginInfo).Username, models.ModelUser) {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": "没有操作权限"}
		c.ServeJSON()
		return
	}

	id, _ := c.GetInt64("id")
	role := models.GetRoleById(id)
	if role == nil {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": fmt.Sprintf("角色[%d]不存在", id)}
		c.ServeJSON()
		return
	}
	if role.Id == 1 {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": fmt.Sprintf("该用户[%s]不能修改", role.Name)}
		c.ServeJSON()
		return
	}

	r := new(models.Role)
	r.Id = role.Id
	r.Name = c.GetString("name")
	i, _ := c.GetInt64("permission")
	r.Permission = i
	
	// log.Printf("RoleController Put Name[%s] Permission[%d]", r.Name, r.Permission)

	_, err := models.UpdateRole(r)
	if err != nil {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": err}
		c.ServeJSON()
		return
	}

	c.Data["json"] = &map[string]interface{}{"status": true, "info": "更新角色成功"}
	c.ServeJSON()

	go models.SaveLog(userinfo.(*LoginInfo).Username, "修改角色", &map[string]interface{}{"name": r.Name, "permission": i})
}

func (c *RoleController) Delete() {
	if !models.CheckToken(c.GetString("token")) {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": "操作失败，请重新刷新界面"}
		c.ServeJSON()
		return
	}

	userinfo := c.GetSession("user")
	if userinfo == nil {
		c.Ctx.Redirect(302, "/accountlogin")
		return
	}

	if !models.CheckPermission(userinfo.(*LoginInfo).Username, models.ModelUser) {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": "没有操作权限"}
		c.ServeJSON()
		return
	}

	id, _ := c.GetInt64("id")
	role := models.GetRoleById(id)
	if role == nil {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": fmt.Sprintf("角色[%d]不存在", id)}
		c.ServeJSON()
		return
	}
	models.DelRoleById(id)

	c.Data["json"] = &map[string]interface{}{"status": true, "info": "删除角色成功"}
	c.ServeJSON()

	go models.SaveLog(userinfo.(*LoginInfo).Username, "删除角色", &map[string]interface{}{"ids": id})
}
