const { myBookings } = require("../../mock/photoBook");

Page({
  data: {
    list: myBookings,
  },

  findOrder(id) {
    return (this.data.list || []).find((item) => item.id === id);
  },

  onTapOrder(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/booking-detail/index?id=${id}`,
    });
  },

  onTapPrimary(e) {
    const id = e.currentTarget.dataset.id;
    const order = this.findOrder(id);
    if (!order) {
      return;
    }

    if (order.primaryText === "再次预约") {
      wx.navigateTo({
        url: "/pages/booking/index",
      });
      return;
    }

    if (!order.canContact) {
      return;
    }

    wx.showToast({
      title: "已复制摄影师联系方式",
      icon: "none",
    });
  },

  onTapSecondary(e) {
    const id = e.currentTarget.dataset.id;
    const list = this.data.list || [];
    const index = list.findIndex((item) => item.id === id);
    if (index === -1) {
      return;
    }

    const order = list[index];
    if (order.secondaryText === "取消预约" && order.canCancel) {
      wx.showModal({
        title: "确认取消",
        content: "取消后可重新预约，你确认要取消本次拍摄吗？",
        confirmColor: "#E5484D",
        success: (res) => {
          if (!res.confirm) {
            return;
          }

          const next = [...list];
          next[index] = {
            ...order,
            status: "已取消",
            statusType: "cancelled",
            statusReason: "你已取消该订单",
            canCancel: false,
            secondaryText: "已结束",
          };

          this.setData({ list: next });
          wx.showToast({
            title: "已取消预约",
            icon: "none",
          });
        },
      });
      return;
    }

    if (order.secondaryText === "改期申请") {
      wx.showToast({
        title: "改期申请已发起",
        icon: "none",
      });
    }
  },
});
