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 MemberWithdrawalLog struct {
	api.Api
}

// 分页查询
func (e MemberWithdrawalLog) GetPage(c *gin.Context) {
	s := appservice.MemberWithdrawalLog{}
	req := dto.MemberWithdrawalLogGetPageAppReq{}
	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, err.Error())
		return
	}

	var count int64
	var list []dto.MemberWithdrawalLogResp

	req.UserId = common.GetUserId(c)
	code := s.GetPage(&req, &list, &count)

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

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

// 用户提现申请
func (e MemberWithdrawalLog) Apply(c *gin.Context) {
	s := appservice.MemberWithdrawalLog{}
	req := dto.MemberWithdrawalLogApplyReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

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

	req.UserId = common.GetUserId(c)
	code := s.Apply(&req)

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

	e.OK(nil, "success")
}

// 用户取消提现
func (e MemberWithdrawalLog) Cancel(c *gin.Context) {
	s := appservice.MemberWithdrawalLog{}
	req := dto.MemberWithdrawalLogCancelReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

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

	userId := common.GetUserId(c)
	code := s.Canceled(&req, userId)

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

	e.OK(nil, "success")
}
