import {get} from '../../../utils/http'
Page({
  data: {
    moveBottom: {}, //打开盒子动画
    moveBack: {}, //关闭盒子动画
    animationIndex: 5, //活动动画下标
    animationStatus: [false, false, false, false], //盒子状态
    giftBagList: [], //礼包商品列表
  },
  onLoad() {
    // 获取礼包列表
    this.getGiftBagList()
  },
  // 打开盒子
  moveBottomFun() {
    var moveBottom = wx.createAnimation({
      transformOrigin: "50% 50%",
      duration: 1000,
      timingFunction: "ease",
      delay: 0
    })
    moveBottom.translateY(-120).step()
    this.setData({
      moveBottom: moveBottom.export(),
    })
  },
  // 关闭盒子
  moveBackFun() {
    var moveBack = wx.createAnimation({
      transformOrigin: "50% 50%",
      duration: 1000,
      timingFunction: "ease",
      delay: 0
    })
    moveBack.translateY(0).step()
    this.setData({
      moveBack: moveBack.export(),
    })
  },
  // 执行盒子动画
  openAnimations(e) {
    let index = e.currentTarget.dataset.index
    this.setData({
      animationIndex: index
    })
    if (this.data.animationStatus[index]) {
      this.moveBackFun()
    } else {
      this.moveBottomFun()
    }
  },
  // 动画结束时执行
  transitionend(e) {
    let index = e.currentTarget.dataset.index
    let animationStatus = `animationStatus[${index}]`
    this.setData({
      [animationStatus]: this.data.animationStatus[index] ? false : true,
      moveBack: {},
      moveBottom: {}
    })
  },
  // 获取圣诞礼包列表
  getGiftBagList() {
    get('v2/api/package/list', {}, (res => {
      if (res.code == 200) {
        this.setData({
          giftBagList: res.data.list
        })
      }
    }))
  },
  // 获取礼包详情
  getGiftBagDetail(id) {
    get('v2/api/package/info', {
      id
    }, (res => {
      let falg = false
      res.data.items.forEach(item => {
        if (item.type == 2) {
          falg = true
        }
      })
      // 判断是否有需要选规格的产品
      if (falg) {
        wx.navigateTo({
          url: `/subPackagesE/pages/giftBagSize/giftBagSize?id=${id}`,
        })
      } else {
        let selectGiftList = []
        res.data.items.forEach(item => {
          item.goods_list.forEach(itemA => {
            let list = {}
            list.item_id = item.id
            list.goods_id = itemA.id
            list.num = 1
            list.goods_name = itemA.goods_name
            list.cover_url = itemA.cover_url
            selectGiftList.push(list)
          })
        })
        wx.navigateTo({
          url: `/subPackagesE/pages/giftBagOrder/giftBagOrder?giftID=${id}&&productList=${JSON.stringify(selectGiftList)}`,
        })
      }
      console.log(res);
    }))
  },
  // 去购买
  goShopping(e) {
    let index = e.currentTarget.dataset.index
    let id = this.data.giftBagList[index].id
    this.getGiftBagDetail(id)
  }
})