package apis

import (
	"fmt"

	"github.com/gin-gonic/gin"
	"github.com/go-admin-team/go-admin-core/sdk/api"
	"github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
	_ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"

	"go-admin/app/admin/models"
	"go-admin/app/admin/service"
	"go-admin/app/admin/service/dto"
	"go-admin/common/actions"
)

type MemberWithdrawalLog struct {
	api.Api
}

// GetPage 获取用户提现记录列表
// @Summary 获取用户提现记录列表
// @Description 获取用户提现记录列表
// @Tags 用户提现记录
// @Param networkName query string false "网络名称"
// @Param status query string false "提现状态(member_withdrawal_status)"
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {object} response.Response{data=response.Page{list=[]models.MemberWithdrawalLog}} "{"code": 200, "data": [...]}"
// @Router /api/v1/member-withdrawal-log [get]
// @Security Bearer
func (e MemberWithdrawalLog) GetPage(c *gin.Context) {
	req := dto.MemberWithdrawalLogGetPageReq{}
	s := service.MemberWithdrawalLog{}
	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
	}

	p := actions.GetPermissionFromContext(c)
	list := make([]models.MemberWithdrawalLog, 0)
	var count int64

	err = s.GetPage(&req, p, &list, &count)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取用户提现记录失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

// Get 获取用户提现记录
// @Summary 获取用户提现记录
// @Description 获取用户提现记录
// @Tags 用户提现记录
// @Param id path int false "id"
// @Success 200 {object} response.Response{data=models.MemberWithdrawalLog} "{"code": 200, "data": [...]}"
// @Router /api/v1/member-withdrawal-log/{id} [get]
// @Security Bearer
func (e MemberWithdrawalLog) Get(c *gin.Context) {
	req := dto.MemberWithdrawalLogGetReq{}
	s := service.MemberWithdrawalLog{}
	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
	}
	var object models.MemberWithdrawalLog

	p := actions.GetPermissionFromContext(c)
	err = s.Get(&req, p, &object)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取用户提现记录失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(object, "查询成功")
}

// Insert 创建用户提现记录
// @Summary 创建用户提现记录
// @Description 创建用户提现记录
// @Tags 用户提现记录
// @Accept application/json
// @Product application/json
// @Param data body dto.MemberWithdrawalLogInsertReq true "data"
// @Success 200 {object} response.Response	"{"code": 200, "message": "添加成功"}"
// @Router /api/v1/member-withdrawal-log [post]
// @Security Bearer
func (e MemberWithdrawalLog) Insert(c *gin.Context) {
	req := dto.MemberWithdrawalLogInsertReq{}
	s := service.MemberWithdrawalLog{}
	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
	}
	// 设置创建人
	req.SetCreateBy(user.GetUserId(c))

	err = s.Insert(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("创建用户提现记录失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(req.GetId(), "创建成功")
}

// Update 修改用户提现记录
// @Summary 修改用户提现记录
// @Description 修改用户提现记录
// @Tags 用户提现记录
// @Accept application/json
// @Product application/json
// @Param id path int true "id"
// @Param data body dto.MemberWithdrawalLogUpdateReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/member-withdrawal-log/{id} [put]
// @Security Bearer
func (e MemberWithdrawalLog) Update(c *gin.Context) {
	req := dto.MemberWithdrawalLogUpdateReq{}
	s := service.MemberWithdrawalLog{}
	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
	}
	req.SetUpdateBy(user.GetUserId(c))
	p := actions.GetPermissionFromContext(c)

	err = s.Update(&req, p)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("修改用户提现记录失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(req.GetId(), "修改成功")
}

// Delete 删除用户提现记录
// @Summary 删除用户提现记录
// @Description 删除用户提现记录
// @Tags 用户提现记录
// @Param data body dto.MemberWithdrawalLogDeleteReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "删除成功"}"
// @Router /api/v1/member-withdrawal-log [delete]
// @Security Bearer
func (e MemberWithdrawalLog) Delete(c *gin.Context) {
	s := service.MemberWithdrawalLog{}
	req := dto.MemberWithdrawalLogDeleteReq{}
	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
	}

	// req.SetUpdateBy(user.GetUserId(c))
	p := actions.GetPermissionFromContext(c)

	err = s.Remove(&req, p)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("删除用户提现记录失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(req.GetId(), "删除成功")
}

// 审核提现
func (e MemberWithdrawalLog) Process(c *gin.Context) {
	s := service.MemberWithdrawalLog{}
	req := dto.MemberWithdrawalLogApprovedReq{}
	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
	}

	err = s.Process(&req)

	if err != nil {
		e.Error(500, err, fmt.Sprintf("审核提现失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(nil, "确认提现成功")
}

// 确认提现
func (e MemberWithdrawalLog) Confirm(c *gin.Context) {
	s := service.MemberWithdrawalLog{}
	req := dto.MemberWithdrawalLogConfirmReq{}
	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
	}

	err = s.Confirm(&req)

	if err != nil {
		e.Error(500, err, fmt.Sprintf("确认到账失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(nil, "确认提现成功")
}
