package erp

import (
	"context"
	"git.kumo.work/shama/service/client"
	workflow2 "git.kumo.work/shama/service/erp/workflow"
	"git.kumo.work/shama/service/lib/bean"
	"time"
)

type workflow struct {
	workflow2.Workflow
}
type ArgsWorkflowList struct {
	Page   bean.Page
	Search WorkflowSearch
}

type WorkflowSearch struct {
	BusinessId   int64 // 业务id
	BusinessType string
	Status       AuditStatus
}
type ReplyWorkflowList struct {
	List  []WorkflowItem
	Total int64
}
type WorkflowItem struct {
	Id             int64       `json:"id"`             // 审批流id
	BusinessId     int64       `json:"businessId"`     // 业务id
	BusinessType   string      `json:"businessType"`   // 业务类型
	BusinessName   string      `json:"businessName"`   // 业务名称
	Title          string      `json:"title"`          // 审批流标题
	Content        string      `json:"content"`        // 审批流内容
	Status         AuditStatus `json:"status"`         // 审批流状态
	CreatedStaffId int64       `json:"createdStaffId"` // 创建人
	CreatedAt      *time.Time  `json:"createdAt"`      // 创建时间
}

// List @TITLE 审核列表
func (w *workflow) List(ctx context.Context, args ArgsWorkflowList) (reply ReplyWorkflowList, err error) {
	xClient, err := client.GetClient(w)
	if err != nil {
		return
	}
	err = xClient.Call(ctx, "List", args, &reply)
	return
}

type ReplyWorkflowInfo struct {
	Id             int64              `json:"id"`             // 审批流id
	BusinessId     int64              `json:"businessId"`     // 业务id
	BusinessType   string             `json:"businessType"`   // 业务类型
	BusinessName   string             `json:"businessName"`   // 业务名称
	Title          string             `json:"title"`          // 审批流标题
	Content        string             `json:"content"`        // 审批流内容
	Status         AuditStatus        `json:"status"`         // 审批流状态
	CreatedStaffId int64              `json:"createdStaffId"` // 创建人
	CreatedAt      *time.Time         `json:"createdAt"`      // 创建时间
	Nodes          []WorkflowNodeItem `json:"nodes"`
}
type WorkflowNodeItem struct {
	Id             int64       `json:"id"`             // 审批流节点id
	Status         AuditStatus `json:"status"`         // 审批流节点状态
	ReviewStaffId  int64       `json:"reviewStaffId"`  // 审批流节点审核人
	ReviewAt       *time.Time  `json:"reviewAt"`       // 审批流节点审核时间
	ReviewComments string      `json:"reviewComments"` // 审批流节点审核意见
	Index          int64       `json:"index"`          // 审批流节点顺序

}
type ArgsWorkflowInfo struct {
	WorkflowId     int64 // 审批流id
	WorkflowNodeId int64 // 审批流节点
}

// Info @TITLE 审核详情
func (w *workflow) Info(ctx context.Context, args ArgsWorkflowInfo) (reply ReplyWorkflowInfo, err error) {
	xClient, err := client.GetClient(w)
	if err != nil {
		return
	}
	err = xClient.Call(ctx, "Info", args, &reply)
	return
}

type ArgsWorkflowAdopt struct {
	StaffId    int64 // 审核人
	WorkflowId int64 // 工作流
}

// Adopt @TITLE 审核通过
func (w *workflow) Adopt(ctx context.Context, args ArgsWorkflowAdopt) (err error) {
	xClient, err := client.GetClient(w)
	if err != nil {
		return
	}
	reply := 0
	err = xClient.Call(ctx, "Adopt", args, &reply)
	return
}

type ArgsWorkflowReject struct {
	StaffId    int64  // 审核人
	WorkflowId int64  // 工作流
	Reason     string // 审核结果
}

// Reject @TITLE 审核驳回
func (w *workflow) Reject(ctx context.Context, args ArgsWorkflowReject) (err error) {
	xClient, err := client.GetClient(w)
	if err != nil {
		return
	}
	reply := 0
	err = xClient.Call(ctx, "Reject", args, &reply)
	return
}