package rigger

import (
	"fmt"
	"net/http"

	"ciyon/web/admin"
	c "ciyon/zciyon"
)

func power_setwhere(post *c.CiyPost) (map[string]any, *c.CiySQL) {
	query := post.Getobj("query")
	csql := c.NewCiySQL("zc_power")
	csql.Where("types like", c.Getstr(query, "types"))
	order := c.Getstr(query, "order", "id desc")
	csql.Order(order)
	query["order"] = order
	return query, csql
}
func Power_init(w http.ResponseWriter, r *http.Request) bool {
	post := c.NewCiyPost(w, r)
	_, userid := admin.Verifyfast(r, c.CiyDB, post)
	if userid == 0 {
		return false
	}
	where, csql := power_setwhere(post)
	pageno := post.Getint("pageno", 1)
	pagecount := post.Getint("pagecount", 10)
	csql.Limit(pageno, pagecount)
	rows, mainrowcount, err := c.CiyDB.Get(csql, post.Getint("count"))
	if err != nil {
		return c.ErrJSON(w, "读取错误", err)
	}
	rows = append(rows, map[string]any{
		"id":        0,
		"groupname": "",
		"name":      "",
		"scope":     "",
		"powers":    "",
		"csort":     10,
	})
	ret := map[string]any{}
	ret["where"] = where
	ret["pageno"] = pageno
	ret["pagecount"] = pagecount
	ret["count"] = mainrowcount
	ret["list"] = rows
	if post.Getbool("field") {
		field := map[string]map[string]any{}
		fshow := ""
		c.FieldAdd(&field, &fshow, -1, "groupname", "分组名")
		c.FieldAdd(&field, &fshow, -1, "name", "模块名称")
		c.FieldAdd(&field, &fshow, -1, "scope", "模块代码")
		c.FieldAdd(&field, &fshow, -1, "powers", "权限项")
		c.FieldAdd(&field, &fshow, -1, "csort", "排序")
		c.FieldAdd(&field, &fshow, -1, "_btn", "操作")
		field["groupname"]["thwidth"] = "8em"
		field["name"]["thwidth"] = "8em"
		field["scope"]["thwidth"] = "6em"
		field["powers"]["thwidth"] = "31em"
		field["csort"]["thwidth"] = "5em"
		ret["fshow"] = fshow
		ret["field"] = field
	}
	if post.Getbool("once") {
		once := map[string]any{}
		input := make([]map[string]any, 0)
		input = append(input, map[string]any{
			"form": "groupname",
			"type": "input",
			"name": "分组名",
			"prop": ` style="width:8em;"`,
		})
		input = append(input, map[string]any{
			"form": "name",
			"type": "input",
			"name": "模块名称",
			"prop": ` style="width:8em;"`,
		})
		input = append(input, map[string]any{
			"form": "scope",
			"type": "input",
			"name": "模块代码",
			"prop": ` style="width:8em;"`,
		})
		once["input"] = input
		ret["once"] = once
	}
	return c.SuccJSON(w, r, ret)
}
func Power_update(w http.ResponseWriter, r *http.Request) bool {
	post := c.NewCiyPost(w, r)
	_, userid := admin.Verifyfast(r, c.CiyDB, post)
	if userid == 0 {
		return false
	}
	if admin.Nopower(c.CiyDB, userid, "p990u") {
		return c.ErrJSON(w, "您未被授权操作")
	}
	id := post.Getint("id")
	groupname := post.Get("groupname")
	if groupname == "" {
		return c.ErrJSON(w, "请填写分组名")
	}
	name := post.Get("name")
	if name == "" {
		return c.ErrJSON(w, "请填写模块名称")
	}
	scope := post.Get("scope")
	if scope == "" {
		return c.ErrJSON(w, "请填写模块代码")
	}
	powers := post.Get("powers")
	if powers == "" {
		return c.ErrJSON(w, "请填写权限项")
	}
	csort := post.Getint("csort")

	var err error
	var datarow map[string]any
	if id > 0 {
		csql := c.NewCiySQL("zc_power")
		csql.Where("id", id)
		datarow, err = c.CiyDB.Getone(csql)
		if datarow == nil {
			return c.ErrJSON(w, "数据不存在", err)
		}
	}
	updata := map[string]any{}
	err = c.CiyDB.Tran(func() error {
		var csql *c.CiySQL
		csql = c.NewCiySQL("zc_power")
		csql.Where("scope", scope)
		csql.Column("id")
		chkid := c.Toint(c.CiyDB.Get1(csql))
		if chkid > 0 && ((id > 0 && chkid != id) || id == 0) {
			return fmt.Errorf("模块代码重复")
		}
		updata["groupname"] = groupname
		updata["name"] = name
		updata["scope"] = scope
		updata["powers"] = powers
		updata["csort"] = csort
		csql = c.NewCiySQL("zc_power")
		if id > 0 {
			csql.Where("id", id)
			_, err = c.CiyDB.Update(csql, updata)
		} else {
			id, err = c.CiyDB.Insert(csql, updata)
		}
		updata["id"] = id
		if err != nil {
			return fmt.Errorf("更新失败:%v", err)
		}
		admin.SaveLogDB(c.CiyDB, "zc_power", datarow, updata)
		return nil
	})
	if err != nil {
		return c.ErrJSON(w, "事务"+err.Error())
	}
	ret := map[string]any{}
	ret["data"] = updata
	return c.SuccJSON(w, r, ret)
}
func Power_del(w http.ResponseWriter, r *http.Request) bool {
	post := c.NewCiyPost(w, r)
	_, userid := admin.Verifyfast(r, c.CiyDB, post)
	if userid == 0 {
		return false
	}
	if admin.Nopower(c.CiyDB, userid, "p990u") {
		return c.ErrJSON(w, "您未被授权操作")
	}
	ids := post.Get("ids")
	if ids == "" {
		return c.ErrJSON(w, "请选择至少一条")
	}
	csql := c.NewCiySQL("zc_power")
	csql.Where("id in", ids)
	rows, _, err := c.CiyDB.Get(csql)
	if err != nil {
		return c.ErrJSON(w, "读取数据错误", err)
	}
	vids := make([]int, 0)
	err = c.CiyDB.Tran(func() error {
		for _, row := range rows {
			delid := c.Toint(row["id"])
			c.Delme(c.CiyDB, delid, "zc_power")
			admin.SaveLogDB(c.CiyDB, "zc_power", row, nil)
			vids = append(vids, delid)
		}
		return nil
	})
	if err != nil {
		return c.ErrJSON(w, "事务"+err.Error())
	}
	ret := map[string]any{}
	ret["ids"] = vids
	return c.SuccJSON(w, r, ret)
}
