<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="4">
                <div class="tools" @click.prevent="collapse" style="position: relative;">
                    <i class="fa fa-align-justify"></i>
                    <span style="position: absolute;width: 350px;left: 60px;">{{user.companyName}}</span>
                </div>
            </el-col>
            <el-col :span="10" class="userinfo">
                <el-dropdown trigger="hover" style="margin-right:30px;">
                    <span class="el-dropdown-link userinfo-inner">
                        <i class="el-icon-user" style="font-size:18px" ></i>
                        咨询客服
                    </span> 
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item >
                            <div>
                            <div>微信扫码</div>
                            <img
                            style="width: 120px; height: 120px"
                            src="../assets/image/code.jpg" />
                            </div>
                        </el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown> 
                    
                到期日期：{{remainingTime}}
                <el-badge class="itemNew" :value="num" :hidden="num == 0">
                    <i class="el-icon-message-solid" style="font-size:24px" v-popover:popover1 @click="drawer = true"></i>
                </el-badge>
                <el-drawer title="消息中心" :visible.sync="drawer" direction="rtl" :with-header="false" size="35%">
                    <el-table :data="popoverData" :height="tableHeight" size="small">
                        <el-table-column property="type" label="消息内容" align="left">
                            <template slot-scope="scope">
                                <el-link type="primary" :underline="false" @click="locationHerf(scope.row.id,scope.row.content, scope.row.type)">
                                    <span style="font-size:13px;">{{scope.row.msg==null?msgTypeTxt[scope.row.type]:scope.row.msg}}</span>
                                </el-link>
                            </template> 
                        </el-table-column>
                        <el-table-column property="type" label="状态" align="center" width="60">
                            <template slot-scope="scope">
                                <span v-if="scope.row.checked == 0" style="color:red">未读</span>
                                <span v-else style="color:green">已读</span>
                            </template>
                        </el-table-column>
                        <el-table-column property="time" label="时间" align="center" width="120"></el-table-column>
                    </el-table>
                </el-drawer>                
                <el-dropdown trigger="hover" style="margin-left:10px;">
                    <span class="el-dropdown-link userinfo-inner">
                        <img src="../assets/image/userHead.png" />
                        {{sysUserName}}
                    </span>
                    <el-dropdown-menu slot="dropdown">
                        <!-- <el-dropdown-item disabled ><span style="font-size:12px;"><i class="el-icon-view" ></i>{{roleArray[user.role]}}</span></el-dropdown-item> -->
                        <el-dropdown-item disabled ><span style="font-size:12px;"><i class="el-icon-view" ></i>{{user.roleName}}</span></el-dropdown-item>
                        <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-scrollbar style="height:100%">
                <!--导航菜单-->
                <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"><i :class="child.iconCls"></i>{{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>
                </el-scrollbar>
            </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="originPassword">
                            <el-input v-model="addForm.originPassword" autocomplete="off" placeholder="请输入原密码" show-password></el-input>
                        </el-form-item>
                        <el-form-item label="新密码" prop="newPassword">
                            <el-input v-model="addForm.newPassword" 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>
    import util from "../common/js/util";
    export default {
        data() {
            return {
                roleArray:["普通员工","超级管理员", "系统管理员", "公司高层","财务管理员", "项目管理员","公司领导"],
                helpImg: '../assets/image/userHead.png',
                user: sessionStorage.getItem("user"),
                sysName: "八爪鱼",
                collapsed: sessionStorage.collapsed!=null?(sessionStorage.collapsed=='true'?true:false):false,
                sysUserName: "",
                menu: [],
                msgTypeTxt:["审批未通过","有新任务啦","任务有新进展","项目日报审核通过"],
                editInformation: false,
                editPassWord: false,
                editLoading: false,
                editLoading2: false,
                addForm: {
                    id: "",
                    originPassword: "",
                    newPassword: ""
                },
                editInfoForm: {
                    id: "",
                    username: "",
                    mobile: "",
                    companyName: ""
                },
                passRule: {
                    originPassword: [{ required: true, message: "请输入原密码", trigger: "blur" }],
                    newPassword: [{ required: true, message: "请输入新密码", trigger: "blur" }],
                    username: [{ required: true, message: "请输入姓名", trigger: "blur" }]
                },

                //时间
                activeDate: new Date(),

                timer: null,
                remainingTime: '',

                drawer: false,
                tableHeight: 0,
                popoverData: [],
                num: 0,
            };
        },
        methods: {
            
            //退出登录
            logout: function() {
                var _this = this;
                this.$confirm("确认退出吗?", "提示", {
                    //type: 'warning'
                }).then(() => {
                    sessionStorage.removeItem("user");
                    location.reload();
                    _this.$router.push("/login");
                });
            },

            //折叠导航栏
            collapse: function() {
                this.collapsed = !this.collapsed;
                sessionStorage.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.manage.editPassword, 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"
                            });
                        });
                    }
                });
            },

            setTime() {
                var d = util.formatDate.cdTime(new Date(new Date().getTime() + this.user.remainingTime), new Date(), 'd');
                var h = util.formatDate.cdTime(new Date(new Date().getTime() + this.user.remainingTime), new Date(), 'h');
                var m = util.formatDate.cdTime(new Date(new Date().getTime() + this.user.remainingTime), new Date(), 'm');
                var s = util.formatDate.cdTime(new Date(new Date().getTime() + this.user.remainingTime), new Date(), 's');
                this.remainingTime = d+'天'+h+'时'+m+'分'+s+'秒';
            },

            // 加载消息
            loadNotice() {
                // this.http.post( this.port.manage.msgList, {},
                // res => {
                //     if (res.code == "ok") {
                //         var list = res.data;
                //         this.popoverData = res.data;
                //         var num = 0;
                //         for(var i in list) {
                //             if(list[i].checked != 1) {
                //                 num ++;
                //             }
                //         }
                //         this.num = num;
                //     } else {
                //         this.$message({
                //             message: res.msg,
                //             type: "error"
                //         });
                //     }
                // },
                // error => {
                //     this.$message({
                //         message: error,
                //         type: "error"
                //     });
                // });
            },

            //点击消息的跳转
            locationHerf(id, date, type) {
                this.http.post( this.port.manage.check, { id: id },
                res => {
                    if (res.code == "ok") {
                        this.loadNotice();
                        if (type == 0 || type == 3) {
                            //审批未通过的消息， 也包括审批通过的通知
                            sessionStorage.msg = date;
                            sessionStorage.from = 1;
                            //本页面再点的话强制转移一下
                            var currentRoute = this.$route.path.split("/");
                            if (currentRoute[1] == "daily") {
                                this.$router.go(0);
                                this.drawer = false;
                                return false;
                            }
                            this.$router.push("/daily");
                            this.drawer = false;
                        } else if (type == 1) {
                            //1- 有新任务待执行
                            this.$router.push("/projectInside/"+date);
                            this.drawer = false;
                        } else if (type == 2) {
                            //2- 任务有新进展
                            this.$router.push("/projectInside/"+date);
                            this.drawer = false;
                        }
                        
                    } else {
                        this.$message({
                            message: res.msg,
                            type: "error"
                        });
                    }
                },
                error => {
                    this.$message({
                        message: error,
                        type: "error"
                    });
                });
            },
        },
        mounted() {
            console.log(this.$router.options.routes, '看看')
            let height = window.innerHeight;
            this.tableHeight = height - 15;
            const that = this;
            window.onresize = function temp() {
                that.tableHeight = window.innerHeight - 15;
            };

            if (this.user) {
                var user = JSON.parse(this.user);
                this.user = user;
                this.sysUserName = user.name || "";
                this.loadNotice();
                if(this.user.remainingTime != "" && this.user.remainingTime != 0) {
                    this.remainingTime = util.formatDate.format(new Date(new Date().getTime() + this.user.remainingTime), "yyyy-MM-dd")
                } else {
                    this.remainingTime = "已过期";
                    clearInterval(this.timer);
                }
            } 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: #606266;
            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: 0 0 0 10px;
                    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;
                        box-shadow: 5px 5px 10px #ddd;
                    }
                }
            }
            .menu-collapsed {
                flex: 0 0 60px;
                width: 60px;
            }
            .menu-expanded {
                flex: 0 0 230px;
                width: 230px;
            }
            .content-container {
                flex: 1;
                width: 80%;
                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>