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 GetAttributeList(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/me_attribute_controller.go"
	logs.Level = "info"
	logs.Function = "GetAttributeList"
	logs.Message = "查询属性主数据"
	logs.Operator = user.Userid
	logs.TimeStamp = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	logs.InsertRecord()
	var data models.PmAttribute
	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 GetAttribute(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.PmAttribute
	me.Attrnr, _ = ctx.URLParamInt("attrnr")
	me.Finr = user.Pid
	result, err := me.SelectOne()
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	supports.Ok(ctx, supports.OptionSuccess, result)
}

func InsertAttribute(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/me_attribute_controller.go"
	logs.Level = "info"
	logs.Function = "InsertAttribute"
	logs.Message = "添加属性主数据"
	logs.Operator = user.Userid
	logs.TimeStamp = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	logs.InsertRecord()

	data := new(models.PmAttribute)
	temdata := ctx.FormValue("data")
	err := json.Unmarshal([]byte(temdata), data)
	data.Finr = user.Pid
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "json解析错误", nil)
		return
	}
	file, info, err := ctx.FormFile("valpicture")
	if err == nil {
		defer file.Close()
		guid := utils.MakeOrderSn("Attr")
		filePath := "public/uploadfile/" + guid + ".jpg"
		savePath := "web/public/uploadfile/" + guid + ".jpg"

		// 上传文件至指定目录
		err = utils.SaveUploadedFile(info, savePath)
		if err != nil {
			log.Printf("上传图片错误:%v", err)
			supports.Error(ctx, iris.StatusBadRequest, "valpicture上传图片错误", nil)
			return
		}
		data.Valpicture = filePath
	}

	file1, pic1, err := ctx.FormFile("pic1")
	if err == nil {
		defer file1.Close()
		guid1 := utils.MakeOrderSn("Attr")
		pic1Path := "public/uploadfile/" + guid1 + ".jpg"
		save1Path := "web/public/uploadfile/" + guid1 + ".jpg"

		// 上传文件至指定目录
		err = utils.SaveUploadedFile(pic1, save1Path)
		if err != nil {
			log.Printf("上传图片错误:%v", err)
			supports.Error(ctx, iris.StatusBadRequest, "pic1上传图片错误", nil)
			return
		}
		data.Picture1 = pic1Path
	}

	file2, pic2, err := ctx.FormFile("pic2")
	if err == nil {
		defer file2.Close()
		guid2 := utils.MakeOrderSn("Attr")
		pic2Path := "public/uploadfile/" + guid2 + ".jpg"
		save2Path := "web/public/uploadfile/" + guid2 + ".jpg"

		// 上传文件至指定目录
		err = utils.SaveUploadedFile(pic2, save2Path)
		if err != nil {
			log.Printf("上传图片错误:%v", err)
			supports.Error(ctx, iris.StatusBadRequest, "pic2上传图片错误", nil)
			return
		}

		data.Picture2 = pic2Path
	}

	data.Lastuser = user.Userid
	data.Credatuz = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	data.Lastmodif = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	err = data.Add()
	if err != nil {
		log.Printf("错误信息:%v", err)
		supports.Error(ctx, iris.StatusBadRequest, "数据已经存在！", nil)
		return
	}
	supports.Ok(ctx, supports.OptionSuccess, data)
}

func UpdateAttribute(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/me_attribute_controller.go"
	logs.Level = "info"
	logs.Function = "UpdateAttribute"
	logs.Message = "更新属性主数据"
	logs.Operator = user.Userid
	logs.TimeStamp = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	logs.InsertRecord()

	data := new(models.PmAttribute)
	temdata := ctx.FormValue("data")
	err := json.Unmarshal([]byte(temdata), data)
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "json解析错误", nil)
		return
	}
	file, info, err := ctx.FormFile("valpicture")
	if err == nil {
		defer file.Close()
		guid := utils.MakeOrderSn("Attr")
		filePath := "public/uploadfile/" + guid + ".jpg"
		savePath := "web/public/uploadfile/" + guid + ".jpg"

		// 上传文件至指定目录
		err = utils.SaveUploadedFile(info, savePath)
		if err != nil {
			log.Printf("上传图片错误:%v", err)
			supports.Error(ctx, iris.StatusBadRequest, "valpicture上传图片错误", nil)
			return
		}
		data.Valpicture = filePath
	}

	file1, pic1, err := ctx.FormFile("pic1")
	if err == nil {
		defer file1.Close()
		guid1 := utils.MakeOrderSn("Attr")
		pic1Path := "public/uploadfile/" + guid1 + ".jpg"
		save1Path := "web/public/uploadfile/" + guid1 + ".jpg"

		// 上传文件至指定目录
		err = utils.SaveUploadedFile(pic1, save1Path)
		if err != nil {
			log.Printf("上传图片错误:%v", err)
			supports.Error(ctx, iris.StatusBadRequest, "pic1上传图片错误", nil)
			return
		}
		data.Picture1 = pic1Path
	}

	file2, pic2, err := ctx.FormFile("pic2")
	if err == nil {
		defer file2.Close()
		guid2 := utils.MakeOrderSn("Attr")
		pic2Path := "public/uploadfile/" + guid2 + ".jpg"
		save2Path := "web/public/uploadfile/" + guid2 + ".jpg"

		// 上传文件至指定目录
		err = utils.SaveUploadedFile(pic2, save2Path)
		if err != nil {
			log.Printf("上传图片错误:%v", err)
			supports.Error(ctx, iris.StatusBadRequest, "pic2上传图片错误", nil)
			return
		}

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

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

func DeleteAttribute(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/me_attribute_controller.go"
	logs.Level = "info"
	logs.Function = "DeleteAttribute"
	logs.Message = "删除属性主数据"
	logs.Operator = user.Userid
	logs.TimeStamp = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	logs.InsertRecord()
	me := new(models.PmAttribute)
	me.Attrnr, _ = ctx.URLParamInt("attrnr")
	me.Finr = user.Pid
	res := me.Del()
	if !res {
		supports.Error(ctx, iris.StatusBadRequest, "删除失败", nil)
		return
	}
	supports.Ok(ctx, "删除成功", "")
}
