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 router = useRouter();
    const dialogFormRef = ref(null);
    const userTableRef = ref(null);

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

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

            // if (type === 'applyfor') {
            //     params = { mainOrderIds: data }
            // }
            // if (type === 'settlement') {
            //     params = { mainOrderIds: data }
            // }

            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) => {
		console.log("TCL: applyforSubmit -> data", data)
		
        return new Promise(async (resolve, reject) => {
            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 (up_res.code === 200) {
                proxy.$modal.msgSuccess("审核成功");
                resolve()
            }
            proxy.$modal.msgSuccess(res.msg);
            reject()
        })
    }

    const settlementSubmit = async (data) => {
		console.log("TCL: settlementSubmit -> data", data)
        const parmas = {
            secondOrderIds: data.mainOrderIds,
            payerMethod: data.payerMethod,
            payerAccount: data.payerAccount,
            payerBankName: data.payerBankName,
            payerName: data.payerName,
            payerRemark: data.payerRemark,
            payerTime:data.payerTime
        }
        let res = null;
        //平台结算区域的申请
        if (jlzj_area_type.value === PING_TAI ) {
            res = await regionalsettlement(parmas)
        }

        //区域结算服务中心
        if (jlzj_area_type.value === QU_YU) {
            res = await serviceSettlement(parmas)
        }
        
        if (res.code === 200) {
            proxy.$modal.msgSuccess("审核成功");
            return;
        }
        proxy.$modal.msgSuccess(res.msg);
    }

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

        try {
            console.log('submit', data,dialogType.value);
            //申请
            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,
        dialogType,
        openDialog,
        exportFile,
        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"] 
            },
            {
                label: '批量费用结算',
                func: () => {
                    const ids = userTableRef.value.ids;
                    console.log('批量删除', ids)
                    ids.length > 0 && openDialog(ids, 'settlement')
                },
                key: 'settlement',
                type: 'primary',
                hasPermi:["finance:settlement"] 
            },
            {
                label: '导出',
                func: (parmas) => {
                    exportFile(parmas);
                },
                key: 'export',
                type: 'primary'
            },
        ],
        scopeBtns: [
            {
                label: '结算申请',
                type: 'primary',
                key: 'examine',
                func: (row) => {
                    console.log(row)
                    openDialog([row.orderId], 'applyfor')
                },
                hasPermi:["finance:apply"],
                show:(row)=>{
					console.log("TCL: exportFile -> row", row.orderStatus === '4')
                    return row.areaSettlementStatus === '10'
                }
            },
            {
                label: '费用结算',
                type: 'primary',
                key: 'examine',
                func: (row) => {
                    console.log(row)
                    openDialog([row.orderId], 'settlement')
                },
                hasPermi:["finance:settlement"],
                show:(row)=>{
					console.log("TCL: exportFile -> row", row.orderStatus === '4')
                    return row.areaSettlementStatus === '40'
                }
            },
            {
                label: '查看详情',
                type: 'primary',
                key: 'details',
                func: (row) => {
                    console.log(row)
                    router.push({
                        path: "/order-details",
                        query: { id: row.orderId }
                    });
                },
                hasPermi:["finance:details"] 
            },
        ],
        dialogData: {
            title: '结算申请',
            column: [
                {
                    label: '收款方式',
                    prop: 'payeeMethod',
                    type: 'select',
                    dictionary: 'settlement_application_payee_method',
                    rules: [
                        { required: true, message: '请选择收款方式', trigger: 'change' }
                    ],
                },
                {
                    label: '收款账号',
                    prop: 'payeeAccount',
                    type: 'input',
                    rules: [
                        { required: true, message: '请输入收款账号', trigger: 'change' }
                    ],
        
                },
                {
                    label: '收款银行名称',
                    prop: 'payeeBankName',
                    type: 'input',
                    rules: [
                        { required: true, message: '请输入收款银行名称', trigger: 'change' }
                    ],
        
                },
                {
                    label: '收款人',
                    prop: 'payeeName',
                    type: 'input',
                    rules: [
                        { required: true, message: '请输入收款人', trigger: 'change' }
                    ],
                },
                {
                    label: '收款人备注',
                    prop: 'payeeRemark',
                    type: 'textarea',
                },
            ]
        },
        costdialogData: {
            title: '费用结算',
            column: [
                {
                    label: '付款方式',
                    prop: 'payerMethod',
                    type: 'select',
                    dictionary: 'settlement_application_payee_method',
                    rules: [
                        { required: true, message: '请选择付款方式', trigger: 'change' }
                    ],
                },
                {
                    label: '付款账号',
                    prop: 'payerAccount',
                    type: 'input',
                    rules: [
                        { required: true, message: '请输入付款账号', trigger: 'change' }
                    ],
        
                },
                {
                    label: '付款银行名称',
                    prop: 'payerBankName',
                    type: 'input',
                    rules: [
                        { required: true, message: '请输入付款银行名称', trigger: 'change' }
                    ],
        
                },
                {
                    label: '付款人',
                    prop: 'payerName',
                    type: 'input',
                    rules: [
                        { required: true, message: '请输入付款人', trigger: 'change' }
                    ],
                },
                {
                    label: '付款时间',
                    prop: 'payerTime',
                    type: 'picker',
                    rules: [
                        { required: true, message: '请选择付款时间', trigger: 'change' }
                    ],
                },
                {
                    label: '付款人备注',
                    prop: 'payerRemark',
                    type: 'textarea',
                },
            ]
        },

    }
}