// pages/minusapply/minusapply.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: {
    isDetails: false, //减分申请详情时状态
    minusPerson:'', //扣分人
    minusTime:'', //日期
    minusName:'', //被扣人
    minusPoints:'', //扣得积分
    minusNorm:'请选择',
    itemPerson:{}, 
    itemNorm: { normItem: '请选择', normScore:'' },//所选规则数据
    CotegoryArray: [],
    Cotegory: [],

    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)
  },
  minusSubmit: function (event){
    var that = this
    var object = event.detail.value
    console.log(object)

    var minusName = object.minusName
    var minusPoints = object.minusPoints
    var minusReason = object.minusReason
    var itemNorm = that.data.itemNorm
    var itemPerson = that.data.itemPerson

    if (!minusName) {
      that.comToast('请选择被扣人')
      return
    }
    if (!minusPoints) {
      that.comToast('请选择扣分标准')
      return
    }
    if (!minusReason) {
      that.comToast('请输入扣分原由')
      return
    }

    var data = {
      openid: wx.getStorageSync('openid'),
      reasons: minusReason,
      normId: itemNorm.normId,
      adviceId: itemPerson.empId
    }
    var url = baseUrl + BASEURL.getUrl.saveMinusInvoices
    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
      })
    })
  },
  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
      })
    })
  },
  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='+false,
      })
    }
  },

  /**
      *修改Title
      */
  changeTitle: function () {
    var that = this;
    var isDetails = that.data.isDetails
    var title
    if (isDetails) {
      title = '减分详情'
    } else {
      title = '减分申请'
    }
    that.setData({
      mername: title
    })
    wx.setNavigationBarTitle({
      title: that.data.mername
    })
  },
  /**
 * 根据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,
          minusTime: res.data.data.invoicesDate,
          minusName: res.data.data.adviceName,
          minusNorm: res.data.data.normName,
          minusPoints: res.data.data.score,
          minusReason: res.data.data.reasons,
          minusPerson: res.data.data.fillName
          // applyJob: res.data.data.fillStation
        })
      }
    }, 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 = '减分详情'
      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 {
      title = '减分申请'
      var nowTime = dateutil.getComDate(new Date, 'yy-mm-dd')
      that.setData({
        minusTime: nowTime
      })
      that.getCotegory()//获得分类列表
    }
    wx.setNavigationBarTitle({
      title: title
    })

    
    // console.log('nowTime', nowTime)
  },

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

  /**
   * 选择被扣人对象
   */
  selctMinusPerson:function(){
    if (!this.data.isDetails){
      wx.navigateTo({
        url: '../selectStaff/selectStaff',
      })
    }
    // return
    // var that = this
    // var data = {  }
    // var url = baseUrl + BASEURL.getUrl.getAllEmp
    // netUtil.requestLoading(url, data, '正在加载数据...', 'GET', function (res) {

    // }, function (res) {
    //   wx.showToast({
    //     title: '加载数据失败',
    //     icon: 'none',
    //     duration: 2000
    //   })
    // })
  },










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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    if (that.data.itemPerson){
      that.setData({
        minusName: that.data.itemPerson.empName
      })
    }

    if (that.data.itemNorm){
      that.setData({
        minusNorm: that.data.itemNorm.normItem,
        minusPoints: that.data.itemNorm.normScore
      })
    }
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})