<template>
    <view class="mine-index">
        <template v-if="!is_login">
            <view class="not-logged-in row verCenter" @click="toUrl">
                <view class="row verCenter">
                    <image class="uni-img" src="http://img.ichunt.com/images/ichunt/minProgram/scm/14ec21333cd69cc5269772811b46b7ac.png" mode="aspectFill"></image>
                    <view class="text column"><text class="t1">未登录</text></view>
                </view>
            </view>
        </template>
        <template v-else>
            <view class="logged-in row bothSide verCenter">
                <view class="row verCenter">
                    <image class="uni-img" src="http://img.ichunt.com/images/ichunt/minProgram/scm/14ec21333cd69cc5269772811b46b7ac.png" mode="aspectFill"></image>
                    <view class="text column">
                        <text class="t1">{{ userInfo.name }}</text>
                        <text class="t2">{{ userInfo.email }}</text>
                    </view>
                </view>
            </view>
            <view class="list column"></view>
            <view class="exit row rowCenter verCenter" @click="exit()">退出账号</view>
        </template>
    </view>
</template>

<script>
    import { API } from '@/util/api.js';
    export default {
        data() {
            return {
                is_login: false,
                userInfo: {}
            };
        },
        onShow() {
            this.getData();
        },
        methods: {
            getData() {
                this.request(API.userInfo, 'POST', {}, false).then(res => {
                    if (res.err_code === 0) {
                        this.userInfo = res.data;
                        this.is_login = true;
                    } else {
                        this.is_login = false;
                    }
                });
            },
            toUrl() {
                // #ifdef MP-WEIXIN
                uni.navigateTo({
                    url: '/pages/mine/auth'
                });
                // #endif

                // #ifdef APP-PLUS
                uni.navigateTo({
                    url: '/pages/mine/login'
                });
                // #endif

            },
            exit() {
                uni.showModal({
                    title: '提示',
                    content: '您确定退出系统嘛',
                    success: res => {
                        if (res.confirm) {
                            this.request(API.unBindUserInfo, 'POST', {}, true).then(res => {});
                            uni.removeStorageSync('token');
                            uni.removeStorageSync('email');
                            uni.switchTab({
                                url: '/pages/home/index'
                            });
                        } else if (res.cancel) {
                            console.log('用户点击取消');
                        }
                    }
                });
            }
        }
    };
</script>

<style lang="scss">
    @import '../../assets/css/mine/index.scss';
</style>