<template>
    <el-row class="container">
        <el-col :span="24" class="header">
            <el-col :span="10" class="logo" :class="collapsed?'logo-collapse-width':'logo-width'" :style="collapsed?'padding:0':''">
                <img v-if="collapsed" class="headImg" src="../assets/image/head_logo.png" />
                <div v-else class="logo-sys">
                    <img class="headImg" src="../assets/image/head_logo.png" />
                    <span>{{sysName}}</span>
                </div>
            </el-col>
            <el-col :span="10">
                <div class="tools" @click.prevent="collapse">
                    <i class="fa fa-align-justify"></i>
                </div>
            </el-col>
            <el-col :span="6" class="userinfo">
                <el-dropdown trigger="hover">
                    <span class="el-dropdown-link userinfo-inner">
                        <img src="../assets/image/userHead.png" />
                        {{sysUserName}}
                    </span>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="reset">修改密码</el-dropdown-item>
                        <!-- <el-dropdown-item @click.native="editInfoOpen">修改信息</el-dropdown-item> -->
                        <el-dropdown-item divided @click.native="logout">退出登录</el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
            </el-col>
        </el-col>

        <el-col :span="24" class="main">
            <aside :class="collapsed?'menu-collapsed':'menu-expanded'">
                <!--导航菜单-->
                <el-menu :default-active="$route.path" class="el-menu-vertical-demo" unique-opened router v-if="!collapsed">
                    <template v-for="(item,index) in $router.options.routes" v-if="!item.hidden">
                        <el-submenu :index="index+''" v-if="!item.leaf">
                            <template slot="title">
                                <i :class="item.iconCls"></i>
                                <span class="itemName">{{item.name}}</span>
                            </template>
                            <el-menu-item v-for="child in item.children" :index="child.path" :key="child.path" v-if="!child.hidden">{{child.name}}</el-menu-item>
                        </el-submenu>
                        <el-menu-item v-if="item.leaf && item.children.length > 0" :index="item.children[0].path">
                            <i :class="item.iconCls"></i> {{item.children[0].name}}
                        </el-menu-item>
                    </template>
                </el-menu>

                <!--导航菜单-折叠后-->
                <ul class="el-menu el-menu-vertical-demo collapsed" v-if="collapsed" ref="menuCollapsed">
                    <li v-for="(item,index) in $router.options.routes" v-if="!item.hidden" class="el-submenu item" :style="{overflow:!item.leaf?'':'hidden'}">
                        <template v-if="!item.leaf">
                            <div class="el-submenu__title" style="padding-left: 20px;" @mouseover="showMenu(index,true)" @mouseout="showMenu(index,false)">
                                <i :class="item.iconCls"></i>
                            </div>
                            <ul class="el-menu submenu" :class="'submenu-hook-'+index" @mouseover="showMenu(index,true)" @mouseout="showMenu(index,false)">
                                <li v-for="child in item.children" v-if="!child.hidden" :key="child.path" class="el-menu-item" style="padding-left: 40px;" :class="$route.path==child.path?'is-active':''" @click="$router.push(child.path)">{{child.name}}</li>
                            </ul>
                        </template>
                        <template v-else>
                            <li class="el-submenu">
                                <div class="el-submenu__title el-menu-item" :class="$route.path==item.children[0].path?'is-active':''" @click="$router.push(item.children[0].path)"
                                    style="padding-left: 20px;height: 56px;line-height: 56px;padding: 0 20px;">
                                    <i :class="item.iconCls"></i>
                                </div>
                            </li>
                        </template>
                    </li>
                </ul>
            </aside>

            <section class="content-container">
                <div class="grid-content bg-purple-light">
                    <el-col :span="24" class="content-wrapper">
                        <transition name="fade" mode="out-in">
                            <router-view></router-view>
                        </transition>
                    </el-col>
                </div>

                <!--修改密码-->
                <el-dialog title="修改密码" width='450px' v-if="editPassWord" :visible.sync="editPassWord" :close-on-click-modal="false" customClass="customWidth">
                    <el-form :model="addForm" label-width="80px" :rules="passRule" ref="addForm">
                        <el-form-item label="新密码" prop="password">
                            <el-input v-model="addForm.password" autocomplete="off" placeholder="请输入新密码" show-password></el-input>
                        </el-form-item>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click.native="editPassWord = false">取消</el-button>
                        <el-button type="primary" @click.native="resetPwd" :loading="editLoading">提交</el-button>
                    </div>
                </el-dialog>

                <!--修改信息-->
                <el-dialog title="修改信息" width='450px' v-if="editInformation" :visible.sync="editInformation" :close-on-click-modal="false" customClass="customWidth">
                    <el-form :model="editInfoForm" label-width="80px" :rules="passRule" ref="editInfoForm">
                        <el-form-item label="姓名" prop="username">
                            <el-input v-model="editInfoForm.username" autocomplete="off" placeholder="请输入姓名"></el-input>
                        </el-form-item>
                    </el-form>
                    <el-form :model="editInfoForm" label-width="80px" :rules="passRule" ref="editInfoForm">
                        <el-form-item label="手机号" prop="mobile">
                        <el-input v-model="editInfoForm.mobile" autocomplete="off" placeholder="请输入手机号" :disabled="true"></el-input>
                        </el-form-item>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click.native="editInformation = false">取消</el-button>
                        <el-button type="primary" @click.native="editInfo" :loading="editLoading2">提交</el-button>
                    </div>
                </el-dialog>
            </section>
        </el-col>
    </el-row>
