import { shallowRef, reactive } from 'vue'
import { useDataTable } from '@/hooks/datatable'
import { getTableColumns } from '@/business/table'
import { queryUserReceiveInfo, queryWrUserReceiptInfo, userReceiveInfo, delUserReceiveInfo, userReceiveIsDefault, userReceiptInfo, delUserReceiptInfo } from '@/services/api/user'
import { useLoginStore } from '@/stores'
import { getCertificateTypeList } from '@/constants/certificate'

export function useAddress() {
    const { getUserId } = useLoginStore()
    const { dataList, total, pageIndex, pageSize } = useDataTable<Ermcp.UserReceiveInfoRsp>()
    const loading = shallowRef(false)
    const columns = shallowRef(getTableColumns('mine_address'))

    const getUserAddressList = () => {
        loading.value = true
        return queryUserReceiveInfo({
            data: {
                userid: getUserId(),
            },
            success: (res) => {
                total.value = res.total
                dataList.value = res.data
            },
            complete: () => {
                loading.value = false
            }
        })
    }

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

export function useAddressForm(selectedRow?: Ermcp.UserReceiveInfoRsp) {
    const { getUserId } = useLoginStore()
    const loading = shallowRef(false)
    const certificateTypeList = getCertificateTypeList()

    const formData = reactive<Proto.UserReceiveInfoReq>({
        ReceiveInfoId: 0,
        UserID: getUserId(),
        CardTypeID: 0,
        ReceiverName: '',
        CardNum: '',
        PhoneNum: '',
        Address: '',
        TakeRemark: '',
    })

    if (selectedRow?.autoid) {
        ({
            autoid: formData.ReceiveInfoId,
            userid: formData.UserID,
            receivername: formData.ReceiverName,
            cardtypeid: formData.CardTypeID,
            cardnum: formData.CardNum,
            phonenum: formData.PhoneNum,
            countryid: formData.CountryID,
            provinceid: formData.ProvinceID,
            cityid: formData.CityID,
            districtid: formData.DistrictID,
            address: formData.Address,
            takeremark: formData.TakeRemark,
        } = selectedRow)
    }

    const addOrUpdate = () => {
        loading.value = true
        return userReceiveInfo({
            data: formData,
            complete: () => {
                loading.value = false
            }
        })
    }

    const deleteAddress = () => {
        loading.value = true
        return delUserReceiveInfo({
            data: {
                ReceiveInfoId: formData.ReceiveInfoId
            },
            complete: () => {
                loading.value = false
            }
        })
    }

    const updateAddressIsDefault = () => {
        loading.value = true
        return userReceiveIsDefault({
            data: {
                UserId: getUserId(),
                AutoId: formData.ReceiveInfoId
            },
            complete: () => {
                loading.value = false
            }
        })
    }

    return {
        loading,
        certificateTypeList,
        formData,
        addOrUpdate,
        deleteAddress,
        updateAddressIsDefault,
    }
}

export function useInvoice() {
    const { getUserId } = useLoginStore()
    const { dataList, total, pageIndex, pageSize } = useDataTable<Ermcp.WrUserReceiptInfoRsp>()
    const loading = shallowRef(false)
    const columns = shallowRef(getTableColumns('mine_invoice'))

    const getUserInvoiceList = () => {
        loading.value = true
        return queryWrUserReceiptInfo({
            data: {
                userid: getUserId(),
            },
            success: (res) => {
                total.value = res.total
                dataList.value = res.data
            },
            complete: () => {
                loading.value = false
            }
        })
    }

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

export function useInvoiceForm(selectedRow?: Ermcp.WrUserReceiptInfoRsp) {
    const { getUserId } = useLoginStore()
    const loading = shallowRef(false)
    const certificateTypeList = getCertificateTypeList()

    const formData = reactive<Proto.UserReceiptInfoReq>({
        UserID: getUserId(), // 用户ID
        UserName: '', // 发票抬头姓名
        ReceiptType: 1, // 发票类型
        TaxpayerID: '', // 纳税人识别号
        ContactInfo: '', // 联系方式
        ReceiptInfoId: 0, // 修改时填用户发票信息id
        ReceiptBank: '', // 发票开户行[发票类型:企业]
        ReceiptAccount: '', // 发票帐号[发票类型:企业]
        Address: '', // 地址[发票类型:企业]
    })

    if (selectedRow?.autoid) {
        ({
            autoid: formData.ReceiptInfoId,
            userid: formData.UserID,
            username: formData.UserName,
            receipttype: formData.ReceiptType,
            taxpayerid: formData.TaxpayerID,
            contactinfo: formData.ContactInfo,
            receiptbank: formData.ReceiptBank,
            receiptaccount: formData.ReceiptAccount,
            address: formData.Address,
            idnum: formData.IDNum,
        } = selectedRow)
    }

    const addOrUpdate = () => {
        loading.value = true
        return userReceiptInfo({
            data: formData,
            complete: () => {
                loading.value = false
            }
        })
    }

    const deleteInvoice = () => {
        loading.value = true
        return delUserReceiptInfo({
            data: {
                ReceiptInfoId: formData.ReceiptInfoId
            },
            complete: () => {
                loading.value = false
            }
        })
    }

    return {
        loading,
        certificateTypeList,
        formData,
        addOrUpdate,
        deleteInvoice,
    }
}