const util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //开票中图
    invoicingImg: 'https://img.bbztx.com/miniProgram/challengeFamily/invoicingDetail/pedding.png',
    //已拒绝图
    rejectedImg: 'https://img.bbztx.com/miniProgram/challengeFamily/invoicingDetail/refuse.png',
    //已开票图
    invoicedImg: 'https://img.bbztx.com/miniProgram/challengeFamily/invoicingDetail/success.png',
    invoiceDetail: {}, //开票详情
    invoiceOneDetail: {}, //当前发票详情
    orderPassword: [], //消费码详情
    attendantNamesList: [], //多个名字存放数组
    scene: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // console.log(options);
    this.setData({
      invoiceDetail: {
        id: options.id,
        status: options.status
      }
    })
    console.log(this.data.invoiceDetail.status);
    this.getInvoiceDetail()
  },
  //获取发票详情
  getInvoiceDetail() {
    let that = this
    let data = that.data.invoiceDetail
    // console.log(data);
    util.ajax({
      func: "articleInvoice/getArticleInvoice",
      data,
      load: false
    }, ({
      data,
      code,
      reason
    }) => {
      if (code == 0) {
        // console.log(data[0].status);
        // if (data.length == 1) {
        //   let orderPassword = JSON.parse(data[0].orderPassword)
        //   console.log(orderPassword);
        //   let arr = []
        //   data.forEach(vl => {
        //     // console.log(data, orderPassword[0].password);
        //     arr.push({
        //       attendantNames: vl.attendantNames,
        //       password: orderPassword[0].password
        //     })
        //   })
        //   that.setData({
        //     invoiceOneDetail: data[0],
        //     attendantNamesList: arr,
        //     'invoiceDetail.status':data[0].status
        //     // orderPassword: JSON.parse(data[0].orderPassword)
        //   })
        // }
        console.log(data.orderPassword);
        let orderPassword = JSON.parse(data.orderPassword)
        console.log(orderPassword);
        let nameList = data.attendantNames
        let psWord = []

        // console.log(orderPassword);
        // data.forEach(vl => {
        //   nameList.push(vl.attendantNames)
        // })
        // console.log(nameList);
        orderPassword.forEach(el => {
          psWord.push(el.password)
        })
        console.log(nameList, psWord);
        let arr = []
        nameList.forEach((val, i) => {
          arr.push({
            attendantNames: val,
            password: orderPassword[i].password
          })
        })
        that.setData({
          invoiceOneDetail: data,
          attendantNamesList: arr,
          'invoiceDetail.status': data.status
        })
        console.log(that.data.attendantNamesList);
      } else {
        util.showTips(reason)
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getInvoiceDetail()
    let query = wx.getLaunchOptionsSync()
    console.log(query);
    this.setData({
      scene: query.scene
    })
    console.log(this.data.scene);
  },
  //修改信息
  editInfo(e) {
    let that = this
    let data = {}
    data.id = that.data.invoiceDetail.id
    data.status = that.data.invoiceDetail.status
    console.log(data);
    // console.log(that.data.invoiceDetail);
    util.ajax({
      func: "articleInvoice/submitUpdateMessage",
      data,
      method: 'POST',
      load: false
    }, ({
      code,
      data,
      reason
    }) => {
      if (code == 0) {
        let detail = {}
        detail.status = that.data.invoiceDetail.status
        detail.info = that.data.attendantNamesList
        detail.data = e.currentTarget.dataset.item
        detail = JSON.stringify(detail)
        // console.log(detail);
        wx.navigateTo({
          url: '/pages/order/invoicingEdit/index?detail=' + detail, //跳转修改信息页面
        })
      } else if (code == -14) {
        util.showTips('申请状态发生变更')
        that.getInvoiceDetail(that.data.invoiceDetail)
        console.log(that.data.invoiceDetail.status);
        that.setData({
          'invoiceDetail.status': Number(reason)
        })
      }
    })

  },
  resubmit() {
    // wx.reLaunch({
    //   url: 'pages/order/invoicing/index',
    // })
    let scene = this.data.scene
    if (scene == 1001) {
      console.log(2);
      wx.navigateBack({
        delta: 2,
      })
    } else if (sene == 1157) {
      console.log(1);
      wx.reLaunch({
        url: 'pages/order/invoicing/index',
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

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

  },

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

  },

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

  },

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

  }
})