// pages/notices/noticeslist.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        noticeList: [],
        CustomBar: app.globalData.CustomBar,
        countTime: 2000, //延迟搜索 时间
        searchWaiting: false, //是否等待搜索倒计时中,
        searchKey: '',
        contentHeight: app.globalData.windowHeight,
        currentPage: 1,
        totalSize: 0,
        categoryId: '',
        pageTitle: '最新公告'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.index == 1) {
            this.setData({
                categoryId: '87a51bb1-451b-4b49-903b-4d847ba9c730',
                pageTitle: '最新公告'
            })
        } else {
            this.setData({
                categoryId: '92737534-c2ef-487c-b763-4d1fb597388f ',
                pageTitle: '考试政策'
            })
        }
        this.getResultsList('')
    },
    showDetail(e) {
        var item = e.currentTarget.dataset.item
        wx.navigateTo({
            url: './noticedetail?id=' + item.contentId,
        })
    },
    doLoadMore() {
        var _self = this
        if (_self.data.totalSize > _self.data.noticeList.length) {
            _self.setData({
                currentPage: ++_self.data.currentPage
            })
            _self.getResultsList(_self.data.searchKey)
        } else {
            wx.showToast({
                title: '无更多数据了',
                icon: 'none'
            })
        }
    },
    doSearch(e) {
        this.setData({
            countTime: 2000,
            searchKey: e.detail.value,
        })
        //是否处于搜索倒计时中
        if (!this.data.searchWaiting) {
            this.timer();
        }
    },
    getResultsList(key) {
        console.log(key)
        var _self = this
        app.restAjax.get(app.restAjax.path(app.apis.getArticleList, [app.baseUrls.cardUrl]), {
                categoryId: _self.data.categoryId,
                page: _self.data.currentPage,
                rows: '10',
                keywords: key
            }, {},
            function (code, data) {
                wx.hideLoading({})
                if (code == 200) {
                    if (data.rows.length > 0) {
                        for (var i = 0; i < data.rows.length; i++) {
                            var item = data.rows[i]
                            if (item.coverPhotos.length > 0) {
                                var photos = item.coverPhotos.split(',')
                                data.rows[i].showPhoto = app.baseUrls.cardUrl + app.baseUrls.baseImgUrl + photos[0]
                            } else {
                                data.rows[i].showPhoto = 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.czcaizhi.com%2FContent%2FUploadFiles%2Fimage%2F20180920%2F20180920133956_3429.jpg&refer=http%3A%2F%2Fwww.czcaizhi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1622110389&t=4ae9d4f943c30ac6d43801515eec97e5'
                            }
                        }
                        if (_self.data.currentPage > 1) {
                            if (data.rows.length > 0) {
                                _self.data.noticeList = _self.data.noticeList.concat(data.rows)
                            }
                        } else {
                            _self.data.noticeList = data.rows
                        }
                        _self.setData({
                            noticeList: _self.data.noticeList,
                            totalSize: data.total
                        })
                    } else {
                        _self.setData({
                            currentPage: _self.data.currentPage--
                        })
                    }
                }
            },
            function (code, err) {
                wx.hideLoading({})
                console.log(err)
            })
    },
    /**
     *  延迟搜索  
     */
    timer() {
        var _self = this;
        this.setData({
            currentPage: 1,
            totalSize: 0,
            searchWaiting: true
        })
        let promise = new Promise((resolve, reject) => {
            let setTimer = setInterval(
                () => {
                    console.log('搜索倒计时: ' + _self.data.countTime);
                    _self.setData({
                        countTime: _self.data.countTime - 1000
                    })
                    if (_self.data.countTime <= 0) {
                        console.log('开始搜索: ' + _self.data.searchKey);

                        _self.setData({
                            countTime: 2000,
                            searchWaiting: false,
                        })
                        resolve(setTimer)
                    }
                }, 1000)
        })
        promise.then((setTimer) => {
            wx.showLoading({
                title: '搜索中...',
            })
            console.log(_self.data.searchKey)
            _self.getResultsList(_self.data.searchKey)
            clearInterval(setTimer) //清除计时器
        })
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var self = this
        this.setData({
            currentPage: self.data.currentPage++
        })
        this.getResultsList('')
    },
})