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

func GetStdeftabList(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/stdeftab_controller.go"
	log.Level = "info"
	log.Function = "GetStdeftabList"
	log.Message = "标准翻译查看"
	log.Operator = user.Userid
	log.TimeStamp = utils.TimeFormat(time.Now(),"yyyyMMddHHmmss")
	log.InsertRecord()
	var data models.Stdeftab
	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.Stdeftyp = ctx.URLParam("stdeftyp")
	data.Statid = ctx.URLParam("statid")
	data.Lang = ctx.URLParam("lang")
	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 GetStdeftab(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.Stdeftab
	me.Finr = user.Pid
	me.Stdeftyp = ctx.URLParam("stdeftyp")
	me.Statid = ctx.URLParam("statid")
	me.Lang = ctx.URLParam("lang")
	result, err := me.SelectOne()
	if err !=nil{
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	result.Clipped()
	supports.Ok(ctx, supports.OptionSuccess, result)
}

func InsertStdeftab(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/workline_controller.go"
	log.Level = "info"
	log.Function = "InsertStdeftab"
	log.Message = "添加生产线"
	log.Operator = user.Userid
	log.TimeStamp = utils.TimeFormat(time.Now(),"yyyyMMddHHmmss")
	log.InsertRecord()
	data := new(models.Stdeftab)
	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 {
		log := new(models.LeitServerLog)
		log.File = "/controllers/stdeftab_controller.go"
		log.Level = "debug"
		log.Function = "InsertStdeftab"
		log.Message = error.Error(err)
		log.Operator = user.Userid
		log.TimeStamp = utils.TimeFormat(time.Now(),"yyyyMMddHHmmss")
		log.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 UpdateStdeftab(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/Stdeftab_controller.go"
	log.Level = "info"
	log.Function = "UpdateStdeftab"
	log.Message = "更新定义"
	log.Operator = user.Userid
	log.TimeStamp = utils.TimeFormat(time.Now(),"yyyyMMddHHmmss")
	log.InsertRecord()
	data := new(models.Stdeftab)
	if err := ctx.ReadJSON(data); err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到相关信息", nil)
		return
	}
	data.Lastuser = user.Userid
	res:= data.Update()
	if !res{
		supports.Error(ctx, iris.StatusBadRequest, "更新失败", nil)
		return
	}

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

func DeleteStdeftab(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/Stdeftab_controller.go"
	log.Level = "info"
	log.Function = "DeleteStdeftab"
	log.Message = "删除"
	log.Operator = user.Userid
	log.TimeStamp = utils.TimeFormat(time.Now(),"yyyyMMddHHmmss")
	log.InsertRecord()
	me:=new(models.Stdeftab)
	me.Finr = user.Pid
	me.Stdeftyp = ctx.URLParam("stdeftyp")
	me.Statid = ctx.URLParam("statid")
	me.Lang = ctx.URLParam("lang")
	res := me.Del()
	if !res {
		supports.Error(ctx, iris.StatusBadRequest, "删除失败", nil)
		return
	}
	supports.Ok(ctx, "删除成功", "")
}

func SelectStdeftabArr(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.Stdeftab)
	me.Finr = user.Pid
	me.Statid = ctx.URLParam("statid")
	data,_ := me.SelectArr()
	supports.Ok(ctx, "成功", data)
}

func SelectStdeftabAll(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.Stdeftab)
	me.Finr = user.Pid
	statid := ctx.URLParam("statid")
	arr := strings.Split(statid,",")
	data,_ := me.SelectAll(arr)
	supports.Ok(ctx, "成功", data)
}