const Parse = getApp().Parse
const company = getApp().globalData.company
var Nova = getApp().Nova
Component({
  /**
   * 组件的属性列表
   */
  properties: {

    options: null,
    bgColor: {
      type: String,
      value: '#009de1'
    },
    active: {
      type: Number,
      value: 1
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    activeItem: 0,

  },
  ready: function () { // 在组件布局完成后执行，确保options参数中有data信息
    this.getCategory()
  
  },
  /**
   * 组件的方法列表
   */
  methods: {
    async changeCate(e) {
      console.log(e);
      let {
        id,
        index
      } = e.currentTarget.dataset
      console.log(id);
      let query = new Parse.Query("ShopGoods")
      query.equalTo("category", id)
      query.equalTo("company", company)
      let res = await query.find()
      let goods = res.reduce((arr, item) => {
        let req = item.toJSON()
        arr.push(req)
        return arr
      }, [])
      console.log(goods);
      this.setData({
        goods,
        activeItem:index
      })
    },
  
    async getCategory() {
      let cate = new Parse.Query("Category")
      cate.equalTo("company", company)
      cate.equalTo("type", "shop")
      let res = await cate.find()
      let categorys = res.reduce((arr, item) => {
        let req = item.toJSON()
        arr.push(req)
        return arr
      }, [])
      console.log(categorys);
      this.setData({
        categorys
      })
      let e = {
        currentTarget: {
          dataset: {
            id: categorys[0].objectId && categorys[0].objectId,
            index:0
          }
        }
      }
      console.log(e);
      categorys.length > 0 && this.changeCate(e)
    },
    goNovaURL: function (ev) {
      let url = ev.currentTarget.dataset.url;
      Nova.goNovaURL(url);
    }
  }
})
