package rigger

import (
	"fmt"
	"net/http"
	"strings"

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

func Cataindex_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
	}
	csql := c.NewCiySQL("zc_cata")
	csql.Where("cbid=0")
	csql.Order("csort,id")
	rows, _, err := c.CiyDB.Get(csql)
	if err != nil {
		return c.ErrJSON(w, "读取错误", err)
	}
	ret := map[string]any{}
	ret["list"] = rows
	if post.Getbool("field") {
		field := map[string]map[string]any{}
		fshow := ""
		c.FieldAdd(&field, &fshow, -1, "_btn", "操作")
		c.FieldAdd(&field, &fshow, -1, "id", "|库索引|")
		c.FieldAdd(&field, &fshow, -1, "name", "库名称")
		c.FieldAdd(&field, &fshow, -1, "codeid", "库代码")
		c.FieldAdd(&field, &fshow, -1, "csort", "排序|")
		c.FieldAdd(&field, &fshow, -1, "extdata", "引用表")
		field["name"]["thwidth"] = "20em"
		field["extdata"]["thwidth"] = "20em"
		ret["fshow"] = strings.TrimLeft(fshow, ",")
		ret["field"] = field
	}
	if post.Getbool("once") {
		once := map[string]any{}
		input := make([]map[string]string, 0)
		input = append(input, map[string]string{
			"form": "codeid",
			"type": "input",
			"name": "库代码",
			"prop": ` style="width:10em;"`,
		})
		input = append(input, map[string]string{
			"form": "name",
			"type": "input",
			"name": "库名称",
			"prop": ` style="width:10em;"`,
		})
		input = append(input, map[string]string{
			"form": "extdata",
			"type": "input",
			"name": "引用表",
			"prop": ` style="width:10em;"`,
		})
		once["input"] = input
		ret["once"] = once
	}
	return c.SuccJSON(w, r, ret)
}
func Cataindex_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, "p601pi") {
		return c.ErrJSON(w, "您未被授权操作")
	}
	id := post.Getint("id")
	codeid := post.Get("codeid")
	if codeid == "" {
		return c.ErrJSON(w, "请填写值")
	}
	name := post.Get("name")
	if name == "" {
		return c.ErrJSON(w, "请填写名称")
	}
	upid := post.Getint("upid")
	csort := post.Getint("csort")
	extdata := post.Get("extdata")
	var err error
	var datarow map[string]any
	if id > 0 {
		csql := c.NewCiySQL("zc_cata")
		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_cata")
		csql.Where("cbid=0")
		csql.Where("codeid", codeid)
		csql.Column("id")
		chkid := c.Toint(c.CiyDB.Get1(csql))
		if chkid > 0 && ((id > 0 && chkid != id) || id == 0) {
			return fmt.Errorf("该库代码重复")
		}
		updata["upid"] = upid
		updata["csort"] = csort
		updata["codeid"] = codeid
		updata["name"] = name
		updata["extdata"] = extdata
		csql = c.NewCiySQL("zc_cata")
		if id > 0 {
			csql.Where("id", id)
			_, err = c.CiyDB.Update(csql, updata)
		} else {
			updata["isuse"] = 1
			id, err = c.CiyDB.Insert(csql, updata)
		}
		updata["id"] = id
		if err != nil {
			return fmt.Errorf("更新失败:%v", err)
		}
		admin.SaveLogDB(c.CiyDB, "zc_cata", 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 Cataindex_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, "p601pi") {
		return c.ErrJSON(w, "您未被授权操作")
	}
	ids := post.Get("ids")
	if ids == "" {
		return c.ErrJSON(w, "请选择至少一条")
	}
	var csql *c.CiySQL
	csql = c.NewCiySQL("zc_cata")
	csql.Where("id in", ids)
	rows, _, _ := c.CiyDB.Get(csql)
	vids := make([]int, 0)
	err := c.CiyDB.Tran(func() error {
		for _, row := range rows {
			delid := c.Toint(row["id"])
			csql = c.NewCiySQL("zc_cata")
			csql.Where("upid", delid)
			csql.Where("cbid=0")
			downcnt := c.Toint(c.CiyDB.Get1(csql))
			if downcnt > 0 {
				return fmt.Errorf("该库有%d个子库，请先删除子库", downcnt)
			}
			c.Delcheck(c.CiyDB, delid, "zc_cata", "cbid", c.Tostr(row["name"]))
			c.Delme(c.CiyDB, delid, "zc_cata")
			admin.SaveLogDB(c.CiyDB, "zc_cata", 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)
}
