var ut = require("../../../../utils/util.js");
var t = function(t) {
    return t && t.__esModule ? t : {
      default: t
    };
  }(require("../../../../utils/LoadMore.js")),
  e = getApp(),
  rq = e.request,
  a = new t.default(),
  s = require("../../../../utils/util.js"),
  oo = e.globalData,
  os = e.globalData.setting;
var regeneratorRuntime = require('../../../../utils/runtime.js');



Page({
  data: {
    url: e.globalData.setting.url,
    resourceUrl: e.globalData.setting.imghost,
    iurl: e.globalData.setting.imghost,
    categories: [{
      name: "全部评价",
      status: 2
    }, {
      name: "待评价",
      status: 0
    }, {
      name: "已评价",
      status: 1
    }],
    activeStatus: 0,
    comments: [],
    currentPage: 1,
    isloaded: 0,
    order_id:0,
    is_no_more:0,
  },
	
  onLoad: function(t) {
    var e = void 0 === t.status ? this.data.activeStatus : t.status;
    this.data.order_id=t.order_id;
		this.setData({
			options: t,
		});
    //a.init(this, "", "comments"),
    //this.requestComments(e);
    this.requestComments_new(e);
  },
	
  onShow: function() {
    wx.getStorageSync("user:comment:update") && (wx.setStorageSync("user:comment:update", !1),
      this.resetData(), this.requestComments_new(this.data.activeStatus));
  },
	
  changeTab: function(t) {
    var e = this;
    if (this.data.activeStatus == t.currentTarget.dataset.status)
      return false;
    this.resetData(), this.requestComments_new(t.currentTarget.dataset.status);
  },

  requestComments_new:async function(t){
        var e = this, th = e;
				let n = '';
				if(this.data.options.card == 1) { // 卡项订单
					n = os.url + '/api/weshop/serviceComment/list?page=' + this.data.currentPage;
				} else {
					n = os.url + '/api/weshop/comment/list?page=' + this.data.currentPage;
				};
				
        this.setData({
            activeStatus: t, // t为tabIndex
        });

        if(this.data.order_id) n+="&order_id="+e.data.order_id;
        var rq_data={store_id: os.stoid, user_id: oo.user_id};
        if (t!= 2) {rq_data['is_comment']=t;}

        var rs_data=null,com_data=this.data.comments;
        await getApp().request.promiseGet(n,{data:rq_data}).then(res=>{
            e.data.currentPage++;
            if(res.data.data && res.data.data.pageData && res.data.data.pageData.length>0){
                rs_data=res.data.data.pageData;
            }else{
                th.setData({is_no_more: 1, comments: null});
            };
						if(res.data.data && res.data.data.total <= res.data.data.pageSize) {
							// console.log('res======>>>', res.data.data);
							th.setData({ is_no_more: 1, });
						};
        })

        if(rs_data){
            for(var ind in rs_data ){
                var em=rs_data[ind]
                await getApp().request.promiseGet("/api/weshop/comment/pageComment", {
                    data:{ store_id: os.stoid, goods_id: em.goods_id,
                        pageSize: 3,  parent_id: em.comment_id, page:1}
                }).then(res=>{
                    if(res.data.data && res.data.data.pageData && res.data.data.pageData.length>0)
                        rs_data[ind].replay_list=res.data.data.pageData;
                })
                if (em.img != undefined && em.img != null && ut.isString(em.img)) rs_data[ind].img = ut.unserialize(em.img);
                var str = em.weapp_img;
                if (str != "" && ut.isString(str)) rs_data[ind].weapp_img = JSON.parse(str);
                rs_data[ind].original_img=this.data.iurl+rs_data[ind].original_img;
            }

            if(!com_data) com_data=rs_data;
            else com_data=  com_data.concat(rs_data);
            th.setData({ comments: com_data })		
        }
		
		th.setData({isloaded: 1,});
    },

  onReachBottom: function() {
    if(this.data.is_no_more) return false;
    //a.canloadMore() && this.requestComments(this.data.activeStatus);
    this.requestComments_new(this.data.activeStatus);
  },


  onPullDownRefresh: function(t) {
    this.resetData(), this.requestComments_new(this.data.activeStatus);
  },
  resetData: function() {
    this.data.comments = null, this.data.currentPage = 1, this.data.is_no_more=0, a.resetConfig();
  },
  /*-----评论-----*/
  comment: function(t) {
    for (var e = t.currentTarget.dataset.recid, a = this.data.comments, s = 0; s < a.length && a[s].rec_id != e; s++);
    if (!(s >= a.length)) {
        var n = "?order_id=" + a[s].order_id;
        n += "&goods_id=" + a[s].goods_id, n += "&price=" + a[s].goods_price;
        wx.navigateTo({
          url: "/packageB/pages/user/add_comment/add_comment" + n
        });
    }
  },

  //图片失败，默认图片
  bind_bnerr2: function(e) {
    var th = this;
    var _errImg = e.currentTarget.dataset.errorimg;
    var _errObj = {};
    _errObj[_errImg] = os.imghost + "/public/images/default_goods_image_240.gif";
    th.setData(_errObj) //注意这里的赋值方式,只是将数据列表中的此项图片路径值替换掉 ;  
  },
	
  //预览图片
  previewImg: function(e) {
    var index = e.currentTarget.dataset.index;
    var sindex = e.currentTarget.dataset.sindex;
    var name = e.currentTarget.dataset.name;
    var imgArr = this.data.comments;
    var image = null;
    if (name == "img") {
      image = imgArr[index].img[sindex];
    } else {
      image = imgArr[index].weapp_img[sindex];
    }
    getApp().globalData.no_clear=1;
    wx.previewImage({
      current: image,
      //当前图片地址
      urls: imgArr[index].weapp_img, //所有要预览的图片的地址集合 数组形式
    })
  },
  //跳到首页
  goto: function(ee) {
    wx.navigateTo({
      url: '/pages/index/index/index',
    })
  },
   go_url(e){
        var url=e.currentTarget.dataset.url;
        getApp().goto(url);
    }

});