Page({
  data: {
    goods: {},
    specList: [],
    selectedSpecs: [], // 用户选择的规格
    buyNum: 1, // 购买数量
    showPopup: false, // 控制弹出层显示
    userInfo: {}, // 用户信息
    viewGood: "",
  },

  onLoad(options) {
    const goods = {
      id: options.id,
      goodName: options.goodName,
      goodPrice: options.goodPrice,
      goodDetail: options.goodDetail,
      goodImage: decodeURIComponent(options.goodImage).split(','),
    };
    const specList = [
      {
        specId: 1,
        title: "枕头类型",
        specValueList: [
          { specValueId: 1, specValue: "普通蓬松枕", isSelected: false, goodImage: "/image/zhentou1.jpg" },
          { specValueId: 2, specValue: "压缩高级枕", isSelected: false, goodImage: "/image/zhentou2.jpg" }
        ]
      },
      {
        specId: 2,
        title: "枕被套 、床单颜色",
        specValueList: [
          { specValueId: 3, specValue: "贝诺灰", isSelected: false, goodImage: "/image/beizi2.jpg" },
          { specValueId: 4, specValue: "开心日记", isSelected: false, goodImage: "/image/beizi3.jpg" },
          { specValueId: 5, specValue: "草莓熊", isSelected: false, goodImage: "/image/beizi4.jpg" },
          { specValueId: 6, specValue: "乖乖狗", isSelected: false, goodImage: "/image/beizi5.jpg" },
          { specValueId: 7, specValue: "蓝心小熊", isSelected: false, goodImage: "/image/beizi6.jpg" },
          { specValueId: 8, specValue: "初夏-紫", isSelected: false, goodImage: "/image/beizi7.jpg" }
        ]
      }
    ];
    this.setData({ goods, specList, viewGood: goods.goodImage[0] });
  },

  openPopup() {
    this.setData({ showPopup: true });
  },

  handlePopupHide() {
    this.setData({ showPopup: false });
  },

  selectSpec(e) {
    const { specid, id } = e.currentTarget.dataset;
    let specList = this.data.specList.map(spec => {
      if (spec.specId === specid) {
        spec.specValueList = spec.specValueList.map(item => {
          item.isSelected = item.specValueId == id;
          return item;
        });
      }
      return spec;
    });
    this.setData({ specList, viewGood: e.currentTarget.dataset.goodimage });
    this.updateSelectedSpecs();
  },
  goToHome() {
    wx.switchTab({
      url: '/page/component/index' // 使用switchTab跳转到首页（tabbar页面）
    });
  },
  updateSelectedSpecs() {
    const selectedSpecs = this.data.specList.map(spec => {
      const selectedItem = spec.specValueList.find(item => item.isSelected);
      return selectedItem ? selectedItem.specValue : null;
    }).filter(item => item);
    this.setData({ selectedSpecs });
  },

  handleBuyNumChange(e) {
    this.setData({
      buyNum: e.detail.value
    });
  },

  confirmSpecs() {
    console.log(this.data.selectedSpecs)
    if (this.data.selectedSpecs.length < this.data.specList.length) {
      wx.showToast({
        title: '请选择所有规格',
        icon: 'none'
      });
      return;
    }

    this.setData({ showPopup: false });

    // wx.getUserProfile({
    //   desc: '用于完善会员资料',
    //   success: (res) => {
    // this.setData({ userInfo: res.userInfo });

    const { id, goodName, goodImage, goodPrice } = this.data.goods;
    const num = this.data.buyNum;
    const url = `../orders/orders?id=${id}&title=${goodName}&image=${goodImage[0]}&price=${goodPrice}&num=${num}&specs=${this.data.selectedSpecs.join(',')}`;

    wx.navigateTo({ url });
    //   },
    //   fail: (err) => {
    //     wx.showToast({
    //       title: '获取用户信息失败',
    //       icon: 'none'
    //     });
    //   }
    // });
  }
});
