import { ref,computed } from "vue";
import { auditorOrderSettlement } from "@/api/finance/settlement.js";
//功能
export default ({proxy,jlzj_area_type}) => {
    const router = useRouter();
     const dialogFormRef = ref(null);
     const userTableRef = ref(null);
     const { 
        lrr_service_status,
        settlement_application_auditor_status,
        area_settlement_status,
        service_centre_settlement_stauts
     } = proxy.useDict("lrr_service_status","settlement_application_auditor_status","area_settlement_status","service_centre_settlement_stauts");

     const tabList =computed(() => {
        let data = []
        data = settlement_application_auditor_status.value.map(item => {
            return {
                title: item.label,
                name: item.value
            }
        })
        return [{
            title: '全部',
           name: ''
        },...data] || [];
    })
     //打开弹窗
     const openDialog = (ids) => {
        console.log('ids', ids);
        try {
            dialogFormRef.value.initForm({ids:ids}, {})
        } 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 submitForm = async (data) => {

        try {
            console.log('submit', data);
            const parmas = {
                ids: data.ids,
                auditorStatus:  data.auditorStatus,
                auditorRemark: data.auditorRemark
            }
            const res = await auditorOrderSettlement(parmas);
               if (res.code === 200) {
                    proxy.$modal.msgSuccess("审核成功");
                    userTableRef.value.resetForm();
                    dialogFormRef.value.handleDialog(false);
                    return;
                }
                proxy.$modal.msgSuccess(res.msg);
        } catch (error) {
            console.log('error', error);
        }
    }
    const list_quyu = ref([
        {
            label: jlzj_area_type.value === '0'?'区域中心':'服务中心',
            prop: 'applicant',
            tableProp: jlzj_area_type.value === '0'?'areaName':'serviceCentreName',
            type: 'input',
            isSearch: true,
            width:'150px'
        },
        {
            label: '提交人',
            prop: 'applyUserName',
            type: 'input',
            width:'150px',
            isSearch: false
        },
        {
            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',
        },
        {
            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:'150px'
        },
        {
            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: 'auditorRemark',
             width:'150px'
        },
        {
            label: '审核状态',
            prop: 'auditorStatus',
            type: 'dict',
            dict: settlement_application_auditor_status,
            width:'150px',
        },
    ])
    const list_fuwu = ref([
        {
            label: jlzj_area_type.value === '0'?'区域中心':'服务中心',
            prop: 'applicant',
            tableProp: jlzj_area_type.value === '0'?'areaName':'serviceCentreName',
            type: 'input',
            isSearch: true,
            width:'150px'
        },
        {
            label: '提交人',
            prop: 'applyUserName',
            type: 'input',
            width:'150px',
            isSearch: false
        },
        {
            label: '订单编号',
            prop: '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',
        },
        {
            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:'150px'
        },
        {
            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: '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: 'auditorRemark',
             width:'150px'
        },
        {
            label: '审核状态',
            prop: 'auditorStatus',
            type: 'dict',
            dict: settlement_application_auditor_status,
            width:'150px',
        },
    ])
    return {
        dialogFormRef,
        userTableRef,
        tabList,
        openDialog,
        exportFile,
        submitForm,
        tableColumn: 
        jlzj_area_type.value === '0'? list_quyu:list_fuwu,
        dialogData: {
            title: '审核',
            column: [
                {
                    label: '审核状态',
                    prop: 'auditorStatus',
                    type: 'radio',
                    rules: [
                        { required: true, message: '请选择审核状态', trigger: 'blur' }
                    ],
                    options: [
                        {
                            label: '通过',
                            value: '20'
                        },
                        {
                            label: '不通过',
                            value: '30'
                        }
                    ]
                },
                {
                    label: '驳回原因',
                    prop: 'auditorRemark',
                    type: 'textarea',
                    show:(form)=>{
                        console.log('驳回原因',form);
                        return form.auditorStatus === '30'
                    }
                },
            ]
        },
        searchBtns:[
            {
                label: '批量审核',
                func: () => {
                    const ids = userTableRef.value.ids;
                    console.log('批量删除', ids)
                    ids.length > 0 && openDialog(ids)
                },
                key: 'applicant',
                type: 'primary',
                show: (tabsValue) => {
                    
                    return  tabsValue === '10'
                }
            },
            {
                label: '导出',
                func: (parmas) => {
                     try {
                        proxy.download("core/orderSettlementApplication/export",{
                            ...parmas,
                            auditorStatus: parmas.tabsValue
                        }, `订单费用结算申请表_${new Date().getTime()}.xlsx`);
                    } catch (error) { 
                    }
                },
                key: 'export',
                type: 'primary'
            },
        ],
        scopeBtns:[
            {
                label: '审核',
                type: 'primary',
                key: 'examine',
                func: (row) => {
                    console.log(row)
                    openDialog([row.id])
                },
                show:(row)=>{
                    return row.auditorStatus === '10'
                }
            },
            // {
            //     label: '查看详情',
            //     type: 'primary',
            //     key: 'details',
            //     func: (row) => {
            //         console.log(row)
            //         router.push({
            //             path: "/order-details",
            //             query: { id: row.mainOrderId }
            //         });
            //     }
            // },
        ]
       
    }
}