import { ref, computed } from "vue";
import { regionalsettlement, serviceSettlement } from "@/api/finance/settlement.js";

//功能
export default ({ proxy, jlzj_area_type }) => {
    const router = useRouter();
    const dialogFormRef = ref(null);
    const userTableRef = ref(null);

    const dialogType = ref('applyfor');
    //打开弹窗 applyfor:申请  settlement：结算
    const openDialog = (params, type) => {
        console.log('ids', ids);
        try {
            dialogType.value = type;

            let params = {};
            let disabledData = {};

            if (type === 'applyfor') {
                params = { mainOrderIds: params }
            }
            if (type === 'settlement') {
                params = { ...params }
                disabledData = {
                    payeeMethod: true,
                    payeeAccount: true,
                    payeeBankName: true,
                    payeeName: true,
                    payeeRemark: true
                }
            }

            dialogFormRef.value.initForm(params, disabledData)
        } catch (error) {
            console.log('error', error);
        } finally {
            console.log('dialogFormRef.value', dialogFormRef.value);
            dialogFormRef.value.handleDialog(true);
        }
    }
    //导出
    const exportFile = async (parmas) => {
        console.log("TCL: exportFile -> parmas", parmas)
        // try {
        //     proxy.download("core/orderSettlementApplication/export",{
        //         ...queryParams.value,
        //     }, `订单费用结算_${new Date().getTime()}.xlsx`);
        // } catch (error) { 
        // }
    }

    const applyforSubmit = (data) => {
        return new Promise(async (resolve, reject) => {
            const parmas = {
                mainOrderIds: data.mainOrderIds,
                payeeMethod: data.payeeMethod,
                payeeAccount: data.payeeAccount,
                payeeBankName: data.payeeBankName,
                payeeName: data.payeeName,
                payeeRemark: data.payeeRemark
            }
            const res = await applyOrderSettlement(parmas);
            if (up_res.code === 200) {
                proxy.$modal.msgSuccess("审核成功");
                resolve()
            }
            proxy.$modal.msgSuccess(res.msg);
            reject()
        })
    }

    const settlementSubmit = async (data) => {
        return new Promise(async (resolve, reject) => {
            const parmas = {
                orderId: data.orderId,
                payeeId: data.payeeId,
                payeeName: data.payeeName,
                payeeBank: data.payeeBank,
                payeeBankAccount: data.payeeBankAccount,
                payeeRemark: data.payeeRemark
            }
            let res = null;
            //平台结算区域的申请
            if (jlzj_area_type.value === '0') {
                res = await applyOrderSettlement(parmas)
            }
            //区域结算平台的申请

            if (jlzj_area_type.value === '10') {
                res = await serviceSettlement(parmas)
            }

            if (up_res.code === 200) {
                proxy.$modal.msgSuccess("审核成功");
                resolve()
            }
            proxy.$modal.msgSuccess(res.msg);
            reject()
        })
    }

    //申请提交
    const submitForm = async (data) => {

        try {
            console.log('submit', data);
            //申请
            if (dialogType.value === 'applyfor') {
                await applyforSubmit(data);
            }
            if (dialogType.value === 'settlement') {
                await settlementSubmit(data);
            }
        } catch (error) {
            console.log('error', error);
        } finally {
            userTableRef.value.resetForm();
            dialogFormRef.value.handleDialog(false);
        }
    }



    return {
        dialogFormRef,
        userTableRef,
        openDialog,
        exportFile,
        submitForm,
        searchBtns: [
            {
                label: '批量申请',
                func: () => {
                    const ids = userTableRef.value.ids;
                    console.log('批量删除', ids)
                    ids.length > 0 && openDialog(ids, 'applyfor')
                },
                key: 'applicant',
                type: 'primary'
            },
            {
                label: '批量结算',
                func: () => {
                    const ids = userTableRef.value.ids;
                    console.log('批量删除', ids)
                    ids.length > 0 && openDialog(ids, 'settlement')
                },
                key: 'settlement',
                type: 'primary',
                show: () => jlzj_area_type.value === '10',//区域公司才可以结算
            },
            {
                label: '导出',
                func: (parmas) => {
                    exportFile(parmas);
                },
                key: 'export',
                type: 'primary'
            },
        ],
        scopeBtns: [
            {
                label: '结算申请',
                type: 'primary',
                key: 'examine',
                func: (row) => {
                    console.log(row)
                    openDialog([row.mainOrderId], 'applyfor')
                },
                // show: (row) => {
                //     return jlzj_area_type.value === '20'
                // }
            },
            {
                label: '费用结算',
                type: 'primary',
                key: 'examine',
                func: (row) => {
                    console.log(row)
                    openDialog([row.mainOrderId], 'settlement')
                },
                show: () => jlzj_area_type.value === '10',//区域公司才可以结算
            },
            {
                label: '查看详情',
                type: 'primary',
                key: 'details',
                func: (row) => {
                    console.log(row)
                    router.push({
                        path: "/order-details",
                        query: { id: row.mainOrderId }
                    });
                }
            },
        ]

    }
}