package supplier

import (
	"context"
	"git.oa00.com/supply-chain/service/client"
	"git.oa00.com/supply-chain/service/lib/bean"
	"git.oa00.com/supply-chain/service/supplier/batch"
	"github.com/shopspring/decimal"
)

type Supplier struct {
	Goods goods
	Apply supplierApply
	supplier
	WarnLiaison      warnLiaison
	WalletApply      supplierWalletApply
	LogisticsCompany logisticsCompany
	ReturnAddress    returnAddress
	Afs              afs
	BatchGoods       batch.Goods
}

type supplier struct {
}

type ArgsSupplierAdd struct {
	SupplierName string // 供应商名称
	Account      string // 账户
	Password     string // 密码
	PayType      uint   // 结算类型
	Liaison      string // 联系人
	Phone        string // 手机号
	Annex        string // 附件
	ApplyUserId  uint   // 申请人Id
}

// Add @Title 添加供应商
func (s *supplier) Add(ctx context.Context, args ArgsSupplierAdd) error {
	xClient, err := client.GetClient(s)
	if err != nil {
		return err
	}
	reply := 0
	return xClient.Call(ctx, "Add", args, &reply)
}

type SupplierSearch struct {
	SupplierName string
	Status       uint
}

type ArgsSupplierLists struct {
	Search SupplierSearch
	Page   bean.Page
}

type ReplySupplierLists struct {
	Lists []SupplierItem
	Total int64
}

type SupplierItem struct {
	Id           uint
	SupplierName string          // 供应商名称
	Status       uint            // 状态
	Prepayment   decimal.Decimal // 预付款余额
	WarnStatus   uint            // 预警状态
	ApplyUserId  uint            // 创建人Id
	CreatedAt    int64           // 创建时间
}

// Lists @Title 供应商列表
func (s *supplier) Lists(ctx context.Context, args ArgsSupplierLists) (reply ReplySupplierLists, err error) {
	xClient, err := client.GetClient(s)
	if err != nil {
		return ReplySupplierLists{}, err
	}
	err = xClient.Call(ctx, "Lists", args, &reply)
	return
}

// Enabled @Title 启用供应商
func (s *supplier) Enabled(ctx context.Context, supplierId uint) error {
	xClient, err := client.GetClient(s)
	if err != nil {
		return err
	}
	reply := 0
	return xClient.Call(ctx, "Enabled", supplierId, &reply)
}

// Disabled @Title 启用供应商
func (s *supplier) Disabled(ctx context.Context, supplierId uint) error {
	xClient, err := client.GetClient(s)
	if err != nil {
		return err
	}
	reply := 0
	return xClient.Call(ctx, "Disabled", supplierId, &reply)
}

type ReplySupplierInfo struct {
	SupplierId   uint
	SupplierName string          // 供应商名称
	Account      string          // 账户名
	PayType      uint            // 结算方式
	Liaison      string          // 联系人
	Phone        string          // 手机号
	Annex        string          // 附件
	WarnStatus   uint            // 预警状态
	WarnValue    decimal.Decimal // 预警值
}

// Info @Title 供应商详情
func (s *supplier) Info(ctx context.Context, supplierId uint) (reply ReplySupplierInfo, err error) {
	xClient, err := client.GetClient(s)
	if err != nil {
		return ReplySupplierInfo{}, err
	}
	err = xClient.Call(ctx, "Info", supplierId, &reply)
	return
}

type ArgsSupplierEdit struct {
	SupplierId   uint
	SupplierName string // 供应商名称
	Password     string // 密码
	PayType      uint   // 结算方式
	Liaison      string // 联系然
	Phone        string // 手机号
	Annex        string // 附件
}

// Edit @Title 供应商详情
func (s *supplier) Edit(ctx context.Context, args ArgsSupplierEdit) error {
	xClient, err := client.GetClient(s)
	if err != nil {
		return err
	}
	reply := 0
	return xClient.Call(ctx, "Edit", args, &reply)
}

type ArgsSupplierUpdateWarn struct {
	SupplierId uint
	WarnStatus uint            // 预警状态 1=开启 2=关闭
	Value      decimal.Decimal // 预警值
}

// UpdateWarn @Title 开启预警
func (s *supplier) UpdateWarn(ctx context.Context, args ArgsSupplierUpdateWarn) error {
	xClient, err := client.GetClient(s)
	if err != nil {
		return err
	}
	reply := 0
	return xClient.Call(ctx, "UpdateWarn", args, &reply)
}

type ReplySupplierItem struct {
	Id   uint   `json:"id"`
	Name string `json:"name"`
}

// Select @Title 筛选非禁用的供应商
func (s *supplier) Select(ctx context.Context, status uint) (reply []ReplySupplierItem, err error) {
	xClient, err := client.GetClient(s)
	if err != nil {
		return nil, err
	}
	err = xClient.Call(ctx, "Select", status, &reply)
	return
}