// 服务中心
import { ref, computed } from "vue";
import {
    applyOrderSettlement,
    serviceSettlement, regionalsettlement
} from "@/api/finance/settlement.js";

const PING_TAI = '0'; //平台
const QU_YU = '3'; //区域
const FU_WU_ZHONG_XIN = '4'; //服务中心

export default ({ proxy, jlzj_area_type }) => {
    const dialogFormRef = ref(null);
    const userTableRef = ref(null);
    const dialogType = ref('applyfor');

    const {
        lrr_service_status,
        service_centre_settlement_stauts
    } = proxy.useDict("lrr_service_status", "service_centre_settlement_stauts");

    const tabList = computed(() => {
        let data = []
        data = service_centre_settlement_stauts.value.map(item => {
            return {
                title: item.label,
                name: item.value
            }
        })
        return data || [];
    })
    const openDialog = (data,type) => {
        try {
            dialogType.value = type;


            let params = { mainOrderIds: data };
            let disabledData = {};

            dialogFormRef.value.initForm(params, disabledData)
        } catch (error) {
            console.log('error', error);
        } finally {
            console.log('dialogFormRef.value', dialogFormRef.value);
            dialogFormRef.value.handleDialog(true);
        }
    }

    //结算申请
    const applyforSubmit = async(data) => {

        const parmas = {
            secondOrderIds: data.mainOrderIds,
            payeeMethod: data.payeeMethod,
            payeeAccount: data.payeeAccount,
            payeeBankName: data.payeeBankName,
            payeeName: data.payeeName,
            payeeRemark: data.payeeRemark,

        }
        const res = await applyOrderSettlement(parmas);
        if (res.code === 200) {
            proxy.$modal.msgSuccess("审核成功");
            userTableRef.value.resetForm();
            dialogFormRef.value.handleDialog(false);
            return
        }
        proxy.$modal.msgError(res.msg);
    }
    //服务中心费用结算
    const handleServiceSettlement = async (data) => {
        try {
            const parmas = {
                secondOrderIds: data.mainOrderIds,
                payerMethod: data.payerMethod,
                payerAccount: data.payerAccount,
                payerBankName: data.payerBankName,
                payerName: data.payerName,
                payerRemark: data.payerRemark,
                payerTime: data.payerTime
            }
            const res = await serviceSettlement(parmas)
            if (res.code === 200) {
                proxy.$modal.msgSuccess("操作成功");
                userTableRef.value.resetForm();
                dialogFormRef.value.handleDialog(false);
                return
            }
            proxy.$modal.msgError(res.msg);
        } catch (error) {
            console.log("TCL: handleRegionalSettlement -> error", error)

        }
    }
    //弹窗申请提交
    const submitForm = async (data) => {
        try {
            console.log('submit', data);
            //申请
            if (dialogType.value === 'applyfor') {
                await applyforSubmit(data);
            }

            if (dialogType.value === 'settlement') {
                await handleServiceSettlement(data);
            }
        } catch (error) {
            console.log('error', error);
        } finally {
            // userTableRef.value.resetForm();
            // dialogFormRef.value.handleDialog(false);
        }
    }

    return {
        dialogFormRef,
        userTableRef,
        tabList,
        tabKey: 'serviceCentreSettlementStatus',
        submitForm,
        searchBtns: [
            {
                label: '批量申请',
                func: () => {
                    const ids = userTableRef.value.ids;
                    console.log('批量删除', ids)
                    ids.length > 0 && openDialog(ids, 'applyfor')
                },
                key: 'applicant',
                type: 'primary',
                show: (tabsValue) => {
                    return jlzj_area_type.value === FU_WU_ZHONG_XIN && [ '10','30'].includes(tabsValue) 
                }
            },
            {
                label: '批量费用结算',
                func: () => {
                    const ids = userTableRef.value.ids;
                    console.log('批量删除', ids)
                    ids.length > 0 && openDialog(ids, 'settlement')
                },
                key: 'settlement',
                type: 'primary',
                show: (tabsValue) => {
                    return jlzj_area_type.value !== FU_WU_ZHONG_XIN &&  tabsValue === '40'
                }
                // hasPermi:["finance:settlement"] 
            },
            {
                label: '导出',
                func: (parmas) => {
                     try {
                        proxy.download("core/users/orders/settlementOrderList/export",{
                            ...parmas,
                            serviceCentreSettlementStatus: parmas.tabsValue
                        }, `订单费用结算表_${new Date().getTime()}.xlsx`);
                    } catch (error) { 
                    }
                },
                key: 'export',
                type: 'primary'
            },
        ],
        dialogType,
        tableColumn: ref([
            {
                label: '订单号',
                prop: 'orderNo',
                tableProp:'outTradeNo',
                type: 'input',
                isSearch: true,
                width: '180px',
            },
            {
                label: '订单开始时间',
                queryLabel: '订单时间',
    
                prop: 'orderTime',
                width: '160px',
                isSearch: true,
                type: 'date',
                keys: ['startTime', 'endTime'],
            },
            {
                label: '订单完成时间',
                prop: 'serviceEndTime',
                width: '160px',
                isSearch: false,
            },
            {
                label: '用户名称',
                prop: 'userNickName',
                width: '140px',
                type: 'input',
                isSearch: false
            },
            {
                label: '用户联系方式',
                prop: 'userPhone',
                width: '140px',
                type: 'input',
                isSearch: true
            },
            {
                label: '志愿者名称',
                prop: 'volunteerName',
                width: '140px',
                type: 'input',
                isSearch: false
            },
            {
                label: '志愿者联系方式',
                prop: 'volunteerPhone',
                width: '140px',
                type: 'input',
                isSearch: true
            },
            {
                label: '服务项目',
                prop: 'businessTierName',
                width: '170px'
            },
            {
                label: '订单总金额',
                prop: 'orderTotalPrice',
                width: '140px'
            },
            {
                label: '订单状态',
                prop: 'orderStatus',
                type: 'dict',
                dict: lrr_service_status,
                isSearch: true
            },
            {
                label: '抽成比例',
                prop: 'serviceCentreDistributionRatio',
                width: '150px'
            },
            {
                label: '抽成金额',
                prop: 'serviceCentreDistributionAmount',
                width: '150px'
            },
            {
                label: '结算状态',
                prop: 'serviceCentreSettlementStatus',
                type: 'dict',
                dict: service_centre_settlement_stauts,
                width: '150px',
            },
            {
                label: '结算时间',
                prop: 'serviceCentreSettlementTime',
                width: '150px'
            },
            {
                label: '备注',
                prop: 'serviceCentreAuditorRemark',
                width: '150px'
            },
        ]),
        scopeBtns: [
            {
                label: '结算申请',
                type: 'primary',
                key: 'examine',
                func: (row) => {
                    console.log(row)
                    openDialog([row.orderId], 'applyfor')
                },
                // hasPermi: ["finance:apply"],
                show: (row) => {
                    return jlzj_area_type.value === FU_WU_ZHONG_XIN && [ '10','30'].includes(row.serviceCentreSettlementStatus)
                }
            },
            {
                label: '费用结算',
                type: 'primary',
                key: 'examine',
                func: (row) => {
                    console.log(row)
                    openDialog([row.orderId], 'settlement')
                },
                // hasPermi: ["finance:settlement"],
                show: (row) => {
                    return row.serviceCentreSettlementStatus === '40' && jlzj_area_type.value !== FU_WU_ZHONG_XIN
                }
            },

        ]
    }
}