<view class="page-container">
  <view class="card bk-hero">
    <view class="bk-title">🔪 砍价专区</view>
    <view class="bk-sub">好友帮砍 · 砍到底价 · 好货低价拿</view>
    <view class="bk-tabs">
      <view class="bk-tab {{view==='list'?'active':''}}" data-view="list" bindtap="switchView">砍价活动</view>
      <view class="bk-tab {{view==='mysess'?'active':''}}" data-view="mysess" bindtap="switchView">我的砍价</view>
    </view>
  </view>

  <view wx:if="{{loading}}" class="bk-loading">加载中...</view>

  <!-- ==================== 活动列表 ==================== -->
  <block wx:elif="{{view==='list'}}">
    <view class="bk-list">
      <view class="card bk-item" wx:for="{{list}}" wx:key="id" data-id="{{item.id}}" bindtap="openDetail">
        <image class="bk-image" src="{{item.product_image}}" mode="aspectFill" />
        <view class="bk-info">
          <view class="bk-name text-ellipsis-2">{{item.name}}</view>
          <view class="bk-price-row">
            <view class="bk-cur">¥<text class="bk-price">{{item.floor_price}}</text><text class="bk-floor">底价</text></view>
            <view class="bk-orig">原价 ¥{{item.original_price}} · 库存 {{item.stock}}</view>
          </view>
          <view class="bk-tip">砍到底价可下单</view>
        </view>
      </view>
      <empty-state wx:if="{{!loading && list.length===0}}" text="暂无砍价活动" />
    </view>
  </block>

  <!-- ==================== 活动详情(发起) ==================== -->
  <block wx:elif="{{view==='detail'}}">
    <view class="card bk-detail" wx:if="{{detail.id}}">
      <image class="bk-detail-img" src="{{detail.product_image}}" mode="aspectFill" />
      <view class="bk-detail-name">{{detail.name}}</view>
      <view class="bk-price-box">
        <view class="bk-row"><text>商品原价</text><text class="bk-row-val">¥{{detail.original_price}}</text></view>
        <view class="bk-row"><text>砍到底价</text><text class="bk-row-val danger">¥{{detail.floor_price}}</text></view>
        <view class="bk-row"><text>活动库存</text><text class="bk-row-val">{{detail.stock}} 件</text></view>
        <view class="bk-row"><text>砍价有效期</text><text class="bk-row-val">{{detail.expire_hours || 24}} 小时</text></view>
      </view>
      <view class="bk-rule">发起砍价后，分享给好友帮砍，每次随机砍 8%~20%，砍到底价即可低价下单。</view>
      <button class="btn btn-danger btn-block bk-submit" loading="{{submitting}}" bindtap="startBargain">发起砍价</button>
      <view class="bk-back" bindtap="backToList">‹ 返回活动列表</view>
    </view>
  </block>

  <!-- ==================== 我的砍价单 ==================== -->
  <block wx:elif="{{view==='mysess'}}">
    <view class="bk-list">
      <view class="card bk-sess-row" wx:for="{{sessions}}" wx:key="id">
        <view class="bk-sess-head">
          <view class="bk-sess-no">{{item.session_no}}</view>
          <view class="bk-sess-status {{item.status}}">{{item.status === 'bargaining' ? '砍价中' : (item.status === 'can_order' ? '可下单' : (item.status === 'ordered' ? '已下单' : '已过期'))}}</view>
        </view>
        <view class="bk-sess-body">
          <image class="bk-image" src="{{item.product_image}}" mode="aspectFill" />
          <view class="bk-info">
            <view class="bk-name text-ellipsis-2">{{item.product_name || item.activity_name}}</view>
            <view class="bk-sess-price">当前 ¥{{item.current_price}} / 底价 ¥{{item.floor_price}} · 已帮砍 {{item.helped_count}} 人</view>
          </view>
        </view>
        <view class="bk-sess-actions">
          <button class="btn btn-small btn-outline" wx:if="{{item.status==='bargaining'}}" data-no="{{item.session_no}}" bindtap="openSession">查看 / 分享砍价</button>
          <button class="btn btn-danger btn-small" wx:if="{{item.status==='can_order'}}" data-no="{{item.session_no}}" bindtap="orderBargain">立即下单 ¥{{item.current_price}}</button>
        </view>
      </view>
      <empty-state wx:if="{{!loading && sessions.length===0}}" text="暂无砍价单，去发起一个吧" />
    </view>
  </block>

  <!-- ==================== 砍价单详情(帮砍/下单) ==================== -->
  <block wx:elif="{{view==='sess'}}">
    <view class="card bk-session" wx:if="{{session.session_no}}">
      <view class="bk-sess-head">
        <view class="bk-sess-no">{{session.session_no}}</view>
        <view class="bk-sess-status {{session.status}}">{{statusLabel(session.status)}}</view>
      </view>
      <view class="bk-sess-body">
        <image class="bk-image" src="{{session.product_image}}" mode="aspectFill" />
        <view class="bk-info">
          <view class="bk-name text-ellipsis-2">{{session.product_name || session.activity_name}}</view>
        </view>
      </view>
      <view class="bk-big-price">¥{{session.current_price}}</view>
      <view class="bk-sess-meta">底价 ¥{{session.floor_price}} · 原价 ¥{{session.original_price}}</view>
      <view class="bk-bar"><view class="bk-bar-in" style="width: {{session.progress}}%"></view></view>
      <view class="bk-sess-hint">已砍 ¥{{session.cut_amount}} · {{statusLabel(session.status)}}</view>

      <view class="bk-help-list">
        <block wx:if="{{session.helps && session.helps.length}}">
          <view class="bk-help" wx:for="{{session.helps}}" wx:key="index">「{{item.nickname || '好友'}}」帮砍了 <text class="danger">¥{{item.amount}}</text></view>
        </block>
        <view wx:else class="bk-help-empty">还没有人帮砍，邀请好友来帮 TA 砍一刀吧～</view>
      </view>

      <view class="bk-sess-actions">
        <button class="btn btn-warning btn-block" wx:if="{{session.status==='bargaining' && !isOwner}}" loading="{{submitting}}" bindtap="helpBargain">🔪 帮 TA 砍一刀</button>
        <button class="btn btn-danger btn-block" wx:if="{{session.status==='bargaining' && isOwner}}" bindtap="shareBargain">📣 邀请好友帮砍</button>
        <button class="btn btn-danger btn-block" wx:if="{{session.status==='can_order' && isOwner}}" loading="{{submitting}}" bindtap="orderBargain" data-no="{{session.session_no}}">立即下单 ¥{{session.current_price}}</button>
      </view>
      <view class="bk-back" bindtap="backToMySessions">‹ 返回我的砍价</view>
    </view>
  </block>
</view>
