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

  /**
   * 页面的初始数据
   */
  data: {
    isIPX: app.globalData.isIPX,
    expressionOptions:[],
    searchDate: {
      bookDate: "",
      templateName: '资产负债表',
      signature: false, // 添加底部落款
    },

    list: [],
    accountingYear: "",
    accountingMonth: "",
    // 显示搜索条件
    visible: false,
    showPickerTime: false,
    subjectName:'请选择科目查询',
    minDate: new Date().getTime(),
    maxDate: new Date().getTime(),
    currentDate: new Date(),
    date:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    UTIL.httpRequest(API.getLoginBook,{}, {
      success: (res) => {
        if (res.code == API.SUCCESS_CODE) {
          let currentDays = res.data.currentDay;
          if (currentDays == null) {
            UTIL.showToastNoneIcon("当前账套未开启!");
            return false;
          }

          let mindate = res.data.startDay.split('-')
          let dealDays = currentDays.split("-");
          this.setData({
            accountingYear:dealDays[0],
            accountingMonth:dealDays[1],
            date: dealDays[0]+'年'+dealDays[1]+'月',
            ['searchDate.bookDate']:currentDays,
            minDate: new Date(mindate[0],mindate[1]-1).getTime(),
          })
          this.initPage()
        }
      }
    });
    UTIL.httpRequest(API.expressionReportByCategory+'资产负债表',{}, {
      success: (res) => {
        if (res.code == API.SUCCESS_CODE) {
          let content = res.data;
          this.setData({
            expressionOptions:content
          })
        }
      }
    });
  },
  initPage() {
    var that = this;
    UTIL.httpRequest(API.assetLiabilityReportByExpTpl,this.data.searchDate, {
      success: (res) => {
        if (res.code == API.SUCCESS_CODE) {
          let content = res.data;
          this.setData({
            list:content.list.map((x) => {
              x.ncyeLeft = that.formatNum(x.ncyeLeft);
              x.qmyeLeft = that.formatNum(x.qmyeLeft);
              x.ncyeRight = that.formatNum(x.ncyeRight);
              x.qmyeRight = that.formatNum(x.qmyeRight);
              return x;
            })
          })
        }
      }
    });
  },
  formatNum(value) {
    if(value === undefined || value === null)
      return '';
    if(typeof(value) === "string")
    {
      if(value.indexOf(',') !== -1)
        return value;
      value = Number(value);
      if(isNaN(value))
        return '';
    }
    if(value === 0)
      return '';
    return this.numFormat(value);
  },
  numFormat(value) {
    if (value == null) {
      return "";
    }
    if (!value) return "0.00";
    value = value.toFixed(2);
    let is_neg = value < 0;
    var intPart = Math.abs(Math.trunc(value)); // 获取整数部分
    var intPartFormat = intPart.toString()
      .replace(/(\d)(?=(?:\d{3})+$)/g, "$1,"); // 将整数部分逢三一断
    if(is_neg)
      intPartFormat = '-' + intPartFormat;
    var floatPart = ".00"; // 预定义小数部分
    var value2Array = value.split(".");
    // =2表示数据有小数位
    if (value2Array.length === 2) {
      floatPart = value2Array[1].toString(); // 拿到小数部分
      if (floatPart.length === 1) {
        // 补0,实际上用不着
        return intPartFormat + "." + floatPart + "0";
      } else {
        return intPartFormat + "." + floatPart;
      }
    } else {
      return intPartFormat + floatPart;
    }
  },
  openPick(){
    this.setData({
      showPickerTime:true
    })
  },
  closePick(){
    this.setData({
      showPickerTime:false
    })
  },
  onConfirm(time) {
    console.log(time.detail);
    let date = UTIL.formatDates(time.detail).split('-');
    this.setData({
      date:date[0] + '年' + date[1] + '月',
      ['queryParams.startDate']:UTIL.formatDates(time.detail),
      showPickerTime : false
    })
    this.getList();
  },
  back:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  closePickEx(){
    this.setData({
      visible : false
    })
  },
  openPickEx(){
    this.setData({
      visible : true
    })
  },
  onConfirmExpression(data){
    console.log(data);
    this.setData({
      subjectName:data.detail.value.name,
      ['searchDate.templateName']: data.detail.value.name,
      visible : false
    })
    this.initPage();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})