package controllers

import (
	"LAPP_LF_MOM_BACKEND/utils"
	"LAPP_LF_MOM_BACKEND/web/middleware/jwts"
	"LAPP_LF_MOM_BACKEND/web/models"
	"LAPP_LF_MOM_BACKEND/web/supports"
	"encoding/json"
	"github.com/kataras/iris/v12"
	"time"
)

func GetScripttabList(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user, *user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	//日志
	log := new(models.LeitServerLog)
	log.File = "/controllers/scripttab_controller.go"
	log.Level = "info"
	log.Function = "GetScripttabList"
	log.Message = "查询导入表"
	log.Operator = user.Userid
	log.TimeStamp = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	log.InsertRecord()
	var data models.Scripttab
	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.Finr = user.Pid
	data.Script = ctx.URLParam("script")
	data.Scripttype = ctx.URLParam("scripttype")
	result, count, err := data.GetPage(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 GetScripttab(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user, *user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	var me models.Scripttab
	me.Finr = user.Pid
	me.Script = ctx.URLParam("script")
	result, err := me.SelectOne()
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	supports.Ok(ctx, supports.OptionSuccess, result)
}

func InsertScripttab(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user, *user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	//日志
	log := new(models.LeitServerLog)
	log.File = "/controllers/scripttab_controller.go"
	log.Level = "info"
	log.Function = "InsertScripttab"
	log.Message = "添加导入表"
	log.Operator = user.Userid
	log.TimeStamp = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	log.InsertRecord()
	data := new(models.Scripttab)
	if err := ctx.ReadJSON(data); err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "json解析错误", nil)
		return
	}
	data.Finr = user.Pid
	data.Lastuser = user.Userid
	data.Credatuz = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	data.Lastmodif = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	err := data.Add()
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "数据已经存在！", nil)
		return
	}
	supports.Ok(ctx, supports.OptionSuccess, data)
}

func UpdateScripttab(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user, *user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	//日志
	log := new(models.LeitServerLog)
	log.File = "/controllers/scripttab_controller.go"
	log.Level = "info"
	log.Function = "UpdateScripttab"
	log.Message = "更新导入表"
	log.Operator = user.Userid
	log.TimeStamp = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	log.InsertRecord()
	data := new(models.Scripttab)
	if err := ctx.ReadJSON(data); err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "json解析失败", nil)
		return
	}
	data.Lastuser = user.Userid
	res := data.Update()
	if !res {
		supports.Error(ctx, iris.StatusBadRequest, "更新失败", nil)
		return
	}

	supports.Ok(ctx, "修改成功", "")
}

func DeleteScripttab(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user, *user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	//日志
	log := new(models.LeitServerLog)
	log.File = "/controllers/scripttab_controller.go"
	log.Level = "info"
	log.Function = "DeleteScripttab"
	log.Message = "删除导入表"
	log.Operator = user.Userid
	log.TimeStamp = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	log.InsertRecord()
	me := new(models.Scripttab)
	me.Finr = user.Pid
	me.Script = ctx.URLParam("script")
	res := me.Del()
	if !res {
		supports.Error(ctx, iris.StatusBadRequest, "删除失败", nil)
		return
	}
	supports.Ok(ctx, "删除成功", "")
}

//上传excel
func UpExcelScripttab(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user, *user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	//日志
	log := new(models.LeitServerLog)
	log.File = "/controllers/scripttab_controller.go"
	log.Level = "info"
	log.Function = "UpExcelScripttab"
	log.Message = "上传excel表格"
	log.Operator = user.Userid
	log.TimeStamp = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	log.InsertRecord()
	var tab models.Scripttab
	data := ctx.FormValue("data")
	err := json.Unmarshal([]byte(data), &tab)
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "json解析错误", 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("test")
	savePath := "web/public/uploadxlsx/" + guid + ".xlsx"

	for _, file := range files {
		// 上传文件至指定目录
		err := utils.SaveUploadedFile(file, savePath)
		if err != nil {
			return
		}
	}

	tab.Finr = user.Pid
	tab.Lastuser = user.Userid
	tab.Lastmodif = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	tab.Credatuz = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	res := tab.ReadXlsx(savePath)
	if res {
		supports.Ok(ctx, supports.OptionSuccess, "")
		return
	}
	supports.Error(ctx, iris.StatusBadRequest, "上传失败", nil)
	return
}

//查询新增数据
func SelectArrScripttab(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user, *user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	me := new(models.Scripttab)
	me.Scripttable = ctx.URLParam("scripttable")
	res, err := me.SelectArr()
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "查询失败", nil)
		return
	}
	supports.Ok(ctx, "成功", res)
}
