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

    /**
     * 页面的初始数据
     */
    data: {
        imgUrl: app.urls.baseImgUrl,
        cardList: [],
        otherCardList: [],
        historyList: [],
        tab: 0,
        oldArr: [],
        token: '',
        page: {
            rows: 10,
            page: 1
        },
        topHeight: app.globalData.CustomBar,
        isRefresh: false,
        isDelPage: false,
        //         <view class="tab-box {{tab == 0 ? 'active':''}}" data-tab="0" bindtap="changeTab">我的名片</view>
        //   <view class="tab-box {{tab == 1 ? 'active':''}}" data-tab="1" bindtap="changeTab">他人名片</view>
        //   <view class="tab-box {{tab == 2 ? 'active':''}}" data-tab="2" bindtap="changeTab">浏览历史</view>
        tabList: [{
            name: '我的名片'
        }, {
            name: '他人名片'
        }, {
            name: '浏览历史'
        }]
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            token: app.globalData.token
        })
        this.getList()
    },

    onShow: function () {
        if (this.data.isRefresh) {
            this.setData({
                isRefresh: false
            })
            wx.startPullDownRefresh({})
        }
    },
    // 我的名片列表
    getList: function () {
        var self = this
        wx.showLoading({
            title: '加载中...',
        })
        wx.showNavigationBarLoading()
        app.http.get(app.urls.getMyCard, {
            header: {
                token: self.data.token
            },
            data: {}
        }).then(res => {
            wx.hideLoading({})
            self.setData({
                cardList: res.data
            })
            if (res.data.length > 0) {
                if (self.data.isDelPage) {
                    self.setData({
                        isDelPage: false
                    })
                    self.setDefaultCard(res.data[0].cardTemplateUseId)
                    //设置第一条为主名片
                }
            }
            wx.hideNavigationBarLoading() //完成停止加载
            wx.stopPullDownRefresh() //停止下拉刷新
        }).catch(res => {
            wx.hideLoading({})
            wx.hideNavigationBarLoading() //完成停止加载
            wx.stopPullDownRefresh() //停止下拉刷新
        })
    },
    // 查看详情
    goDetail: function (e) {
        var id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '../myCardDetail/myCardDetail?id=' + id
        })
    },
    // 获取他人名片列表
    getOtherCard: function () {
        wx.showLoading({
            title: '加载中...',
        })
        var self = this
        app.http.get(app.urls.otherCardList, {
                header: {
                    token: self.data.token
                },
                data: {}
            }).then(res => {
                wx.hideLoading({})
                wx.stopPullDownRefresh({})
                var curArr
                if (res.data.rows.length > 0) {
                    curArr = self.data.otherCardList.concat(res.data.rows)
                    self.setData({
                        otherCardList: curArr
                    })
                } else {
                    self.setData({
                        [page.page]: self.data.page.page - 1
                    })
                }
            })
            .catch(err => {
                wx.hideLoading({})
                wx.stopPullDownRefresh({})
            })
    },
    getHistory() {
        wx.showLoading({
            title: '加载中...',
        })
        var _self = this
        app.http.get(app.urls.getBrowseHistoryList, {
                header: {
                    token: app.globalData.token
                }
            })
            .then(res => {
                wx.hideLoading({})
                wx.stopPullDownRefresh({})
                var curArr
                if (res.data.rows.length > 0) {
                    curArr = _self.data.historyList.concat(res.data.rows)
                    _self.setData({
                        historyList: curArr
                    })
                } else {
                    let key = `_self.data.page.page`
                    _self.setData({
                        [key]: _self.data.page.page - 1
                    })
                }
            })
            .catch(err => {
                wx.hideLoading({})
                wx.stopPullDownRefresh({})
                console.log(err)
            })
    },
    // 切换
    changeTab(e) {
        var cur = e.detail.index
        if (this.data.tab != cur) {
            this.setData({
                tab: cur
            })
            if (cur == 0) {
                this.setData({
                    cardList: [],
                    page: {
                        rows: 10,
                        page: 1
                    }
                })
                this.getList()
            } else if (cur == 1) {
                this.setData({
                    otherCardList: [],
                    page: {
                        rows: 10,
                        page: 1
                    }
                })
                this.getOtherCard()
            } else if (cur == 2) {
                this.setData({
                    historyList: [],
                    page: {
                        rows: 10,
                        page: 1
                    }
                })
                this.getHistory()
            }
        }
    },
    // 名片置顶
    toggleTop: function (e) {
        var self = this
        var bagId = e.currentTarget.dataset.bag
        var isTop = e.currentTarget.dataset.top
        if (isTop == '0') {
            isTop = true
        } else {
            isTop = false
        }
        app.http.put(app.urls.setTop.format({
            cardTemplateUseBagId: bagId,
            cardTemplateUseTop: isTop
        }), {
            header: {
                token: app.globalData.token
            },
            data: {}
        }).then(res => {
            console.log(res.data)
            wx.showToast({
                title: '设置成功！',
            })
            self.setData({
                otherCardList: []
            })
            self.getOtherCard()
        })
    },


    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        if (this.data.tab == '0') {
            this.setData({
                cardList: [],
                page: {
                    rows: 10,
                    page: 1
                }
            })
            this.getList()
        } else if (this.data.tab == '1') {
            this.setData({
                otherCardList: [],
                page: {
                    rows: 10,
                    page: 1
                }
            })
            this.getOtherCard()
        } else if (this.data.tab == '2') {
            this.setData({
                historyList: [],
                page: {
                    rows: 10,
                    page: 1
                }
            })
            this.getHistory()
        }

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var self = this
        if (this.data.tab == 1) {
            this.setData({
                [page.page]: self.data.page.page++
            })
            this.getOtherCard()
        }
    },
    showCard(e) {
        wx.navigateTo({
            url: '../otherCard/othercarddetail?cardTemplateUseId=' + e.currentTarget.dataset.id,
        })
    },
    setDefaultCard(id) {
        var self = this
        app.http.put(app.urls.setDefaultCard.format({
            cardTemplateUseId: id
        }), {
            header: {
                token: self.data.token
            }
        }).then(res => {
            wx.startPullDownRefresh({})
        }).catch(res => {})
    },
})