// pages/personCenter/personCenter.js
import {
    getInfo,
    logout,
    applyActivity,
    limitCount,
    getActivityStatus
} from "../../api/api"
import {
    throttle
} from "../../utils/util"
import {
    createStoreBindings
} from "mobx-miniprogram-bindings"
import {
    store
} from '../../store/store.js'
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isLogin: false,
        showApply: false,
        showCancle: true,
        userInfo: {
            hospitalName: '',
            name: '',
            photo: '',
            passNum: '',
            waitNum: '',
            refuseNum: '',

        },
        waitSubmitNum: 0,
        img_host: app.hostConfig().imghost
    },
    onCancelApply() {
        this.setData({
            showApply: false
        })
    },
    onConfirmApply: throttle(function () {
        this.setData({
            showApply: false
        })
        this.handleApply();
    }),
    goApply() {
        getActivityStatus().then(result=>{
            if(result){
                if(result.status==1){
                    wx.showToast({
                        title:'恭喜您，您的病例交流活动申请已通过审核',
                        icon: 'none'
                    })
                    
                }else if(result.status==2){
                    wx.showToast({
                        title:'您的病例交流活动申请正在审核中',
                        icon: 'none'
                    })
                }
               
            }else{
                this.getLimit();
            }
        })     

    },
    getLimit(){
        limitCount({
            type:3
        }).then(result => {
            console.log(result);
            if (result < 5) {
                wx.showToast({
                    title: '病例审核通过5份及以上可申请病例交流活动',
                    icon: 'none'
                })
            } else {
                this.setData({
                    showApply:true
                })
                           
            }

        })
    },
    handleApply() {
        applyActivity().then(res => {
            // wx.showToast({
            //     title: '申请成功',
            //     icon: 'none'
            // })
        })
    },
    goLogin: throttle(function () {
        app.method.navigateTo({
            url: '/case/pages/mobileLogin/mobileLogin'
        })
    }),
    goAgree: throttle(function () {
        app.method.navigateTo({
            url: '/case/pages/agreement/agreement',
        })
    }),
    goBind: throttle(function () {
        app.method.navigateTo({
            url: '/case/pages/bankCard/bankCard',
        })
    }),
    isHasToken(){
        let tokenStr=''
        const { envVersion } = wx.getAccountInfoSync().miniProgram;
        if(envVersion=="develop" || envVersion=="trial"){
          tokenStr="DEV_CASE_TOKEN"
        }else{
          tokenStr="PROD_CASE_TOKEN"
        }
        let token = wx.getStorageSync(tokenStr);
        if(token){
            return true
        }else {
            return false
        }
       },
    goDescription: throttle(function () {
        app.method.navigateTo({
            url: '/case/pages/agreement/agreement?type=description',
        })
    }),
    handleLogout: throttle(function () {
        logout().then(res => {
            // wx.clearStorageSync()
            const {
                envVersion
            } = wx.getAccountInfoSync().miniProgram;
            let token = ''
            if (envVersion == "develop" || envVersion == "trial") {
                token = "DEV_CASE_TOKEN"
            } else {
                token = "PROD_CASE_TOKEN"
            }
            wx.setStorageSync(token, '');
            wx.setStorageSync('draftTime', '');
            wx.reLaunch({
                url: '/case/pages/mobileLogin/mobileLogin'
            })
        })
    }),
    handleGetInfo() {
        let {
            userInfo
        } = this.data;
        getInfo().then(res => {
            this.setData({
                isLogin: true
            });
            for (const key in userInfo) {
                this.setData({
                    ['userInfo.' + key]: res[key]
                })
            }
        }).catch(error => {
            if (error.code == 30007) {
                this.setData({
                    isLogin: false
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

        this.storeBindings = createStoreBindings(this, {
            store,
            fields: [, "active"],
            actions: ["updateActive"],
        });

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let caseDraft = wx.getStorageSync('caseDraft');
        if (caseDraft) {
            this.setData({
                waitSubmitNum: 1
            })
        } else {
            this.setData({
                waitSubmitNum: 0
            })
        }
       
        this.handleGetInfo();   
        this.updateActive('center')
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        this.storeBindings.destroyStoreBindings();
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    // onShareAppMessage() {

    // }
})