const app = getApp();

Component({
    properties: {
        orderId: { type: String, value: '' }
    },

    data: {
        order: null,
        loading: false,
        errorMsg: '',
        // 跨平台字段
        isCross: false,       // 是否跨平台订单
        dispatchType: 0,      // 1-我方派单，2-对方派单
        partnerClubId: '',    // 对方俱乐部ID
    },

    lifetimes: {
        attached() {
            if (this.properties.orderId) {
                this.fetchOrderDetail();
            }
        }
    },

    methods: {
        // 获取订单详情（含跨平台字段）
        fetchOrderDetail() {
            if (!this.properties.orderId) return;
            this.setData({ loading: true, errorMsg: '' });
            const baseUrl = app.globalData.apiBaseUrl;
            wx.request({
                url: `${baseUrl}/dingdan/order_detail/`,   // 需后端返回 is_cross, dispatch_type, partner_club_id
                method: 'POST',
                data: { order_id: this.properties.orderId },
                header: { 'content-type': 'application/json' },
                success: (res) => {
                    if (res.statusCode === 200 && res.data && res.data.code === 0) {
                        const order = res.data.data;
                        // 处理图片URL
                        if (order.tupian && !order.tupian.startsWith('http')) {
                            order.tupian = app.globalData.ossImageUrl + order.tupian;
                        }
                        this.setData({
                            order,
                            loading: false,
                            isCross: order.is_cross === 1,
                            dispatchType: order.dispatch_type || 0,
                            partnerClubId: order.partner_club_id || '',
                        });
                        // 通知父页面状态
                        this.triggerEvent('statusready', {
                            isCross: this.data.isCross,
                            dispatchType: this.data.dispatchType,
                            partnerClubId: this.data.partnerClubId
                        });
                    } else {
                        this.setData({ errorMsg: res.data?.msg || '获取订单失败', loading: false });
                    }
                },
                fail: () => {
                    this.setData({ errorMsg: '网络请求失败', loading: false });
                }
            });
        },

        // 发送订单卡片消息（原有）
        sendOrderMessage() {
            if (!this.data.order) return;
            this.triggerEvent('sendorder', { order: this.data.order });
        },

        // 跨平台消息转发（供父页面调用）
        forwardMessage(payload) {
            const that = this;
            const baseUrl = app.globalData.apiBaseUrl;
            return new Promise((resolve, reject) => {
                wx.request({
                    url: `${baseUrl}/dingdan/kptxxts`,
                    method: 'POST',
                    data: {
                        order_id: that.properties.orderId,
                        message_type: payload.type || 'text',
                        content: payload.content || '',
                        image_url: payload.imageUrl || '',   // 图片等
                    },
                    header: { 'content-type': 'application/json' },
                    success: (res) => {
                        if (res.statusCode === 200 && res.data && res.data.code === 0) {
                            resolve(res.data);
                        } else {
                            reject(res.data);
                        }
                    },
                    fail: (err) => {
                        reject(err);
                    }
                });
            });
        }
    }
});