/**
 * 登记相关
 *
 * 采购 - 待点价 - 点价登记
 * 采购 - 履约交收 - (交收登记, 款项登记, 发票登记, 入库登记)
 *
 */

import { commonResultInfo, getRequestResultInfo } from "@/common/methods/request";
import { operationContractReq } from "@/services/proto/contract";
import {
    ErmcpContractOperateApplyReq,
    FundsReq,
    InvoiceReq,
    SettlementReq,
    SomePriceReq
} from "@/services/proto/contract/interface";
import { objectToUint8Array } from "@/utils/objHandle";
import { purchaseStateSign } from "@/views/business/purchase/setup";
import Long from "long";
import { Ref } from "vue";
import {getUserId} from "@/services/bus/account";
import {getLongTypeLoginID} from "@/services/bus/login";
import * as long from "long";
import {LongType} from "@/services/socket/login/interface";
import {getTimeValue} from "@/utils/time";
import {v4 as uuidv4} from "uuid";

/**
 * 这里负责 点价登记, 交收登记, 款项登记, 发票登记, 入库登记
 */

/**
 * 点价登记
 * @param spotcontractid 合同id
 * @param req 点价价格以及数量
 */
export function somePriceReq(spotcontractid: string, req: SomePriceReq, loading: Ref<boolean>): Promise<string> {
    return operationContractRsp(1,
        operationContractReqBuilder(req, spotcontractid, 1),
        loading)
}

/**
 * 交收登记
 */
export function settlementReq(spotcontractid: string, req: SettlementReq, loading: Ref<boolean>): Promise<string> {
    return operationContractRsp(2,
        operationContractReqBuilder(req, spotcontractid, 2),
        loading)
}

/**
 * 款项登记
 */
export function fundsReq(spotcontractid: string, req: FundsReq, loading: Ref<boolean>, Remark: String): Promise<string> {
    return operationContractRsp(3,
        operationContractReqBuilder(req, spotcontractid, 3),
        loading)
}

/**
 * 发票登记
 */
export function invoiceReq(spotcontractid: string, req: InvoiceReq, loading: Ref<boolean>, attachUrl?: String): Promise<string> {
    return operationContractRsp(4,
        operationContractReqBuilder(req, spotcontractid, 4, "" ,attachUrl),
        loading)
}


/**
 * 请求报文组装
 * @param req 特定的请求包结构
 * @param id 操作的合同id
 * @param type 操作类型
 */
export function operationContractReqBuilder(req: Object, id: string, type: number, Remark?: String, attachUrl?: String): ErmcpContractOperateApplyReq {
    return {
        OperateType: 1, // uint32 操作类型-1:登记2:确认3：拒绝4：撤销
        Remark: '',
        Info: {
            OperateApplyType: type, // uint32 操作申请类型-1:点价2:结算3:款项4:发票
            RelatedID: Long.fromString(id), // uint64 现货合同ID(602+Unix秒时间戳(10位)+xxxxxx)
            DetailJson: objectToUint8Array(req), // bytes 明细JSON {}
            AttachUrl: attachUrl === undefined ? "" : objectToUint8Array(attachUrl as string) // bytes 附件
        },
    } as ErmcpContractOperateApplyReq
}


/**
 * 登记返回处理
 * @param type 登记类型
 * @param req 请求数据
 * @param loading
 */
export function operationContractRsp(type: number, req: ErmcpContractOperateApplyReq, loading: Ref<boolean>): Promise<string> {
    const sign = getRequestResultInfo(purchaseStateSign, type)  // 接口请求后的返回提示 这里统一进行管理
    const result = operationContractReq(req)
    return commonResultInfo(result, sign, loading)
}