// page/component/details/details.js
Page({
  onLoad: function (options) {
    const goods = {
      id: options.id,
      title: options.title,
      price: options.price,
      detail: options.detail,
      image: decodeURIComponent(options.image).split(','),
    };
    this.setData({ goods });
  },
  data: {
    goods: {},
    list: [
      '/image/beizi1.jpg',
      '/image/beizi1.jpg'
    ],
    num: 1,
    totalNum: 0,
    hasCarts: false,
    curIndex: 0,
    show: false,
    scaleCart: false,
    userInfo: {},
    interval: 3000,
    duration: 800,
  },

  onBuyClick() {
    const self = this;
    // 获取用户信息
    wx.getUserProfile({
      desc: '用于完善会员资料', // 提示信息，表明获取用户信息的用途
      success: (res) => {
        console.log('用户信息:', res.userInfo);

        // 将用户信息保存到data中（或发送给服务器）
        self.setData({
          userInfo: res.userInfo
        });

        // 构建订单信息的 URL
        const { id, title, image, price } = self.data.goods;
        const num = self.data.num;
        const url = `../orders/orders?id=${id}&title=${title}&image=${image[0]}&price=${price}&num=${num}`;

        // 跳转到订单页面
        wx.navigateTo({
          url: url,
        });
      },
      fail: (err) => {
        console.error('获取用户信息失败:', err);
      }
    });
  },
  addCount() {
    let num = this.data.num;
    num++;
    this.setData({
      num: num
    })
  },
  reduceCount() {
    let num = this.data.num;
    if (num > 1) {
      num--;
    }
    this.setData({
      num: num
    })
  },

  addToCart() {
    const self = this;
    const num = this.data.num;
    let total = this.data.totalNum;

    self.setData({
      show: true
    })
    setTimeout(function () {
      self.setData({
        show: false,
        scaleCart: true
      })
      setTimeout(function () {
        self.setData({
          scaleCart: false,
          hasCarts: true,
          totalNum: num + total
        })
      }, 200)
    }, 300)

  },

  bindTap(e) {
    const index = parseInt(e.currentTarget.dataset.index);
    this.setData({
      curIndex: index
    })
  }

})