// pages/results/resultslist.js
const app = getApp()
Page({

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

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.checkInfo()
    },
    getResultsList(key) {
        var _self = this
        wx.showLoading({
            title: '加载中...',
        })
        app.restAjax.get(app.restAjax.path(app.apis.getAchievement + _self.data.idCardNumber, [app.baseUrls.cardUrl]), {}, {
                headers: {
                    token: app.globalData.token
                }
            },
            (code, data) => {
                wx.hideLoading({})
                if (code == 200) {
                    _self.setData({
                        resultsList: data
                    })
                }
            }, (code, error) => {
                wx.hideLoading({})
                wx.showToast({
                    title: '网络错误',
                    icon: 'error'
                })
            })
    },
    showDetail(e) {
        // var item = e.currentTarget.dataset.item
        // wx.navigateTo({
        //     url: './resultsdetail',
        // })
    },
    doSearch(e) {
        this.setData({
            countTime: 2000,
            searchKey: e.detail.value,
        })
        //是否处于搜索倒计时中
        if (!this.data.searchWaiting) {
            this.timer();
        }
    },
    checkInfo: function () {
        var self = this
        app.restAjax.get(app.restAjax.path(app.apis.checkEditedInfo, [app.baseUrls.testUrl]), {}, {
            headers: {
                token: wx.getStorageSync('token')
            }
        },
          function (code, data) {
            if (!data.idCardNumber) {
                wx.showToast({
                  icon:'none',
                  title: '请先前往个人中心填写个人信息！',
                })
                setTimeout(function () {
                    wx.reLaunch({
                      url: '../index/index',
                    })
                }, 1500)
            } else {
                self.setData({
                    idCardNumber: data.idCardNumber
                })
                self.getResultsList('')
            }
          },
          function (code, err) {
            console.log(err)
          })
    },
    /**
     *  延迟搜索  
     */
    timer() {
        var _self = this;
        this.setData({
            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) => {
            _self.getResultsList(_self.data.searchKey)
            clearInterval(setTimer) //清除计时器
        })
    },
})