<template>
    <div class="index">
        <div class="header">
            <div class="header-left">
                <img src="../assets/image/logo.png" alt="">
            </div>
            <div class="header-center">

            </div>
            <div class="header-right" v-if="userInfo">
                <img src="../assets/image/avatar.png" alt="">
                <div class="user-data">
                    <div class="name">欢迎，{{ userInfo.nickName }}</div>
                    <div class="time" v-if="time"> {{ time.year.year }}-{{ time.year.month }}-{{ time.year.date }}</div>
                </div>
                <div class="login-out" @click="loginOut"></div>
            </div>
        </div>
        <div class="index-content">
            <div class="left collapse scroll">
                <el-collapse v-model="activeNames">
                    <template v-for="item, index in leftList">
                        <el-collapse-item :name="index + 1" :key="index" v-if="!item.isRouter">
                            <template slot="title" class="collapse-title">
                                <div class="img"><img :src="item.img" alt=""></div> {{ item.name }}
                            </template>
                            <ul class="table-ul">
                                <li class="table-li" @click="toRouter1(item1)"
                                    :class="item.routerList.indexOf(routerNow) != -1 && routerIndex == item1.index ? 'active' : ''"
                                    v-for="item1, index1 in item.list" :key="index1">{{ item1.name }}
                                </li>
                            </ul>
                        </el-collapse-item>
                        <div @click="toRouter1(item)" :class="routerNow == item.router ? 'active' : ''" class="no-list"
                            v-else>
                            <div class="img"><img :src="item.img" alt=""></div>{{ item.name }}
                        </div>
                    </template>

                </el-collapse>
            </div>
            <div class="right right-page">
                <router-view></router-view>
            </div>
        </div>
    </div>
</template>
<script>
import { getnowtime } from '../assets/js/nowTime'
export default {
    data() {
        return {
            time: null,//当前时间


            activeNames: [3],
            leftList: [
                { name: '种植计划', img: require('../assets/image/vrcode-icon0.png'), list: [], router: 'plantingPlan', isRouter: true, },
                { name: '生产环节', routerList: ['agricultural'], img: require('../assets/image/vrcode-icon1.png'), list: [{ name: '农事管理',router:'agricultural' }, { name: '施肥记录' }, { name: '打药记录' }, { name: '采摘记录' }, { name: '清洁记录' }, { name: '病虫害管理' },] },
                { name: '产品追溯', routerList: [], img: require('../assets/image/vrcode-icon2.png'), list: [{ name: '溯源批次' }, { name: '配置溯源版面' }, { name: '各种报告库' }] },
                { name: '基础信息设置', routerList: [], img: require('../assets/image/vrcode-icon3.png'), list: [{ name: '作物品种' }, { name: '种植栽培模式' }, { name: '农事作业工序' }, { name: '人员管理' }, { name: '农场信息管理' }, { name: '供应商管理' }, { name: '生产资料管理' }, { name: '农机工具管理' }, { name: '作物外观图片库' },] },],
            routerNow: 'plantingPlan',
            deviceName: 1,
            routerIndex: 1,
            equipmentList: [],
            userInfo: null,
            loading: true,//页面加载
            IDTimer: null,
        }
    },
    watch: {
        "$route"(newName, oldName) {
            this.gerRouter();
        },
    },
    mounted() {
        const that = this
        this.getTime()
        var router = this.$route.query
        if (router.token && router.userid) {
            localStorage.setItem('token', router.token)

            this.api.getUser({ userId: router.userid }).then(res => {
                var data = res.data.data
                data.userid = router.userid
                localStorage.setItem('userInfo', JSON.stringify(data))
                this.userInfo = JSON.parse(localStorage.getItem('userInfo'))
                this.gerRouter();
                // this.getEqbyid()
            })
            return
        } else {
            if (localStorage.getItem('token')) {
                this.userInfo = JSON.parse(localStorage.getItem('userInfo'))
            }
            this.gerRouter();
            // this.getEqbyid()
        }
        this.IDTimer && clearInterval(this.IDTimer)
        this.IDTimer = setInterval(() => {
            // that.getEqbyid()
        }, 120000);

    },
    beforeDestroy() {
        this.IDTimer && clearInterval(this.IDTimer)
    },
    methods: {

        gerRouter() {
            var store = this.$store.state
            this.routerIndex = this.$route.query.index ? this.$route.query.index : store.equipmentIndex
            this.routerNow = this.$route.name;
            if (store.equipmentList.length) {
                this.deviceName = store.equipmentList[this.routerIndex - 1].deviceName
            } else {

            }

        },
        loginOut() {
            this.api.loginOut().then((res) => {

                if (res.data.code == 200) {
                    this.$message({
                        message: '退出登录成功',
                        type: "success",
                    });
                    localStorage.removeItem('token')
                    localStorage.removeItem('userInfo')
                    this.$router.push({ name: 'login' })
                } else {
                    this.$message.error("请求出错");
                }
            })
        },
        toHome() {
            this.$router.push({ name: 'home' })
        },
      
        toRouter1(item) {
            if (item.router) {
                if (item.http) {
                    window.location.href = item.http;
                }else{
                    this.$router.push({ name: item.router })
                }
            } else {
                this.$message('当前页面正在努力开发中');
            }
        },
        getTime() {
            this.time = getnowtime()
            const that = this
            setInterval(() => {
                that.time = getnowtime()
            }, 1000);
        },
    },

}
</script>
<style lang="scss"></style>
  