package admin

import (
	"fmt"
	"net/http"

	c "ciyon/zciyon"
)

func Index_init(w http.ResponseWriter, r *http.Request) bool {
	//menu，url: 普通链接~原型图 4378,key  普通链接~原型图 共存
	//menu，pow: d=删除|e=修改   p[id]e  默认p[id]v 显示权限   角色权限menu url<>'' 可选择授权
	post := c.NewCiyPost(w, r)
	_, userid := Verifyfast(r, c.CiyDB, post)
	if userid == 0 {
		return false
	}
	csql := c.NewCiySQL("zc_icon")
	csql.Where("icontarget", 10).Column("targetid as id,icon")
	iconrows, _, err := c.CiyDB.Get(csql)
	if err != nil {
		return c.ErrJSON(w, "遇到menu读取错误", err)
	}
	csql = c.NewCiySQL("zc_menu")
	csql.Where("isuse", 1).Order("csort desc,id").Column("id,upid,name,url,pow")
	menurows, _, err := c.CiyDB.Get(csql)
	if err != nil {
		return c.ErrJSON(w, "遇到menu读取错误", err)
	}
	if len(menurows) == 0 {
		menu := map[string]any{}
		menu["id"] = 2
		menu["upid"] = 0
		menu["name"] = "您无任何菜单权限"
		menu["url"] = ""
		menu["pow"] = ""
		menurows = append(menurows, menu)
	}
	csql = c.NewCiySQL("zc_mnufav")
	csql.Where("favtarget", 10).Where("user", userid).Column("menuid")
	mnufavrows, _, err := c.CiyDB.Get(csql)
	if err != nil {
		return c.ErrJSON(w, "遇到mnufav读取错误", err)
	}
	ret := map[string]any{}
	ret["icon"] = iconrows
	ret["menu"] = menurows
	ret["mnufav"] = mnufavrows
	ret["welcome"] = map[string]any{
		"url":  "welcome.html",
		"name": "控制台",
	}
	ret["title"] = "Ciyon SaaS总控台"
	return c.SuccJSON(w, r, ret)
}

func Index_favadd(w http.ResponseWriter, r *http.Request) bool {
	post := c.NewCiyPost(w, r)
	_, userid := Verifyfast(r, c.CiyDB, post)
	if userid == 0 {
		return false
	}
	menuid := post.Getint("id")
	csql := c.NewCiySQL("zc_mnufav")
	csql.Where("favtarget", 10).Where("user", userid).Where("menuid", menuid)
	favrow, err := c.CiyDB.Getone(csql)
	if err != nil {
		return c.ErrJSON(w, "遇到fav读取错误", err)
	}
	if favrow == nil {
		updata := map[string]any{}
		updata["favtarget"] = 10
		updata["user"] = userid
		updata["menuid"] = menuid
		updata["addtimes"] = c.Tostamp()
		csql := c.NewCiySQL("zc_mnufav")
		_, err = c.CiyDB.Insert(csql, updata)
		if err != nil {
			return c.ErrJSON(w, "添加fav失败", err)
		}
	}
	return c.SuccJSON(w, r)
}
func Index_favdel(w http.ResponseWriter, r *http.Request) bool {
	post := c.NewCiyPost(w, r)
	_, userid := Verifyfast(r, c.CiyDB, post)
	if userid == 0 {
		return false
	}
	menuid := post.Getint("id")
	csql := c.NewCiySQL("zc_mnufav")
	csql.Where("favtarget", 10).Where("user", userid).Where("menuid", menuid)
	_, err := c.CiyDB.Delete(csql)
	if err != nil {
		return c.ErrJSON(w, "删除fav失败", err)
	}
	return c.SuccJSON(w, r)
}

func Index_setssh(w http.ResponseWriter, r *http.Request) bool {
	post := c.NewCiyPost(w, r)
	_, userid := Verifyfast(r, c.CiyDB, post)
	if userid == 0 {
		return false
	}
	if Nopower(c.CiyDB, userid, "x1ssh") {
		return c.ErrJSON(w, "您未被授权操作")
	}
	able := post.Getbool("able") //true，打开SSH允许远程访问。   false，关闭防火墙禁止访问
	if able {
		//开启后，需定时关闭防火墙
		fmt.Println("开启SSH远程访问")
	}
	return c.SuccJSON(w, r)
}

// if strings.HasPrefix(r.Header.Get("Content-Type"), "multipart/form-data") {
// 	err := r.ParseMultipartForm(5 * 1024 * 1024 * 1024)
// 	if err == nil {
// 		Clog("POST MultipartForm:", r.MultipartForm) //.Value["aaa"]
// 	} else {
// 		Clog("POST MultipartForm err:", err)
// 	}
// 	file, header, err := r.FormFile("file")
// 	Clog("POST FormFile:", file, header, err)
// } else {
// 	post := c.NewCiyPost(w, r)
// 	fff := post.Get("str", c.CIYPOST_ALLOW_HTML)
// 	Clog("post str:", fff)
// 	// ddd = post.Getdate("date")
// 	// Clog("post time:", ddd)
// 	// det = c.Todate(ddd, "Y-m-d H:i:s")

// 	//Clog("post json:", post)
// }
// Clog("Headers:", r.Header)
// Clog("Cookies:", r.Cookies())
// Clog("Query:", r.URL.Query())
// if err := r.ParseForm(); err != nil {
//     Clog("Error parsing form:", err)
// }

// Clog("POST Form:", r.Form)         //get/post混合
// Clog("POST PostForm:", r.PostForm) //xform

// err := r.ParseMultipartForm(5 * 1024 * 1024 * 1024)
// if err == nil {
//     Clog("POST MultipartForm:", r.MultipartForm) //.Value["aaa"]
// } else {
//     Clog("POST MultipartForm err:", err)
// }
// file, header, err := r.FormFile("file")
// Clog("POST FormFile:", file, header, err)
// // 获取特定POST参数
// // name := r.PostForm.Get("aaa")
// // Clog("POST Param Get:", name)

// body, err := io.ReadAll(r.Body)
// if err != nil {
//     http.Error(w, "Error reading request body", http.StatusInternalServerError)
//     return
// }
// Clog("POST json:", string(body))
// // 获取请求的URL
// Clog("URL:", r.URL)
// Clog("Scheme:", r.URL.Scheme)
// Clog("Opaque:", r.URL.Opaque)
// Clog("User:", r.URL.User)
// Clog("Host:", r.URL.Host)
// Clog("Path:", r.URL.Path)
// Clog("RawPath:", r.URL.RawPath)
// Clog("OmitHost:", r.URL.OmitHost)
// Clog("ForceQuery:", r.URL.ForceQuery)
// Clog("RawQuery:", r.URL.RawQuery)
// Clog("Fragment:", r.URL.Fragment)
// Clog("RawFragment:", r.URL.RawFragment)
