import {
    fileList,
    readFile,
    findCountByType,
} from '../../api/projectFiles'

const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        initData: {},
        listData: [],
        qbCount: 0,
        sjbmCount: 0,
        jtgsCount: 0,
        zgsCount: 0,
        activeState: "1",
        pageNum: 1,
        pageSize: 10,
        lastDataSize: 10,
        list: [],
        deptIds:"",
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            initData: {
                text: app.globalData.projectName,
                id: app.globalData.projectId
            },
            pageNum: 1,
            pageSize: 10,
            lastDataSize: 10,
            listData: []
        })
        //获取缓存数据
    wx.getStorage({
        key: 'userinfo',
        success: res => {
            let fileDepts = "";
            if(res.data.minRoleId>3){
                if(res.data.minRoleId==4){
                    fileDepts += app.globalData.projectInfoList[0].deptId;
                }else{
                    app.globalData.projectInfoList.forEach(pro =>{
                        fileDepts += pro.projectInfoList[0].deptId;
                    });
                }
            }
          this.setData({
            deptIds:fileDepts
          })
          this.getListData();
        }
      })
    },

    /**
     * 查询文件列表
     */
    getListData() {
        //进入这里说明数据加载完毕
        if (this.data.lastDataSize < this.data.pageSize) {
            //app.toast("已经到底了,暂无可继续加载数据!")
            return;
        }
        let that = this;
        
        this.queryCount();
        fileList({
            fileBelong: this.data.activeState,
            pageNum: this.data.pageNum,
            pageSize: this.data.pageSize,
            remark:this.data.deptIds
        }).then(res => {
            //这里处理this.data.lastDataSize=this.data.pageSize
            if (that.data.list.length > 0 && res.rows.length > 0 && that.data.list[0].id == res.rows[0].id) {
                that.setData({
                    lastDataSize: 0,
                })
            } else {
                that.setData({
                    pageNum: that.data.pageNum + 1,
                    lastDataSize: res.rows.length,
                    list: res.rows,
                    listData: that.data.listData.concat(res.rows)
                })
            }
        });
    },

    /**
     * 统计文件传达
     */
    queryCount() {
        let that = this;
        findCountByType({remark:this.data.deptIds}).then(res => {
            if (res.data.length > 0) {
                let sjbm = 0,
                    jtgs = 0,
                    zgs = 0;
                res.data.forEach(item => {
                    if (item.fileBelong == "1") {
                        sjbm += item.total;
                    } else if (item.fileBelong == "2") {
                        jtgs += item.total;
                    } else {
                        zgs += item.total;
                    }
                });
                that.setData({
                    sjbmCount: sjbm,
                    jtgsCount: jtgs,
                    zgsCount: zgs
                })
            }
        });
    },

    /**
     * 加载新数据
     */
    onScrollToLower() {
        console.log("滚动条到底了,开始加载新数据");
        this.getListData();
    },

    /**
     * 下载并打开文档
     * @param {*} e 
     */
    openFile: function (e) {
        let {
            id,filePath
        } = e.currentTarget.dataset.set
        readFile(id);
        wx.downloadFile({
            // 示例 url,并非真实存在
            url: app.globalData.uploadUrl + '/common/download/resource?resource=' + filePath,
            success: function (res) {
                const _path = res.tempFilePath
                let fpt = _path.split(".");
                wx.openDocument({
                    filePath: _path,
                    fileType: fpt[fpt.length - 1],
                    success: function (res) {
                        console.log('打开文档成功')
                    },
                    fail: function (res) {
                        console.log(res)
                    }
                })
            }
        })
        //app.toast("暂不支持下载!如需下载请前往后台管理系统!!")
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },
    /**
     * 标签切换
     */
    trainingTypeJump(e) {
        let index = e.currentTarget.dataset.index;
        this.setData({
            activeState: index + '',
            pageNum: 1,
            pageSize: 10,
            lastDataSize: 10,
            list: [],
            listData: [],
        });

        this.getListData();
    },

    /**
     * 项目切换
     * @param {*} e 
     */
    onProjectSelect(e) {
        let projectId = e.detail.id;
        let projectName = e.detail.text;
        app.globalData.projectId = projectId;
        app.globalData.projectName = projectName;
        this.onLoad();
    },

    returnToPage: function () {
        wx.redirectTo({
            url: '../../pages/safety_manage/index',
        })
    }
})