// pages/Bookkeeping/Bookkeeping.js
import * as UTIL from '../../utils/util.js';
import * as API from '../../utils/API.js';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isIPX: app.globalData.isIPX,
    result:[],
    result2:[],
    showGroup:'1',
    flowList:[],
    showProjectFundType:false,
    checkedStatusText:'全部',
    checkedStatus:'1',
    incomeExpensesType:'1',
    listNy:[],
    farmingTypeOptions:[{
      dictLabel:'全部',
      dictValue:''
    }],
    buildTypeOptions:[{
      dictLabel:'全部',
      dictValue:''
    }],
    unusedTypeOptions:[{
      dictLabel:'全部',
      dictValue:''
    }],
    resourceType:1,//资源分类 字典  1:农用地  2：建设用地  3：未利用地、林木
    resourceSort:'',//资源类型
    searchName:'',//搜索资源名称
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'resource_farming_type', {method:'GET'}, {
      success: (res) => {
        let options = that.data.farmingTypeOptions;
        res.data.map(rr=>{
          options.push(rr)
        })
        that.setData({
          farmingTypeOptions:options,
        })
      }
    })
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'resources_build_type', {method:'GET'}, {
      success: (res) => {
        let options = that.data.buildTypeOptions;
        res.data.map(rr=>{
          options.push(rr)
        })
        that.setData({
          buildTypeOptions:options,
        })
      }
    })
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'resources_unused_type', {method:'GET'}, {
      success: (res) => {
        let options = that.data.unusedTypeOptions;
        res.data.map(rr=>{
          options.push(rr)
        })
        that.setData({
          unusedTypeOptions:options,
        })
      }
    })
    setTimeout(function(){
      that.SRSelect();
    },1000)
  },
  SRSelect(){
    var that = this;
    //收入事项
    let data = {
      pageNum:'1',
      pageSize:'999',
      resourceType:that.data.resourceType,
      resourceSort:that.data.resourceSort,
      name:that.data.searchName,
    }
    UTIL.httpRequest(API.URL_GET_GETRESOURCELIST,data ,{
      success: (res) => {
        if (res.code == API.SUCCESS_CODE) {
          let list = res.rows;
          for (let i = 0; i < list.length; i++) {
            const element = list[i];
            list[i].resourceSortText = UTIL.getTransform(element.resourceSort,that.data.resourceType == 1 ? that.data.farmingTypeOptions : that.data.resourceType == 2 ? that.data.buildTypeOptions : that.data.resourceType == 3 ? that.data.unusedTypeOptions : '');
            // list[i].jieAmount = Number(element.jieAmount).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, ($0, $1) => {
            //   return $1 + ","; }).replace(/\.$/, "");
          }
          this.setData({
            listNy:list,
          });
        }
      }
    })
  },
  onChange(event) {
    this.setData({result:event.detail})
  },
  onChange2(event) {
    this.setData({result2:event.detail})
  },
  switchTab(e){
    var that = this;
    this.setData({
      showGroup:e.currentTarget.dataset.gid,
      resourceType:e.currentTarget.dataset.gid,
      resourceSort:'',
      checkedStatusText:'全部',
      searchName:''
    })
    that.SRSelect();
  },
  back:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  goSearch(e){
    console.log(e);
    var that = this;
    that.setData({
      searchName:e.detail
    })
    that.SRSelect();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this ;
  },
  openPopup(even){
    this.setData({
      showProjectFundType:true
    })
  },
  onConfirmPick(event){
    var that = this;
    this.setData({
      [event.currentTarget.dataset.name]: false,
      checkedStatus: event.detail.value.dictValue,
      checkedStatusText: event.detail.value.dictLabel,
      resourceSort: event.detail.value.dictValue,
    });
    that.SRSelect()
  },
  closeBox(even){
    console.log(even.currentTarget.dataset.name);
    this.setData({
      [even.currentTarget.dataset.name]:false
    })
  },
  goSubmit(){
    var that = this ;
    console.log(that.data.showGroup);
    if(that.data.showGroup){
      if(that.data.result.length==0){
        UTIL.showToastNoneIcon('至少选择一个收入事项！');
        return;
      }
      let url = wx.getStorageSync('dressCode')+ API.URL_GET_GETBOOKKEEP
      wx.request({
        url,
        method:"POST",
        timeout: 60000,
        data:that.data.result,
        header: {
          'Authorization':'Bearer '+getApp().globalData.userInfo.token
        },
        success: function (response) {
          wx.showToast({
            title: "提交成功！",
            duration: 2000,
            icon:"success"
          })
          that.setData({
            result:[]
          })
          that.SRSelect();
        }
      })
    }else{
      if(that.data.result2.length==0){
        UTIL.showToastNoneIcon('至少选择一个支出事项！');
        return;
      }
      let url =wx.getStorageSync('dressCode')+ API.URL_GET_GETBOOKKEEP
      wx.request({
        url,
        method:"POST",
        timeout: 60000,
        data:that.data.result2,
        header: {
          'Authorization':'Bearer '+getApp().globalData.userInfo.token
        },
        success: function (response) {
          wx.showToast({
            title: "提交成功！",
            duration: 2000,
            icon:"success"
          })
          that.setData({
            result2:[]
          })
        }
      })
    }
    
  },
  goSee(event){
    wx.navigateTo({
      url: 'detail/detail?id='+event.currentTarget.dataset.id+'&typeid='+this.data.showGroup,
    })
  },
  goSeeZC(event){
    wx.navigateTo({
      url: '/pages/Bookkeeping/seeBookZC/seeBookZC?id='+event.currentTarget.dataset.id,
    })
  },
  goUpdate(event){
    wx.navigateTo({
      url: 'edit/edit?id='+event.currentTarget.dataset.id+'&typeid='+this.data.showGroup+'&listindex='+event.currentTarget.dataset.index,
    })
  },
  goUpdateZC(event){
    wx.navigateTo({
      url: '/pages/Bookkeeping/updateZC/update?id='+event.currentTarget.dataset.id,
    })
  },
  delete(e){
    console.log(e);
    var that = this;
    UTIL.httpRequest(API.URL_GET_FLOWREMOVE + e.currentTarget.dataset.id, {method:'GET'},{
      success: (res) => {
        if (res.code == API.SUCCESS_CODE) {
          that.data.flowListSR.splice(e.currentTarget.dataset.index, 1); 
          wx.showToast({
            title: '删除成功！',
            icon: 'success',
            duration: 2000
          })
          that.setData({
            flowListSR : that.data.flowListSR,
            flowListSRNum:that.data.flowListSRNum-1
          })
        }
      }
    })
  },
  delete2(e){
    console.log(e);
    var that = this;
    UTIL.httpRequest(API.URL_GET_FLOWREMOVE + e.currentTarget.dataset.id, {method:'GET'},{
      success: (res) => {
        if (res.code == API.SUCCESS_CODE) {
          that.data.flowListZC.splice(e.currentTarget.dataset.index, 1); 
          wx.showToast({
            title: '删除成功！',
            icon: 'success',
            duration: 2000
          })
          that.setData({
            flowListZC : that.data.flowListZC,
            flowListZCNum:that.data.flowListZCNum-1
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

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

  },

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

  },

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

  },

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

  }
})