package api

import (
	model "LAPP_LF_MOM_BACKEND/models/api"
	"LAPP_LF_MOM_BACKEND/web/middleware/jwts"
	"LAPP_LF_MOM_BACKEND/web/models"
	"LAPP_LF_MOM_BACKEND/web/supports"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/core/router"
)

/******************************************************************************
 *
 * @Function Name            : RegisterReceiveScrew
 *-----------------------------------------------------------------------------
 *
 * @Description              : 提供一个接收screw
 *
 * @Function Parameters      : 路由分组
 *
 * @Function Parameters      : 路径
 *
 * @Function Parameters      : 实际处理请求的方法
 *
 * @Author                   : zhangxin
 *
 * @Date                     : 2021-05-14
 *
 ******************************************************************************/
func RegisterReceiveScrew(party router.Party, path string, method func(*model.Screw) error) {

	party.Post(path, func(ctx iris.Context) {

		var screwDefect model.Screw
		if err := ctx.ReadJSON(&screwDefect); err != nil {
			supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil)
			return
		}
		if screwDefect.STN == "" || screwDefect.PARTNO == "" {
			supports.Error(ctx, iris.StatusBadRequest, "参数错误", nil)
			return
		}
		err := method(&screwDefect)
		if err != nil {
			supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil)
			return
		}
		supports.Ok(ctx, supports.OptionSuccess, nil)
	})
}


/******************************************************************************
 *
 * @Function Name            : RegisterGetNewestScrew
 *-----------------------------------------------------------------------------
 *
 * @Description              : 获取最新的screw
 *
 * @Function Parameters      : 路由分组
 *
 * @Function Parameters      : 路径
 *
 * @Function Parameters      : 实际处理请求的方法
 *
 * @Author                   : zhangxin
 *
 * @Date                     : 2021-05-14
 *
 ******************************************************************************/
func RegisterGetNewestScrew(party router.Party, path string, method func(usertab *models.Usertab, stn string) (model.ScrewWithData, error)) {

	party.Get(path, func(ctx iris.Context) {
		user, ok := jwts.ParseToken(ctx)
		if !ok {
			supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
			return
		}
		stn := ctx.URLParam("stn")
		if stn == "" {
			supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
			return
		}
		result, err := method(user, stn)
		if err != nil {
			supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil)
			return
		}
		supports.Ok(ctx, supports.OptionSuccess, result)
	})
}

/******************************************************************************
 *
 * @Function Name            : RegisterGetScrewLi
 *-----------------------------------------------------------------------------
 *
 * @Description              : 获取screw list
 *
 * @Function Parameters      : 路由分组
 *
 * @Function Parameters      : 路径
 *
 * @Function Parameters      : 实际处理请求的方法
 *
 * @Author                   : zhangxin
 *
 * @Date                     : 2021-05-14
 *
 ******************************************************************************/
func RegisterGetScrewLi(party router.Party, path string, method func(user *models.Usertab, stn string) ([]model.ScrewWithData, error)) {

	party.Get(path, func(ctx iris.Context) {
		user, ok := jwts.ParseToken(ctx)
		if !ok {
			supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
			return
		}
		stn := ctx.URLParam("stn")
		if stn == "" {
			supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
			return
		}
		result, err := method(user, stn)
		if err != nil {
			supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil)
			return
		}
		supports.Ok(ctx, supports.OptionSuccess, result)
	})
}