package front

import (
	"encoding/json"
	"go.mongodb.org/mongo-driver/bson"
	"mongodb"
	qu "qfw/util"
	. "util"
)

func (f *Front) RoleManager() {
	defer qu.Catch()
	if f.Method() == "POST" {
		data, _ := Mgo.Find("role", nil, nil, nil, false, -1, -1)
		f.ServeJson(map[string]interface{}{
			"data": data,
		})
	} else {
		f.Render("com/role.html")
	}
}

func (f *Front) RoleNew() {
	defer qu.Catch()
	if f.Method() == "POST" {
		name := f.GetString("role_name")
		level := f.GetString("role_level")
		_id := f.GetString("_id")
		if _id != "" {
			set := bson.M{
				"$set": bson.M{
					"name":  name,
					"level": level,
				},
			}
			bol := Mgo.UpdateById("role", mongodb.StringTOBsonId(_id), set)
			if bol {
				f.ServeJson(map[string]interface{}{
					"rep": true,
				})
			}
		} else {
			_id := Mgo.Save("role", bson.M{"name": name, "level": level})
			if _id != "" {
				f.ServeJson(map[string]interface{}{
					"rep": true,
				})
			}
		}
	} else {
		f.Render("com/role.html")
	}
}

func (f *Front) RoleEdit(role string) {
	defer qu.Catch()
	if f.Method() == "POST" {
		maps := map[string]interface{}{
			"role." + role: true,
		}
		data, _ := Mgo.Find("menu_first", maps, nil, nil, false, -1, -1)
		f.ServeJson(map[string]interface{}{
			"data": data,
		})
	} else {
		f.T["role"] = role
		f.Render("com/role_edit.html", &f.T)
	}
}

func (f *Front) RoleFirst() {
	defer qu.Catch()
	data, _ := Mgo.Find("menu_first", `{"delete":false}`, nil, nil, false, -1, -1)
	f.ServeJson(map[string]interface{}{
		"data": data,
	})
}

func (f *Front) RoleSecond() {
	defer qu.Catch()
	pid := f.GetString("_pid")
	query := bson.M{
		"s_pid": pid,
	}
	data, _ := Mgo.Find("menu_second", query, nil, nil, false, -1, -1)
	f.ServeJson(map[string]interface{}{
		"data": data,
	})
}

func (f *Front) RoleSave() {
	defer qu.Catch()
	pid := f.GetString("_id")
	role := f.GetString("role")
	secondStr := f.GetString("secondStr")   //右边 选中的
	secondStr1 := f.GetString("secondStr1") //左边 未选中的

	secondmenus := make([]string, 0)
	secondmenus1 := make([]string, 0)
	s_id := json.Unmarshal([]byte(secondStr), &secondmenus)
	s_id1 := json.Unmarshal([]byte(secondStr1), &secondmenus1)

	if s_id == nil && s_id1 == nil {
		for _, v := range secondmenus {
			maps := map[string]interface{}{
				"_id": mongodb.StringTOBsonId(v),
			}
			data := map[string]interface{}{
				"role." + role: true,
			}
			data1 := map[string]interface{}{
				"$set": data,
			}
			Mgo.Update("menu_second", maps, data1, true, false)
		}
		for _, v := range secondmenus1 {
			maps := map[string]interface{}{
				"_id": mongodb.StringTOBsonId(v),
			}
			data := map[string]interface{}{
				"role." + role: false,
			}
			data1 := map[string]interface{}{
				"$set": data,
			}
			Mgo.Update("menu_second", maps, data1, true, false)
		}
	}
	b := false
	if (len(secondmenus) == 0 && len(secondmenus1) == 0) || (len(secondmenus) > 0) {
		maps := map[string]interface{}{
			"_id": mongodb.StringTOBsonId(pid),
		}
		data := map[string]interface{}{
			"role." + role: true,
		}
		data1 := map[string]interface{}{
			"$set": data,
		}
		b = Mgo.Update("menu_first", maps, data1, true, false)
	}
	if len(secondmenus) == 0 && len(secondmenus1) > 0 {
		maps := map[string]interface{}{
			"_id": mongodb.StringTOBsonId(pid),
		}
		data := map[string]interface{}{
			"role." + role: false,
		}
		data1 := map[string]interface{}{
			"$set": data,
		}
		b = Mgo.Update("menu_first", maps, data1, true, false)
	}
	f.ServeJson(map[string]interface{}{
		"rep": b,
	})
}

func (f *Front) RoleDel() {
	defer qu.Catch()
	_id := f.GetString("_id")
	role := f.GetString("role")
	maps := map[string]interface{}{
		"_id": mongodb.StringTOBsonId(_id),
	}
	data := map[string]interface{}{
		"role." + role: false,
	}
	data1 := map[string]interface{}{
		"$set": data,
	}
	b := Mgo.Update("menu_first", maps, data1, true, false)
	maps = map[string]interface{}{
		"s_pid": _id,
	}
	count, _ := Mgo.Find("menu_second", maps, nil, nil, false, -1, -1)
	if len(*count) != 0 {
		for _, c := range *count {
			maps = map[string]interface{}{
				"_id": c["_id"],
			}
			Mgo.Update("menu_second", maps, data1, true, false)
		}
	}
	f.ServeJson(map[string]interface{}{
		"rep": b,
	})
}

func (f *Front) RoleSecondEdit() {
	defer qu.Catch()
	menuid := f.GetString("_id")
	mark := f.GetString("mark")
	role := f.GetString("role")
	if mark == "" {
		maps := map[string]interface{}{
			"s_pid": menuid,
		}
		datas, _ := Mgo.Find("menu_second", maps, nil, nil, false, -1, -1)
		f.ServeJson(map[string]interface{}{
			"data": datas,
		})
	} else {
		maps := map[string]interface{}{
			"s_pid":        menuid,
			"role." + role: true,
		}
		data, _ := Mgo.Find("menu_second", maps, nil, nil, false, -1, -1)
		maps1 := map[string]interface{}{
			"s_pid":        menuid,
			"role." + role: false,
		}
		name, _ := Mgo.FindById("menu_first", menuid, `{s_name:1}`)
		s_name := (*name)["s_name"]
		data1, _ := Mgo.Find("menu_second", maps1, nil, nil, false, -1, -1)
		f.ServeJson(map[string]interface{}{
			"name":  s_name,
			"data":  data,
			"data1": data1,
		})
	}
}
