package controllers

import (
	"LAPP_LF_MOM_BACKEND/conf"
	"LAPP_LF_MOM_BACKEND/utils"
	"LAPP_LF_MOM_BACKEND/web/middleware/jwts"
	"LAPP_LF_MOM_BACKEND/web/models"
	"LAPP_LF_MOM_BACKEND/web/supports"
	"github.com/kataras/iris/v12"
	"io"
	"log"
	"net/http"
	"time"
)

//上传excel
func TabInfo(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user, *user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	//获取通过iris.WithPostMaxMemory获取的最大上传值大小。
	maxSize := ctx.Application().ConfigurationReadOnly().GetPostMaxMemory()
	err := ctx.Request().ParseMultipartForm(maxSize)
	if err != nil {
		ctx.StatusCode(iris.StatusInternalServerError)
		ctx.WriteString(err.Error())
		return
	}
	form := ctx.Request().MultipartForm
	files := form.File["upload[]"]

	guid := utils.MakeOrderSn(user.Userid)
	savePath := "web/public/uploadxlsx/" + guid + ".xlsx"

	for _, file := range files {
		// 上传文件至指定目录
		err := utils.SaveUploadedFile(file, savePath)
		if err != nil {
			log := new(models.LeitServerLog)
			log.File = "/controllers/TabInfo"
			log.Level = "debug"
			log.Function = "TabInfo"
			log.Message = error.Error(err)
			log.Operator = user.Userid
			log.TimeStamp = utils.TimeFormat(time.Now(),"yyyyMMddHHmmss")
			log.InsertRecord()
			return
		}
	}
	tab := new(models.Tabcolname)
	tab.Lastuser = user.Userid
	res := tab.ReadXlsx(savePath)
	if res {
		supports.Ok(ctx, supports.OptionSuccess, "")
		return
	}
	supports.Error(ctx, iris.StatusBadRequest, "上传失败", nil)
	return
}

//下载模板
func Download(ctx iris.Context) {
	file := conf.ExampleFile
	ctx.Header("Content-Disposition", `attachment; filename="example.xlsx"`)
	ctx.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
	res, err := http.Get(file)
	defer res.Body.Close()
	if err != nil {
		log.Println("获取文件错误")
	}
	io.Copy(ctx.ResponseWriter(), res.Body)

}

//分页查询
func TabDataListPage(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user, *user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	var data models.Tabcolname
	var err error
	var pageSize = 10
	var pageIndex = 1

	if size := ctx.URLParam("pageSize"); size != "" {
		pageSize = utils.ValueToInt(size, 0)
	}

	if index := ctx.URLParam("pageIndex"); index != "" {
		pageIndex = utils.ValueToInt(index, 0)
	}

	data.Colname = ctx.URLParam("colname")
	result, count, err := data.DataListPage(pageSize, pageIndex)
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	res := make(map[string]interface{})
	res["data"] = result
	res["count"] = count
	res["pageIndex"] = pageIndex
	res["pageSize"] = pageSize
	supports.Ok(ctx, supports.OptionSuccess, res)
}

//返回所有数据
func TabDataList(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user, *user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	var data models.Tabcolname
	var err error
	result, err := data.DataList()
	res := map[string]string{}
	for _,v:= range result{
		key := v.Tabname +"-"+v.Colname
		val := v.Textlabel
		res[key] = val
	}
	if err != nil {
		log := new(models.LeitServerLog)
		log.File = "/controllers/TabDataList"
		log.Level = "debug"
		log.Function = "TabDataList"
		log.Message = error.Error(err)
		log.Operator = user.Userid
		log.TimeStamp = utils.TimeFormat(time.Now(),"yyyyMMddHHmmss")
		log.InsertRecord()
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	supports.Ok(ctx, supports.OptionSuccess, res)
}

//删除数据
func DeleteTab(ctx iris.Context) {
	var data models.Tabcolname
	data.Tabname = ctx.URLParam("tabname")
	data.Colname = ctx.URLParam("colname")
	ok, err := data.DelData()
	if !ok || err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	supports.Ok(ctx, "删除成功", "")
}
