import { shallowRef, reactive } from 'vue'
import { v4 } from 'uuid'
import { ClientType } from '@/constants/client'
import { useDataTable } from '@/hooks/datatable'
import { getTableColumns } from '@/business/table'
import { queryGZBSFWOrder, queryGZCJBSOrderFile, queryGZBSFWOrderDetail, queryFworderoperate, bsfwMemberOperate } from '@/services/api/customs'
import { useLoginStore } from '@/stores'
import Long from 'long'

const loginStore = useLoginStore()
const { userId, loginInfo } = loginStore.$toRefs()

// 保税服务
export function useBSFWOrderList() {
    const { dataList, total, pageIndex, pageSize } = useDataTable<Ermcp.GZBSFWOrderRsp>()
    const loading = shallowRef(false)

    const getBSFWOrderList = async (params: Partial<Ermcp.GZBSFWOrderReq> = {}) => {
        try {
            loading.value = true
            const res = await queryGZBSFWOrder({
                page: pageIndex.value,
                pagesize: pageSize.value,
                userid: userId.value,
                executestatus: params.executestatus,
            })
            total.value = res.total
            dataList.value = res.data
        } finally {
            loading.value = false
        }
    }

    return {
        loading,
        dataList,
        total,
        pageIndex,
        pageSize,
        getBSFWOrderList,
    }
}

// 保税服务单据明细
export function useBSFWOrderDetail(selectedRow: Ermcp.GZBSFWOrderRsp) {
    const loading = shallowRef(false)
    const detailList = shallowRef<Ermcp.GZBSFWOrderDetailRsp[]>([])

    const columns: Ermcp.TableColumn[] = [
        { prop: 'ordercontent', label: '内容' },
        { prop: 'netweigthct', label: '净重(克拉)' },
        { prop: 'netweigthgm', label: '净重(克)' },
        { prop: 'grossweightgm', label: '毛重(克)' },
        { prop: 'perprice', label: '单价(美元/克拉)', width: 180 },
        { prop: 'totalamount', label: '总值(美元)' },
        { prop: 'colorinfo', label: '彩钻颜色' },
        { prop: 'remark', label: '其它' },
    ]

    const getBSFWOrderDetailList = async () => {
        try {
            loading.value = true
            const res = await queryGZBSFWOrderDetail({
                userid: userId.value,
                orderid: selectedRow.orderid,
            })
            detailList.value = res.data
        } finally {
            loading.value = false
        }
    }

    return {
        loading,
        detailList,
        columns,
        getBSFWOrderDetailList,
    }
}

// 出境文件下载
export function useGZCJBSOrderFileList(orderid: string) {
    const { dataList, total, pageIndex, pageSize } = useDataTable<Ermcp.GZCJBSOrderFileRsp>()
    const loading = shallowRef(false)
    const columns = shallowRef(getTableColumns('customs_bsfw_file'))

    const getGZCJBSOrderFileList = async () => {
        try {
            loading.value = true
            const res = await queryGZCJBSOrderFile({
                orderid,
            })
            total.value = res.total
            dataList.value = res.data
        } finally {
            loading.value = false
        }
    }

    return {
        loading,
        dataList,
        total,
        pageIndex,
        pageSize,
        columns,
        getGZCJBSOrderFileList,
    }
}

// 保税服务单据操作
export function useBSFWOrderOperate({ orderid, gzbsstatus }: Ermcp.GZBSFWOrderRsp) {
    const loading = shallowRef(false)
    const details = shallowRef<Ermcp.FworderoperateRsp>()

    const getFworderoperate = () => {
        queryFworderoperate({
            orderid,
            status: gzbsstatus
        }).then((res) => {
            if (res.data.length) {
                details.value = res.data[0]
            }
        })
    }

    return {
        loading,
        details,
        getFworderoperate,
    }
}

// 保税服务操作
export function useBSFWMemberOperate(orderId: string) {
    const { UserID, LoginID, LoginCode } = loginInfo.value
    const loading = shallowRef(false)

    const formData = reactive<Proto.BSFWMemberOperateReq>({
        UserID, // 用户ID,必填
        OperateID: LoginID, // 操作人ID,必填，LoginAccount的LoginID
        OperateAccount: LoginCode || LoginID.toString(), // 操作人账户，LoginAccount的LoginCode,LoginCode为空则填LoginID,必
        OrderID: Long.fromString(orderId), // 单据ID,必填
        OperateType: 0, // 操作类型,必填,1:上传发货单2：上传盖章单据3：确认预付款
        FileDetails: [], // 文件列表
        Remark: '', // 备注
        OtherLogistics: '', // 指定其它物流，OperateType=1:上传发货单时填写
        ClientType: ClientType.Web, // 终端类型
        ClientSerialNo: '', // 客户端流水号
    })

    const formSubmit = async () => {
        try {
            loading.value = true
            return await bsfwMemberOperate({
                ...formData,
                ClientSerialNo: v4(),
            })
        } finally {
            loading.value = false
        }
    }

    return {
        loading,
        formData,
        formSubmit,
    }
}