package basic

import (
	"errors"
	"github.com/gin-gonic/gin"
	"go-admin/app/admin/models"
	"go-admin/logger"
	"go-admin/tools"
	"go-admin/tools/app"
	"net/http"
)

type CashierCreateRequest struct {
	Name        string   `json:"name" binding:"required"`      // 账号名称
	BankName    string   `json:"bank_name"`                    // 银行全称
	BankAccount string   `json:"bank_account"`                 // 银行账号
	StoreNums   []uint32 `json:"store_num" binding:"required"` // 门店编号
}

// CashierCreate 新增收付款账号
// @Summary 新增收付款账号
// @Tags 收付款账号设置
// @Produce json
// @Accept json
// @Param request body CashierCreateRequest true "新增收付款账号模型"
// @Success 200 {object} app.Response
// @Router /api/v1/cashier/create [post]
func CashierCreate(c *gin.Context) {
	var req = new(CashierCreateRequest)

	if err := c.ShouldBindJSON(&req); err != nil {
		logger.Errorf("CashierCreate ShouldBindJSON err:", err.Error())
		app.Error(c, http.StatusBadRequest, errors.New("param err"), "参数错误")
		return
	}

	err := tools.Validate(req)
	if err != nil {
		app.Error(c, http.StatusBadRequest, err, err.Error())
		return
	}

	if len(req.StoreNums) <= 0 {
		app.Error(c, http.StatusBadRequest, errors.New("param err"), "有效店铺为空")
		return
	}

	cashier := &models.ErpCashier{
		Name:        req.Name,
		BankName:    req.BankName,
		BankAccount: req.BankAccount,
		State:       1,
		Type:        1, //默认为1，新增的收付款方式
		Remark:      "",
	}

	err = models.CreateAccount(cashier, req.StoreNums)
	if err != nil {
		app.Error(c, http.StatusBadRequest, err, err.Error())
		return
	}

	app.OK(c, nil, "OK")
	return
}

type CashierEditRequest struct {
	CashierId uint32 `json:"cashier_id" binding:"required"` // 账号id
	CashierCreateRequest
}

// CashierEdit 编辑收付款账号
// @Summary 编辑收付款账号
// @Tags 收付款账号设置
// @Produce json
// @Accept json
// @Param request body CashierEditRequest true "编辑收付款账号模型"
// @Success 200 {object} app.Response
// @Router /api/v1/cashier/edit [post]
func CashierEdit(c *gin.Context) {
	var req = new(CashierEditRequest)

	if err := c.ShouldBindJSON(&req); err != nil {
		logger.Errorf("CashierEdit ShouldBindJSON err:", err.Error())
		app.Error(c, http.StatusBadRequest, errors.New("param err"), "参数错误")
		return
	}

	err := tools.Validate(req)
	if err != nil {
		app.Error(c, http.StatusBadRequest, err, err.Error())
		return
	}

	if len(req.StoreNums) <= 0 {
		app.Error(c, http.StatusBadRequest, errors.New("param err"), "有效店铺为空")
		return
	}

	cashier := &models.ErpCashier{
		Name:        req.Name,
		BankName:    req.BankName,
		BankAccount: req.BankAccount,
		State:       1,
		Remark:      "",
	}
	cashier.ID = req.CashierId
	err = models.UpdateAccount(cashier, req.StoreNums)
	if err != nil {
		app.Error(c, http.StatusBadRequest, err, err.Error())
		return
	}

	app.OK(c, nil, "OK")
	return
}

// CashierDelete 删除收付款账号
// @Summary 删除收付款账号
// @Tags 收付款账号设置
// @Produce json
// @Accept json
// @Param request body CashierDetailRequest true "删除收付款账号模型"
// @Success 200 {object} app.Response
// @Router /api/v1/cashier/delete [post]
func CashierDelete(c *gin.Context) {
	var req = new(CashierDetailRequest)

	if err := c.ShouldBindJSON(&req); err != nil {
		//logger.Error(err)
		app.Error(c, http.StatusBadRequest, errors.New("param err"), "参数错误")
		return
	}

	err := tools.Validate(req)
	if err != nil {
		app.Error(c, http.StatusBadRequest, err, err.Error())
		return
	}

	err = models.DeleteAccount(req.CashierId)
	if err != nil {
		app.Error(c, http.StatusBadRequest, err, err.Error())
		return
	}

	app.OK(c, nil, "OK")
	return
}

type CashierListRequest struct {
	StoreId   uint32 `json:"store_id"`  // 门店编号
	PageIndex int    `json:"pageIndex"` // 页码
	PageSize  int    `json:"pageSize"`  // 页条数
}

// CashierList 查询收付款账号列表
// @Summary 查询收付款账号列表
// @Tags 收付款账号设置
// @Produce json
// @Accept json
// @Param request body CashierListRequest true "查询收付款账号列表模型"
// @Success 200 {object} models.ErpCashierListResp
// @Router /api/v1/cashier/list [post]
func CashierList(c *gin.Context) {
	var req = new(CashierListRequest)

	if err := c.ShouldBindJSON(&req); err != nil {
		//logger.Error(err)
		app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
		return
	}

	if req.StoreId != 0 { // 门店id非空时进行校验
		if !models.IsExistingStoreById(int(req.StoreId)) {
			app.Error(c, http.StatusBadRequest, errors.New("该门店id不存在，请传正确的门店id"), "该门店id不存在，请传正确的门店id")
			return
		}
	}

	list, err := models.GetAccountList(int(req.StoreId), req.PageSize, req.PageIndex)
	if err != nil {
		app.Error(c, http.StatusBadRequest, err, err.Error())
		return
	}

	app.OK(c, list, "OK")
	return
}

type CashierDetailRequest struct {
	CashierId uint32 `json:"cashier_id" binding:"required"` // 账号id
}

// CashierDetail 查询收付款账号详情
// @Summary 查询收付款账号详情
// @Tags 收付款账号设置
// @Produce json
// @Accept json
// @Param request body CashierDetailRequest true "查询收付款账号详情模型"
// @Success 200 {object} models.ErpCashierDetail
// @Router /api/v1/cashier/detail [post]
func CashierDetail(c *gin.Context) {
	var req = new(CashierDetailRequest)

	if err := c.ShouldBindJSON(&req); err != nil {
		//logger.Error(err)
		app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
		return
	}

	err := tools.Validate(req)
	if err != nil {
		app.Error(c, http.StatusBadRequest, err, err.Error())
		return
	}

	list, err := models.GetAccountDetail(int(req.CashierId))
	if err != nil {
		app.Error(c, http.StatusBadRequest, err, err.Error())
		return
	}

	app.OK(c, list, "OK")
	return
}
