import { shallowRef, reactive } from 'vue'
import { v4 } from 'uuid'
import { ClientType } from '@/constants/client'
import { Market } from '@/constants/market'
import { useDataTable } from '@/hooks/datatable'
import { queryDiamondList } from '@/services/api/goods'
import { zsBuyOrderDesting, zsBuyOrderDestingNegPrice, zsSellOrderDestingApply } from '@/services/api/trade'
import { useLoginStore } from '@/stores'
import moment from 'moment'
import Long from 'long'

/**
 * 钻石买摘牌(立即购买)
 * @returns 
 */
export function useBuyOrderDesting(selectedRow: Ermcp.DiamondDetailsRsp) {
    const { getUserId, getFirstAccountId } = useLoginStore()
    const loading = shallowRef(false)

    const formSubmit = () => {
        loading.value = true
        return zsBuyOrderDesting({
            data: {
                Header: {
                    AccountID: getFirstAccountId(),
                },
                UserID: getUserId(),
                AccountID: getFirstAccountId(),
                RelatedWRTradeOrderID: Long.fromString(selectedRow.wrtradeorderid),
                OrderQty: selectedRow.remainqty,
                OrderSrc: 1,
                ClientSerialNo: v4(),
                ClientOrderTime: moment().format('YYYY-MM-DD HH:mm:ss'),
                ClientType: ClientType.Web,
                OperatorID: getUserId(),
                MarketID: Market.GZ
            },
            complete: () => {
                loading.value = false
            }
        })
    }

    return {
        loading,
        formSubmit
    }
}

/**
 * 钻石买摘牌(买询价)
 * @returns 
 */
export function useBuyOrderBargain(selectedRow: Ermcp.DiamondDetailsRsp) {
    const { getUserId, getFirstAccountId } = useLoginStore()
    const loading = shallowRef(false)

    const formData = reactive<Proto.ZSBuyOrderDestingNegPriceReq>({
        Header: {
            AccountID: getFirstAccountId(),
        },
        UserID: getUserId(), // 用户ID，必填
        AccountID: getFirstAccountId(), // 资金账号，必填
        RelatedOrderID: Long.fromString(selectedRow.wrtradeorderid), // 卖委托单号，必填
        OrderQty: selectedRow.remainqty, // 议价数量，2位小数，必填
        OrderSrc: 1, // 委托来源
        ClientType: ClientType.Web, // 终端类型
        OperatorID: getUserId(), // 操作员账号ID
        MarketID: Market.GZ, // 市场ID,必填
        ApplyRemark: '' // 申请备注
    })

    const formSubmit = () => {
        loading.value = true
        return zsBuyOrderDestingNegPrice({
            data: {
                ...formData,
                ClientSerialNo: v4(),
                ClientOrderTime: moment().format('YYYY-MM-DD HH:mm:ss')
            },
            complete: () => {
                loading.value = false
            }
        })
    }

    return {
        loading,
        formData,
        formSubmit
    }
}

/**
 * 钻石卖摘牌(卖询价)
 * @returns 
 */
export function useSellOrderBargain(selectedRow: Ermcp.BuyOrderRsp) {
    const { getUserId, getFirstAccountId } = useLoginStore()
    const { dataList } = useDataTable<Ermcp.MyWRPositionRsp>()
    const loading = shallowRef(false)

    const formData = reactive<Proto.ZSSellOrderDestingApplyReq>({
        Header: {
            AccountID: getFirstAccountId(),
        },
        UserID: getUserId(), // 用户ID，必填
        AccountID: getFirstAccountId(), // 资金账户ID，必填
        BuyWRTradeOrderID: Long.fromString(selectedRow.wrtradeorderid), // 买挂牌委托单ID,必填
        WRStandardID: 0, // 现货商品ID，必填
        ApplyQty: 0, // 申请数量,必填，2位小数
        LadingBillID: Long.fromNumber(0), // 提单ID,必填
        SubNum: 0, // 提单子单号,必填
        ApplyRemark: '', // 备注
        ClientType: ClientType.Web, // 终端类型
        ClientSerialNo: '', // 客户端流水号
        MarketID: Market.GZ // 市场ID,必填
    })

    const columns = shallowRef<Ermcp.TableColumn[]>([
        { prop: 'warehousenamedisplay', label: '仓库', show: true },
        { prop: 'zsshapetypedisplay', label: '形状', width: 200, show: true },
        { prop: 'zscolortype1display', label: '颜色', show: true },
        { prop: 'zsclaritytype1display', label: '净度', show: true },
        { prop: 'zscuttype1display', label: '切工', show: true },
        { prop: 'zspolishtype1display', label: '抛光', show: true },
        { prop: 'zssymmetrytype1display', label: '对称', show: true },
        { prop: 'zsfluorescencetype1display', label: '荧光', show: true },
        { prop: 'sizedisplay', label: '尺寸', show: true },
        { prop: 'price', label: '价格', show: true },
        { prop: 'weight', label: '克拉重量', show: true },
        { prop: 'priceper', label: '克拉单价', show: true },
    ])

    const formSubmit = () => {
        loading.value = true
        return zsSellOrderDestingApply({
            data: {
                ...formData,
                ClientSerialNo: v4()
            },
            complete: () => {
                loading.value = false
            }
        })
    }

    // 获取商品列表
    const getDiamondList = () => {
        return queryDiamondList({
            data: {
                wruserid: getUserId(),
                zscategorys: selectedRow.zscategory.toString(),
            },
            success: (res) => {
                dataList.value = res.data.filter((e) => e.ftotalqty - e.ffreezeqty > 0)
            },
        })
    }

    return {
        loading,
        dataList,
        columns,
        formData,
        formSubmit,
        getDiamondList,
    }
}