<template>
    <div class="header">
        <!-- 折叠按钮 -->
        <div class="collapse-btn" @click="collapseChage">
            <i v-if="!collapse" class="el-icon-s-fold"></i>
            <i v-else class="el-icon-s-unfold"></i>
        </div>
        <div class="logo">{{menuTitle}}</div>
        <div class="header-right">
            <div class="header-user-con">
                <!-- 用户头像 -->
                <div class="user-avator">
                    <img :src="headUrl" />
                </div>
                <!-- 用户名下拉菜单 -->
                <el-dropdown class="user-name" trigger="click">
                    <span class="el-dropdown-link">
                        {{username}}
                        <i class="el-icon-caret-bottom"></i>
                    </span>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item divided>
                            <span @click="clickHead">修改头像</span>
                            <input
                                type="file"
                                name="avatar"
                                style="display:none"
                                accept="image/gif, image/jpeg, image/jpg, image/png"
                                @change="changeImage($event)"
                                ref="avatarInput"
                            />
                        </el-dropdown-item>
                        <el-dropdown-item divided>
                            <span @click="editPersonVisible = true">修改密码</span>
                        </el-dropdown-item>
                        <el-dropdown-item divided>
                            <span @click="loginOut">退出登录</span>
                        </el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>

                <!-- 修改用户密码 -->
                <el-dialog title="修改用户密码" :visible.sync="editPersonVisible" width="350px" center>
                    <el-form :model="editPerson" :inline="true">
                        <ElFormItem label="用户账户">
                            <div>{{username}}</div>
                        </ElFormItem>
                        <ElFormItem label="修改密码">
                            <el-input v-model="editPerson.password" placeholder="请输入新密码"></el-input>
                        </ElFormItem>
                    </el-form>
                    <span slot="footer" class="dialog-footer">
                        <el-button @click="cancelEdit">取 消</el-button>
                        <el-button type="primary" @click="comfirmEdit">确 定</el-button>
                    </span>
                </el-dialog>
            </div>
        </div>
    </div>
</template>
<script>
import bus from '../common/bus';
import ajaxAuth from '../../script/api/auth.js';
import ajaxStorage from '../../script/api/storage.js';
import globalData from '@/components/common/globaldata/global.data.vue';
export default {
    data() {
        return {
            menuTitle:'',
            editPersonVisible: false,
            collapse: false,
            fullscreen: false,
            name: '',
            message: 2,
            editPerson: {
                password: ''
            },
            headUrl: ''
        };
    },
    computed: {
        username() {
            let username = localStorage.getItem('ms_username');
            return username ? username : this.name;
        }
    },
    created(){
        let currentHost=window.location.host;
        // console.log(currentHost);
        if(currentHost.indexOf('lixiang')!=-1){
            this.menuTitle='狸想培训信息管理系统';
        }else{
            this.menuTitle='鲸致生活数据看板';
        }
        if(globalData.userInfo!=null){
this.headUrl=ajaxStorage.reqFile(globalData.userInfo.headImg);
        }
        
    },
    methods: {
        changeImage(e) {
            var file = e.target.files[0];
            let that = this;
            if (file) {
                // console.log('上传文件大小' + file.size);
                let param = new FormData();
                //通过append向form对象添加数据
                param.append('createAccount', globalData.userInfo.userAccount);
                param.append('file', file);
                ajaxStorage.$ajaxAddFile(param).then(res => {
                    that.uploadHead(res.data.data.id);
                });
            }
        },
        uploadHead(imgId) {
            //修改头像
            this.headUrl =  ajaxStorage.reqFile(imgId);
            ajaxAuth
                .$ajaxUpdateUserHead({
                    id: globalData.userInfo.id,
                    headImg: imgId
                })
                .then(res => {
                    globalData.initUserAuth();
                    globalData.initSelfCityStation();
                    globalData.initHmCityStation();
                });
        },
        clickHead() {
            this.$refs.avatarInput.click();
        },
        comfirmEdit() {
            if (this.editPerson.password == null || this.editPerson.password == '') {
                this.$message.warning('密码不能为空!');
                return;
            }
            if (this.editPerson.password.length > 17) {
                this.$message.warning('密码长度不能超过18位');
                return;
            }
            ajaxAuth
                .$ajaxUpdateuserpassword({
                    userId: globalData.userInfo.id,
                    userPassword: this.editPerson.password
                })
                .then(res => {
                    if (res.data.errno == 0) {
                        this.$message.success('修改成功！');
                        this.editPersonVisible = false;
                        //清除缓存
                        this.editPerson.password = '';
                    } else {
                        this.$message.warning('修改失败！');
                    }
                });
        },
        cancelEdit() {
            this.editPersonVisible = false;
            //清除缓存
            this.editPerson.password = '';
        },
        // 用户名下拉菜单选择事件
        loginOut() {
            ajaxAuth.$ajaxLoginout().then(res => {
                localStorage.removeItem('token');
                // console.log(res);
            });

            localStorage.removeItem('token');
            localStorage.removeItem('userInfo');
            localStorage.removeItem('menulist');
            localStorage.removeItem('buttonperm');
            localStorage.removeItem('ms_username');

            localStorage.removeItem('allCity');
            localStorage.removeItem('allStation');
            localStorage.removeItem('userCity');
            localStorage.removeItem('userStation');
            localStorage.removeItem('userWareHouse');

            globalData.userInfo = {};
            globalData.token = {};
            this.$router.push('/login');
            setTimeout(function() {
                window.location.reload();
            }, 250);
        },
        // 侧边栏折叠
        collapseChage() {
            this.collapse = !this.collapse;
            bus.$emit('collapse', this.collapse);
        }
    },
    mounted() { 
        // console.log("头像url："+this.headUrl);
        if (document.body.clientWidth < 1500) {
            this.collapseChage();
        }
    }
};
</script>
<style scoped>
.header {
    position: relative;
    box-sizing: border-box;
    width: 100%;
    height: 70px;
    font-size: 22px;
    color: #fff;
}
.collapse-btn {
    float: left;
    padding: 0 21px;
    cursor: pointer;
    line-height: 70px;
    color:#333
}
.header .logo {
    float: left;
    width: 250px;
    line-height: 70px;
    color:#333
}
.header-right {
    float: right;
    padding-right: 50px;
}
.header-user-con {
    display: flex;
    height: 70px;
    align-items: center;
}
.btn-fullscreen {
    transform: rotate(45deg);
    margin-right: 5px;
    font-size: 24px;
}
.btn-bell,
.btn-fullscreen {
    position: relative;
    width: 30px;
    height: 30px;
    text-align: center;
    border-radius: 15px;
    cursor: pointer;
}
.btn-bell-badge {
    position: absolute;
    right: 0;
    top: -2px;
    width: 8px;
    height: 8px;
    border-radius: 4px;
    background: #f56c6c;
    color: #fff;
}
.btn-bell .el-icon-bell {
    color: #fff;
}
.user-name {
    margin-left: 10px;
}
.user-avator {
    margin-left: 20px;
}
.user-avator img {
    display: block;
    width: 40px;
    height: 40px;
    border-radius: 50%;
}
.el-dropdown-link {
    color: #333;
    cursor: pointer;
}
.el-dropdown-menu__item {
    text-align: center;
}
</style>
