package rigger

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

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

func Depart_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_depart")
	csql.Order("csort desc,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, fshow := c.CiyDB.GetField(csql)
		c.FieldAdd(&field, &fshow, -1, "_btn", "操作")
		field["csort"]["thwidth"] = "8em"
		ret["fshow"] = 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": "name",
			"type": "input",
			"name": "组织名称",
			"prop": ` style="width:8em;"`,
		})
		once["input"] = input
		ret["once"] = once
	}
	return c.SuccJSON(w, r, ret)
}
func Depart_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, "p501u") {
		return c.ErrJSON(w, "您未被授权操作")
	}
	id := post.Getint("id")
	name := post.Get("name")
	if name == "" {
		return c.ErrJSON(w, "请填写部门名称")
	}
	upid := post.Getint("upid")
	csort := post.Getint("csort")
	isuse := post.Getint("isuse")
	csql := c.NewCiySQL("zc_depart")
	csql.Where("id", id)
	datarow, err := c.CiyDB.Getone(csql)
	if err != nil {
		return c.ErrJSON(w, "数据不存在", err)
	}
	err = c.CiyDB.Tran(func() error {
		updata := map[string]any{}
		updata["name"] = name
		updata["isuse"] = isuse
		updata["upid"] = upid
		updata["csort"] = csort
		csql = c.NewCiySQL("zc_depart")
		csql.Where("id", id)
		_, err = c.CiyDB.Update(csql, updata)
		if err != nil {
			return fmt.Errorf("更新失败:%v", err)
		}
		admin.SaveLogDB(c.CiyDB, "zc_depart", datarow, updata)
		return nil
	})
	if err != nil {
		return c.ErrJSON(w, "事务"+err.Error())
	}
	return c.SuccJSON(w, r)
}
func Depart_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, "p501d") {
		return c.ErrJSON(w, "您未被授权操作")
	}
	ids := post.Get("ids")
	if ids == "" {
		return c.ErrJSON(w, "请选择至少一条")
	}
	csql := c.NewCiySQL("zc_depart")
	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.Delcheck(c.CiyDB, delid, "zc_admin", "departid", "人员")
			c.Delcheck(c.CiyDB, delid, "zc_depart", "upid", "下级部门")
			c.Delme(c.CiyDB, delid, "zc_depart")
			admin.SaveLogDB(c.CiyDB, "zc_depart", 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 Depart_modifyupid(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, "p501u") {
		return c.ErrJSON(w, "您未被授权操作")
	}
	id := post.Getint("id")
	newupid := post.Getint("newupid")
	csql := c.NewCiySQL("zc_depart")
	csql.Where("id", id)
	datarow, err := c.CiyDB.Getone(csql)
	if err != nil {
		return c.ErrJSON(w, "数据不存在", err)
	}

	err = c.CiyDB.Tran(func() error {
		updata := map[string]any{}
		updata["upid"] = newupid
		csql = c.NewCiySQL("zc_depart")
		csql.Where("id", id)
		_, err = c.CiyDB.Update(csql, updata)
		if err != nil {
			return fmt.Errorf("操作数据库失败:%v", err)
		}
		admin.SaveLogDB(c.CiyDB, "zc_depart", datarow, updata)
		return nil
	})
	if err != nil {
		return c.ErrJSON(w, "事务"+err.Error())
	}
	return c.SuccJSON(w, r)
}
func Depart_multiadd(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, "p501u") {
		return c.ErrJSON(w, "您未被授权操作")
	}
	upid := post.Getint("upid")
	multi := strings.Split(post.Get("multi"), "\n")
	cnt := 0
	err := c.CiyDB.Tran(func() error {
		for _, m := range multi {
			m = strings.TrimSpace(m)
			if m == "" {
				continue
			}
			updata := map[string]any{}
			updata["name"] = m
			updata["isuse"] = 1
			updata["upid"] = upid
			updata["csort"] = 10
			csql := c.NewCiySQL("zc_depart")
			_, err := c.CiyDB.Insert(csql, updata)
			if err != nil {
				return fmt.Errorf("新增失败:%v", err)
			}
			cnt++
			admin.SaveLogDB(c.CiyDB, "zc_depart", nil, updata)
		}
		return nil
	})
	if err != nil {
		return c.ErrJSON(w, "事务"+err.Error())
	}
	if cnt == 0 {
		return c.ErrJSON(w, "没有任何新增")
	}
	return c.SuccJSON(w, r)
}
