// pages/institutions/institutionslist.js
const app = getApp()
var utils = require('../../utils/util.js')
Page({

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

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getInstitutionsList('')
    },
    doLoadMore() {
        var _self = this
        if (_self.data.totalSize > _self.data.institutionsList.length) {
            _self.setData({
                currentPage: ++_self.data.currentPage
            })
            _self.getInstitutionsList(_self.data.searchKey)
        } else {
            wx.showToast({
                title: '无更多数据了',
                icon: 'none'
            })
        }
    },
    doNavigation(e) {
        var item = e.currentTarget.dataset.item
        if (item.institutionX == '' || item.institutionY == '') {
            wx.showToast({
                title: '机构坐标有误',
                icon: 'error'
            })
        } else {
            var locs = utils.bdMapToQQMap(item.institutionX, item.institutionY)
            var lng = locs[0]
            var lat = locs[1]
            wx.openLocation({
                latitude: Number(lat),
                longitude: Number(lng),
            })
        }
    },
    showDetail(e) {
        var item = e.currentTarget.dataset.item
        wx.navigateTo({
            url: './institutionsdetail?id=' + item.institutionId,
        })
    },
    doSearch(e) {
        this.setData({
            countTime: 2000,
            searchKey: e.detail.value,
        })
        //是否处于搜索倒计时中
        if (!this.data.searchWaiting) {
            this.timer();
        }
    },
    getInstitutionsList(key) {
        var _self = this
        app.restAjax.get(app.restAjax.path(app.apis.getInstitutionList, [app.baseUrls.cardUrl]), {
                page: _self.data.currentPage,
                keywords: key
            }, {},
            (code, data) => {
                wx.hideLoading({})
                if (code == 200) {
                    for (var i = 0; i < data.rows.length; i++) {
                        var item = data.rows[i]
                        if (item.institutionImage.length > 0) {
                            var photos = item.institutionImage.split(',')
                            data.rows[i].showPhoto = app.baseUrls.cardUrl + app.baseUrls.baseImgUrl + photos[0]
                        } else {
                            data.rows[i].showPhoto = '../../images/icons/institutions_default.jpeg'
                        }
                    }
                    if (_self.data.currentPage > 1) {
                        if (data.rows.length > 0) {
                            _self.data.institutionsList = _self.data.institutionsList.concat(data.rows)
                        }
                    } else {
                        _self.data.institutionsList = data.rows
                    }
                    _self.setData({
                        institutionsList: _self.data.institutionsList,
                        totalSize: data.total
                    })
                }
            }, (code, err) => {
                wx.hideLoading({})
            })
    },
    // 
    /**
     *  延迟搜索  
     */
    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.getInstitutionsList(_self.data.searchKey)
            clearInterval(setTimer) //清除计时器
        })
    },
})