package erp

import (
	"context"
	client2 "git.kumo.work/shama/service/client"
	custom2 "git.kumo.work/shama/service/erp/custom"
	"git.kumo.work/shama/service/lib/bean"
	"github.com/shopspring/decimal"
	"time"
)

type custom struct {
	custom2.Custom
}
type ArgsCustomList struct {
	Page   bean.Page
	Search CustomSearch
}
type CustomSearch struct {
	Name          string  // 公司名称
	ShortName     string  // 公司简称
	Rank          string  // 客户等级
	TradeCountry  string  // 贸易国别
	Origin        string  // 客户来源
	DepartmentIds []int64 // 所属部门
	BanFlag       int64   // 禁用标记 1=禁用 2=启用
	StaffIds      []int64 // 业务员
}
type ReplyCustomList struct {
	List  []CustomItem `json:"list"`
	Total int64        `json:"total"`
}

type CustomItem struct {
	Id               int64           `json:"id"`
	Serial           string          `json:"serial"`
	BanFlag          int64           `json:"banFlag"`
	Name             string          `json:"name"`
	ShortName        string          `json:"shortName"`
	Type             string          `json:"type"`
	Rank             string          `json:"rank"`
	Origin           string          `json:"origin"`
	TradeCountry     string          `json:"tradeCountry"`
	Country          string          `json:"country"`
	City             string          `json:"city"`
	TradeScope       string          `json:"tradeScope"`
	Intent           string          `json:"intent"`
	CompanyPhone     string          `json:"companyPhone"`
	CompanyFax       string          `json:"companyFax"`
	CompanyWebsite   string          `json:"companyWebsite"`
	CompanyZipCode   string          `json:"companyZipCode"`
	CompanyAddress   string          `json:"companyAddress"`
	CompanyCnAddress string          `json:"companyCnAddress"`
	SalesCurrency    string          `json:"salesCurrency"`
	PayWay           string          `json:"payWay"`
	PayDown          decimal.Decimal `json:"payDown"`
	PayCycle         int64           `json:"payCycle"`
	PayTerms         string          `json:"payTerms"`
	CargoCompany     string          `json:"cargoCompany"`
	Info             string          `json:"info"`
	MarkText         string          `json:"markText"`
	MarkImg          string          `json:"markImg"`
	ImgFilePaths     []string        `json:"imgFilePaths"`
	Status           int64           `json:"status"`
	CreatedStaffId   int64           `json:"createdStaffId"`
	CreatedAt        *time.Time      `json:"createdAt"`
	UpdatedAt        *time.Time      `json:"updatedAt"`
}

// List @TITLE 客户列表
func (c *custom) List(ctx context.Context, args ArgsCustomList) (reply ReplyCustomList, err error) {
	xClient, err := client2.GetClient(c)
	if err != nil {
		return
	}
	err = xClient.Call(ctx, "List", args, &reply)
	return
}

type ArgsCustomAdd struct {
	StaffId int64
	CustomAdd
}

type CustomAdd struct {
	Serial           string          // 客户编号
	BanFlag          int64           // 禁用标记 1=禁用 2=启用
	Name             string          // 公司名称
	ShortName        string          // 公司简称
	Type             string          // 客户类型
	Rank             string          // 客户等级
	Origin           string          // 客户来源
	TradeCountry     string          // 贸易国别
	Country          string          // 国家区域
	City             string          // 所在城市
	TradeScope       string          // 业务范围
	Intent           string          // 意向产品
	CompanyPhone     string          // 公司电话
	CompanyFax       string          // 公司传真
	CompanyWebsite   string          // 公司网址
	CompanyZipCode   string          // 公司邮编
	CompanyAddress   string          // 公司英文详细地址
	CompanyCnAddress string          // 公司详细中文地址
	SalesCurrency    string          // 销售币种
	PayWay           string          // 付款方式
	PayDown          decimal.Decimal // 定金比例
	PayCycle         int64           // 付款周期
	PayTerms         string          // 价格条款
	CargoCompany     string          // 货运公司
	Info             string          // 备注
	MarkText         string          // 唛头文字
	MarkImg          string          // 唛头图片
	ImgFilePaths     []string        // 图片地址集合
}

// Add @TITLE 添加客户
func (c *custom) Add(ctx context.Context, args ArgsCustomAdd) (customId int64, err error) {
	xClient, err := client2.GetClient(c)
	if err != nil {
		return
	}
	err = xClient.Call(ctx, "Add", args, &customId)
	return
}

type ReplyCustomInfo struct {
	Id               int64           `json:"id"`
	Serial           string          `json:"serial"`           // 客户编号
	BanFlag          int64           `json:"banFlag"`          // 禁用标记 1=禁用 2=启用
	Name             string          `json:"name"`             // 公司名称
	ShortName        string          `json:"shortName"`        // 公司简称
	Type             string          `json:"type"`             // 客户类型
	Rank             string          `json:"rank"`             // 客户等级
	Origin           string          `json:"origin"`           // 客户来源
	TradeCountry     string          `json:"tradeCountry"`     // 贸易国别
	Country          string          `json:"country"`          // 国家区域
	City             string          `json:"city"`             // 所在城市
	TradeScope       string          `json:"tradeScope"`       // 业务范围
	Intent           string          `json:"intent"`           // 意向产品
	CompanyPhone     string          `json:"companyPhone"`     // 公司电话
	CompanyFax       string          `json:"companyFax"`       // 公司传真
	CompanyWebsite   string          `json:"companyWebsite"`   // 公司网址
	CompanyZipCode   string          `json:"companyZipCode"`   // 公司邮编
	CompanyAddress   string          `json:"companyAddress"`   // 公司英文详细地址
	CompanyCnAddress string          `json:"companyCnAddress"` // 公司详细中文地址
	SalesCurrency    string          `json:"salesCurrency"`    // 销售币种
	PayWay           string          `json:"payWay"`           // 付款方式
	PayDown          decimal.Decimal `json:"payDown"`          // 定金比例
	PayCycle         int64           `json:"payCycle"`         // 付款周期
	PayTerms         string          `json:"payTerms"`         // 价格条款
	CargoCompany     string          `json:"cargoCompany"`     // 货运公司
	Info             string          `json:"info"`             // 备注
	MarkText         string          `json:"markText"`         // 唛头文字
	MarkImg          string          `json:"markImg"`          // 唛头图片
	ImgFilePaths     []string        `json:"imgFilePaths"`     // 图片地址集合
	Status           int64           `json:"status"`
	CreatedStaffId   int64           `json:"createdStaffId"`
	CreatedAt        *time.Time      `json:"createdAt"`
	UpdatedAt        *time.Time      `json:"updatedAt"`
}

// Info @TITLE 客户详情
func (c *custom) Info(ctx context.Context, customId int64) (reply ReplyCustomInfo, err error) {
	xClient, err := client2.GetClient(c)
	if err != nil {
		return
	}
	err = xClient.Call(ctx, "Info", customId, &reply)
	return
}

type ArgsCustomEdit struct {
	CustomId int64
	CustomAdd
}

// Edit @TITLE 编辑客户
func (c *custom) Edit(ctx context.Context, args ArgsCustomEdit) (err error) {
	xClient, err := client2.GetClient(c)
	if err != nil {
		return
	}
	reply := 0
	return xClient.Call(ctx, "Edit", args, &reply)
}