// pages/pointapply/pointapply.js
var BASEURL = require('../../utils/wxconstant.js')
var baseUrl = BASEURL.BaseUrl()
var baseutils = require('../../utils/baseUtils.js')
var netUtil = require('../../utils/network.js')
var dateutil = require('../../utils/DateTime.js')
var myInfoData = wx.getStorageSync('myInfoData')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    applyJob:'',
    applyName:'',
    applyStatus:'',
    applyTime:'',
    addPoints:'',
    itemNorm: { normItem: '请选择', normScore:'' },//所选规则数据
    applyReason:'',
    CotegoryArray:[],
    Cotegory:[],
    invoicesStatus: 0,//单据状态1：待审批，2：已审批，3：不同意？
    isDetails: false, //加分申请详情时状态
    applyNorm:'',
    detailsData:{} ,   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this 
    var isDetails = options.getisDetails
    var itemdata 
    if (options.getItemdata){
      itemdata = JSON.parse(options.getItemdata)
      that.setData({
        isDetails: isDetails
      })
    }
    console.log('itemdata', itemdata)
    that.initView(itemdata) //修改标题
    // that.getDept()//获取公司部门列表

  },
  bindPickerChange: function (e) {
    var that = this
    console.log('picker发送选择改变，携带值为', e.detail)
    var categoryId = that.data.Cotegory[e.detail.value].categoryId
    if (categoryId){
      wx.navigateTo({
        url: '../selectnorm/selectnorm' + '?getcategoryId=' + categoryId + '&getAddType=' + true,
      })
    }
  },
  getDept:function(){
    var that = this
    var data = { parentId:0}
    var url = baseUrl + BASEURL.getUrl.getDept
    netUtil.requestLoading(url, data, '正在加载数据...', 'GET', function (res) {

    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
        icon: 'none',
        duration: 2000
      })
    })
  },
  getCotegory:function(){
    var that = this
    var data = { parentId: 0 }
    var url = baseUrl + BASEURL.getUrl.getCotegory
    netUtil.requestLoading(url, data, '正在加载数据...', 'GET', function (res) {
      if(res.data.success && res.data.data){
        that.setData({
          Cotegory:res.data.data
        })
        var array = []
        for (var i = 0; i < res.data.data.length; i++){
          array[i] = res.data.data[i].categoryName
          if (i == res.data.data.length-1){
            console.log('array', array)
            that.setData({
              CotegoryArray:array 
            })
          }
        }
      }
    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
        icon: 'none',
        duration: 2000
      })
    })
  },
  initView: function (itemdata){
    var that = this;
    var isDetails = that.data.isDetails
    var title
    if (isDetails) {
      title = '加分申请详情'
      if (itemdata && itemdata.invoicesId){
        var invoicesStatus = itemdata.invoicesStatus
        var applyStatus 

        if (invoicesStatus == 0){
          applyStatus = '待审批'
        } else if (invoicesStatus == 1){
          applyStatus = '已审批'
        }else{
          applyStatus = '未通过'
        }
        that.setData({
          applyStatus: applyStatus,
          invoicesStatus: invoicesStatus,
        })
        that.initDetailsView(itemdata.invoicesId)
      }
    } else {
      that.getCotegory()//获得分类列表
      title = '积分申请'
      that.setData({
        applyJob: myInfoData.station,
        applyName: myInfoData.empName
      })
    }
    that.setData({
      mername: title
    })
    wx.setNavigationBarTitle({
      title: that.data.mername
    })
    var nowTime = dateutil.getComDate(new Date,'yy-mm-dd')
    that.setData({
      applyTime: nowTime
    })
    // console.log('nowTime', nowTime)
  },
 /**
   * 提交申请
   */
  applySubmit: function (event) {
    var that = this
    var object = event.detail.value
    console.log(object)

    var applyName = object.applyName
    var applyJob = object.applyJob
    var itemNorm = that.data.itemNorm
    var applyReason = object.applyReason

    var applyName = object.applyName

    if (!itemNorm.normId) {
      that.comToast('请选择申请标准')
      return
    }
    if (!applyReason){
      that.comToast('请输入申请事由')
      return
    }

    var data = { 
      openid: wx.getStorageSync('openid'), 
      reasons: applyReason,
      normId: itemNorm.normId
    }
    var url = baseUrl + BASEURL.getUrl.saveAddInvoices
    netUtil.requestLoading(url, data, '正在加载数据...', 'POST', function (res) {
      if(res.data.success){
        that.comToast("提交成功")
        setTimeout(function(){
          wx.navigateBack({
          })
        },1000)
      }
    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
        icon: 'none',
        duration: 2000
      })
    })
  },

  comToast: function (title) {
    wx.showToast({
      title: title,
      icon: 'none',
      duration: 2000
    })
  },


/**
 * 根据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({
          detailsData: res.data.data,
          applyTime: res.data.data.invoicesDate,
          applyName: res.data.data.fillName,
          applyNorm: res.data.data.normName,
          applyReason: res.data.data.reasons,
          applyJob: res.data.data.fillStation,
          addPoints: res.data.data.score
        })
      }
    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
        icon: 'none',
        duration: 2000
      })
    })
  },
  /**
   * 撤回 : 目前做的是删除单据操作
   */
  cancelApply:function(){
    var that = this
    var data = { invoicesId: that.data.detailsData.invoicesId }
    var url = baseUrl + BASEURL.getUrl.withdrawInvoices
    netUtil.requestLoading(url, data, '正在加载数据...', 'GET', function (res) {
      if(res.data.success && res.data.data){
        that.comToast(res.data.data)
        setTimeout(function () {
          wx.navigateBack({
          })
        }, 1000)
      }
    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
        icon: 'none',
        duration: 2000
      })
    })
  },





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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    that.setData({
      applyNorm:that.data.itemNorm.normItem,
      addPoints: that.data.itemNorm.normScore
    })

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})