package ap

import (
	"fmt"
	"net/http"

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

func paper_setwhere(post *c.CiyPost) (map[string]any, *c.CiySQL) {
	query := post.Getobj("query")
	csql := c.NewCiySQL("ap_paper")
	csql.Where("name like", c.Getstr(query, "name"))
	csql.Where_numrange("readcnt", c.Getstr(query, "readcnt_1"), c.Getstr(query, "readcnt_2"), 1)
	csql.Where("inputuser", c.Getstr(query, "inputuser"))
	csql.Where_daterange("uptimes", c.Getstr(query, "uptimes"))
	order := c.Getstr(query, "order", "id desc")
	csql.Order(order)
	query["order"] = order
	return query, csql
}
func Paper_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 := paper_setwhere(post)
	pageno := post.Getint("pageno", 1)
	pagecount := post.Getint("pagecount", 10)
	csql.Limit(pageno, pagecount)
	csql.Column("!content")
	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", "操作")
		field["name"]["thwidth"] = "30em"
		field["readcnt"]["order"] = "r"
		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": "name",
			"type": "input",
			"name": "名称",
			"prop": ` style="width:8em;"`,
		})
		input = append(input, map[string]any{
			"form": "readcnt",
			"type": "num",
			"name": "阅读数",
			"prop": ` style="width:4em;"`,
		})
		input = append(input, map[string]any{
			"form":   "inputuser",
			"type":   "select",
			"name":   "撰写人",
			"select": "adminuser",
			"all":    "全部",
		})
		input = append(input, map[string]any{
			"form": "uptimes",
			"type": "daterange",
			"name": "更新时间",
		})
		once["input"] = input
		ret["once"] = once
	}
	return c.SuccJSON(w, r, ret)
}
func Paper_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, "p10201u") {
		return c.ErrJSON(w, "您未被授权操作")
	}
	id := post.Getint("id")
	name := post.Get("name")
	title := post.Get("title")
	if name == "" {
		return c.ErrJSON(w, "请填写文档标题")
	}
	content := post.Get("content")
	if content == "[MD]" {
		return c.ErrJSON(w, "请填写内容")
	}
	var err error
	var datarow map[string]any
	if id > 0 {
		csql := c.NewCiySQL("ap_paper")
		csql.Where("id", id)
		datarow, err = c.CiyDB.Getone(csql)
		if datarow == nil {
			return c.ErrJSON(w, "数据不存在", err)
		}
	}
	err = c.CiyDB.Tran(func() error {
		var csql *c.CiySQL
		csql = c.NewCiySQL("ap_paper")
		csql.Where("name", name)
		csql.Column("id")
		chkid := c.Toint(c.CiyDB.Get1(csql))
		if chkid > 0 && ((id > 0 && chkid != id) || id == 0) {
			return fmt.Errorf("数据已存在")
		}
		updata := map[string]any{}
		updata["name"] = name
		updata["title"] = title
		updata["inputuser"] = userid
		updata["uptimes"] = c.Tostamp()
		updata["content"] = content
		csql = c.NewCiySQL("ap_paper")
		if id > 0 {
			csql.Where("id", id)
			_, err = c.CiyDB.Update(csql, updata)
		} else {
			id, err = c.CiyDB.Insert(csql, updata)
			updata["newid"] = id
		}
		if err != nil {
			return fmt.Errorf("更新失败:%v", err)
		}
		admin.SaveLogDB(c.CiyDB, "ap_paper", datarow, updata)
		return nil
	})
	if err != nil {
		return c.ErrJSON(w, "事务"+err.Error())
	}
	return c.SuccJSON(w, r)
}
func Paper_getdata(w http.ResponseWriter, r *http.Request) bool {
	post := c.NewCiyPost(w, r)
	_, userid := admin.Verifyfast(r, c.CiyDB, post)
	if userid == 0 {
		return false
	}
	id := post.Getint("id")
	data := post.Getbool("data")
	ret := map[string]any{}
	if data {
		csql := c.NewCiySQL("ap_paper")
		csql.Where("id", id)
		var err error
		ret["data"], err = c.CiyDB.Getone(csql)
		if err != nil {
			return c.ErrJSON(w, "读取失败:"+err.Error())
		}
	}
	return c.SuccJSON(w, r, ret)
}
func Paper_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, "p10201d") {
		return c.ErrJSON(w, "您未被授权操作")
	}
	ids := post.Get("ids")
	if ids == "" {
		return c.ErrJSON(w, "请选择至少一条")
	}
	csql := c.NewCiySQL("ap_paper")
	csql.Where("id in", ids)
	rows, _, err := c.CiyDB.Get(csql)
	if err != nil {
		return c.ErrJSON(w, "读取数据错误", err)
	}
	err = c.CiyDB.Tran(func() error {
		for _, row := range rows {
			rowid := c.Toint(row["id"])
			c.Delme(c.CiyDB, rowid, "ap_paper")
			admin.SaveLogDB(c.CiyDB, "ap_paper", row, nil)
		}
		return nil
	})
	if err != nil {
		return c.ErrJSON(w, "事务"+err.Error())
	}
	return c.SuccJSON(w, r)
}
