 var e = getApp(),
  a = e.globalData.setting,
  os = a,
  t = e.request,
  d = e.globalData;
Page({ 

  /**
   * 页面的初始数据
   */
  data: {
    url: a.url, //接口网址
    iurl: a.imghost,
    defimgurl: "/miniapp/images/no-head.jpg",
    stars_index: -1, //当前点击的星星下标
    labels: [], //星级标签数组
    sub_evaluate: 0, //是否评价成功
    proposal: "", //用户输入的建议
    proposal_size: 100, //评价意见和建议限定字数
    number: "", //订单单号
    beautician: "", //美容师信息
    fromType: "服务评价", //评价来源
    arrangeTime: "", //服务日期
    is_evaluate: 0, //是否评价
    EvaluateDatetime: "", //评价时间
	is_act:0, //是否能购获取图片 
	actId:0,   //活动ID
	giftBagId:0, //礼包ID
	
  },
  //点击星级
  check_stars: function(e) {
    var th = this;
    var is_evaluate = th.data.is_evaluate; //0可点击
    if (is_evaluate == 0) {
      var index = e.currentTarget.dataset.index;
      var stars_index = th.data.stars_index;
      if (index != stars_index) {
        var url = "/api/weshop/marketing/reservation/star/label/list";
        getApp().request.promiseGet(url, {
          data: {
            Star: index + 1,
            storeId: a.stoid,
            TagID:'0C7EE27C-AA5F-4969-841A-CC994EF3B12E'//3.0默认综合评价
          }
        }).then(res => {
          if (res.data.code == 0) {
            th.setData({
              labels: res.data.data
            })
          } else {
            getApp().my_warnning(res.data.msg, 0, th);
            return;
          }
        })
        th.setData({
          stars_index: index
        })
      }
    }
  },
  //输入评价意见和建议
  input_proposal: function(e) {
    var th = this;
    var value = e.detail.value;
    th.setData({
      proposal: value
    })
  },
  //图片失败，默认图片
  bind_bnerr1: function (e) {
    var th = this;
    var _errImg = e.target.dataset.errorimg;
    var _Img = e.target.dataset.img;
    if (_Img != undefined) {
      var _errObj = {};
      _errObj[_errImg] = "/miniapp/images/no-head.jpg";
      th.setData(_errObj) //注意这里的赋值方式,只是将数据列表中的此项图片路径值替换掉 ;
    }
  },
  //选择标签
  check_label: function(e) {
    var th = this;
    var evaluate = th.data.is_evaluate; //0为未评价，未评价才可点击标签
    if (evaluate == 0) {
      var index = e.currentTarget.dataset.index;
      var label_id = th.data.labels[index].check;
      var labels = 'labels[' + index + '].check';
      if (label_id) {
        label_id = 0
      } else {
        label_id = 1
      }
      th.setData({
        [labels]: label_id
      })
    }
  },
  //查询美容师信息
  query_bea: function() {
    var th = this;
    var url = "/api/weshop/marketing/reservation/reservation/page";
    var number = th.data.number; //订单编号
    getApp().request.promiseGet(url, {
      data: {
        storeId: a.stoid,
        userId: th.data.options.userid,
        number: number
      }
    }).then(res => {
      if (res.data.code == 0) {
        var data = res.data.data.pageData;
        var arrangeTime = data[0].ArrangeTime;
        th.setData({
          beautician: data,
          arrangeTime: arrangeTime
        })
      } else {
        getApp().my_warnning(res.data.msg, 0, th);
      }
    })
  },
  //获取预约评价
  evaluate_get: function() {
    var th = this;
    var url = "/api/weshop/evaluation/evaluate/get";
    getApp().request.promiseGet(url, {
      data: {
        number: th.data.number,
        storeId: a.stoid,
      }
    }).then(res => {
      if (res.data.code == 0) {
        th.setData({
          stars_index: res.data.data.Star-1,
          labels: res.data.data.EvaluationList,
          proposal: res.data.data.Remark,
          EvaluateDatetime: res.data.data.EvaluateDatetime.substring(0, 19)
        })
      } else {
        getApp().my_warnning(res.data.msg, 0, th);
      }
    })
  },
  //提交评价
  evaluate_save: function() {
    var th = this;
    var stars = th.data.stars_index; //评价星级
    var proposal = th.data.proposal;
    //判断是否评级
    if (stars < 0) {
      getApp().my_warnning("请进行评分", 0, th);
      return false;
    } else if (proposal == "") {
      getApp().my_warnning("给点建议呗", 0, th);
      return false;
    }
    wx.showLoading({
      title: '加载中',
    })
    var labels = th.data.labels; //星级标签数组
    var evaLabList = []; //用户选择的评价标签
    // 查询评价标签
    for (var i = 0; i < labels.length; i++) {
      if (labels[i].check) {
        evaLabList.push(labels[i].LabelName);
      }
    }
    var url = th.data.url + "/api/weshop/evaluation/evaluate/insert";
    var posNo = th.data.number; //收银单号
    var proposal = th.data.proposal; //用户建议内容
    var fromType = th.data.fromType; //评价来源
    var json = {
      "evaLabList": evaLabList,
      "fromType": 2,
      "number": posNo,
      "remark": proposal,
      "star": stars + 1,
      "storeId": a.stoid,
    };
    //var data = JSON.stringify(json); //转换成json格式


    getApp().request.promisePost(url, {
      data: json,is_json:1,is_normal:1
    }).then(res => {
        wx.hideLoading();
        if (res.data.code == 0) {
          getApp().my_warnning("评价成功", 1, th);
          var namber=th.data.number;
          setTimeout(function() {
            wx.redirectTo({
              url: "/packageG/pages/user/my_service/tment_eval?Number="+namber+"&evaluate="+1
            });
          }, 2000);
        } else {
          getApp().my_warnning(res.data.msg, 0, th);
        }

    })

  },
  //图片失败，默认图片
  bind_bnerr1: function(e) {
    var th = this;
    var _errImg = e.target.dataset.errorimg;
    var _Img = e.target.dataset.img;
    if (_Img != undefined) {
     
      var _errObj = {};
      _errObj[_errImg] = th.data.iurl+"/miniapp/images/no-head.jpg";
      th.setData(_errObj) //注意这里的赋值方式,只是将数据列表中的此项图片路径值替换掉 ;
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var th = this;
    th.setData({
			options,
      number: options.Number,
      is_evaluate: options.evaluate
    })
    th.query_bea();
	th.judge_act();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var th = this;
    var is_evaluate = th.data.is_evaluate;
    if (is_evaluate == 1) {
      th.evaluate_get();
    }
  },
  //评价其他跳转
  evaluate_qt:function(){
    
    wx.redirectTo({
      url: "/packageG/pages/user/my_service/tment_order_list?state="+2
    });
  },
  //点击领取礼包
  clike_lb:function(){
        
        var id=this.data.actId;
        var gifbagid = this.data.gifbagid;   
    console.log(id,"订单", this.data.number),
        wx.redirectTo({
          url: "/packageF/pages/giftpack/evaluategift/evaluategift?actId="+id+ "&orderType=2&orderNumber="+this.data.number
        });
   
  },

  //--评价有礼判断---
  judge_act: function () {
    var th = this;
    var url = "/api/weshop/marketing/comment/act/judge";
    getApp().request.promiseGet(url, {
      data: {
        orderNumber: th.data.number,
        orderType: 2,
        storeId: a.stoid,
        userId: th.data.options.userid,
      }
    }).then(res => {
      if (res.data.code == 0) {
        var id = res.data.data.id;
        var giftbagid = res.data.data.giftbagid;
        th.setData({
          is_act: 1,
          actId: id,
          giftBagId: giftbagid
        })
      } else{
        th.setData({
          is_act:0
        })
      }
    })
  },
  
})