package rigger

import (
	"net/http"

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

func online_setwhere(post *c.CiyPost) (map[string]any, *c.CiySQL) {
	query := post.Getobj("query")
	csql := c.NewCiySQL("zc_online")
	csql.Where("ip like", c.Getstr(query, "ip"))
	csql.Where("user", c.Getstr(query, "user"))
	csql.Where_daterange("addtimes", c.Getstr(query, "addtimes"))
	order := c.Getstr(query, "order", "id desc")
	csql.Order(order)
	query["order"] = order
	return query, csql
}
func Online_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 := online_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)
	}
	ret := map[string]any{}
	ret["where"] = where
	ret["pageno"] = pageno
	ret["pagecount"] = pagecount
	ret["count"] = mainrowcount
	ret["list"] = rows
	if post.Getbool("field") {
		field, fshow := c.CiyDB.GetField(csql)
		c.FieldAdd(&field, &fshow, 0, "_btn", "操作")
		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": "addtimes",
			"type": "daterange",
			"name": "登录时间",
		})
		input = append(input, map[string]any{
			"form":   "user",
			"type":   "select",
			"name":   "用户",
			"select": "adminuser",
			"all":    "全部",
		})
		input = append(input, map[string]any{
			"form": "ip",
			"type": "input",
			"name": "登录IP",
			"prop": ` style="width:8em;"`,
		})
		once["input"] = input
		ret["once"] = once
	}
	return c.SuccJSON(w, r, ret)
}
func Online_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, "p505d") {
		return c.ErrJSON(w, "您未被授权操作")
	}
	ids := post.Get("ids")
	if ids == "" {
		return c.ErrJSON(w, "请选择至少一条")
	}
	csql := c.NewCiySQL("zc_online")
	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_online")
			admin.SaveLogDB(c.CiyDB, "zc_online", 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)
}

func Online_logout(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, "p505o") {
		return c.ErrJSON(w, "您未被授权操作")
	}
	id := post.Getint("id")
	csql := c.NewCiySQL("zc_online")
	csql.Where("id", id)
	rows, _ := c.CiyDB.Getone(csql)
	if rows == nil {
		return c.ErrJSON(w, "记录不存在")
	}
	if rows["user"] == userid {
		return c.ErrJSON(w, "不能下线自己")
	}
	c.CiyDB.Execute("update zc_online set sid='' where id=?", id)
	updata := map[string]any{}
	updata["id"] = id
	updata["sid"] = ""
	ret := map[string]any{}
	ret["data"] = updata
	return c.SuccJSON(w, r, ret)
}
