// 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:true,
    flowList:[],
    showProjectFundType:false,
    checkedStatusText:'未提交',
    checkedStatus:'1',
    incomeExpensesType:'1'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'checked_status', {method:'GET'}, {
      success: (res) => {
        that.setData({
          capitalExpenditureTypeOptions:res.data,
        })
      }
    })
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'income_type', {method:'GET'}, {
      success: (res) => {
        that.setData({
          incomeTypeOptions:res.data,
        })
      }
    })
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'capital_expenditure_type', {method:'GET'}, {
      success: (res) => {
        that.setData({
          expenditureTypeOptions:res.data,
        })
      }
    })
    that.SRSelect();
    that.ZCSelect();
  },
  SRSelect(){
    var that = this;
    //收入事项
    let data = {
      pageNum:'1',
      pageSize:'999',
      accountType:'',
      checkedStatus:that.data.checkedStatus,
      incomeExpensesType:'1',
    }
    UTIL.httpRequest(API.URL_GET_GETFLOWLIST,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].checkedStatusText = UTIL.getTransform(element.checkedStatus,that.data.capitalExpenditureTypeOptions);
            list[i].incomeTypeText = UTIL.getTransform(element.incomeType,that.data.incomeTypeOptions);
            list[i].jieAmount = Number(element.jieAmount).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, ($0, $1) => {
              return $1 + ","; }).replace(/\.$/, "");
          }
          this.setData({
            flowListSR:list,
            flowListSRNum:res.total
          });
        }
      }
    })
  },
  ZCSelect(){
    var that = this;
    //支出事项
    let data2 = {
      pageNum:'1',
      pageSize:'999',
      accountType:'',
      checkedStatus:that.data.checkedStatus,
      incomeExpensesType:'2',
    }
    UTIL.httpRequest(API.URL_GET_GETFLOWLIST,data2 ,{
      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].checkedStatusText = UTIL.getTransform(element.checkedStatus,that.data.capitalExpenditureTypeOptions);
            list[i].expenditureTypeText = UTIL.getTransform(element.expenditureType,that.data.expenditureTypeOptions);
            list[i].daiAmount = Number(element.daiAmount).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, ($0, $1) => {
              return $1 + ","; }).replace(/\.$/, "");
          }
          this.setData({
            flowListZC:list,
            flowListZCNum:res.total
          });
        }
      }
    })
  },
  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,
      result:[],
      result2:[],
    })
    if(e.currentTarget.dataset.gid){
      //收入事项
      let data = {
        pageNum:'1',
        pageSize:'999',
        accountType:'',
        checkedStatus:this.data.checkedStatus,
        incomeExpensesType:'1',
      }
      UTIL.httpRequest(API.URL_GET_GETFLOWLIST,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].checkedStatusText = UTIL.getTransform(element.checkedStatus,that.data.capitalExpenditureTypeOptions);
              list[i].incomeTypeText = UTIL.getTransform(element.incomeType,that.data.incomeTypeOptions);
              list[i].jieAmount = Number(element.jieAmount).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, ($0, $1) => {
                return $1 + ","; }).replace(/\.$/, "");
            }
            this.setData({
              flowListSR:list,
              flowListSRNum:res.total
            });
          }
        }
      })
    }else{
      let data2 = {
        pageNum:'1',
        pageSize:'999',
        accountType:'',
        checkedStatus:this.data.checkedStatus,
        incomeExpensesType:'2',
      }
      UTIL.httpRequest(API.URL_GET_GETFLOWLIST,data2 ,{
        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].checkedStatusText = UTIL.getTransform(element.checkedStatus,that.data.capitalExpenditureTypeOptions);
              list[i].expenditureTypeText = UTIL.getTransform(element.expenditureType,that.data.expenditureTypeOptions);
              list[i].daiAmount = Number(element.daiAmount).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, ($0, $1) => {
              return $1 + ","; }).replace(/\.$/, "");
            }
            this.setData({
              flowListZC:list,
              flowListZCNum:res.total
            });
          }
        }
      })
    }
    
  },
  back:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this ;
    that.SRSelect();
    that.ZCSelect();
  },
  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,
      result:[],
      result2:[]
    });
      let data = {
        pageNum:'1',
        pageSize:'999',
        accountType:'',
        checkedStatus:event.detail.value.dictValue,
        incomeExpensesType:'1',
      }
      UTIL.httpRequest(API.URL_GET_GETFLOWLIST,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];
              element.checkedStatusText = UTIL.getTransform(element.checkedStatus,that.data.capitalExpenditureTypeOptions);
              element.incomeTypeText = UTIL.getTransform(element.incomeType,that.data.incomeTypeOptions);
              element.jieAmount = Number(element.jieAmount).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, ($0, $1) => {
                return $1 + ","; }).replace(/\.$/, "");
            }
            this.setData({
              flowListSR:list,
              flowListSRNum:res.total
            });
          }
        }
      })
      let data2 = {
        pageNum:'1',
        pageSize:'999',
        accountType:'',
        checkedStatus:event.detail.value.dictValue,
        incomeExpensesType:'2',
      }
      UTIL.httpRequest(API.URL_GET_GETFLOWLIST,data2 ,{
        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].checkedStatusText = UTIL.getTransform(element.checkedStatus,that.data.capitalExpenditureTypeOptions);
              list[i].expenditureTypeText = UTIL.getTransform(element.expenditureType,that.data.expenditureTypeOptions);
              list[i].daiAmount = Number(element.daiAmount).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, ($0, $1) => {
                return $1 + ","; }).replace(/\.$/, "");
            }
            this.setData({
              flowListZC:list,
              flowListZCNum:res.total
            });
          }
        }
      })
    
  },
  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 = 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 = 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:[]
          })
          that.ZCSelect();
        }
      })
    }
    
  },
  goSee(event){
    wx.navigateTo({
      url: '/pages/Bookkeeping/seeBook/seeBook?id='+event.currentTarget.dataset.id,
    })
  },
  goSeeZC(event){
    wx.navigateTo({
      url: '/pages/Bookkeeping/seeBookZC/seeBookZC?id='+event.currentTarget.dataset.id,
    })
  },
  goUpdate(event){
    wx.navigateTo({
      url: '/pages/Bookkeeping/update/update?id='+event.currentTarget.dataset.id,
    })
  },
  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() {

  }
})