package front

import (
	"fmt"
	"github.com/go-xweb/xweb"
	"mongodb"
	qu "qfw/util"
	"strconv"
	. "util"
)

var UserMenu map[string][]map[string]interface{} //存储菜单

func init() {
	UserMenu = make(map[string][]map[string]interface{})
}

type Front struct {
	*xweb.Action
	index xweb.Mapper `xweb:"/front/index"` //index页面
	//user
	login              xweb.Mapper `xweb:"/"`                        //登录页面
	code               xweb.Mapper `xweb:"/code"`                    //获取图片验证码
	logout             xweb.Mapper `xweb:"/front/logout"`            //注销
	updatePwd          xweb.Mapper `xweb:"/front/updatepwd"`         //更新密码
	userGroup          xweb.Mapper `xweb:"/front/group"`             //查所有用户组
	userGroupNew       xweb.Mapper `xweb:"/front/group/new"`         //用户组新建
	userGroupState     xweb.Mapper `xweb:"/front/group/state"`       //用户组状态
	userGroupStateBulk xweb.Mapper `xweb:"/front/group/bulks_setup"` //批量修改用户组状态
	userGroupDel       xweb.Mapper `xweb:"/front/group/del"`         //用户组删除
	userManage         xweb.Mapper `xweb:"/front/group/user"`        //用户管理
	userState          xweb.Mapper `xweb:"/front/group/user/state"`  //用户状态
	userDel            xweb.Mapper `xweb:"/front/group/user/del"`    //用户删除
	userModify         xweb.Mapper `xweb:"/front/group/user/modify"` // 修改用户信息
	groupList          xweb.Mapper `xweb:"/front/group/list"`        //用户组选择列表
	userList           xweb.Mapper `xweb:"/front/group/user/list"`   //用户组的普通用户列表

	//menu
	menu           xweb.Mapper `xweb:"/front/menu"`            //查一级菜单
	menuSave       xweb.Mapper `xweb:"/front/menu/save"`       //保存一级菜单
	menuDel        xweb.Mapper `xweb:"/front/menu/del"`        //删除一级菜单
	menuSecond     xweb.Mapper `xweb:"/front/menuSecond"`      //查二级菜单
	menuSecondSave xweb.Mapper `xweb:"/front/menuSecond/save"` //保存二级菜单
	menuSecondDel  xweb.Mapper `xweb:"/front/menuSecond/del"`  //删除二级菜单
	personalMenu   xweb.Mapper `xweb:"/front/personalMenu"`
	//role
	roleManager    xweb.Mapper `xweb:"/front/role"`             //角色权限管理
	roleNew        xweb.Mapper `xweb:"/front/role/save"`        //新增角色
	roleEdit       xweb.Mapper `xweb:"/front/role/edit/(.*)"`   //角色权限查看
	roleFirst      xweb.Mapper `xweb:"/front/role/first"`       //一级权限的查看
	roleSecond     xweb.Mapper `xweb:"/front/role/second"`      //二级权限的查看
	roleSave       xweb.Mapper `xweb:"/front/role/edit/save"`   //权限编辑保存
	roleDel        xweb.Mapper `xweb:"/front/role/edit/del"`    //权限编辑删除
	roleSecondEdit xweb.Mapper `xweb:"/front/role/second/edit"` //二级权限编辑

	//project
	projectList              xweb.Mapper `xweb:"/front/project"`                   //项目列表
	projectSave              xweb.Mapper `xweb:"/front/project/save"`              //新增项目
	projectQualityAssessment xweb.Mapper `xweb:"/front/project/qualityAssessment"` //数据质量评估
	projectTaskSave          xweb.Mapper `xweb:"/front/project/task/save"`         //用户组任务分发
	projectTaskList          xweb.Mapper `xweb:"/front/project/task/list"`         //用户组任务分发列表
	projectTaskRetrieve      xweb.Mapper `xweb:"/front/project/task/retrieve"`     //用户组任务收回
	projectTaskRepulse       xweb.Mapper `xweb:"/front/project/task/repulse"`      //用户组任务打回
	projectTaskClose         xweb.Mapper `xweb:"/front/project/task/close"`        //用户组任务关闭
	projectGetEntnameList    xweb.Mapper `xweb:"/front/project/getEntnameList"`    //模糊查询公司名称

	userTaskSave     xweb.Mapper `xweb:"/front/user/task/save"`     //用户任务分发
	userTaskList     xweb.Mapper `xweb:"/front/user/task/list"`     //用户任务列表
	userTaskRetrieve xweb.Mapper `xweb:"/front/user/task/retrieve"` //用户任务收回

	groupTaskListByAdmin xweb.Mapper `xweb:"/front/group/admin/task/list"` //用户组任务列表(系统管理员权限)
	groupTaskListByGroup xweb.Mapper `xweb:"/front/group/task/list"`       //用户组任务列表(用户组权限)
	groupTaskDeliver     xweb.Mapper `xweb:"/front/group/task/deliver"`    //用户组任务交付
	groupTaskExport      xweb.Mapper `xweb:"/front/group/task/export"`     //用户组任务导出
}

func (f *Front) Index() {
	defer qu.Catch()
	f.Render("index.html")
}

func (f *Front) PersonalMenu() {
	user := f.GetSession("user").(map[string]interface{})
	list := UserMenu[qu.ObjToString(user["s_login"])]
	f.ServeJson(map[string]interface{}{
		"data": list,
	})
}

func GetUserMenu(role int) []map[string]interface{} {
	list := []map[string]interface{}{}
	maps := map[string]interface{}{
		"delete": false,
	}
	if role != 0 {
		maps[fmt.Sprintf("role.%d", role)] = true
	}
	data, _ := Mgo.Find("menu_first", maps, nil, nil, false, -1, -1)
	for _, d := range *data {
		_id := d["_id"]
		maps = map[string]interface{}{
			"delete": false,
			"s_pid":  mongodb.BsonIdToSId(_id),
		}
		if role != 0 {
			maps[fmt.Sprintf("role.%d", role)] = true
		}
		secdatas, _ := Mgo.Find("menu_second", maps, nil, nil, false, -1, -1)
		secmenumap := map[string]interface{}{}
		for index, secdata := range *secdatas {
			secmenumap[strconv.Itoa(index+1)] = secdata
		}
		if len(secmenumap) != 0 {
			d["secondmenu"] = secmenumap
		}
		list = append(list, d)
	}
	return list
}
