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

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

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getResultsList('')
    },
    showDetail(e) {
        var item = e.currentTarget.dataset.item
        wx.navigateTo({
            url: './answerdetail?id=' + item.contentId,
        })
    },
    doLoadMore() {
        var _self = this
        if (_self.data.totalSize > _self.data.answerList.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) {
        var _self = this
        app.restAjax.get(app.restAjax.path(app.apis.getArticleList, [app.baseUrls.cardUrl]), {
                categoryId: '494bf3cc-cab6-4710-a0b2-d84f4591c992',
                page: _self.data.currentPage,
                rows: '10',
                keywords: key
            }, {},
            function (code, data) {
                wx.hideLoading({})
                if (code == 200) {
                    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 = '../../images/icons/answer_default.jpeg'
                        }
                    }
                    if (_self.data.currentPage > 1) {
                        if (data.rows.length > 0) {
                            _self.data.answerList = _self.data.answerList.concat(data.rows)
                        }
                    } else {
                        _self.data.answerList = data.rows
                    }
                    _self.setData({
                        answerList: _self.data.answerList,
                        totalSize: data.total
                    })
                }
            },
            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: '搜索中...',
            })
            _self.getResultsList(_self.data.searchKey)
            clearInterval(setTimer) //清除计时器
        })
    },
})