<!-- pages/kaohe_jinpai/kaohe_jinpai.wxml -->
<view class="page-root">
  <!-- 顶部Tab切换 -->
  <view class="tab-bar">
    <view class="tab-item {{activeTab === 'all_tags' ? 'active' : ''}}" data-tab="all_tags" bindtap="switchTab">考核标签</view>
    <view class="tab-item {{activeTab === 'my_records' ? 'active' : ''}}" data-tab="my_records" bindtap="switchTab">我的考核</view>
  </view>

  <!-- 考核标签面板 -->
  <view class="content-area" wx:if="{{activeTab === 'all_tags'}}">
    <scroll-view scroll-y="true" class="scroll-view">
      <view wx:for="{{plateList}}" wx:key="bankuai_id" class="plate-group">
        <view class="plate-title">{{item.mingcheng}}</view>
        <view wx:for="{{item.tags}}" wx:key="id" class="tag-card" bindtap="showTagDetail" data-tag="{{item}}">
          <view class="tag-header">
            <chenghao-tag mingcheng="{{item.name}}" texiaoJson="{{item.texiao_json}}" />
            <text wx:if="{{item.jinpai}}" class="jinpai-badge">金牌</text>
          </view>
          <view class="fee-line">
            <text wx:for="{{item.fee_list}}" wx:key="cishu" class="fee-item">{{item.cishu}}次 ¥{{item.feiyong}}</text>
            <text wx:if="{{item.fee_list.length > 0}}" class="fee-item">≥{{item.fee_list.length}}次 ¥{{item.fee_list[item.fee_list.length-1].feiyong}}</text>
          </view>
          <view wx:if="{{!myTags.includes(item.name)}}" class="apply-btn" data-tag="{{item}}" bindtap="openApplyModal">申请考核</view>
          <view wx:else class="owned-tip">已拥有</view>
        </view>
      </view>
    </scroll-view>
  </view>

  <!-- 我的考核面板 -->
  <view class="content-area" wx:if="{{activeTab === 'my_records'}}">
    <view class="filter-row">
      <view class="filter-tag {{recordFilter === 'pending' ? 'active' : ''}}" data-filter="pending" bindtap="filterRecord">进行中/未通过</view>
      <view class="filter-tag {{recordFilter === 'passed' ? 'active' : ''}}" data-filter="passed" bindtap="filterRecord">已通过</view>
    </view>
    <scroll-view scroll-y="true" class="scroll-view" bindscrolltolower="onReachBottom">
      <view wx:for="{{recordList}}" wx:key="jilu_id" class="record-card">
        <view class="card-top">
          <text class="tag-name">{{item.chenghao_name}}</text>
          <text class="status-text" style="color: {{item.statusColor}}">{{item.statusText}}</text>
        </view>
        <view class="card-info">
          <text>板块：{{item.bankuai_name}}</text>
          <text>考核官：{{item.shenheguan_id || '待分配'}}</text>
          <!-- 联系考核官：纯文字蓝色下划线 -->
          <view wx:if="{{item.shenheguan_id}}" class="contact-text-link" data-item="{{item}}" bindtap="contactKaoheguan">联系考核官</view>

          <text>游戏ID：{{item.dashou_youxi_id}}</text>
          <text>备注：{{item.dashou_beizhu}}</text>
          <text>第{{item.cishu}}次考核 · 共缴 ¥{{item.total_fee}}</text>

          <!-- 进行中/未通过时显示本次及预计下次费用 -->
          <view wx:if="{{item.zhuangtai !== 1}}" class="fee-predict">
            <text>本次考核费用：¥{{item.current_fee}}</text>
            <text>预计下次费用：¥{{item.next_fee}}</text>
          </view>

          <text wx:if="{{item.last_fail_reason}}">上次拒绝原因：{{item.last_fail_reason}}</text>
        </view>
        <!-- 未通过时显示“再次考核”按钮，传递 tag_obj 以便计算费用 -->
        <view wx:if="{{item.zhuangtai === 2}}" class="reapply-btn" data-tag="{{item.tag_obj}}" data-jiluid="{{item.jilu_id}}" data-reapply="{{true}}" bindtap="openApplyModal">再次考核</view>
      </view>
      <view wx:if="{{recordLoading}}" class="loading-more">加载中...</view>
      <view wx:if="{{!recordHasMore && recordList.length > 0}}" class="no-more">—— 没有更多了 ——</view>
    </scroll-view>
  </view>

  <!-- 申请考核弹窗 -->
  <view wx:if="{{showApplyModal}}" class="modal-mask" bindtap="closeApplyModal">
    <view class="modal-dialog" catchtap="noop">
      <view class="modal-header">申请考核 - {{selectedTag.name}}</view>
      <view class="form-group">
        <text class="form-label">游戏昵称</text>
        <input class="form-input" value="{{applyGameNick}}" bindinput="onNickInput" placeholder="请输入游戏昵称" />
      </view>
      <view class="form-group">
        <text class="form-label">备注</text>
        <textarea class="form-textarea" value="{{applyRemark}}" bindinput="onRemarkInput" placeholder="区服、时间段等（请勿填写无关内容，违者封号）" maxlength="100" />
      </view>

      <!-- 指定考核官区域 -->
      <view class="form-group">
        <text class="form-label">指定考核官（可选）</text>
        <view wx:if="{{!canSpecify}}" class="kg-disabled-tip">
          <text>已有考核官：{{originalShenheguanId}}，不可重新指定</text>
        </view>
        <view wx:else>
          <view wx:if="{{!selectedKaoheguanInfo}}" class="select-kg-btn" bindtap="openKaoheguanModal">选择考核官</view>
          <view wx:else class="selected-kg-card">
            <image class="kg-avatar" src="{{selectedKaoheguanInfo.avatar}}" mode="aspectFill" />
            <view class="kg-info">
              <text class="kg-uid">UID {{selectedKaoheguanInfo.yonghuid}}</text>
              <text class="kg-nick" wx:if="{{selectedKaoheguanInfo.dashou_nick}}">{{selectedKaoheguanInfo.dashou_nick}}</text>
              <text class="kg-status {{selectedKaoheguanInfo.is_renzheng ? 'renzheng' : 'not-renzheng'}}">{{selectedKaoheguanInfo.is_renzheng ? '已认证' : '未认证'}}</text>
            </view>
            <view class="clear-kg-btn" bindtap="clearSelectedKaoheguan">取消</view>
          </view>
        </view>
      </view>

      <view wx:if="{{applyFee > 0}}" class="fee-info">本次需缴纳：<text class="fee-amount">¥{{applyFee}}</text></view>
      <view class="modal-actions">
        <view class="btn-cancel" bindtap="closeApplyModal">取消</view>
        <view class="btn-confirm" bindtap="submitApply">确认申请{{applyFee > 0 ? '并支付' : ''}}</view>
      </view>
    </view>
  </view>

  <!-- 考核官选择弹窗 -->
  <view wx:if="{{showKaoheguanModal}}" class="modal-mask" bindtap="closeKaoheguanModal">
    <view class="modal-dialog" catchtap="noop">
      <view class="modal-header">选择考核官</view>
      <view class="search-box">
        <input class="search-input" value="{{searchKeyword}}" bindinput="onSearchInput" placeholder="搜索用户ID或昵称" />
      </view>
      <scroll-view scroll-y="true" class="kg-scroll">
        <view wx:for="{{filteredKaoheguanList}}" wx:key="yonghuid" class="kg-item" data-kg="{{item}}" bindtap="selectKaoheguan">
          <image class="kg-avatar" src="{{item.avatar}}" mode="aspectFill" />
          <view class="kg-info">
            <text class="kg-uid">UID {{item.yonghuid}}</text>
            <text wx:if="{{item.dashou_nick}}" class="kg-nick">{{item.dashou_nick}}</text>
            <text class="kg-status {{item.is_renzheng ? 'renzheng' : 'not-renzheng'}}">{{item.is_renzheng ? '已认证' : '未认证'}}</text>
            <text class="kg-state {{item.shenhe_zhuangtai === 0 ? 'free' : 'busy'}}">{{item.shenhe_zhuangtai === 0 ? '空闲' : '忙碌'}}</text>
          </view>
        </view>
        <view wx:if="{{filteredKaoheguanList.length === 0}}" class="empty-tip">暂无符合条件的考核官</view>
      </scroll-view>
      <view class="modal-actions">
        <view class="btn-cancel" bindtap="closeKaoheguanModal">取消</view>
      </view>
    </view>
  </view>

  <global-notification id="global-notification" />
</view>