package rigger

import (
	"fmt"
	"net/http"

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

func logdb_setwhere(post *c.CiyPost) (map[string]any, *c.CiySQL) {
	query := post.Getobj("query")
	csql := c.NewCiySQL("zc_log")
	liid := c.Getint(query, "liid")
	if liid == 1 {
		csql.Where("readuser=0")
	}
	if liid == 2 {
		csql.Where("readuser>0")
	}
	csql.Where("loguser", c.Getstr(query, "loguser"))
	csql.Where("readuser", c.Getstr(query, "readuser"))
	csql.Where("types", c.Getstr(query, "types"))
	csql.Where_daterange("addtimes", c.Getstr(query, "addtimes"))
	csql.Where("logs like", c.Getstr(query, "logs"))
	order := c.Getstr(query, "order", "id desc")
	csql.Order(order)
	query["order"] = order
	return query, csql
}
func Logdb_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 := logdb_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)
		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":   "loguser",
			"type":   "select",
			"name":   "操作人",
			"prop":   ` style="min-width:6em;"`,
			"select": "adminuser",
			"all":    "全部",
		})
		input = append(input, map[string]any{
			"form":   "readuser",
			"type":   "select",
			"name":   "审阅人",
			"prop":   ` style="min-width:6em;"`,
			"select": "adminuser",
			"all":    "全部",
		})
		input = append(input, map[string]any{
			"form": "types",
			"type": "input",
			"name": "LOG分类",
			"prop": ` style="width:8em;"`,
		})
		input = append(input, map[string]any{
			"form": "addtimes",
			"type": "daterange",
			"name": "发生日期",
		})
		input = append(input, map[string]any{
			"form": "logs",
			"type": "input",
			"name": "详情",
			"prop": ` style="width:8em;"`,
		})
		once["input"] = input
		ret["once"] = once
	}
	return c.SuccJSON(w, r, ret)
}
func Logdb_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, "p553d") {
		return c.ErrJSON(w, "您未被授权操作")
	}
	ids := post.Get("ids")
	if ids == "" {
		return c.ErrJSON(w, "请选择至少一条")
	}
	csql := c.NewCiySQL("zc_log")
	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_log")
			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 Logdb_multiread(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, "p553s") {
		return c.ErrJSON(w, "您未被授权操作")
	}
	ids := post.Get("ids")
	csql := c.NewCiySQL("zc_log")
	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 {
			rowid := c.Toint(row["id"])
			updata := map[string]any{}
			updata["readuser"] = userid
			csql = c.NewCiySQL("zc_log")
			csql.Where("id", rowid)
			_, err = c.CiyDB.Update(csql, updata)
			if err != nil {
				return fmt.Errorf("更新失败:%v", err)
			}
			vids = append(vids, rowid)
		}
		return nil
	})
	if err != nil {
		return c.ErrJSON(w, "事务"+err.Error())
	}
	ret := map[string]any{}
	ret["ids"] = vids
	return c.SuccJSON(w, r, ret)
}
