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

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.order_id) {
      this.setData({
        order_id: order_id,
      });
    }
    if (options.api) {
      this.setData({
        api: api,
      });
    }
    if (options.name) {
      this.setData({
        type: name,
      });
    }
  },

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

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this._initData();
    this._getDistributor(); // 获取订单信息
  },

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  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,
    });
  },
  _initData() {
    this.setData({
      ratetextshow: ["极差", "失望", "一般", "满意", "惊喜"],
      ratetext: {
        text1: "惊喜",
        text2: "惊喜",
      },
      level: 5,
      level2: 5,
      comment: "", //评论内容
      distributor: {},
      order_list: [], // 商品列表
      jsondata: {},
      order_info: {},
    });
  },
  _getDistributor() {
    let getApi = "";
    let that = this;
    let urlStr = "";
    if (this.data.type === "delivery") {
      //评价补货商
      getApi = "plugin.kingtimes.frontend.distributor.distributorOrderDetail";
      urlStr += app.getNetAddresss(getApi);
      urlStr += "&distributor_order_id=" + this.data.order_id;
    } else if (this.data.type === "distributor") {
      //评价配送站
      getApi = "plugin.kingtimes.frontend.order.orderDetail.index";
      urlStr += app.getNetAddresss(getApi);
      urlStr += "&order_id=" + this.data.order_id;
    }
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          for (let i = 0; i < res.data.order_goods.length; i++) {
            res.data.order_goods[i].gross_price = (
              Number(res.data.order_goods[i].goods_price) /
              res.data.order_goods[i].total
            ).toFixed(2);
          }
          that.setData({
            order_info: res.data,
            order_list: res.data.order_goods,
            distributor: res.data.distributor,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  toComment() {
    let that = this;
    if (this.data.comment.length == 0) {
      wx.showModal({
        title: "提示",
        content: "您还没有输入相关的评论内容",
        success(res) {
          if (res.confirm) {
            that._submitData();
          }
        },
      });
    } else {
      this._submitData();
    }
  },
  //提交数据到服务器
  _submitData() {
    let that = this;
    let urlStr = app.getNetAddresss(that.data.api);
    let json = {};
    if (this.data.type === "delivery") {
      json = {
        distributor_order_id: that.data.order_id,
        distributor_id: that.data.distributor.id,
        provider_id: that.data.order_info.provider_id,
        content: that.data.comment,
        level: that.data.level,
      };
    } else if (this.data.type === "distributor") {
      json = {
        distributor_order_id: that.data.order_id,
        distributor_id: that.data.distributor.id,
        content: that.data.comment,
        level: that.data.level,
        efficiency_level: that.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) {},
    });
  },
});
