// pages/member/myEvaluation/myEvaluation.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    language: '',
    selected: 0,
    //待评价
    wait: [],
    waitLoadMore: false,
    waitPage: 1,
    waitLastPage: 0,
    //已评价
    comment: [],
    commentLoadMore: false,
    commentPage: 1,
    commentLastPage: 0,
    //其他
    other: []
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {

  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en});
  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {
    this._initData();
    this._getNetData(this.data.selected);
  },

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

  },

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

  },

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

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {
    this.getMoreData();
  },

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

  },
  _getNetData(status) {
    let that = this;
    let urlStr = app.getNetAddresss("order.my-comment.paging");
    if(app.globalData.store_alone_temp == 1) {
      // 开启门店独立模块
      urlStr = app.getNetAddresss("plugin.store-alone-temp.frontend.member.my-comment");
    }
    urlStr += '&page=' + (status == '0' ? this.data.waitPage : this.data.commentPage);
    urlStr += '&status=' + status;
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var response = resdata.data;
        if (response.result == 1) {
          if (status == 0) {
            that.setData({
              wait: (response.data.list.data.length > 0) ? response.data.list.data : [],
              waitLoadMore: true,
              waitLastPage: response.data.list.last_page ? response.data.list.last_page : 0,
            });
          } else if (status == 1) {
            that.setData({
              comment: (response.data.list.data.length > 0) ? response.data.list.data : [],
              commentLoadMore: true,
              commentLastPage: response.data.list.last_page ? response.data.list.last_page : 0,
            }); 
          }
        }
        // var res = resdata.data;
        // if (res.result == 1) {
        //   var myData = res.data;
        //   if (status == 0) {
        //     that.setData({
        //       wait: myData.list
        //     });
        //   } else if (status == 1) {
        //     that.setData({
        //       comment: myData.list
        //     });
        //   } else {
        //     that.setData({
        //       other: myData.list
        //     });
        //   }
        // }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },

  // 加载更多
  getMoreData () {
    let that = this;
    let urlStr = app.getNetAddresss("order.my-comment.paging");
    let selected = this.data.selected;
    let typePr = selected == '0' ? 'wait' : 'comment';
    if (this.data[`${typePr}Page`] >= this.data[`${typePr}LastPage`]) {
      return false;
    }
    this.setData({
      [`${typePr}LoadMore`]: false,
      [`${typePr}Page`]: this.data[`${typePr}Page`] + 1
    });
    urlStr += '&page=' + this.data[`${typePr}Page`];
    urlStr += '&status=' + selected;
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var response = resdata.data;
        that.setData({
          [`${typePr}LoadMore`]: true
        });
        if (response.result == 1) {
          let afterArr = that.data[typePr];
          that.setData({
            [`${typePr}`]: afterArr.concat(response.data.list.data)
          });
        } else {
          that.setData({
            [`${typePr}Page`]: that.data[`${typePr}Page`] - 1,
            [`${typePr}LoadMore`]: false
          });
          return;
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });

  },
  //评价
  toComment(e) {
    let order_id = e.target.dataset.itemid;
    let good = e.target.dataset.good;
    //evaluate
    wx.navigateTo({
      url: '/packageD/member/evaluate/evaluate?order_id=' + order_id + '&id=' + good.id
    });

  },
  opration(e) {
    let btn = e.target.dataset.btn;
    let item = e.target.dataset.good;
    //value-1 追加评价value-2 查看评价详情
    if (btn && btn.value == 1) {
      //addevaluate
      wx.navigateTo({
        url: '/packageD/member/addevaluate/addevaluate?order_id=' + item.order.id + '&item=' + JSON.stringify(item)
      });
    } else {
      //CommentDetails
      let options = `order_id=${item.has_one_comment.order_id}&goods_id=${item.has_one_comment.goods_id}&comment_id=${item.has_one_comment.id}&order_goods_id=${item.id}`
      wx.navigateTo({
        url: '/packageD/member/CommentDetails/CommentDetails?' + options
      });

    }
  },
  _initData() {
    this.setData({
      selected: 0,
      //待评价
      wait: [],
      waitLoadMore: false,
      waitPage: 1,
      waitLastPage: 0,
      //已评价
      comment: [],
      commentLoadMore: false,
      commentPage: 1,
      commentLastPage: 0,
      //其他
      other: []
    });
  },
  swichTabTItem(e) {
    let index = e.detail.index;
    this.setData({
      selected: index
    });
    if (this.data.selected == 0) {
      this.setData({
        wait: [],
        waitLoadMore: false,
        waitLastPage: 0,
        waitPage: 1
      });
    } else {
      this.setData({
        comment: [],
        commentLoadMore: false,
        commentPage: 1,
        commentLastPage: 0,
      });
    }
    this._getNetData(this.data.selected);
  },
});
