// 区域
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,
        area_settlement_status
    } = proxy.useDict("lrr_service_status", "service_centre_settlement_stauts", "area_settlement_status");

    const tabList = computed(() => {
        let data = []
        data = area_settlement_status.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;
        }
    }

    //区域费用结算
    const handleRegionalSettlement = 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 regionalsettlement(parmas);
            if (res.code === 200) {
                proxy.$modal.msgSuccess("操作成功");
                userTableRef.value.resetForm();
                dialogFormRef.value.handleDialog(false);
                return;
            }
        } 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 handleRegionalSettlement(data);
            }
        } catch (error) {
            console.log('error', error);
        }
    }

    const list_pingtai = ref([
        {
            label: '区域中心',
            prop: 'applicant' ,
            tableProp:'areaName',
            type: 'input',
            isSearch: true,
            width: '180px',
        },
        {
            label: '订单编号',
            prop: 'orderNo',
            tableProp:'outTradeNo',
            type: 'input',
            isSearch: true,
            width: '200px',
        },
        {
            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: false,
            width: '100px'
        },
        {
            label: '区域公司抽成比例',
            prop: 'areaDistributionRatio',
            width: '150px'
        },
        {
            label: '区域公司抽成金额',
            prop: 'areaDistributionAmount',
            width: '150px'
        },
        {
            label: '区域公司结算状态',
            prop: 'areaSettlementStatus',
            type: 'dict',
            dict: area_settlement_status,
            width: '150px',
        },
        {
            label: '开户行',
            prop: 'areaPayeeBankName',
             width:'150px'
        },
        {
            label: '收款账户',
            prop: 'areaPayeeAccount',
             width:'180px',
             type: 'input',
            isSearch: true
        },
        {
            label: '收款人',
            prop: 'areaPayeeName',
             width:'150px'
        }
       
    ])

    const list_quyu = ref([
        {
            label: '订单编号',
            prop: 'orderNo',
            tableProp:'outTradeNo',
            type: 'input',
            isSearch: true,
            width: '200px',
        },
        {
            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: false,
            width: '100px'
        },
        {
            label: '区域公司抽成比例',
            prop: 'areaDistributionRatio',
            width: '150px'
        },
        {
            label: '区域公司抽成金额',
            prop: 'areaDistributionAmount',
            width: '150px'
        },
        {
            label: '区域公司结算状态',
            prop: 'areaSettlementStatus',
            type: 'dict',
            dict: area_settlement_status,
            width: '150px',
        },
        {
            label: '区域公司结算时间',
            prop: 'areaSettlementTime',
            width: '150px',
        },
        {
            label: '开户行',
            prop: 'areaPayeeBankName',
             width:'150px'
        },
        {
            label: '收款账户',
            prop: 'areaPayeeAccount',
             width:'180px',
             type: 'input',
            isSearch: true
        },
        {
            label: '收款人',
            prop: 'areaPayeeName',
             width:'150px'
        },
        {
            label: '备注',
            prop: 'areaAuditorRemark',
            width: '150px'
        },
       
    ])
    return {
        dialogFormRef,
        userTableRef,
        tabList,
        tabKey: 'areaSettlementStatus',
        submitForm,
        searchBtns: [
            {
                label: '批量申请',
                func: () => {
                    const ids = userTableRef.value.ids;
                    console.log('批量删除', ids)
                    ids.length > 0 && openDialog(ids, 'applyfor')
                },
                key: 'applicant',
                type: 'primary',
                // hasPermi:["finance:apply"] 
                show: (tabsValue) => {
					console.log("TCL: tabsValue",jlzj_area_type.value, tabsValue)
                    return jlzj_area_type.value !== PING_TAI && [ '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 === PING_TAI &&  tabsValue === '40'
                }
                // hasPermi:["finance:settlement"] 
            },
             {
                label: '导出',
                func: (parmas) => {
                     console.log("TCL: parmas", parmas)
                     try {
                        proxy.download("core/users/orders/settlementOrderList/export",{
                            ...parmas,
                            areaSettlementStatus: parmas.tabsValue
                        }, `订单费用结算表_${new Date().getTime()}.xlsx`);
                    } catch (error) { 
                    }
                },
                key: 'export',
                type: 'primary'
            },
        ],
        dialogType,
        tableColumn:   jlzj_area_type.value === PING_TAI?list_pingtai:list_quyu,
        scopeBtns: [
            {
                label: '结算申请',
                type: 'primary',
                key: 'examine',
                func: (row) => {
                    console.log(row)
                    openDialog([row.orderId], 'applyfor')
                },
                hasPermi: ["finance:apply"],
                show: (row) => {
                    return [ '10','30'].includes(row.areaSettlementStatus) && jlzj_area_type.value !== PING_TAI
                }
            },
            {
                label: '费用结算',
                type: 'primary',
                key: 'examine',
                func: (row) => {
                    console.log(row)
                    openDialog([row.orderId], 'settlement')
                },
                hasPermi: ["finance:settlement"],
                show: (row) => {
                    return row.areaSettlementStatus === '40' && jlzj_area_type.value === PING_TAI
                }
            },

        ]
    }
}