import { Post } from "../../../../lib/request/method";
import { cancelAppointmentMethod } from "../../request";
Page({
  data: {
    id: 0,
    recordListId: '',
    recordList: [],
    showAppointmentSuccess: true,
    //是否显示修改预约按钮
    isShowUpdateButton: false,
    //isShowCancleButton
    isShowCancleButton: false,
    qrCodeUrl: "",
    appointmentInfo: {
      operateTime: '',
      operateDuration: '',
      conditioningProgramSupplierName: '',
      cpSupplierDetailAddress: '',
      cpSupplierPhone: '',
      cpSupplierQrCode: '',
    },
    showAppointmentInfo: false,
  },
  onLoad(options: any) {
    if (options.id) {
      this.setData({
        id: options?.id
      })
    }
  },
  onShow() {
    if (this.data.id) {
      this.getVerifyRecordList(this.data.id);
    }
  },
  // 生成二维码
  generateQRCode(qrCodeUrl: any) {
    this.setData({
      qrCodeUrl
    });
  },
  // 获取核销记录列表
  async getVerifyRecordList(id: number) {
    try {
      const res = await Post(
        `/patientCrManage/pofflineCp/${id}`,
        {},
        {
          transform({ data }: any) {
            return data;
          },
        }
      );
      if (res && res.patientConditioningSwagItemOffline && JSON.stringify(res.patientConditioningSwagItemOffline) !== '{}') {
        this.setData({
          showAppointmentInfo: true,
          isShowUpdateButton: res.isShowUpdateButton,
          isShowCancleButton: res.isShowCancleButton,
          recordListId: res.patientConditioningSwagItemOffline.id,
          appointmentInfo: res.patientConditioningSwagItemOffline,
        });
        this.generateQRCode(this.data.appointmentInfo.cpSupplierQrCode);
      } else {
        this.setData({
          showAppointmentInfo: false,
        });
      }
      if (res && res.patientConditioningSwagItemOfflines && res.patientConditioningSwagItemOfflines.length > 0) {
        this.setData({
          recordList: res.patientConditioningSwagItemOfflines,
        });
      } else {
        this.setData({
          recordList: [],
        });
      }
    } catch (error) {
      console.error("核销记录列表", error);
    }
  },
  // 去评价
  onReview(e: WechatMiniprogram.TouchEvent) {
    const { evaluateTime } = e.currentTarget.dataset.goods;
    if (evaluateTime) {
      // 线下服务评价详情
      wx.navigateTo({
        url: `/module/order/pages/offline-evaluateDetail/offline-evaluateDetail?goodsInfo=${encodeURIComponent(JSON.stringify(e.currentTarget.dataset.goods))}`,
      });
    } else {
      // 去线下服务评价
      wx.navigateTo({
        url: `/module/order/pages/offline-evaluate/offline-evaluate?goodsInfo=${encodeURIComponent(JSON.stringify(e.currentTarget.dataset.goods))}`,
      });

    }
  },

  onCancelAppointment() {
    wx.showModal({
      title: "取消预约",
      content: "预约取消后无法恢复！",
      showCancel: true,
      confirmText: "确定",
      cancelText: "再想想",
      success: async (res) => {
        if (!res.confirm) return;
        try {
          wx.showLoading({ title: "取消中" });
          await cancelAppointmentMethod(Number(this.data.recordListId || 0));
          wx.hideLoading();
          wx.showToast({ title: "取消成功", icon: "success" });
          // 取消成功之后到取消预约页面
          setTimeout(() => {
            const appointmentInfoParam = encodeURIComponent(
              JSON.stringify(this.data.appointmentInfo || {})
            );
            wx.navigateTo({
              url: `/module/care/pages/cancelAppointment/cancelAppointment?appointmentInfo=${appointmentInfoParam}`,
            });
          }, 300);
        } catch (error: any) {
          wx.hideLoading();
          wx.showToast({
            title: error?.errMsg || "取消失败",
            icon: "none",
          });
        }
      },
    });
  },

  onModifyAppointment() {
    const arrangeTime =
      (this.data.appointmentInfo as any).arrangeTime ||
      (this.data.appointmentInfo as any).appointmentTime ||
      "";

    wx.navigateTo({
      url: `/module/order/pages/appointment/appointment?mode=edit&arrangeTime=${encodeURIComponent(arrangeTime)}`,
    });
  },

});