</template>

<script>
    export default {
        data() {
            return {
                user: sessionStorage.getItem("user"),
                sysName: "工时管家",
                collapsed: false,
                sysUserName: "",

                editInformation: false,
                editPassWord: false,
                editLoading: false,
                editLoading2: false,
                addForm: {
                    id: "",
                    password: ""
                },
                editInfoForm: {
                    id: "",
                    username: "",
                    mobile: "",
                    companyName: ""
                },
                passRule: {
                    password: [{ required: true, message: "请输入新密码", trigger: "blur" }],
                    username: [{ required: true, message: "请输入姓名", trigger: "blur" }]
                },

                //时间
                activeDate: new Date()
            };
        },
        methods: {
            //退出登录
            logout: function() {
                var _this = this;
                this.$confirm("确认退出吗?", "提示", {
                    //type: 'warning'
                }).then(() => {
                    sessionStorage.removeItem("user");
                    _this.$router.push("/login");
                });
            },

            //折叠导航栏
            collapse: function() {
                this.collapsed = !this.collapsed;
            },

            showMenu(i, status) {
                this.$refs.menuCollapsed.getElementsByClassName(
                    "submenu-hook-" + i
                )[0].style.display = status ? "block" : "none";
            },

            //打开编辑信息界面
            editInfoOpen() {
                this.editInformation = true;
                this.editInfoForm.id = JSON.parse(sessionStorage.getItem("user")).id;
                this.editInfoForm.username = JSON.parse(
                    sessionStorage.getItem("user")
                ).username;
                this.editInfoForm.mobile = JSON.parse(
                    sessionStorage.getItem("user")
                ).account;
                this.editInfoForm.companyName = JSON.parse(
                    sessionStorage.getItem("user")
                ).companyName;
            },

            //编辑信息
            editInfo() {
                this.$refs.editInfoForm.validate(valid => {
                    if (valid) {
                    this.editLoading2 = true;
                    this.http.post(
                        this.port.pwd.resetPwd,
                        { id: this.editInfoForm.id, username: this.editInfoForm.username },
                        res => {
                        this.editLoading2 = false;
                        this.editInformation = false;
                        if (res.code == "ok") {
                            this.$message({
                            message: "修改成功",
                            type: "success"
                            });
                            //读取并覆盖session storage
                            var userObject = JSON.parse(sessionStorage.getItem("user"));
                            userObject.username = this.editInfoForm.username;
                            sessionStorage.setItem("user", JSON.stringify(userObject));
                            this.sysUserName = this.editInfoForm.username;
                        } else {
                            this.$message({
                            message: res.msg,
                            type: "error"
                            });
                        }
                        },
                        error => {
                        this.editLoading2 = false;
                        this.editInformation = false;
                        this.$message({
                            message: error,
                            type: "error"
                        });
                        }
                    );
                    }
                });
            },

            reset() {
                this.editPassWord = true;
                this.addForm.id = JSON.parse(sessionStorage.getItem("user")).id;
                this.addForm.account = JSON.parse(sessionStorage.getItem("user")).account;
            },

            resetPwd() {
                this.$refs.addForm.validate(valid => {
                    if (valid) {
                    this.editLoading = true;
                    this.http.post(
                        this.port.pwd.resetPwd,
                        this.addForm,
                        res => {
                        this.editLoading = false;
                        this.editPassWord = false;
                        if (res.code == "ok") {
                            this.$message({
                            message: "修改成功,请重新登录",
                            type: "success"
                            });
                            this.$router.push("/login");
                        } else {
                            this.$message({
                            message: res.msg,
                            type: "error"
                            });
                        }
                        },
                        error => {
                        this.editLoading = false;
                        this.editPassWord = false;
                        this.$message({
                            message: error,
                            type: "error"
                        });
                        }
                    );
                    }
                });
            },
        },
        mounted() {
            if (this.user) {
                var user = JSON.parse(this.user);
                this.user = user;
                this.sysUserName = user.name || "";
            } else {
                this.$router.push("/login");
            }
        }
    };
