// pages/member/myOrder/invoice/invoice.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    order_id: '',
    invoice_type: '',
    invoice_title: '',
    order_number: "",
    rise_type: "",
    dowm: "",
    email: false
  },

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

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

  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function () {
    this._initData();
    this._getInvoiceDet();
    this._initDownload();
  },

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

  },

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

  },

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

  },

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

  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function () {

  },
  _initData() {
    this.setData({
      invoice_type: '',
      invoice_title: '',
      order_number: "",
      rise_type: "",
      dowm: "",
    });
  },
  _getInvoiceDet() {
    let that = this;
    let urlStr = app.getNetAddresss("order.detail");
    urlStr += '&order_id=' + this.data.order_id;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            invoice_type: res.data.invoice_type ? "纸质发票" : "电子发票",
            rise_type: res.data.invoice_state ? "已开票" : "未开票",
            invoice_title: res.data.collect_name,
            order_number: res.data.order_sn,
            email: res.data.email || false
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  _initDownload() {
    let that = this;
    let urlStr = app.getNetAddresss("order.rise.get-invoice");
    urlStr += '&order_id=' + this.data.order_id;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            dowm: res.data.invoice
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  checkInvoice() {
    let that = this;
    let urlStr = app.getNetAddresss("order.rise.get-invoice");
    urlStr += '&order_id=' + this.data.order_id;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          let invoicearr = [];
          if (res.data.invoice) {
            invoicearr.push(res.data.invoice);
          }
          let name = that.getExtension(res.data.invoice);
          if (name == '.pdf') {
            wx.downloadFile({
              url: invoicearr[0],
              success: function (res) {
                console.log(res);
                let filePath = res.tempFilePath;
                wx.openDocument({
                  filePath: filePath,
                  success: function (res) {
                    console.log('打开文档成功');
                  }
                });

              }
            });
          } else {
            if (invoicearr.length > 0) {
              wx.previewImage({
                current: invoicearr[0], // 当前显示图片的http链接
                urls: invoicearr // 需要预览的图片http链接列表
              });
            }
          }


        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getExtension(name) {
    return name.substring(name.lastIndexOf("."));
  }
});