import I18nBehavior from "../../../../i18n/behavior";
import { getOfflineTreatmentListMethod } from "../../request";

Page({
  behaviors: [I18nBehavior],
  data: {
    i18n: {
      offlineTreatment: { title: '线下', ing: '进行中', finish: '完成' },
    },
    currentTab: "",
    treatmentList: [],
    isLoading: false,
    treatmentId: "",
  },
  async onLoad(options: any) {
    if (options.id) {
      this.setData({
        treatmentId: options.id,
      });
    }
    // await this.getOfflineTreatmentList("");
  },
  async onShow() {
    // 返回本页时要沿用当前 Tab 的筛选条件
    await this.getOfflineTreatmentList(this.data.currentTab ?? "");
  },
  // 跳转到核销记录
  onRecord(e: any) {
    const { item } = e.currentTarget.dataset;
    const { id, offlineId, conditioningProgramName, offlineDuration, itemImgSecond, estimatedStartDate } = item;
    const goodsInfo = {
      name: conditioningProgramName || '',
      duration: offlineDuration || 0,
      image: itemImgSecond || '',
      offlineId: offlineId || '',
      serviceTime: estimatedStartDate || '',
    }
    wx.setStorageSync('goodsInfo', JSON.stringify(goodsInfo));
    if (id) {
      wx.navigateTo({
        url: `/module/care/pages/care/verifyRecord?id=${id}`,
      });
    }
  },
  // 去预约
  goAppointment(e: any) {
    const { item } = e.currentTarget.dataset;
    const { offlineId, conditioningProgramName, offlineDuration, itemImgSecond, estimatedStartDate } = item;
    const goodsInfo = {
      name: conditioningProgramName || '',
      duration: offlineDuration || 0,
      image: itemImgSecond || '',
      offlineId: offlineId || '',
      serviceTime: estimatedStartDate || '',
    }
    if (offlineId) {
      wx.setStorageSync('goodsInfo', JSON.stringify(goodsInfo));
      // 去预约页面
      wx.navigateTo({
        url: `/module/order/pages/appointment/appointment`,
      });
    } else {
      wx.showToast({
        title: "线下服务ID不能为空",
        icon: "none",
      });
    }
  },
  onTabChange(e: any) {
    const progress = e.detail.value;
    this.setData({
      currentTab: progress,
    });
    this.getOfflineTreatmentList(progress);
  },
  // 获取线下非药物治疗记录列表
  async getOfflineTreatmentList(progress: string) {
    this.setData({ isLoading: true });
    try {
      const res = await getOfflineTreatmentListMethod(progress);
      const list = Array.isArray(res?.data) ? res.data : [];
      const treatmentId = this.data.treatmentId;
      if (treatmentId && list.length > 0) {
        const highlightedList = list.map((item: any) => ({
          ...item,
          isHighlighted: item.id === treatmentId || String(item.id) === String(treatmentId),
        }));
        // 将高亮的项目移到最前面
        const highlightedItem = highlightedList.find((item: any) => item.isHighlighted);
        const otherItems = highlightedList.filter((item: any) => !item.isHighlighted);
        const sortedList = highlightedItem ? [highlightedItem, ...otherItems] : highlightedList;

        this.setData({ treatmentList: sortedList, isLoading: false });
      } else {
        // 只有在数据加载完成后才更新列表
        this.setData({ treatmentList: list, isLoading: false });
      }
    } catch (error: any) {
      wx.showToast({
        title: error.errMsg || "获取数据失败",
        icon: "none",
      });
      this.setData({ treatmentList: [], isLoading: false });
    }
  },
});
