<view class="chat-page">
  <scroll-view class="msg-list" scroll-y="true"
               scroll-into-view="{{scrollToView}}"
               refresher-enabled="{{true}}"
               refresher-triggered="{{isRefreshing}}"
               bindrefresherrefresh="onPullDownRefresh"
               style="top: 20rpx; bottom: calc({{bottomSafeHeight}}rpx + {{keyboardHeight}}px + env(safe-area-inset-bottom) + 20rpx);">
    <view class="loading-more" wx:if="{{loadingHistory}}">加载更早消息...</view>
    <block wx:for="{{messages}}" wx:key="messageId">
      <view class="msg-wrapper" data-messageid="{{item.messageId}}" bindlongpress="showAction">
        <view class="time-tag" wx:if="{{item.showTime}}">{{item.formattedTime}}</view>
        <view class="msg-row {{item.senderId === currentUser.id ? 'right' : 'left'}}">
          <image wx:if="{{item.senderId !== currentUser.id}}" class="avatar" src="{{toAvatar || '/images/default-avatar.png'}}" mode="aspectFill" />
          <view class="bubble {{item.senderId === currentUser.id ? 'bubble-right' : 'bubble-left'}}"
                data-messageid="{{item.messageId}}"
                data-text="{{item.type==='text' ? item.payload.text : ''}}"
                bindtap="{{item.type==='text' ? 'onBubbleTap' : (item.type==='image' ? 'previewImage' : '')}}"
                data-url="{{item.type==='image' ? item.payload.url : ''}}">
            <text wx:if="{{item.type === 'text'}}" class="msg-text">{{item.payload.text}}</text>
            <image wx:elif="{{item.type === 'image'}}" class="msg-image" src="{{item.payload.url}}" mode="widthFix" />
            <view wx:elif="{{item.type === 'order'}}" class="order-bubble">
              <text class="order-label">[订单]</text>
              <text class="order-info">ID: {{item.payload.orderId}}</text>
            </view>
          </view>
          <image wx:if="{{item.senderId === currentUser.id}}" class="avatar" src="{{currentUser.avatar}}" mode="aspectFill" />
        </view>
        <view wx:if="{{item.senderId === currentUser.id && item.status === 'success'}}" class="read-tag">
          {{item.read ? '已读' : '未读'}}
        </view>
      </view>
    </block>
    <view id="msg-bottom" style="height:20rpx;"></view>
  </scroll-view>

  <view class="input-area" style="bottom: {{keyboardHeight}}px; padding-bottom: calc(10rpx + env(safe-area-inset-bottom));">
    <view class="plus-panel {{showPlusPanel ? 'show' : ''}}" catchtap="closePlusPanel">
      <view class="plus-grid">
        <view class="plus-item" catchtap="openEmojiFromPlus">
          <text class="plus-icon">😀</text>
          <text class="plus-label">表情</text>
        </view>
        <view class="plus-item" catchtap="chooseImage">
          <text class="plus-icon">📷</text>
          <text class="plus-label">图片</text>
        </view>
        <view class="plus-item" catchtap="openOrderSender">
          <text class="plus-icon">📋</text>
          <text class="plus-label">订单</text>
        </view>
      </view>
    </view>

    <view class="emoji-mask" wx:if="{{showEmojiPanel}}" catchtap="closeEmojiPanel"></view>
    <view class="emoji-panel {{showEmojiPanel ? 'show' : ''}}">
      <scroll-view scroll-y="true" class="emoji-scroll">
        <view class="emoji-grid">
          <block wx:for="{{emojiList}}" wx:key="*this">
            <view class="emoji-item" data-emoji="{{item}}" catchtap="insertEmoji">{{item}}</view>
          </block>
        </view>
      </scroll-view>
    </view>

    <view class="pending-image" wx:if="{{pendingImage}}">
      <image class="pending-thumb" src="{{pendingImage}}" mode="aspectFill" />
      <view class="pending-remove" catchtap="clearPendingImage">✕</view>
    </view>

    <view class="write-row">
      <input class="text-input" placeholder="消息" value="{{inputText}}" bindinput="onInput" adjust-position="{{false}}" bindkeyboardheightchange="onKeyboardHeightChange" cursor-spacing="10"/>
      <view class="send-btn" bindtap="sendMessage">发送</view>
      <view class="plus-btn" bindtap="togglePlusPanel">
        <text class="plus-icon-single">➕</text>
      </view>
    </view>
  </view>

  <view class="detail-modal" wx:if="{{showDetailModal}}" catchtap="hideDetail">
    <view class="detail-content" catchtap="noop">
      <text class="detail-text" user-select="true">{{detailText}}</text>
      <button class="copy-btn" bindtap="copyDetail">复制</button>
    </view>
  </view>

  <order-sender visible="{{showOrderSender}}" bind:send="onSendOrder" bind:close="closeOrderSender" />
</view>