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

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

export function useAddress() {
    const { dataList, total, pageIndex, pageSize } = useDataTable<Ermcp.UserReceiveInfoRsp>()
    const loading = shallowRef(false)

    const getUserAddressList = async () => {
        try {
            loading.value = true
            const res = await queryUserReceiveInfo({
                userid: userId.value,
            })
            total.value = res.total
            dataList.value = res.data
        } finally {
            loading.value = false
        }
    }

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

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

    const formData = reactive<Proto.UserReceiveInfoReq>({
        ReceiveInfoId: 0,
        UserID: userId.value,
        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 = async () => {
        try {
            loading.value = true
            return await userReceiveInfo(formData)
        } finally {
            loading.value = false
        }
    }

    const deleteAddress = async () => {
        try {
            loading.value = true
            return await delUserReceiveInfo({
                ReceiveInfoId: formData.ReceiveInfoId
            })
        } finally {
            loading.value = false
        }
    }

    const updateAddressIsDefault = async () => {
        try {
            loading.value = true
            return await userReceiveIsDefault({
                UserId: userId.value,
                AutoId: formData.ReceiveInfoId
            })
        } finally {
            loading.value = false
        }
    }

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

export function useInvoice() {
    const { dataList, total, pageIndex, pageSize } = useDataTable<Ermcp.WrUserReceiptInfoRsp>()
    const loading = shallowRef(false)

    const getUserInvoiceList = async () => {
        try {
            loading.value = true
            const res = await queryWrUserReceiptInfo({
                userid: userId.value,
            })
            total.value = res.total
            dataList.value = res.data
        } finally {
            loading.value = false
        }
    }

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

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

    const formData = reactive<Proto.UserReceiptInfoReq>({
        UserID: userId.value, // 用户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 = async () => {
        try {
            loading.value = true
            return await userReceiptInfo(formData)
        } finally {
            loading.value = false
        }
    }

    const deleteInvoice = async () => {
        try {
            loading.value = true
            return await delUserReceiptInfo({
                ReceiptInfoId: formData.ReceiptInfoId
            })
        } finally {
            loading.value = false
        }
    }

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