// pages/member/myOrder/DeliveryEvaluate/DeliveryEvaluate.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    language: "",
    ratetextshow: ["极差", "失望", "一般", "满意", "惊喜"],
    ratetext: {
      text1: "惊喜",
      text2: "惊喜",
    },
    good: "",
    order_id: "",
    api: "",
    content: "",
    level: 5,
    level2: 5,
    order: "",
    comment: "", //评论内容
    distributor: {},
    order_list: [], // 商品列表
    type: "", // 从哪一个页面跳转过来
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.order_id) {
      this.setData({
        order_id: options.order_id,
      });
    }
    if (options.api) {
      this.setData({
        api: options.api,
      });
    }
    if (options.name) {
      this.setData({
        type: options.name,
      });
    }
    if (this.data.type === "delivery") {
      //评价补货商
      this.setData({
        getApi: "plugin.kingtimes.frontend.distributor.distributorOrderDetail",
      });
      this.setData({
        data: {
          distributor_order_id: this.data.order_id,
        },
      });
    } else if (this.data.type === "distributor") {
      //评价配送站
      this.setData({
        getApi: "plugin.kingtimes.frontend.order.orderDetail.index",
      });
      this.setData({
        data: {
          order_id: this.data.order_id,
        },
      });
    }
    this.getDistributor(); // 获取订单信息
  },

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

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  getDistributor() {
    let that = this;
    let urlStr = app.getNetAddresss(that.getApi);
    if (this.data.type === "delivery") {
      //评价补货商
      urlStr += "&distributor_order_id=" + this.data.order_id;
    } else if (this.data.type === "distributor") {
      //评价配送站
      urlStr += "&order_id=" + this.data.order_id;
    }
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            order_info: response.data,
            order_list: that.data.order_info.order_goods,
            distributor: that.data.order_info.distributor,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getStar(event) {
    let value = event.detail;
    this.setData({
      level: value,
      "ratetext.text1": this.data.ratetextshow[value - 1],
    });
  },
  getStar_2(event) {
    let value = event.detail;
    this.setData({
      level2: value,
      "ratetext.text2": this.data.ratetextshow[value - 1],
    });
  },
  commentChange(e) {
    let val = e.detail;
    this.setData({
      comment: val,
    });
  },
  toComment(e) {
    let item = e.target.dataset.good;
    let that = this;
    if (this.data.comment.length == 0) {
      wx.showModal({
        title: "提示",
        content: "您还没有输入相关的评论内容",
        success(res) {
          if (res.confirm) {
            that._submitData(item);
          }
        },
      });
    } else {
      this._submitData(item);
    }
  },
  //提交数据到服务器
  submitData() {
    let that = this;
    let urlStr = app.getNetAddresss(that.data.api);
    let json = {};
    if (this.data.type === "delivery") {
      json = {
        distributor_order_id: that.order_id,
        distributor_id: that.distributor.id,
        provider_id: that.order_info.provider_id,
        content: this.data.comment,
        level: this.data.level,
      };
    } else if (this.data.type === "distributor") {
      json = {
        distributor_order_id: that.order_id,
        distributor_id: that.distributor.id,
        content: this.data.comment,
        level: this.data.level,
        efficiency_level: this.data.level2,
      };
    }
    let eltion = JSON.stringify(json);
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: eltion,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1500,
            success: function () {
              setTimeout(function () {
                //要延时执行的代码
                wx.navigateBack({
                  delta: 1,
                });
              }, 1500); //延迟时间
            },
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {},
    });
  },
});
