import { shallowRef, ref, reactive, computed } from 'vue'
import { v4 } from 'uuid'
import { useDataTable } from '@/hooks/datatable'
import { queryGzbscinOutOrder, queryBScinOutOrderDetail, queryBScOutOrderDetailatt, queryGzbscusermonthpay, queryGzbscuserpowerfee, queryBscinoutorder, bscInAndOutWareHouseApply, bscUploadFile, bscConfirmPay } from '@/services/api/bonded'
import { useLoginStore } from '@/stores'
import Long from 'long'

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

export type RequiredType<T, K extends keyof T> = Partial<T> & Pick<T, K> // 指定某个属性为必选

// 保税仓出入库申请列表
export function useGzbscinOutOrder() {
    const { dataList, total, pageIndex, pageSize } = useDataTable<Ermcp.GzbscinOutOrderRsp>()
    const loading = shallowRef(false)

    const getGzbscinOutOrder = async (params: RequiredType<Ermcp.GzbscinOutOrderReq, 'ordertype'>) => {
        try {
            loading.value = true
            const res = await queryGzbscinOutOrder({
                page: pageIndex.value,
                pagesize: pageSize.value,
                userid: userId.value,
                ...params,
            })
            total.value = res.total
            dataList.value = res.data
        } finally {
            loading.value = false
        }
    }

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

// 保税仓出库申请明细附表
export function useBScinOutOrderDetail(orderid: string) {
    const orderDetailList = ref<Ermcp.BScinOutOrderDetailRsp[]>([]) // 明细列表
    const orderDetailAttList = ref<Ermcp.BScOutOrderDetailattRsp[]>([]) // 附表列表

    const getBScinOutOrderDetail = async (params: Partial<Ermcp.BScinOutOrderDetailReq> = {}) => {
        const res = await queryBScinOutOrderDetail({
            userid: userId.value,
            orderid,
            ...params,
        })
        orderDetailList.value = res.data
    }

    const getBScOutOrderDetailatt = async (params: Partial<Ermcp.BScOutOrderDetailattReq> = {}) => {
        const res = await queryBScOutOrderDetailatt({
            userid: userId.value,
            orderid,
            ...params,
        })
        orderDetailAttList.value = res.data
    }

    return {
        orderDetailList,
        orderDetailAttList,
        getBScinOutOrderDetail,
        getBScOutOrderDetailatt,
    }
}

// 保税仓进出仓申请
export function useBscInAndOutWareHouseApply(OrderType: number) {
    const { UserID, LoginID, LoginCode } = loginInfo.value
    const loading = shallowRef(false)

    const formData = reactive<Proto.BSCInAndOutWareHouseApplyReq>({
        UserID, // 用户ID,必填
        UserName: '', // 申请方名称,必填
        UserAddress: '', // 申请方地点,必填
        ContactName: '', // 申请方联系人,必填
        ContactNum: '', // 申请方联系电话,必填
        LogisticsCompany: '', // 物流公司名称,必填
        LogisticsNo: '', // 托运单号,选填
        OrderType, // 单据类型,必填1:进仓2:出仓
        BSCGoodsListDetails: [], // 明细列表(数组),必填
        BSCOutWareHouseSchedules: [], // 出仓附表(数组),必填
        OperateID: LoginID, // 操作人ID,必填
        OperateAccount: LoginCode, // 操作人账户,必填
        ClientSerialNo: '', // 客户端流水号
    })

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

    return {
        loading,
        formData,
        formSubmit,
    }
}

// 保税仓上传文件
export function useBscUploadFile(orderId: string) {
    const { UserID, LoginID, LoginCode } = loginInfo.value
    const loading = shallowRef(false)

    const formData = reactive<Proto.BSCUploadFileReq>({
        UserID, // 用户ID,必填
        OrderID: Long.fromString(orderId), // 单据ID,必填
        FileDetails: [], // 文件列表,必填
        ClientSerialNo: '', // 客户端流水号
        OperateID: LoginID, // 操作人ID,必填
        OperateAccount: LoginCode, // 操作人账户,必填
    })

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

    return {
        loading,
        formData,
        formSubmit,
    }
}

// 保税仓计费管理列表
export function useGzbscusermonthpay() {
    const { dataList, total, pageIndex, pageSize } = useDataTable<Ermcp.GzbscusermonthpayRsp>()
    const loading = shallowRef(false)

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

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

// 保税仓计费管理详情
export function useGzbscusermonthpayDetails(trademonth: string) {
    const powerfeeList = shallowRef<Ermcp.GzbscuserpowerfeeRsp[]>([]) // 电费明细列表
    const bondedList = shallowRef<Ermcp.BscinoutorderRsp[]>([]) // 保税仓明细列表

    // 进口明细列表
    const importList = computed(() => bondedList.value.filter((e) => e.ordertype === 1))
    // 出境明细列表
    const exportList = computed(() => bondedList.value.filter((e) => e.ordertype === 2 && e.outtype === 1))
    // 转厂明细列表
    const transferList = computed(() => bondedList.value.filter((e) => e.ordertype === 2 && e.outtype === 2))

    queryGzbscuserpowerfee({
        userid: userId.value,
        trademonth,
    }).then((res) => {
        powerfeeList.value = res.data
    })

    queryBscinoutorder({
        userid: userId.value,
        jckdate: trademonth,
    }).then((res) => {
        bondedList.value = res.data
    })

    return {
        powerfeeList,
        importList,
        exportList,
        transferList,
    }
}

// 保税仓确认支付
export function useBscConfirmPay(TradeMonth: string) {
    const loading = shallowRef(false)

    const formSubmit = async () => {
        try {
            loading.value = true
            return await bscConfirmPay({
                UserID: userId.value,
                TradeMonth,
                ClientSerialNo: v4() // 客户端流水号
            })
        } finally {
            loading.value = false
        }
    }

    return {
        loading,
        formSubmit,
    }
}