// pages/member/myOrder/express/express.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    companyShow: false,
    form: {
      refund_id: "",
      express_sn: "",
      express_company: "",
    },
    express: [],
    expressName: "",
    expressCode: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.refund_id) {
      this.setData({
        "form.refund_id": options.refund_id,
      });
    }
    this._initData();
    this._getExpressInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

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

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

  scanCode(){
    wx.scanCode({
      onlyFromCamera: true,
      success:(res)=>{
        this.setData({
          'form.express_sn':res.result
        });
      }
    });
  },

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  _initData() {
    this.setData({
      companyShow: false,
      "form.express_sn": "",
      "form.express_company": "",
      express: [],
      expressName: "",
      expressCode: "",
    });
  },
  companyShowbtn: function () {
    this.setData({
      companyShow: true,
    });
  },
  expressclear() {
    this.setData({
      "form.express_sn": "",
    });
  },
  //快递单号输入
  expressbtn(e) {
    let express_sn = e.detail;
    this.setData({
      "form.express_sn": express_sn,
    });
  },
  //关闭快递选择
  companyShowClose: function () {
    this.setData({
      companyShow: false,
    });
  },
  _getExpressInfo() {
    let that = this;
    let urlStr = app.getNetAddresss("dispatch.express-list");
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          var myData = res.data;
          that.setData({
            express: myData.express_companies,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  submitInfo() {
    let urlStr = app.getNetAddresss("refund.operation.send");
    urlStr += "&refund_id=" + this.data.form.refund_id;
    urlStr += "&express_sn=" + this.data.form.express_sn;
    urlStr += "&express_company_name=" + this.data.expressName;
    urlStr += "&express_company_code=" + this.data.expressCode;
    app._getNetWork({
      url: urlStr,
      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) {
        console.log(res);
      },
    });
  },
  companyConfirm(e) {
    let item = e.currentTarget.dataset.item;
    this.setData({
      expressName: item.name,
      expressCode: item.value,
      companyShow: false,
    });
  },
});
