// pages/applyjoinapprovel/applyjoinapprovel.js
var BASEURL = require('../../utils/wxconstant.js')
var baseUrl = BASEURL.BaseUrl()
var baseutils = require('../../utils/baseUtils.js')
var netUtil = require('../../utils/network.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    approvelStatus:2,//单据账套 0：待审批，1：已审批，2：我发起的
    applyName:'',
    applyPhone:'',
    enterDate:'',
    birthDate:'',
    applyMail:'',
    companyName:'',
    isapplyPoint:true, //积分审批

    applyStatus:'',
    applyTime: '',
    applyJob: '',
    applyNorm: '',
    applyPoint:'',
    fromType:1,
    isISend:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var Itemdata = options.getItemdata
    var approvelStatus = JSON.parse(Itemdata).invoicesStatus
    var invoicesId = JSON.parse(Itemdata).invoicesId
    var fromType = options.getfromType
    var isISend
    console.log('fromType=', fromType)
    var title
    if (Itemdata){
      console.log('Itemdata=', JSON.parse(Itemdata))
      // console.log('approvelStatus=', approvelStatus)
      title = fromType == 1 ? '待审批' : (fromType == 2 ? '已审批' : '我发起的')
      isISend = fromType == 3 ? true : false
      
      that.setData({
        approvelStatus: approvelStatus,
        invoicesId: invoicesId,
        fromType: fromType,
        isISend: isISend,
        applyStatus: approvelStatus == 1 ? '待审批' : (approvelStatus == 2 ? '已审批' : '未通过')
        // applyStatus: approvelStatus == 
      })

     
      wx.setNavigationBarTitle({
        title: title
      })
      that.initDetailsView(invoicesId)
    }
  },
  /**
   * 根据invoicesId 获取加分单据详情数据
   */
  initDetailsView: function (invoicesId) {
    var that = this
    var data = { invoicesId: invoicesId }
    var url = baseUrl + BASEURL.getUrl.getInvoices
    netUtil.requestLoading(url, data, '正在加载数据...', 'GET', function (res) {
      if (res.data.success && res.data.data) {
        that.setData({
          applyTime: res.data.data.invoicesDate,
          applyName: res.data.data.fillName,
          applyJob: res.data.data.fillStation,
          applyNorm: res.data.data.normName,
          applyReason: res.data.data.reasons,
          applyPoint: res.data.data.score,
          adviceName: res.data.data.adviceName,
          auditName: res.data.data.auditName,
          beMinusPerson: res.data.data.adviceName
        })
      }
    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
        icon: 'none',
        duration: 2000
      })
    })
  },
  agreeBtnClick:function(){
    this.doApprovel(1)
  },

  disagreeBtnClick:function(){
    this.doApprovel(2)

  },
  doApprovel: function (status){
    var that = this
    var data = { status: status, invoicesId: that.data.invoicesId }
    var url = baseUrl + BASEURL.getUrl.approval
    netUtil.requestLoading(url, data, '正在加载数据...', 'GET', function (res) {
      if(res.data.success && res.data.data){
        that.comToast(res.data.data)
        setTimeout(function () {
          wx.navigateBack({
          })
        }, 1000)
      }else{
        that.comToast(res.data.data)
      }
    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
        icon: 'none',
        duration: 2000
      })
    })
  },


  comToast: function (title) {
    wx.showToast({
      title: title,
      icon: 'none',
      duration: 2000
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})