import { api_FangCangMoneyReportdownload, api_UserBalance, api_UserMoneyDetail, api_XiKeMoneyReportdownload, api_YingChiMoneyReportdownload,api_jianAiMoneyReportdownload } from '@/api/usermoneysum/apiUserMoneySum';
import { UserMoneyDetailParam, UserMoneyDetailRow } from '@/api/usermoneysum/apiUserMoneySumModel';
import { ActionOptions } from '@/components/DropDownButtons/type';
import { BasicTableProps } from '@/components/VxeTable';
import { useApiDownTemplate } from '@/hooks/useApiDownTemplate';
import { BaseFeesetAction } from '@/type/BaseFeesetAction';
import { format, isArray, isBoolean } from '@/utils/is';
import { useErrorConfirm } from '@/utils/useErrorConfirm';
import { useReturnData } from '@/utils/useReturnData';
import { Tickets, DocumentChecked } from "@element-plus/icons-vue";
import { ref, reactive, unref } from 'vue';

const { report } = useErrorConfirm()
/**
 * 查看客户总账
 */
function userMoneySumDetail(onClose: Function, onShow: Function, usermoneyGridOptions: BasicTableProps) {
    const { resData } = useReturnData()
    // 表单数据
    const queryUserMoneySumDetailForm = reactive<UserMoneyDetailParam>({
        orderIdList: [],//单号
        customerId: undefined,//客户编号
        currencyCode: undefined,//币种
        operatingType: undefined,//操作类型
        customerType: undefined,//费用类型
        productId: undefined,//产品编号
        orgId: undefined,//分公司编号
        countryCode: undefined,//国家/地区编号
        transactionNumber: undefined,//交易号
        validData: true,//有效数据
        subjectType: undefined,//主体类型
        beginDate: undefined,//起始时间
        endDate: undefined,//结束时间
        billDate: undefined,//账单时间
        reportQueryDateType: undefined,//时间类型
        pageNum: 1,
        pageSize: 10,
        total: 10,
    })
    // 列表数据
    const dealData = reactive({
        mainList: [],
        validData: [
            {
                value: true,
                label: '有效数据'
            },
            {
                value: false,
                label: '全部数据'
            }
        ],
        loading: false,
        isOrderIdList: false,//显示/应酬输入框框
        inputMultiOrders: '',//单号文本框1
        inputOrders: '',//单号文本框2
        reportType: 0,//报表类型
    })
    // 操作类别
    const actionOptions: ActionOptions[] = [
        {
            value: BaseFeesetAction.DETAIL,//状态
            label: "明细",//文本
            type: "primary",//按钮样式
            icon: Tickets,//按钮图标
            permissions: []
        },
        {
            value: BaseFeesetAction.BALANCED,
            label: "平账",
            type: "success",
            icon: DocumentChecked,
            permissions: ['sys:userMoneyDetail:balance']
        },
    ]
    const queryExpenseItemDetailRef = ref<{ show: Fn }>()
    // 细刻模板
    const { hanleOpenDownload: downloadXiKeMoney } = useApiDownTemplate(api_XiKeMoneyReportdownload)
    // 英驰模板
    const { hanleOpenDownload: downloadYingChi } = useApiDownTemplate(api_YingChiMoneyReportdownload)
    // 方仓模板
    const { hanleOpenDownload: downloadFangCang } = useApiDownTemplate(api_FangCangMoneyReportdownload)

    // 简爱
    const { hanleOpenDownload: downloadJianAi } = useApiDownTemplate(api_jianAiMoneyReportdownload)

    

    // 隐藏
    const onclose = () => {
        orderIdListhide()
        // 清空数据
        onClose()
    }
    const clearForm = () => {
        dealData.reportType = 0
        hanlClearOrderIdList()
        for (const key of Object.keys(queryUserMoneySumDetailForm)) {
            if (isBoolean(queryUserMoneySumDetailForm[key])) {
                queryUserMoneySumDetailForm[key] = true;
            } else if (isArray(queryUserMoneySumDetailForm[key])) {
                queryUserMoneySumDetailForm[key] = [];
            } else {
                // 否则设置为undefined
                queryUserMoneySumDetailForm[key] = undefined;
            }
        }
    }
    // 初始化列表数据
    const initList = async () => {
        orderIdListhide()
        dealData.loading = true
        try {
            const res = await api_UserMoneyDetail(queryUserMoneySumDetailForm)
            if (res.code == 200) {
                // dealData.mainList = res.data.records
                usermoneyGridOptions.data = res.data.records
            }
        } catch (error) {
            report(error)
        } finally {
            dealData.loading = false
        }
    }
    // 显示
    const show = (data: Recordable) => {
        // 标题
        onShow()
        initList()
    }
    // 搜索
    const searchBtn = () => {
        initList()
    }
    //重置
    const resetBtn = () => {
        clearForm()
        initList()
    }
    // 账单导出
    const exportBillBtn = async () => {
        switch (dealData.reportType) {
            case 0://细刻
                downloadXiKeMoney(queryUserMoneySumDetailForm)
                break;
            case 1://英驰
                downloadYingChi(queryUserMoneySumDetailForm)
                break;
            case 2://方仓
                downloadFangCang(queryUserMoneySumDetailForm)
                break;
            case 9:  // 简爱
                downloadJianAi(queryUserMoneySumDetailForm)
                break
            default:
                break;
        }
    }
    // 操作
    const handleClickChange = async (data: UserMoneyDetailRow) => {
        console.log(data, ":data");
        switch (data.types) {
            case BaseFeesetAction.DETAIL://明细
                unref(queryExpenseItemDetailRef)?.show(data)
                break;
            case BaseFeesetAction.BALANCED://平账
                const params = {
                    id: data.id,
                    orderId: data.orderId,
                    customerType: data.customerType
                }
                const res = await api_UserBalance(params)
                const resCode = resData(res, '平账成功！')
                if (resCode !== 500) initList()
                break;
            default:
                break;
        }
    }

    // 显示小提示框输入单号
    const orderIdListhide = () => dealData.isOrderIdList = false
    const showOrderIdList = () => dealData.isOrderIdList = true
    // 清除单号按钮
    const hanlClearOrderIdList = () => {
        orderIdListhide()
        dealData.inputMultiOrders = ''
        dealData.inputOrders = ''
        queryUserMoneySumDetailForm.orderIdList = []
    }
    // 确定单号按钮
    const confirmMultiOrders = () => {
        const orders = dealData.inputMultiOrders.split('\n')
        queryUserMoneySumDetailForm.orderIdList = orders.filter(item => item)
        dealData.inputOrders = orders.join(',')
        dealData.inputOrders = orders.join(',')
        orderIdListhide()
    }
    // 回车事件
    const confirmMultiEnterOrders = () => {
        if (dealData.inputOrders.length > 0) {
            dealData.inputMultiOrders = format(dealData.inputOrders, '\n')
            confirmMultiOrders()
            orderIdListhide()
        }
    }

    return {
        queryUserMoneySumDetailForm,
        dealData,
        actionOptions,
        queryExpenseItemDetailRef,
        onclose,
        show,
        searchBtn,
        resetBtn,
        exportBillBtn,
        handleClickChange,
        showOrderIdList,
        hanlClearOrderIdList,
        confirmMultiOrders,
        confirmMultiEnterOrders,
    }
}

export {
    userMoneySumDetail
}