</script>

<style scoped lang="scss">
    .el-menu-vertical-demo i {
        margin-right: 10px;
    }

    .container {
        position: absolute;
        top: 0px;
        bottom: 0px;
        width: 100%;
        .header {
            height: 60px;
            line-height: 60px;
            background: #20a0ff;
            color: #fff;
            .userinfo {
                text-align: right;
                padding-right: 35px;
                float: right;
                .userinfo-inner {
                    cursor: pointer;
                    color: #fff;
                    img {
                        width: 40px;
                        height: 40px;
                        border-radius: 20px;
                        margin: 10px 10px 10px 10px;
                        float: left;
                    }
                }
                .itemNew {
                    height: 25px;
                    margin-right: 25px;
                    i {
                        vertical-align: top;
                    }
                }
            }
            .logo {
                height: 60px;
                font-size: 21px;
                padding-left: 20px;
                padding-right: 20px;
                border-color: rgba(238, 241, 146, 0.3);
                border-right-width: 1px;
                border-right-style: solid;
                img {
                    width: 40px;
                    float: left;
                    margin: 10px 10px 10px 18px;
                }
                img.headImg {
                    margin: 0;
                    width: 40px;
                    height: 40px;
                    margin: 10px 0 0 10px;
                }
                .logo-sys {
                    height: 100%;
                    line-height: 100%;
                    img.headImg {
                        width: 40px;
                        height: 40px;
                        margin: 10px 0 0 10px;
                        vertical-align: middle;
                    }
                    span {
                        display: inline-block;
                        height: 100%;
                        line-height: 100%;
                        margin-left: 15px;
                        margin: 20px 0 0 15px;
                        vertical-align: middle;
                    }
                }
                .txt {
                    color: #fff;
                }
            }
            .logo-width {
                width: 230px;
            }
            .logo-collapse-width {
                width: 60px;
            }
            .tools {
                padding: 0px 23px;
                width: 14px;
                height: 60px;
                line-height: 60px;
                cursor: pointer;
            }
        }
        .main {
            display: flex;
            position: absolute;
            top: 60px;
            bottom: 0px;
            overflow: hidden;
            aside {
                flex: 0 0 230px;
                width: 230px;
                .el-menu {
                    height: 100%;
                }
                .collapsed {
                    width: 60px;
                    .item {
                        position: relative;
                    }
                    .submenu {
                        position: absolute;
                        top: 0px;
                        left: 60px;
                        z-index: 99999;
                        height: auto;
                        display: none;
                    }
                }
            }
            .menu-collapsed {
                flex: 0 0 60px;
                width: 60px;
            }
            .menu-expanded {
                flex: 0 0 230px;
                width: 230px;
            }
            .content-container {
                flex: 1;
                width: 80%;
                padding: 10px;
                overflow-y: auto;
                .breadcrumb-container {
                    .title {
                        width: 200px;
                        float: left;
                        color: #475669;
                    }
                    .breadcrumb-inner {
                        float: right;
                    }
                }
                .content-wrapper {
                    background-color: #fff;
                    box-sizing: border-box;
                }
            }
        }
    }

    .popover-item {
        padding: 10px;
        border-bottom: 1px #eee solid;
    }

    .popover-item > div > p {
        margin: 0;
        line-height: 18px;
    }

    .popover-title {
        color: #409eff;
        padding-bottom: 8px;
    }

    .popover-type {
        color: #aaa;
        font-size: 8px;
        float: right;
    }

    .popover-button {
        font-weight: 900;
        padding: 10px;
        text-align: center;
        cursor: pointer;
    }

    .isRead {
        color: #999 !important;
    }
</style>