<template>
  <div class="basic-layout" :style="basicStyle">
    <div class="titleBar">
      <div class="title">
        <div class="logo">
          <img src="@/assets/logo.png">
        </div>
        <div class="txt">流量统计监控系统</div>
      </div>
      <div class="windowTool">
        <div @click="miniSize">
          <el-tag
            type="primary"
            effect="dark">
            <i class="el-icon-minus"/>
          </el-tag>
        </div>
        <div>
          <el-tag
            v-if="isMaxSize"
            @click="restore"
            type="primary"
            effect="dark">
            <i class="el-icon-copy-document"/>
          </el-tag>

          <el-tag
            v-else
            @click="maxsize"
            type="primary"
            effect="dark">
            <i class="el-icon-full-screen"/>
          </el-tag>
        </div>
        <div>
          <el-tag
            type="primary"
            effect="dark"
            class="close-tag"
            @click="close">
            <i class="el-icon-close"/>
          </el-tag>
        </div>
      </div>
    </div>
    <el-container>
      <el-aside width="auto">
        <el-menu
          :router="true"
          :default-active="menuIndex"
          :unique-opened="true"
          :collapse-transition="false"
          active-text-color="#1989ff"
          :collapse="isCollapse"
        >
          <template v-for="item in routes">
            <el-menu-item :index="getIndex(item.path)" :key="item.path">
              <i :class="item.meta.icon"/>
              <span slot="title">{{ item.meta.title }}</span>
            </el-menu-item>
          </template>
        </el-menu>
        <div style="border-right: solid 1px #e6e6e6;padding-bottom: 16px">
          <el-tag
            type="primary"
            effect="dark"
            @click="handleCollapse">
            <i class="el-icon-s-unfold"/>
          </el-tag>
        </div>
      </el-aside>
      <el-container>
        <el-main class="page-content">
          <router-view class="router-view"/>
        </el-main>
        <!--<el-footer height="28px">Copyright © 2022 辽宁津港科技技术部</el-footer>-->
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import { asyncRouterMap } from '@/config/router.config'
const { ipcRenderer } = require('electron')

export default {
  name: 'BasicLayout',
  data () {
    return {
      isAlive: true,
      isCollapse: true,
      isMaxSize: false,
      basicStyle: {},
      routes: [],
      defaultIndex: '/',
      activeName: 'first',
    }
  },
  computed: {
    ...mapState({
      menuIndex: (state, getter) => state.route.menuIndex,
    }),
  },
  created () {
    ipcRenderer.removeAllListeners('mainWin-max')
    ipcRenderer.on('mainWin-max', (event, data) => {
      this.isMaxSize = data
    })
  },
  mounted () {
    this.routes = asyncRouterMap[0].children
    console.log('aaaaaaaaaaaa =>', this.routes)
    const _this = this
    _this.basicStyle = {
      width: window.screenWidth + 'px',
      height: window.innerHeight + 'px'
    }
    window.onresize = () => (() => {
      window.screenWidth = document.body.clientWidth
      _this.basicStyle = {
        width: window.screenWidth + 'px',
        height: window.innerHeight + 'px'
      }
    })()
  },
  methods: {
    ...mapMutations(['SET_MENU_INDEX']),
    handleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    miniSize () {
      const win = window.remote.getCurrentWindow()
      win.minimize()
    },
    //恢复
    restore () {
      window.remote.getCurrentWindow().restore()
    },
    //最大化
    maxsize () {
      const win = window.remote.getCurrentWindow()
      win.maximize()
    },
    //关闭
    close () {
      const win = window.remote.getCurrentWindow()
      win.destroy()
    },
    handleClick (e) {
      console.log('wwwwwwwwwwwwwwwwwwwwwwwwwwwww', e)
    },
    getIndex(path) {
      return path === '/home' ? '/' : path
    },
  },
  beforeDestroy () {
    window.onresize = null
    ipcRenderer.removeAllListeners('mainWin-max')
  }
}
</script>

<style lang="less" scoped>

  .basic-layout {
    display: flex;
    flex-direction: column;

    .titleBar {
      height: 38px;
      line-height: 36px;
      background: #1890ff;
      display: flex;
      justify-content: space-between;
      color: white;
      user-select: none;

      .title {
        flex: 1;
        display: flex;
        -webkit-app-region: drag;

        .logo {
          padding-left: 8px;
          padding-right: 6px;
        }

        .logo img {
          width: 20px;
          height: 20px;
          margin-top: 7px;
        }

        .txt {
          text-align: left;
          flex: 1;
          font-size: 14px;
        }
      }

      .windowTool {
        margin-right: 5px;

        div {
          height: 100%;
          width: 38px;
          display: inline-block;
          cursor: pointer;
        }

        .el-tag {
          .el-icon-close {
            border-radius: 50%;
            text-align: center;
            position: relative;
            cursor: pointer;
            font-size: 12px;
            height: 16px;
            width: 16px;
            line-height: 16px;
            vertical-align: middle;
            top: -1px;
            right: 0;
          }
        }

        .el-tag--dark {
          background-color: #1890ff;
          border-color: #1890ff;
          color: #fff;
        }

        .el-tag:hover {
          color: #fff;
          background-color: #409eff
        }

        .el-tag:active {
          opacity: .5;
        }
      }
    }

    .el-header {
      padding: 0;
      /*color: #ffffff;*/
      display: flex;
      align-items: center;
      border-bottom: 1px solid #e6e6e6;
    }

    .el-aside {
      height: 100%;
      display: flex;
      flex-direction: column;
      overflow: hidden;
      margin-bottom: 8px;

      .el-menu {
        flex: 1;
      }
    }

    .el-tag:active {
      opacity: .5;
    }

    .page-content {
      padding: 0;
    }

    .router-view {
      height: calc(100% - 42px);
      overflow-y: scroll;
    }
  }
</style>
