package fronted

import (
	"go-admin/app/admin/service/appservice"
	"go-admin/app/admin/service/common"
	"go-admin/app/admin/service/dto"
	"go-admin/common/service/sysservice/sysstatuscode"
	statuscode "go-admin/common/status_code"

	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"github.com/go-admin-team/go-admin-core/sdk/api"
)

type MemberRenwalLog struct {
	api.Api
}

// 分页查询
func (e MemberRenwalLog) GetPage(c *gin.Context) {
	s := appservice.MemberRenwalLog{}
	req := dto.MemberRenwalLogPageAppReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.Form, binding.Query).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, "server error")
		return
	}
	userId := common.GetUserId(c)
	req.Language = common.GetLanguage(c)
	data := make([]dto.MemberRenwalLogResp, 0)
	var count int64
	code := s.GetPage(userId, &req, &data, &count)

	if code != statuscode.OK {
		e.Error(code, nil, sysstatuscode.GetStatusCodeDescription(c, code))
	}

	e.PageOK(data, int(count), req.PageIndex, req.PageSize, "success")
}

func (e MemberRenwalLog) Apply(c *gin.Context) {
	s := appservice.MemberRenwalLog{}
	req := dto.MemberRenwalCreateAppReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, "server error")
		return
	}

	req.UserId = common.GetUserId(c)
	data := dto.MemberRenwalCreateAppResp{}
	code := s.Renwal(&req, &data)

	if code != statuscode.OK {
		e.Error(code, nil, sysstatuscode.GetStatusCodeDescription(c, code))
		return
	}

	e.OK(data, "success")
}
