<template>
    <view>
        <view class="header-con">
            <view class="header" v-if="token">
                <view class="left">
                    <image :src="user.headimg" class="headimage mr-10" @click="handleDrawer"></image>
                    <view>
                        <view class="name" @click="handleDrawer">
                            <text class="overflow">{{user.name}}</text>
                            <icon class="iconfont icon-yqfqiehuan"></icon>
                        </view>
                        <view class="age">
                            <view>
                                性别:{{!user.sex?"未知":user.sex==1?'男':'女'}}
                            </view>
                            <view>
                                年龄:{{user.mage?user.mage:"0岁"}}
                            </view>
                        </view>
                    </view>
                </view>
                <view class="celiang_r" @click="handleBluetoothClick">
                    <icon class="t-icon t-icon-tizhongcheng"></icon>
                    <text>上秤测量</text>
                </view>
            </view>
            <view class="header2" v-else @click="handleLogin">
                <view class="text"><text>登录</text>查看更多信息</view>
            </view>
        </view>
        <!-- denglu -->
        <userLogin></userLogin>
        <!-- 左侧 -->
        <leftdrawer></leftdrawer>
    </view>
</template>

<script>
    import {
        mapState
    } from "vuex";
    import userLogin from '@/components/userLogin.vue'
    import leftdrawer from "@/components/drawer/drawer.vue"
    export default {
        components: {
            userLogin,
            leftdrawer
        },
        data() {
            return {
                devType: "",
                deviceId: ""
            }
        },
        props: {
            token: {
                type: String,
                default: null
            },
        },
        computed: {
            ...mapState(["user"]),
        },
        mounted() {
            // this.token = uni.getStorageSync('token')
        },
        methods: {
            // 上称测量
            handleBluetoothClick() {
                let that = this
                if (!that.token) {
                    that.$store.commit("changeUserLogin", true)
                    return
                }
                uni.openBluetoothAdapter({
                    success: e => {
                        that.$store.commit("changeBluetooth", true);
                        if (that.devType && that.deviceId) {
                            that.$tools.handlePages(that.devType, that.deviceId)
                            return
                        }
                        uni.navigateTo({
                            url: "/pages/search/devType"
                        })
                        console.log('初始化蓝牙成功:' + e.errMsg);
                    },
                    fail: err => {
                        console.log('初始化蓝牙失败:' + err.errMsg);
                        return this.$tools.getBluetoothAdapter(err)
                    }
                });
            },
            // 登录
            handleLogin() {
                this.$store.commit("changeUserLogin", true);
            },
            handleDrawer() {
                this.$store.commit("changeDrawe", true);
            },
        }
    }
</script>

<style scoped lang="scss">
</style>
