// packageF/debt/component/sort/sort.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    debtId:{
      type:null
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    inputs: '',
    good_page: 1,
    order_field: '',
    order_by: '',
    is_hot: 0,
    cid: '',
    debtId: ''
  },
  lifetimes: {
    attached() {
      this.getGoodsData();
      console.log('attached')
    },
    moved() {
      console.log('moved')
    },
    detached() {
      console.log('detached')
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    goToDetail(e){
      let id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '/packageA/detail_v2/detail_v2?id='+id+'&debtId='+this.data.debtId,
      })
    },
    toggle(e) {
      let idx = e.currentTarget.dataset.idx;
      let order_field = '';
      switch (idx) {
        case 2:
          this.setData({
            hascolor: 2
          });
          if (this.data.colorshow == '2_2') {
            this.setData({
              colorshow: '2_1',
            });
            order_field = 'show_sales';
            this.setData({
              order_field: order_field,
              order_by: 'asc'
            });
          } else {
            this.setData({
              colorshow: '2_2',
            });
            order_field = 'show_sales';
            this.setData({
              order_field: order_field,
              order_by: 'desc'
            });
          }
          break;
        case 3:
          this.setData({
            hascolor: 3,
          });
          if (this.data.colorshow == '3_2') {
            this.setData({
              colorshow: '3_1'
            });
            order_field = 'price';
            this.setData({
              order_field: order_field,
              order_by: 'asc'
            });
          } else {
            this.setData({
              colorshow: '3_2'
            });
            order_field = 'price';
            this.setData({
              order_field: order_field,
              order_by: 'desc'
            });
          }
          break;
        case 5:
          this.setData({
            hascolor: 5,
          });
          if (this.data.colorshow == '5_2') {
            this.setData({
              colorshow: '5_1'
            });
            order_field = 'id';
            this.setData({
              order_field: order_field,
              order_by: 'asc'
            });
          } else {
            this.setData({
              colorshow: '5_2'
            });
            order_field = 'id';
            this.setData({
              order_field: order_field,
              order_by: 'desc'
            });
          }
          break;
      }
      this.getGoodsData();
    },
    getGoodsData() {
      let that = this;
      let urlStr = app.getNetAddresss("goods.goods.search-goods");
      // urlStr += '&page=' + this.data.good_page;
      // urlStr += '&order_field=' + this.data.order_field;
      // urlStr += '&order_by=' + this.data.order_by;
      // urlStr += '&search[plugin_id]=[-1,44,92]';
      let json = {
        page:this.data.good_page,
        order_field:this.data.order_field,
        order_by:this.data.order_by,
        "search[plugin_id]":[-1,44,92]
      };
      app._postNetWork({
        url: urlStr,
        data:json,
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              goodsArr: res.data.data,
              good_total_page: res.data.last_page,
              current_page: res.data.current_page
            });
            that.triggerEvent('getSort',{good_total_page:that.data.good_total_page,current_page:that.data.current_page})
          }
        },
        fail: function (res) {
          console.log(res.msg);
        }
      });
    },
    nexTgetGoodsData() {
      let urlStr = app.getNetAddresss("goods.goods.search-goods");
      // urlStr += '&page=' + (Number(this.data.good_page) + 1);
      // urlStr += '&order_field=' + this.data.order_field;
      // urlStr += '&order_by=' + this.data.order_by;
      // urlStr += '&search[plugin_id]=[-1,44,92]';
      let json = {
        page:(Number(this.data.good_page) + 1),
        order_field:this.data.order_field,
        order_by:this.data.order_by,
        "search[plugin_id]":[-1,44,92]
      };
      app._postNetWork({
        url: urlStr,
        data:json,
        success: ((resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            this.setData({
              goodsArr: this.data.goodsArr.concat(res.data.data),
              good_total_page: res.data.last_page,
              current_page: res.data.current_page
            });
            this.triggerEvent('getSort',{good_total_page:this.data.good_total_page,current_page:this.data.current_page})
          }
        }),
        fail: ((res) => {
          wx.showToast({
            title: res.msg,
          });
        })
      });
    }
  }
})