import config from '../../../config'
import {
    getToken
} from '../../../utils/auth'
import {
    quueryCount,
    myAwaitFlowTaskList,
    myFinishedFlowTaskList,
} from '../../../api/flowable'
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        projectId: '',
        projectName: '',
        initData: {},
        activeState: "dsh",
        dshCount: 0,
        yshCount: 0,
        listData: [],
        pageNum: 1,
        pageSize: 10,
        total: 0,
        imgBaseUrl: config.baseImgUrl
    },

    //项目切换 返回值
    onProjectSelect(e) {
        let projectId = e.detail.id;
        let projectName = e.detail.text;
        app.globalData.useProjectId = projectId;
        app.globalData.useProjectName = projectName;
        this.onLoad();
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if (getToken()) {
            this.setData({
                pageNum: 1,
                pageSize: 10,
                listData: [],
                total: 0,
                dshCount: 0,
                yshCount: 0,
                activeState: "dsh",
                projectId: app.globalData.useProjectId,
                projectName: app.globalData.useProjectName,
                initData: {
                    id: app.globalData.useProjectId,
                    text: app.globalData.useProjectName,
                }
            });
            this.getListData();
            this.getFlowableCount();
        } else {
            console.log("未查询到Token...{}...准备重新登录")
            wx.redirectTo({
                url: '../login/login',
            })
        }
    },

    /**
     * 查询列表数据
     */
    getListData() {
        let params = "pageNum=" + this.data.pageNum + "&pageSize=" + this.data.pageSize + "&activeTags=depts&startProId=" + app.globalData.useProjectId;
        if (this.data.activeState == 'dsh') {
            myAwaitFlowTaskList(params).then(res => {
                if (res.code == "200") {
                    res.rows.forEach(item => {
                        item.businessImg = this.data.imgBaseUrl + item.businessImg;
                        item.durationStr = this.getDurationDate(item.duration);
                    });
                    this.setData({
                        total: res.total,
                        listData: this.data.listData.concat(res.rows)
                    })
                }
            });
        } else {
            myFinishedFlowTaskList(params).then(res => {
                if (res.code == "200") {
                    res.rows.forEach(item => {
                        item.businessImg = this.data.imgBaseUrl + item.businessImg;
                        item.durationStr = this.getDurationDate(item.duration);
                    });
                    this.setData({
                        total: res.total,
                        listData: this.data.listData.concat(res.rows)
                    })
                }
            });
        }
    },

    /**
     * 统计分包单位数据
     */
    getFlowableCount() {
        let params = "activeTags=depts&startProId=" + app.globalData.useProjectId;
        quueryCount(params).then(res => {
            if (res.code == 200) {
                this.setData({
                    dshCount: res.data.await,
                    yshCount: res.data.finished
                })
            }
        });
    },

    /**
     * 统计审批数据
     */
    onScrollToLower() {
        let nal = Math.ceil(this.data.total / this.data.pageSize);
        if (this.data.pageNum < nal) {
            this.setData({
                pageNum: this.data.pageNum + 1
            });
            this.getListData();
        }
    },

    /**
     * 切换页签数据
     */
    switchTabJump(e) {
        let index = e.currentTarget.dataset.index;
        let nav = "";
        if (index == 1) {
            nav = 'dsh';
        } else {
            nav = 'ysh';
        }
        if (nav != this.data.activeState) {
            this.setData({
                activeState: nav,
                pageNum: 1,
                pageSize: 10,
                listData: [],
            });
            this.getListData();
        }
    },

    /**
     * 点击栏目
     * @param {*} e 
     */
    findDetail(e) {
        let {
            taskId,
            taskName,
            procInsId,
            deployId,
            category,
            businessKey,
            finishTime
        } = e.currentTarget.dataset.set
        wx.setStorageSync('nav-types', "depts");
        if (finishTime) {
            //详情页面
            wx.redirectTo({
                url: `../detailTask/index?taskId=${taskId}&taskName=${taskName}&procInsId=${procInsId}&deployId=${deployId}&category=${category}&businessKey=${businessKey}`
            })
        } else {
            //审批页面
            wx.redirectTo({
                url: `../approveTask/index?taskId=${taskId}&taskName=${taskName}&procInsId=${procInsId}&deployId=${deployId}&category=${category}&businessKey=${businessKey}`
            })
        }
    },

    /**
     * 办理时间计算
     * @param {*} val 
     */
    getDurationDate(val) {
        let day = 0;
        let hours = 0;
        let min = val;
        if (min > 1440) {
            day = parseInt(min / 1440);
            min = min % 1440;
            if (min > 60) {
                hours = parseInt(min / 60);
                min = min % 60;
            }
        } else if (min > 60) {
            hours = parseInt(min / 60);
            min = min % 60;
        }
        if (day > 0) {
            if (day < 10) day = "0" + day;
            if (hours < 10) hours = "0" + hours;
            if (min < 10) min = "0" + min;
            return day + "天" + hours + "小时" + min + "分钟";
        }
        if (hours > 0) {
            if (hours < 10) hours = "0" + hours;
            if (min < 10) min = "0" + min;
            return hours + "小时" + min + "分钟";
        }
        if (min > 0) {
            if (min < 10) min = "0" + min;
            return min + "分钟";
        }
        if (min == 0) {
            return "1分钟";
        }
    },

    /**
     * 返回页面
     */
    returnToPage: function () {
        wx.redirectTo({
            url: '../../project_more/index',
        })
    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})