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"
	"log"
	"time"
)

func GetPmAssettypeList(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user, *user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	//日志
	logs := new(models.LeitServerLog)
	logs.File = "/controllers/PmAssettype_controller.go"
	logs.Level = "info"
	logs.Function = "GetPmAssettypeList"
	logs.Message = "资产类型查看"
	logs.Operator = user.Userid
	logs.TimeStamp = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	logs.InsertRecord()
	var data models.PmAssettype
	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)
	}
	descr := ctx.URLParamTrim("descr")
	data.Finr = user.Pid
	result, count, err := data.GetPage(descr, 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 GetAllPmAssettypes(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user, *user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	//日志
	logs := new(models.LeitServerLog)
	logs.File = "/controllers/PmAssettype_controller.go"
	logs.Level = "info"
	logs.Function = "GetAllPmAssettypes"
	logs.Message = "资产类型查看"
	logs.Operator = user.Userid
	logs.TimeStamp = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	logs.InsertRecord()
	var data models.PmAssettype
	var err error

	data.Finr = user.Pid
	result, err := data.GetAll()
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	supports.Ok(ctx, supports.OptionSuccess, result)
}

func GetPmAssettype(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.PmAssettype
	assettype := ctx.URLParam("assettype")
	me.Finr = user.Pid
	me.Assettype = assettype
	result, err := me.SelectOne()
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	supports.Ok(ctx, supports.OptionSuccess, result)
}

func InsertPmAssettype(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user, *user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	//日志
	logs := new(models.LeitServerLog)
	logs.File = "/controllers/PmAssettype_controller.go"
	logs.Level = "info"
	logs.Function = "InsertPmAssettype"
	logs.Message = "添加资产类型"
	logs.Operator = user.Userid
	logs.TimeStamp = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	logs.InsertRecord()

	//获取通过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)
	filPath := "public/uploadfile/" + guid + ".jpg"
	savePath := "web/public/uploadfile/" + guid + ".jpg"

	for _, file := range files {
		// 上传文件至指定目录
		err := utils.SaveUploadedFile(file, savePath)
		if err != nil {
			log.Printf("上传图片错误:%v", err)
			return
		}
	}

	data := new(models.PmAssettype)
	data.Finr = user.Pid
	data.Lastuser = user.Userid
	data.Bitmap = filPath
	data.Credatuz = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	data.Lastmodif = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	info := ctx.FormValue("data")
	err = json.Unmarshal([]byte(info), data)
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "json解析错误", nil)
		return
	}
	err = data.Add()
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "添加失败！", nil)
		return
	}
	supports.Ok(ctx, supports.OptionSuccess, data)
}

func UpdatePmAssettype(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user, *user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	//日志
	logs := new(models.LeitServerLog)
	logs.File = "/controllers/PmAssettype_controller.go"
	logs.Level = "info"
	logs.Function = "UpdatePmAssettype"
	logs.Message = "更新资产类型"
	logs.Operator = user.Userid
	logs.TimeStamp = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	logs.InsertRecord()

	//获取通过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[]"]

	data := new(models.PmAssettype)
	info := ctx.FormValue("data")
	err = json.Unmarshal([]byte(info), data)
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "json解析错误", nil)
		return
	}
	original, err := data.SelectOne()
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到原有信息", nil)
		return
	}
	data.Bitmap = original.Bitmap
	for _, file := range files {
		if !utils.ValueIsEmpty(file.Filename) {
			guid := utils.MakeOrderSn(user.Userid)
			filPath := "public/uploadfile/" + guid + ".jpg"
			savePath := "web/public/uploadfile/" + guid + ".jpg"
			// 上传文件至指定目录
			err := utils.SaveUploadedFile(file, savePath)
			if err != nil {
				log.Printf("上传图片错误:%v", err)
				return
			}
			data.Bitmap = filPath
		}
	}
	data.Lastuser = user.Userid
	data.Lastmodif = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")

	res := data.Update()
	if !res {
		supports.Error(ctx, iris.StatusBadRequest, "更新失败！", nil)
		return
	}

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

func DeletePmAssettype(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user, *user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	//日志
	logs := new(models.LeitServerLog)
	logs.File = "/controllers/PmAssettype_controller.go"
	logs.Level = "info"
	logs.Function = "DeletePmAssettype"
	logs.Message = "删除资产类型"
	logs.Operator = user.Userid
	logs.TimeStamp = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	logs.InsertRecord()
	me := new(models.PmAssettype)
	finr := ctx.URLParam("finr")
	assettype := ctx.URLParam("assettype")
	me.Finr = utils.ValueToInt(finr, 0)
	me.Assettype = assettype
	res := me.Del()
	if !res {
		supports.Error(ctx, iris.StatusBadRequest, "删除失败", nil)
		return
	}
	supports.Ok(ctx, "删除成功", "")
}
