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"
	"github.com/kataras/iris/v12"
	"time"
)

func GetPmLocationList(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/PmLocation_controller.go"
	logs.Level = "info"
	logs.Function = "GetPmLocationList"
	logs.Message = " 地点定义查看"
	logs.Operator = user.Userid
	logs.TimeStamp = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	logs.InsertRecord()
	var data models.PmLocation
	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 GetPmLocation(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.PmLocation
	locaid, _ := ctx.URLParamInt("locaid")
	me.Finr = user.Pid
	me.Locaid = locaid
	result, err := me.SelectOne()
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	supports.Ok(ctx, supports.OptionSuccess, result)
}

func InsertPmLocation(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/PmLocation_controller.go"
	logs.Level = "info"
	logs.Function = "InsertPmLocation"
	logs.Message = "添加 地点定义"
	logs.Operator = user.Userid
	logs.TimeStamp = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	logs.InsertRecord()

	data := new(models.PmLocation)
	data.Finr = user.Pid
	data.Lastuser = user.Userid
	data.Credatuz = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	data.Lastmodif = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	if err := ctx.ReadJSON(data); err != nil {
		logs := new(models.LeitServerLog)
		logs.File = "/controllers/PmLocation_controller.go"
		logs.Level = "debug"
		logs.Function = "InsertPmLocation"
		logs.Message = error.Error(err)
		logs.Operator = user.Userid
		logs.TimeStamp = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
		logs.InsertRecord()
		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 UpdatePmLocation(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/PmLocation_controller.go"
	logs.Level = "info"
	logs.Function = "UpdatePmLocation"
	logs.Message = "更新 地点定义"
	logs.Operator = user.Userid
	logs.TimeStamp = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	logs.InsertRecord()
	data := new(models.PmLocation)
	if err := ctx.ReadJSON(data); err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	data.Lastuser = user.Userid
	fileds := []string{"locaname","loca_cust1","loca_cust2","loca_cust3","loca_cust4","lastmodif","lastuser"}
	err := data.Update(fileds)
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "更新失败", nil)
		return
	}

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

func DeletePmLocation(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/PmLocation_controller.go"
	logs.Level = "info"
	logs.Function = "DeletePmLocation"
	logs.Message = "删除 地点定义"
	logs.Operator = user.Userid
	logs.TimeStamp = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	logs.InsertRecord()
	me := new(models.PmLocation)
	locaid, _ := ctx.URLParamInt("locaid")
	me.Finr = user.Pid
	me.Locaid = locaid
	err := me.Del()
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "删除失败", nil)
		return
	}
	supports.Ok(ctx, "删除成功", "")
}

//查询数据
func SelectArrPmLocation(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.PmLocation)
	me.Finr = user.Pid
	res, err := me.SelectAll()
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "查询失败", nil)
		return
	}
	supports.Ok(ctx, "成功", res)
}

//查询数据
func SelectAarrPmLocations(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.PmLocation)
	me.Finr = user.Pid
	res, err := me.SelectLocalions()
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "查询失败", nil)
		return
	}
	supports.Ok(ctx, "成功", res)
}

//递归查询数据
func GetMenuPmLocation(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.PmLocation)
	me.Finr = user.Pid
	result, err := me.TreeAsset()
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	supports.Ok(ctx, supports.OptionSuccess, result)
}
