// pages/kaohe_jilu/kaohe_jilu.js
const app = getApp();
import request from '../../utils/request.js';

Page({
  data: {
    // 板块筛选
    moduleList: [],
    xuanzhongBankuaiId: 0,        // 0代表全部
    // 状态筛选：0-全部 1-审核中 2-已通过 3-未通过
    statusFilter: 0,
    // 记录列表
    recordList: [],
    page: 1,
    pageSize: 5,
    hasMore: true,
    isLoading: false,
    isLoadingMore: false,
    // 展开的记录ID集合（字符串数组）
    expandedIds: [],
    // OSS地址
    ossImageUrl: app.globalData.ossImageUrl || '',
  },

  onLoad() {
    this.loadModules();
  },

  // 加载板块（复用审核大厅接口）
  async loadModules() {
    try {
      const res = await request({ url: '/dengji/shdthqmk', method: 'POST' });
      if (res && res.data.code === 0) {
        const modules = res.data.data.modules || [];
        this.setData({ moduleList: modules, xuanzhongBankuaiId: 0 });
        this.loadRecords(true);
      } else {
        wx.showToast({ title: '加载模块失败', icon: 'none' });
      }
    } catch (e) {
      wx.showToast({ title: '网络错误', icon: 'none' });
    }
  },

  // 选择板块
  selectModule(e) {
    const bankuaiId = e.currentTarget.dataset.id;
    if (bankuaiId === this.data.xuanzhongBankuaiId) return;
    this.setData({
      xuanzhongBankuaiId: bankuaiId,
      recordList: [],
      page: 1,
      hasMore: true,
      expandedIds: []
    });
    this.loadRecords(true);
  },

  // 选择状态
  selectStatus(e) {
    const status = e.currentTarget.dataset.status;
    if (status === this.data.statusFilter) return;
    this.setData({
      statusFilter: status,
      recordList: [],
      page: 1,
      hasMore: true,
      expandedIds: []
    });
    this.loadRecords(true);
  },

  // 加载历史记录
  async loadRecords(isRefresh = false) {
    if (this.data.isLoading) return;
    const page = isRefresh ? 1 : this.data.page;
    if (!isRefresh && !this.data.hasMore) return;

    this.setData({ isLoading: true, isLoadingMore: !isRefresh });

    try {
      const res = await request({
        url: '/dengji/khghqkhjl',
        method: 'POST',
        data: {
          bankuai_id: this.data.xuanzhongBankuaiId || undefined,
          status: this.data.statusFilter || undefined,
          page: page,
          page_size: this.data.pageSize
        }
      });

      this.setData({ isLoading: false, isLoadingMore: false });

      if (res && res.data.code === 0) {
        const list = res.data.data.list || [];
        const hasMore = res.data.data.has_more;
        // 拼接头像
        const oss = this.data.ossImageUrl;
        const processedList = list.map(item => {
          let avatar = item.shenqingren_avatar || '';
          if (avatar && !avatar.startsWith('http')) {
            avatar = oss + avatar;
          }
          return { ...item, shenqingren_avatar: avatar };
        });
        const newList = isRefresh ? processedList : [...this.data.recordList, ...processedList];
        this.setData({ recordList: newList, page: page + 1, hasMore: hasMore });
      } else {
        wx.showToast({ title: '加载记录失败', icon: 'none' });
      }
    } catch (e) {
      this.setData({ isLoading: false, isLoadingMore: false });
      wx.showToast({ title: '网络错误', icon: 'none' });
    }
  },

  // 上拉加载更多
  onReachBottom() {
    if (this.data.hasMore && !this.data.isLoading && !this.data.isLoadingMore) {
      this.loadRecords(false);
    }
  },

  // 展开/收起详情
  toggleExpand(e) {
    const jiluId = e.currentTarget.dataset.id;
    // 确保 jiluId 是字符串
    const id = String(jiluId);
    const expandedIds = this.data.expandedIds.slice();  // 拷贝
    const index = expandedIds.indexOf(id);
    if (index > -1) {
      expandedIds.splice(index, 1);
    } else {
      expandedIds.push(id);
    }
    this.setData({ expandedIds });
  },

  // 去评判（跳转考核打分页）
  goJudge(e) {
    const item = e.currentTarget.dataset.item;
    if (!item) return;
    wx.navigateTo({ url: '/pages/kaohe_dafen/kaohe_dafen?jilu_id=' + item.jilu_id });
  },

  // 阻止冒泡
  noop() {}
});