// pages/store/store.js
var app = getApp();
var WXAPI = require('../../utils/server.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    animation: {}, // 动画
    animation_mask: {}, // 动画
    showModalStatus: false, // modal是否需要渲染
    currentIndex: 0,
    navTab: [{'type':'推荐','status':'','num':0},
             {'type':'普通商品','status':'payment','num':0},
             {'type':'预售商品','status':'send','num':0},
             {'type':'商品分类','status':'receipt','num':0}],
    currentStatus: '',
    listArray: [],
    page: 1,
    shop: {},
    shop_name: '',
    shop_logo: '',
    goods_list: [],
    shop_info: {},
    type: 1,
    shopId: '',
    showModalStatus: false,
    animationData: '',
    buynum: 1,
    attrList: [],
    sizeid: '',
  },
  switchTab: function(e) {
    var dataset = e.currentTarget.dataset
    this.setData({
      currentIndex: dataset.idx,
      currentStatus: dataset.item.status,
      type: dataset.idx==2?4:dataset.idx==3?3:dataset.idx+1
    });
    this.reloadData(this.data.shopId);
  },
  // 数据加载
  reloadData: function(shopId) {
    var that = this;
    WXAPI.storeHome({
      accessId: app.globalData.accessId,
      storeId: 239,
      storeType: 1,
      shop_id: shopId,
      shop_list_id: '',
      type: that.data.type,
      latitude: 31.772752,
      longitude: 119.946973,
    }).then(res => {
      if (res.code == 200) {
        var data = res.data;
        that.setData({
          shop: data,
          shop_name: data.shop_name,
          shop_logo: data.shop_logo,
          shop_info: data.shop_list[0],
          goods_list: data.list,
        })
      }
    });
  },
  navToStore() {
    var info = this.data.shop_info
    wx.navigateTo({
      url: '/pages/store/myStore?shopId='+info.id,
    })
  },
  navToList: function (e) {
    // console.log(e)
    var that = this
    var info = e.currentTarget.dataset.info;
    wx.navigateTo({
      url: '/pages/listdetail/listdetail?id='+info.cid+'&title='+info.pname,
    })
  },
  navToDetail: function(e) {
    var info = e.currentTarget.dataset.info;
    wx.navigateTo({
      url: '/pages/product/detail?productId='+info.id,
    })
  },
  showAttribute: function(e) {
    var productId = e.currentTarget.dataset.id;
    var that = this;
    that.loadProductDetail(productId);
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    });
    this.animation = animation
    animation.translateY(300).step();
    this.setData({
      // paytype: type,
      animationData: animation.export()
    })
    if (e.currentTarget.dataset.status == 1) {
      this.setData({
        showModalStatus: true
      });
    }
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation
      })
      if (e.currentTarget.dataset.status == 0) {
        this.setData({
          showModalStatus: false
        });
      }
    }.bind(this), 200);
  },
  // 商品详情数据获取 
  loadProductDetail: function (productId) {
    var that = this;
    WXAPI.productIndex({
      accessId: app.globalData.accessId,
      storeId: 239,
      pro_id: productId,
      vipSource: 0
    }).then(res => {
      if (res.code == 200) {
        var data = res.data;
        var pro = data.pro;
        that.setData({
          itemData: pro,
          attrList: data.attrList,
          sizeid: data.attrList[0].attr[0].attrId,
        });
      }
    });
  },
  // 弹窗
  setModalStatus: function (e) {
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    });
    //定义点击的类型
    var type = e.target.dataset.type ? e.target.dataset.type : false;
    //控制两种不同显示方式 
    if (type) {
      this.setData({
        xefl: false,
      })
    } else {
      this.setData({
        xefl: true,
      })
      type = this.data.paytype;
    }
    this.animation = animation
    animation.translateY(300).step();
    this.setData({
      paytype: type,
      animationData: animation.export()
    })
    if (e.currentTarget.dataset.status == 1) {
      this.setData({
        showModalStatus: true
      });
    }
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation
      })
      if (e.currentTarget.dataset.status == 0) {
        this.setData({
          showModalStatus: false
        });
      }
    }.bind(this), 200)
  },
  // 加减
  changeNum: function (e) {
    var that = this;
    var num = that.data.itemData.num;
    if (e.target.dataset.alphaBeta == 0) {
      if (this.data.buynum <= 1) {
        wx.showToast({
          title: '不能再少了',
          icon: 'none',
          duration: 1000
        })
      } else {
        this.setData({
          buynum: this.data.buynum - 1
        })
      };
    } else {
      if (that.data.buynum < num) {
        this.setData({
          buynum: this.data.buynum + 1
        })
      } else {
        wx.showToast({
          title: '不能再多了',
          icon: 'none',
          duration: 1000
        })
      }
    };
  },
  /**
   * 规格属性点击事件
   */
  onChangeShowState: function (event) {
    var that = this;
    var listItem = this.data.attrList;
    var items = listItem[event.currentTarget.dataset.idx];
    var item = items.attr[event.currentTarget.dataset.index];
    if (!item.enable) {
      return;
    }
    var select = !item.select;
    for (var i = 0; i < items.attr.length; i++) {
      items.attr[i].select = false;
    }
    item.select = select;
    // 获取点击属性列表
    var canGetInfo = [];
    for (var skuIndex = 0; skuIndex < listItem.length; skuIndex++) {
      for (var skuIndexIn = 0; skuIndexIn < listItem[skuIndex].attr.length; skuIndexIn++) {
        if (listItem[skuIndex].attr[skuIndexIn].enable && listItem[skuIndex].attr[skuIndexIn].select) {
          canGetInfo.push(listItem[skuIndex].attr[skuIndexIn]);
        }
      }
    }
    var canGetInfoLog = "";
    var skuBeanList = this.data.skuBeanList;
    var haveSkuBean = [];
    // 对应库存清单扫描
    for (var skuBeanIndex = 0; skuBeanIndex < skuBeanList.length; skuBeanIndex++) {
      var iListCount = 0;
      for (var skuBeanIndexIn = 0; skuBeanIndexIn < skuBeanList[skuBeanIndex].attributes.length; skuBeanIndexIn++) {
        if (canGetInfo.length == skuBeanList[skuBeanIndex].attributes.length) {
          if (skuBeanList[skuBeanIndex].attributes[skuBeanIndexIn].attributeValId == canGetInfo[skuBeanIndexIn].id) {
            iListCount++;
          }
        } else {
          canGetInfoLog = "库存清单不存在此属性" + " ";
        }
      }
      if (iListCount == skuBeanList[skuBeanIndex].attributes.length) {
        haveSkuBean.push(skuBeanList[skuBeanIndex]);
      }
    }
    for (var iox = 0; iox < canGetInfo.length; iox++) {
      canGetInfoLog += canGetInfo[iox].attributeValue + " ";
    }
    if (haveSkuBean.length != 0) {
      //选中设置
      var itemData = that.data.itemData;
      itemData.photo_x = haveSkuBean[0].imgurl;
      itemData.price_yh = haveSkuBean[0].price;
      itemData.num = haveSkuBean[0].count;
      var choujiangid = that.data.choujiangid;
      var sizeid = haveSkuBean[0].cid;
      that.setData({
        itemData: itemData,
        sizeid: sizeid,
        choujiangid: choujiangid,
        value: canGetInfoLog
      });
    } else {
      that.setData({
        sizeid: '',
        value: ''
      });
    }
    // 重新赋值
    this.setData({
      attrList: listItem,
      infoText: canGetInfoLog,
    })
    // 重新sku运算
    this.onData();
  },
  //加入购物车
  addShopCart: function (e) {
    // console.log(e.currentTarget.dataset)
    var info = e.currentTarget.dataset.info;
    //添加到购物车
    var that = this;
    WXAPI.productAddCart({
      accessId: app.globalData.accessId,
      storeId: 239,
      storeType: 1,
      pro_id: info.pro_id,
      attribute_id: that.data.sizeid,
      num: that.data.buynum,
      type: 'addcart',
    }).then(res => {
      if (res.code == 200) {
        //设置购物车刷新
        app.d.purchase = 1;
        wx.showToast({
          title: '已添加到购物车',
          icon: 'success',
          duration: 2000
        });
        that.setData({
          showModalStatus: false
        });
      } else {
        wx.showToast({
          icon: 'loading',
          title: res.message,
          duration: 2000
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      shopId: options.shopId
    });
    this.reloadData(options.shopId);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})