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

  /**
   * 页面的初始数据
   */
  data: {
    isIPX: app.globalData.isIPX,
    list:[]
  },
  goList(e){
    let type = e.currentTarget.dataset.type;
    if(type == 'yeb'){
      wx.navigateTo({
        url: 'list_balance_ranking/list_balance_ranking',
      })
    }
    if(type == 'fzb'){
      wx.navigateTo({
        url: 'detailed_liabilities/detailed_liabilities',
      })
    }
    if(type == 'gkb'){
      wx.navigateTo({
        url: 'detailed_incomeAndExpenditurePublic/detailed_incomeAndExpenditurePublic',
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  back:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this;
    that.setData({
      imgUrl:wx.getStorageSync('dressCode')
    })
    UTIL.httpRequest(API.URL_GET_GETOPENLIST, {method:'GET'},{
      success: (res) => {
        if(res.code == 200){
          for (let i = 0; i < res.rows.length; i++) {
            const element = res.rows[i];
            if(element.openPic){
              element.openPic = element.openPic.split(',');
            }
            if(element.openFile){
              element.openFile = element.openFile.split(',');
            }
          }
          // wx.getStorageSync('dressCode')+
          if (res.rows.length>0) {
            that.setData({
              bookId:res.rows[0].bookId,
              deptId:res.rows[0].deptId,
            })
          }
          
          console.log(res.rows);
          that.setData({
            list:res.rows
          })
        }else{
          UTIL.showToastNoneIcon(res.msg);
        }
      }
    })
  },
  openPreview(e){
    let array = [];
    array.push(e.currentTarget.dataset.url)
    wx.previewImage({
      urls: array,
      showmenu:true,
    })
  },
  // 预览文件
  previewFile(e) {
    if(!e.currentTarget.dataset.url) {
        return false
    }
    UTIL.showLoading()
  
    // 单次下载允许的最大文件为 200MB
    wx.downloadFile({
        url: e.currentTarget.dataset.url,
        success: function (res) {
            console.log(res, "wx.downloadFile success res")
            if(res.statusCode != 200) {
                UTIL.hideLoadingWithErrorTips()
                return false
            }
            var Path = res.tempFilePath //返回的文件临时地址，用于后面打开本地预览所用
            wx.openDocument({
                filePath: Path,
                showMenu: true,
                success: function (res) {
                    console.log('打开成功');
                    UTIL.hideLoadings()
                }
            })
        },
        fail: function (err) {
            console.log(err, "wx.downloadFile fail err");
            UTIL.hideLoadingWithErrorTips()
        }
    })
  
  },
  goSearch(e){
    var that = this;
    let query = {
      openName:e.detail,
      bookId:that.data.bookId,
      deptId:that.data.deptId,
      method:'GET'
    }
    UTIL.httpRequest(API.URL_GET_GETOPENLIST,query,{
      success: (res) => {
        if(res.code == 200){
          for (let i = 0; i < res.rows.length; i++) {
            const element = res.rows[i];
            element.openPic = wx.getStorageSync('dressCode')+element.openPic;
            element.openFile = wx.getStorageSync('dressCode')+element.openFile;
          }
          that.setData({
            list:res.rows
          })
        }else{
          UTIL.showToastNoneIcon(res.msg);
        }
      }
    })
  },
  goFinace(e){
    let date = e.currentTarget.dataset.date;
    let bookId = e.currentTarget.dataset.bookid;
    let deptId = e.currentTarget.dataset.deptid;
    wx.navigateTo({
      url: 'finance?date='+date+'&bookId='+bookId+'&deptId='+deptId,
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

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

  },

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

  },

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

  },

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

  }
})