var t = getApp(), oo = t.globalData, a = t.globalData.setting, os = a, e = t.request, s = require("../../../../utils/util.js"),ut=s,
  i = require("../../../../utils/selectFiles.js");
var regeneratorRuntime = require('../../../../utils/runtime.js');

Page({
  data: {
    url: a.url,
    resourceUrl: a.resourceUrl,
    iurl: a.imghost,
    defaultPhoto: a.resourceUrl + "/static/images/camera.png",
    uploadPath: [],  //上传后文件路径
    maxWord: 0,
    is_anonymous: !1,
    goods_rank: 0,
    service_rank: 0,
    deliver_rank: 0,
    content: "",
    options: null,
    isLongPress: !1,
    userinfo: null,
    imgs: [],        //本地文件再上传时的路径
    lenMore:0,
    goods_id: "", //商品id
    order_id: "", //订单id
  },
  onLoad: async function (a) {
    var th = this;
		if(a.card == 1) {
			wx.setNavigationBarTitle({
				title: '评价卡项',
			});
			// console.log('card==1');
			await getApp().request.promiseGet('/api/weshop/recharge/user/servicelist/list',{ 
				data:{
					store_id:os.stoid,
					card_id:a.goods_id,
					order_id:a.order_id,
				},
			}).then(res => {
				// console.log('card===1', res.data);
				if(res.data.code == 0) {
					a.img = th.data.iurl + res.data.data[0].img_url;
					a.goods_name = res.data.data[0].service_name;
					a.goods_num = res.data.data[0].qty;
				};
			  // if(ut.ajax_ok(res)) {
			  //     a.img = th.data.iurl + res.data.data.pageData[0].original_img;
			  //     a.goods_name = res.data.data.pageData[0].goods_name;
			  //     a.goods_num = res.data.data.pageData[0].goods_num;
			  // }
			});
		} else {
			wx.setNavigationBarTitle({
				title: '评价宝贝',
			});
			// console.log('card==0');
			await getApp().request.promiseGet("/api/weshop/ordergoods/pageGoods", { data:{store_id:os.stoid,goods_id:a.goods_id, order_id:a.order_id}}).then(res => {
			
			  if(ut.ajax_ok(res)) {
			      a.img = th.data.iurl + res.data.data.pageData[0].original_img;
			      a.goods_name = res.data.data.pageData[0].goods_name;
			      a.goods_num = res.data.data.pageData[0].goods_num;
			  }
			})
		};
    
    this.setData({ options: a });
    t.getUserInfo(function (e) {
      console.log(e);
      th.setData({userinfo: e})
    })
    th.setData({
      goods_id: a.goods_id,
      order_id: a.order_id
    })
  },
  keyUpChangeNum: function (t) {
    this.setData({
      maxWord: t.detail.value.length
    }), this.data.content = t.detail.value;
  },
  checkAnonymous: function () {
    this.setData({
      is_anonymous: !this.data.is_anonymous
    });
  },
  checkGoodsRank: function (t) {
    this.setData({
      goods_rank: parseInt(t.currentTarget.dataset.i) + 1
    });
  },
  checkServiceRank: function (t) {
    this.setData({
      service_rank: parseInt(t.currentTarget.dataset.i) + 1
    });
  },
  checkDeliverRank: function (t) {
		this.setData({
			deliver_rank: parseInt(t.currentTarget.dataset.i) + 1
		});
  },
  selectPhotos: function (t) {
    if (this.data.isLongPress) this.data.isLongPress = !1; else {
      var a = this;
      i.selectPhotos(this.data.filePaths, t.currentTarget.dataset.idx, function (t) {
        a.setData({
          filePaths: t
        });
      });
    }
  },
  removePhoto: function (t) {
    var a = this;
    this.data.isLongPress = !0, i.removePhoto(this.data.filePaths, t.currentTarget.dataset.idx, function (t) {
      a.setData({
        filePaths: t
      });
    });
  },
  submitComment: function (i) {
    var a = this;
		let url = '';
		if(this.options.card == 1) {
			this.setData({
				deliver_rank: 5
			});
			if (!a.data.service_rank || !a.data.goods_rank)
			    return getApp().my_warnning("请先打分",0,a);
		} else {
			if (!a.data.service_rank || !a.data.goods_rank || !a.data.deliver_rank)
			    return getApp().my_warnning("请先打分",0,a);
		};
		
		
    
    // if(a.data.content=="")
    //   return getApp().my_warnning("请填写评价内容",0,a);

    //this.uploadPhotos(function () {
      var p_data= {
        email: ' ',
        is_show: 1,
        goods_rank: a.data.goods_rank,
        service_rank: a.data.service_rank,
        deliver_rank: a.data.deliver_rank,
        content: a.data.content,
        goods_id: a.data.options.goods_id,
        order_id: a.data.options.order_id,
        user_id: oo.user_id,
        store_id: os.stoid,
        username: a.data.userinfo.nickname,
        source_type:1
      }
      if(a.data.uploadPath){
        p_data['weapp_img']=JSON.stringify(a.data.uploadPath);
      }
      if(a.data.is_anonymous){
        p_data['is_anonymous']=1;
      }
			if(this.data.options.card == 1) {
				// delete p_data.goods_id;
				p_data['service_id'] = this.data.options.goods_id;
				url = '/api/weshop/serviceComment/savecomment';
			} else {
				url = '/api/weshop/comment/savecomment';
			};
			

      e.post(url, {
        data:p_data,
        success: function (res) {
          if(res.data.code==0){
          wx.setStorageSync("user:comment:update", !0), t.showSuccess("评论成功", function () {
            // wx.navigateBack();
            var url = i.currentTarget.dataset.url;
            getApp().goto(url);
          });
          }else{
            getApp().my_warnning(res.data.msg,0,a);
          }
        }
      });
    //});
  },


  uploadPhotos: function (t, a) {
    if (0 == this.data.imgs.length) return t();
    if ("number" != typeof a) a = 0; else if (a >= this.data.imgs.length) return t();
    var s = this;
    e.uploadFile("/api/weshop/comment/uploadCommentImg", {
      filePath: s.data.imgs[a],
      name: "file",
      success: function (e) {
        console.log("uploadPhotos");
        console.log(e.data);
        if(e.data.code==0)
           s.data.uploadPath[a] = e.data.data, s.uploadPhotos(t, a + 1);
      }
    });
  },

  //图片失败，默认图片
  bind_bnerr2: function (e) {
    var _errImg = e.target.dataset.errorimg;
    var _errObj = {};
    _errObj[_errImg] = a.imghost + "/public/images/default_goods_image_240.gif";
    this.setData(_errObj) //注意这里的赋值方式,只是将数据列表中的此项图片路径值替换掉 ;  
  },
  // 上传图片
  chooseImg: function (e) {
    var that = this;
    var imgs = this.data.imgs;
    if (imgs.length >= 3){
        if(this.data.lenMore==0){
          this.setData({ lenMore: 1 });
        }
        return false;
    }

    var imgs = this.data.imgs;

    getApp().globalData.no_clear=1;
    wx.chooseImage({
       count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;

        getApp().request.uploadFile("/api/weshop/comment/uploadCommentImg", {
          filePath: tempFilePaths[0],
          name: "file",
          success: function (e) {
            if(e.data.code==0) {
              that.data.uploadPath.push(e.data.data);
              imgs.push(tempFilePaths[0]);
              that.setData({
                imgs: imgs
              });
            }
          }
        });
      }
    });
  },
  // 删除图片
  deleteImg: function (e) {
    var imgs = this.data.imgs;
    var index = e.currentTarget.dataset.index;
    imgs.splice(index, 1);
    this.setData({
      imgs: imgs
    });
  },
  // 预览图片
  previewImg: function (e) {
    //获取当前图片的下标
    var index = e.currentTarget.dataset.index;
    //所有图片
    var imgs = this.data.imgs;
    getApp().globalData.no_clear=1;
    wx.previewImage({
      //当前显示图片
      current: imgs[index],
      //所有图片
      urls: imgs
    })
  },

  txtInpt:function (e) {
    this.setData({
      content: e.detail.value
    })
  },

  onUnload: function () {
    wx.navigateBack({ delta:1 })
  },

  
});