import {
    ApiModel
} from "../../../utils/api";
const _Http = new ApiModel();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        tabsActive: 1, //tabs 下标
        msgList: [],
        pageNumber: 1,
        pageTotal: 1
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getList()
        setTimeout(() => {
            this.setHeight();
        }, 500);
    },
    setHeight() {
        const that = this,
            h = getApp().globalData.myNavBorHeight + getApp().globalData.safeAreaBottom;
        wx.getSystemInfo({
            success(res) {
                that.setData({
                    scrollH: res.windowHeight - h - 7
                })
            }
        })
    },
    /* 阅读信息 */
    readMsg(e) {
        const {
            index,
            id,
            fisread
        } = e.currentTarget.dataset;
        if (fisread == 0) _Http.basic({
            "accesstoken": wx.getStorageSync('userData').token,
            "classname": "system.message.Message",
            "method": "readMessage",
            "content": {
                "tmessageid": id
            }
        }).then(res => {
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
            });
            const name = 'msgList[' + index + '].fisread';
            this.setData({
                [name]: 1
            })
            //更新徽标
            this.selectComponent("#gxshuju").unReadMessageCount();
            setTimeout(() => {
                this.getTabBar().setData({
                    'tabbarList[3].fcount': getApp().globalData.msgFcount
                })
            }, 500)
        });
    },
    /* tabs切换 */
    tabsOnChange(e) {
        const {
            index,
            title
        } = e.detail;
        this.setData({
            tabsActive: index,
            pageNumber: 1,
            pageTotal: 1
        })
        this.getList()
    },
    /* 列表请求 */
    getList() {
        let type = "公共";
        switch (this.data.tabsActive) {
            case 1:
                type = "商户";
                break;
            case 2:
                type = "团队";
                break;
            default:
                break;
        }
        _Http.basic({
            "accesstoken": wx.getStorageSync('userData').token,
            "classname": "system.message.Message",
            "method": "queryMessage",
            "content": {
                "getdatafromdbanyway": true,
                "pageNumber": this.data.pageNumber,
                "pageSize": 20,
                "ftype": type
            }
        }).then(res => {
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: 'none'
            });
            let data = res.data,
                date = new Date(),
                opt = {
                    "Y": date.getFullYear().toString(), // 年
                    "m": (date.getMonth() + 1).toString(), // 月
                    "d": date.getDate().toString(), // 日
                    "H": date.getHours().toString(), // 时
                    "M": date.getMinutes().toString(), // 分
                    "S": date.getSeconds().toString() // 秒
                };
            for (let i = 0; i < data.length; i++) {
                let arr = data[i].createdate.split(' '),
                    YmD = arr[0].split('-'), //年月日
                    HM = arr[1].slice(0, arr[1].lastIndexOf(':')), //小时，分钟
                    optm = parseInt(opt.m),
                    m = parseInt(YmD[1]),
                    optd = parseInt(opt.d),
                    d = parseInt(YmD[2])

                //同年
                if (parseInt(opt.Y) == parseInt(YmD[0])) {
                    if (opt.m > 10) opt.m = "0" + opt.m;
                    //同月
                    if (optm == m) {
                        if (optd == d) {
                            data[i].time = '今天 ' + HM;
                        } else if (optd - d == 1) {
                            data[i].time = '昨天 ' + HM;
                        } else if (optd - d == 2) {
                            data[i].time = '前天 ' + HM;
                        } else if (optd - d >= 3) {
                            data[i].time = '三天前'
                        } else if (optd - d >= 7) {
                            data[i].time = '七天前'
                        }
                    } else {
                        //不同月
                        if (optm - m == 1) {
                            data[i].time = '1月前'
                        } else if (optm - m == 2) {
                            data[i].time = '2月前'
                        } else if (optm - m >= 3 && opt.m - YmD[1] < 6) {
                            data[i].time = '3月前'
                        } else if (optm - m >= 6) {
                            data[i].time = '半年前'
                        }
                    }
                } else {
                    data[i].time = YmD[0] + '年'
                }
            };
            let msgList = data;
            if (this.data.pageNumber != 1) {
                msgList = this.data.msgList.concat(data);
            };
            this.setData({
                msgList,
                pageTotal: res.pageTotal
            })
        })
    },
    /* 上拉触底 加载数据 */
    listLoadMore() {
        if (this.data.pageTotal > this.data.pageNumber) {
            this.setData({
                pageNumber: this.data.pageNumber + 1
            })
        } else {
            return
        };
        this.getList();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getTabBar().init();
        this.selectComponent("#gxshuju").unReadMessageCount();
        setTimeout(() => {
            this.getTabBar().setData({
                'tabbarList[3].fcount': getApp().globalData.msgFcount
            })
        }, 500)
    },

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

    },

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

    },

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

    },

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

    },

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

    }
})