// pages/handle/expenditureAudit/expenditureAudit.js
import * as UTIL from '../../../utils/util.js';
import * as API from '../../../utils/API.js';

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isIPX: app.globalData.isIPX,
    form:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      id:options.id,
      typeid:options.typeid,
      listindex:options.listindex
    })
    wx.showLoading({
      title: '正在加载模板',
      mask:true
    })
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'resource_farming_type', {method:'GET'}, {
      success: (res) => {
        that.setData({
          farmingTypeOptions:res.data,
        })
      }
    })
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'resources_build_type', {method:'GET'}, {
      success: (res) => {
        that.setData({
          buildTypeOptions:res.data,
        })
      }
    })
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'resources_unused_type', {method:'GET'}, {
      success: (res) => {
        that.setData({
          unusedTypeOptions:res.data,
        })
      }
    })
    /**使用情况 字典  1:自用  2:闲置  3:出租或外借  4:其他*/ 
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'use_type', {method:'GET'}, {
      success: (res) => {
        that.setData({
          useTypeOptions:res.data,
        })
      }
    })
    setTimeout(function(){
      //详情
      UTIL.httpRequest(API.URL_GET_GETRESOURCEGET + options.id, {method:'GET'}, {
        success: (res) => {
          res.data.resourceSortText = UTIL.getTransform(res.data.resourceSort,that.data.typeid == 1 ? that.data.farmingTypeOptions : that.data.typeid == 2 ? that.data.buildTypeOptions : that.data.typeid == 3 ? that.data.unusedTypeOptions : '');

          res.data.useTypeText = UTIL.getTransform(res.data.useType,that.data.useTypeOptions);

          that.setData({
            form:res.data
          })
        }
      })
    },1000)
    
   wx.hideLoading()
  },
  onChangeInput(e){
    var that = this;
    that.setData({
      ['form.'+e.currentTarget.dataset.formname]:e.detail
    })
  },
  goSubmit(){
    console.log(this.data.form);
    var that = this;
    let data = that.data.form;
    if (data.name == '') {
      UTIL.showToastNoneIcon('请填写资源名称！');return;
    }else if(data.resourceSortText == '' || data.resourceSort == null){
      UTIL.showToastNoneIcon('请选择资源类型！');return;
    }
    data.method = 'POST'
    UTIL.httpRequest(API.URL_GET_GETRESOURCEEDIT, data, {
      success: (res) => {
        if (res.code == API.SUCCESS_CODE) {
          let pages = getCurrentPages();
          let currentPage = null;   //当前页面
          let prevPage = null;  //上一个页面
          currentPage = pages[pages.length - 1]; //获取当前页面，将其赋值
          prevPage = pages[pages.length - 2]; //获取上一个页面，将其赋值
          if (prevPage) {
            prevPage.setData({
              ["listNy["+that.data.listindex+"]"]: that.data.form,//将想要传的信息赋值给上一个页面data中的值
            })
          }
          wx.showToast({
            title: '保存成功',
            icon: 'success',
            duration: 2000,
            complete(){
              
              setTimeout(function(){
                that.back();
              },2000)
            }
          })
        }
      }
    })
  },
  closeBox(even){
    console.log(even.currentTarget.dataset.name);
    this.setData({
      [even.currentTarget.dataset.name]:false
    })
  },
  onConfirma(even){
    console.log(even.currentTarget.dataset.name);
    this.setData({
      [even.currentTarget.dataset.name]:false,
      [even.currentTarget.dataset.value]: even.detail.value.dictValue,
      [even.currentTarget.dataset.value+'Text']: even.detail.value.dictLabel,
    })
  },
  openBox(even){
    console.log(even.currentTarget.dataset.name);
    this.setData({
      [even.currentTarget.dataset.name]:true,
    })
  },
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      [e.currentTarget.dataset.value]: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },
  back:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

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

  },

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

  },

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

  },

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

  }
})