<template>
  <div id="app">
    <!-- <img src="./assets/logo.png"> -->
    <header v-if="$route.path !== '/'">
      <span class="pageTitle">{{ $route.meta.pageTitle }}</span>
      <div class="right">
        <span class="userInfo">{{ userInfo.username || 'username' }}</span>
        <span class="logout" @click="logout()">退出</span>
      </div>
    </header>
    <nav v-if="$route.path !== '/'">
      <div class="sysTitle">
        数据管理系统
      </div>
      <div class="selectPt" @click="toggle.ptList = !toggle.ptList">
        <p>当前渠道：{{ ptSelectedData.pt }}</p>
        <ul :class="{ 'active': toggle.ptList }">
          <li v-for="n,i in ptListData" @click="selectPtChange(n)">{{ n.pt }}</li>
        </ul>
      </div>
      <div class="list">
        <!-- 如果修改了 nav 中的 list 列表，不要忘了重新配置一下 navAutoShow 方法（就在下面） -->
        <div v-if="userInfo.lv >= 2" class="list-title icon-1" :class="{ 'active': toggle.listTitle === 0 }" @click="toggle.listTitle = 0">
          <a href="javascirpt:void(0)">检测模块</a>
          <img src="~static/img/xiala.png" alt="">
        </div>
        <div v-if="userInfo.lv >= 2" class="list-item">
          <router-link  :class="{ 'active': $route.path === '/checkInfo' }" :to="{ path: '/checkInfo' }">检测信息</router-link>
          <router-link  :class="{ 'active': $route.path === '/checkTemplate' }" :to="{ path: '/checkTemplate' }">检测模板</router-link>
          <router-link  :class="{ 'active': $route.path === '/processingRecord' }" :to="{ path: '/processingRecord' }">处理记录</router-link>
        </div>
        <div class="list-title icon-2" :class="{ 'active': toggle.listTitle === 1 }" @click="toggle.listTitle = 1">
          <a href="javascirpt:void(0)">数据模块</a>
          <img src="~static/img/xiala.png" alt="">
        </div>
        <div class="list-item">
          <router-link :class="{ 'active': $route.path === '/channelData' }" :to="{ path: '/channelData' }">渠道数据</router-link>
          <router-link :class="{ 'active': $route.path === '/detailedData' }" :to="{ path: '/detailedData' }">详细数据</router-link>
        </div>
        <div v-if="userInfo.lv >= 3" class="list-title  icon-3" :class="{ 'active': toggle.listTitle === 2 }" @click="toggle.listTitle = 2">
          <a href="javascirpt:void(0)">用户模块</a>
          <img src="~static/img/xiala.png" alt="">
        </div>
        <div v-if="userInfo.lv >= 3" class="list-item">
          <router-link :class="{ 'active': $route.path === '/userManage' }" :to="{ path: '/userManage' }">用户管理</router-link>
        </div>
        <div v-if="userInfo.lv >= 3" class="list-title  icon-4" :class="{ 'active': toggle.listTitle === 3 }" @click="toggle.listTitle = 3">
          <a href="javascirpt:void(0)">配置模块</a>
          <img src="~static/img/xiala.png" alt="">
        </div>
        <div v-if="userInfo.lv >= 3" class="list-item">
          <router-link :class="{ 'active': $route.path === '/databaseInfo' }" :to="{ path: '/databaseInfo' }">数据库信息</router-link>
          <router-link :class="{ 'active': $route.path === '/fieldTableInfo' }" :to="{ path: '/fieldTableInfo' }">字段表信息</router-link>
          <router-link :class="{ 'active': $route.path === '/channelTableInfo' }" :to="{ path: '/channelTableInfo' }">渠道表信息</router-link>
        </div>
      </div>
    </nav>
    <section :class="{ 'loginPage': $route.path === '/' }">
      <router-view :user-info="userInfo" :all-fields="allFields" :pt-selected-data="ptSelectedData" v-if="ptListData.length && ptSelectedData.pt" />
      <!-- <footer>&copy;2012-2017 zhiweidata.com, All Rights Reserved.京ICP备13015804号-1备案京公网安备11010502034130号</footer> -->
    </section>

    <!-- 自定义 alert 弹框 -->
    <div class="mask" v-show="alert.toggle">
      <div id="alert" class="popup">
        <div class="alert-title">
          {{ alert.title }}
        </div>
        <div class="alert-content">
          <span>{{ alert.content }}</span>
        </div>
        <div class="alert-btn">
          <button type="button" class="btn1" @click="alertClose()">确定</button>
        </div>
      </div>
    </div>

  </div>
</template>

<script>
import { bus, requestLeave, requestGetShowChannel, requestChangePt, requestGetAllFields } from '@/vuex/request.js'

export default {
  name: 'app',
  data () {
    return {
      toggle: {
        listTitle: 0,
        ptList: false
      },
      alert: {
        toggle: false,
        title: '测试标题',
        content: '测试内容'
      },
      userInfo: {},
      ptListData: [],
      ptSelectedData: {},
      allFields: []
    }
  },
  mounted () {
    // 刷新后重新获取用户信息
    if (!window.sessionStorage.getItem('userInfo')) {
      this.$router.push({'path': '/'})
    } else {
      this.userInfo = JSON.parse(window.sessionStorage.getItem('userInfo'))
    }
    // 页面刷新后，左侧 nav 仍能正确显示当前页
    this.navAutoShow()
    // 获取渠道列表
    this.getPtList()
  },
  created () {
    // 全局函数
    bus.$on('setUserInfo', (userInfo) => {
      // 左侧 nav 正确显示当前页 （用于防止退出登陆后再次登陆时可能会出的BUG）
      this.navAutoShow()
      // 登陆后保存用户信息到缓存
      this.userInfo = userInfo
      // 将用户信息保存到session （用于刷新后重新获取用户信息）
      window.sessionStorage.setItem('userInfo', JSON.stringify(userInfo))
    })
    bus.$on('pop-up-show', (selector) => {
      window.$(selector).transition({
        translate: '-50%, -50%',
        scale: 1,
        opacity: 1
      }, 500, 'easeOutBack')
    })
    bus.$on('pop-up-close', (selector) => {
      window.$(selector).transition({
        translate: '-50%, -50%',
        scale: 0,
        opacity: 0
      }, 500, 'ease')
    })
    bus.$on('alert', (str, title) => {
      let s = str
      let t = title || '来自 ' + window.location.host + ' 的消息'
      this.alert.content = s
      this.alert.title = t
      this.alert.toggle = true
      setTimeout(() => {
        bus.$emit('pop-up-show', '#alert.popup')
      })
    })
  },
  methods: {
    selectPtChange (n) {
      this.ptSelectedData = n
      let data = {
        pt: n.pt
      }
      requestChangePt({}, data, (response) => {
        if (response.data) {
          if (response.data.state) {
            this.ptSelectedData = n
            this.$router.push({ path: '/reload' })
          } else {
            bus.$emit('alert', response.data.message)
          }
        } else {
          bus.$emit('alert', response + '.')
        }
      })
    },
    getPtList () {
      requestGetShowChannel((response) => {
        if (response.data) {
          if (response.data.state) {
            this.ptListData = response.data.data
            this.ptSelectedData = response.data.data[0]
          } else {
            bus.$emit('alert', response.data.message)
          }
        } else {
          bus.$emit('alert', response + '.')
        }
      })
    },
    navAutoShow () {
      if (this.$route.path.search(/^(\/checkInfo|\/checkTemplate|\/processingRecord|\/)$/g) !== -1) {
        this.toggle.listTitle = 0
      } else if (this.$route.path.search(/^(\/channelData|\/detailedData)$/g) !== -1) {
        this.toggle.listTitle = 1
      } else if (this.$route.path.search(/^(\/userManage)$/g) !== -1) {
        this.toggle.listTitle = 2
      } else if (this.$route.path.search(/^(\/fieldTableInfo|\/databaseInfo|\/channelTableInfo)$/g) !== -1) {
        this.toggle.listTitle = 3
      }
    },
    alertClose () {
      bus.$emit('pop-up-close', '#alert.popup')
      setTimeout(() => {
        this.alert.toggle = false
      }, 500)
    },
    logout () {
      let data = {
        username: this.userInfo.username
      }
      requestLeave({}, data, (response) => {
        if (response.data) {
          if (response.data.state) {
            bus.$emit('alert', response.data.message)
            window.sessionStorage.removeItem('userInfo')
            this.$router.push({'path': '/'})
          } else {
            bus.$emit('alert', response.data.message)
          }
        } else {
          bus.$emit('alert', response + '.')
        }
      })
    }
  },
  watch: {
    $route (val, oldVal) {
      this.navAutoShow()
    },
    ptSelectedData (val, oldVal) {
      if (val === undefined) {
        return false
      }
      let data = {
        pt: this.ptSelectedData.pt
      }
      requestGetAllFields({}, data, (response) => {
        if (response.data) {
          if (response.data.state) {
            this.allFields = response.data.data
          } else {
            bus.$emit('alert', response.data.message)
          }
        } else {
          bus.$emit('alert', response + '.')
        }
      })
    }
  }
}
</script>

<style lang="scss">
body{
  height: 100%;
}
#app {
  /*font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;*/
  position: absolute;
  width: 100%;
  min-width: 1366px;
  min-height: 100%;
  background-color: #394048;
  line-height: 1.5;
  text-align: left;
  select{
    padding-right: 41px;
  }
  &>header{
    position: absolute;
    right: 0;
    top: 0;
    width: calc(100% - 230px);
    height: 50px;
    background-color: #F8FCFF;
    padding: 0 50px;
    .pageTitle{
      font-size: 20px;
      line-height: 50px;
    }
    .right{
      float: right;
      height: 50px;
      span{
        display: inline-block;
        position: relative;
        line-height: 50px;
        padding: 0 10px 0 35px;
        background-repeat: no-repeat;
        background-position: 5px center;
        background-size: 24px auto;
        &:nth-of-type(1){ background-image: url('~static/img/user.png'); }
        &:nth-of-type(2){ background-image: url('~static/img/exit.png'); }
      }
      .logout{
        transition: all 0.2s ease;
        &:hover{
          background-color: #E5E5E5;
          cursor: pointer;
        }
        &:before{
          display: inline-block;
          content: "";
          position: absolute;
          width: 1px;
          height: 65%;
          background-color: #000;
          left: 0;
          top: 50%;
          transform: translateY(-50%);
        }
      }
    }
  }
  &>nav{
    position: absolute;
    left: 0;
    top: 0;
    width: 230px;
    height: 100%;
    background-color: #282D33;
    color: #9ea9b9;
    user-select: none;
    .sysTitle{
      height: 50px;
      line-height: 180px;
      overflow: hidden;
      background-image: url('~static/img/nav-title.png');
      background-repeat: no-repeat;
      background-position: center;
      margin-bottom: 20px;
    }
    .selectPt{
      border: 1px solid #454c56;
      margin: 0 20px 20px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      box-shadow: 0 0 7px rgba(0, 0, 0, .9);
      position: relative;
      cursor: pointer;
      z-index: 1;
      p{
        height: 40px;
        line-height: 40px;
        color: #ccc;
        background-color: #282D33;
        font-size: 16px;
        font-weight: bold;
        position: relative;
        z-index: 1;
      }
      ul{
        position: absolute;
        width: calc(100% + 2px);
        left: -1px;
        line-height: 40px;
        border: 1px solid #454c56;
        border-top: none;
        box-shadow: 0 0 7px rgba(0, 0, 0, .9);
        li{
          background-color: #282D33;
          transition: all .3s ease;
          margin-top: -40px;
          &:hover{
            background-color: #14181B;
          }
        }
        &.active li{
          margin-top: 0px;
        }
        &:before{
          display: block;
          content: "";
          width: 100%;
          height: 1px;
          position: absolute;
          left: -1px;
          top: -1px;
          border: 1px solid #454c56;
          border-top: none;
          border-bottom: none;
        }
      }
    }
    .list-title a, .list-item a{
      display: block;
      text-align: center;
      line-height: 50px;
      text-indent: -15px;
      transition: all .2s ease;
      &:hover, &.active{
        color: #fff;
      }
    }
    .list-title{
      position: relative;
      background-repeat: no-repeat;
      background-size: 40px 40px;
      background-position: 20px center;
      &.icon-1{ background-image: url('~static/img/list-1.png'); }
      &.icon-2{ background-image: url('~static/img/list-2.png'); }
      &.icon-3{ background-image: url('~static/img/list-3.png'); }
      &.icon-4{ background-image: url('~static/img/list-4.png'); }
      img{
        position: absolute;
        right: 20px;
        top: 50%;
        transform: translateY(-50%) rotate(90deg);
        transition: all 0.2s ease;
      }
      &.active{
        img{
          transform: translateY(-50%) rotate(0deg);
        }
        &+.list-item a{
          margin-top: 0px;
        }
      }
    }
    .list-item{
      overflow: hidden;
      a{
        margin-top: -50px;
        &:hover, &.active{
          background-color: #14181B;
        }
      }
    }
  }
  &>section{
    position: absolute;
    right: 0;
    top: 50px;
    width: calc(100% - 230px);
    height: calc(100% - 50px);
    overflow-y: auto;
    &.loginPage{
      top: 0;
      width: 100%;
      height: 100%;
    }
    /*定义滚动条高宽及背景 高宽分别对应横竖滚动条的尺寸*/
    &::-webkit-scrollbar{
      width: 8px;
      height: 8px;
      background-color: #F5F5F5;
    }
    /*定义滚动条轨道 内阴影+圆角*/
    &::-webkit-scrollbar-track{
      // border-radius: 10px;
      background-color: #394048;
      border-left: 1px solid #121315;
    }
    /*定义滑块 内阴影+圆角*/
    &::-webkit-scrollbar-thumb{
      border-radius: 2px;
      background-color: rgba(73,175,247,0.3);
      &:hover{
        background-color: rgba(73,175,247,0.7);
      }
    }
    &>div{
      position: relative;
      padding-bottom: 150px;
      min-height: 100%;
      color: #c0c7cd;
      &:after{
        display: block;
        content: "©2012-2017 zhiweidata.com, All Rights Reserved.京ICP备13015804号-1备案京公网安备11010502034130号";
        position: absolute;
        width: 100%;
        height: 30px;
        line-height: 30px;
        text-align: center;
        left: 0;
        bottom: 0;
        color: #7F868E;
        font-size: 12px;
      }
    }
  }
}
#alert{
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%) scale(0);
  width: 470px;
  height: 270px;
  background-color: #394048;
  border: 1px solid #454E57;
  color: #828991;
  border-radius: 5px;
  overflow: hidden;
  .alert-title{
    line-height: 40px;
    border-bottom: 1px solid #4E5560;
    padding: 0 20px;
    font-size: 18px;
  }
  .alert-content{
    height: 170px;
    padding: 20px 20px 0 20px;
    overflow-y: auto;
    /*定义滚动条高宽及背景 高宽分别对应横竖滚动条的尺寸*/
    &::-webkit-scrollbar{
      width: 8px;
      height: 8px;
      background-color: #F5F5F5;
    }
    /*定义滚动条轨道 内阴影+圆角*/
    &::-webkit-scrollbar-track{
      // border-radius: 10px;
      background-color: #282D33;
      // border-left: 1px solid #121315;
    }
    /*定义滑块 内阴影+圆角*/
    &::-webkit-scrollbar-thumb{
      border-radius: 2px;
      background-color: rgba(73,175,247,0.3);
      &:hover{
        background-color: rgba(73,175,247,0.7);
      }
    }
  }
  .alert-btn{
    text-align: center;
  }
}
</style>

<!-- 项目公共样式 -->

<style lang="scss">
  .searchInput{
    width: 412px;
    height: 36px;
    border-radius: 3px;
    border: 1px solid #4E5560;
    background-color: #262D33;
    background-image: url('~static/img/search.png');
    background-repeat: no-repeat;
    background-position: 10px center;
    padding: 3px 6px 3px 45px;
    color: #d4d4d4;
    transition: all .3s ease;
    font-size: 16px;
    &:focus{
      border-color: #49B0F7;
      background-image: url('~static/img/search_focus.png');
    }
    &::-webkit-input-placeholder { /* WebKit browsers */
      color: #868D93;
    }
    &:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
      color: #868D93;
    }
    &::-moz-placeholder { /* Mozilla Firefox 19+ */
      color: #868D93;
    }
    &:-ms-input-placeholder { /* Internet Explorer 10+ */
      color: #868D93;
    }
  }
  .btn1{
    transition: all .3s ease;
    &:not([disabled]){
      width: 122px;
      height: 33px;
      border: 1px solid #4E5560;
      background-color: #262D33;
      color: #c0c7cd;
      border-radius: 2px;
      &:hover{
        background-color: #49AFF7;
        color: #fff;
        border-color: #49AFF7;
      }
    }
  }
  .btn2{
    transition: all .3s ease;
    &:not([disabled]){
      width: 84px;
      height: 33px;
      border: 1px solid #4E5560;
      // background-color: #262D33;
      background-color: transparent;
      color: #c0c7cd;
      border-radius: 2px;
      &:hover{
        background-color: #49AFF7;
        color: #fff;
        border-color: #49AFF7;
      }
    }
  }
  .btn3{
    transition: all .3s ease;
    &:not([disabled]){
      width: 125px;
      height: 39px;
      border: 2px solid #242C2F;
      background-color: transparent;
      letter-spacing: 5px;
      text-indent: 5px;
      color: #c0c7cd;
      border-radius: 2px;
      &:hover{
        background-color: #49AFF7;
        color: #fff;
        border-color: #242C2F;
      }
    }
  }
  .btn4{
    transition: all .3s ease;
    &:not([disabled]){
      display: block;
      width: 80%;
      height: 50px;
      margin: auto;
      border: 2px solid #48515A;
      background-color: transparent;
      letter-spacing: 20px;
      text-indent: 20px;
      color: #49515C;
      border-radius: 2px;
      &:hover{
        background-color: #49AFF7;
        color: #fff;
        border-color: #49AFF7;
      }
    }
  }
  .table{
    background-color: #282D33;
    padding: 15px;
    table{
      // color: #868D93;
      line-height: 50px;
      border-collapse:collapse;
      tr{
        td{
          border: 1px solid #394048;
          padding: 0 10px;
          position: relative;
        }
        &:nth-of-type(2n+3) td{ background-color: #262B31; }
        &.empty>td:after{
          display: inline;
          content: "-";
          color: #383F47;
        }
      }
    }
  }
  label.checkbox{
    display: inline-block;
    line-height: 1;
    input[type=checkbox]{
      &+span{
        width: 25px;
        height: 25px;
        line-height: 22px;
        border-radius: 0;
        transition: all .3s ease;
        overflow: hidden;
        border-color: #383F47;
        position: relative;
        top: 7px;
      }
      &:checked+span:after{
        display: inline-block;
        content: "✔";
        width: 100%;
        height: 100%;
        font-size: 26px;
        color: #49B0F7;
      }
      &+span:hover{
        border: 1px solid #49B0F7;
      }
      &:checked+span{
        border: 1px solid #49B0F7;
      }
    }
  }
  label.toggle{
    display: inline-block;
    line-height: 1;
    input[type=checkbox]{
      display: none;
      &+span{
        display: inline-block;
        width: 45px;
        height: 25px;
        line-height: 22px;
        border-radius: 25px;
        transition: all .3s ease;
        overflow: hidden;
        border: 1px solid #383F47;
        position: relative;
        top: 7px;
        box-shadow: 0 0 0 1px #262B31 inset;
      }
      &+span:after{
        display: inline-block;
        content: "";
        width: 21px;
        height: 21px;
        border-radius: 21px;
        // background-color: #49B0F7;
        background-color: #383F47;
        position: absolute;
        left: 1px;
        top: 1px;
        transition: all .3s ease;
        box-shadow: 0 0 0 1px #262B31;
      }
      &+span:before{
        display: inline-block;
        content: "";
        width: 21px;
        height: 21px;
        background-color: #14171a;
        position: absolute;
        left: 1px;
        top: 1px;
        border-radius: 21px;
        transition: all .3s ease;
      }
      &:checked+span{
        border: 1px solid #49B0F7;
        &:after{
          background-color: #49B0F7;
          left: 21px;
        }
        &:before{
          width: 42px;
        }
      }
      &+span:hover{
        border: 1px solid #49B0F7;
        &:after{
          background-color: #49B0F7;
        }
      }
    }
  }
  .pageList{
    text-align: center;
    user-select: none;
    color: #68757D;
    div{
      display: inline-block;
      padding: 0 10px;
      min-width: 34px;
      height: 34px;
      line-height: 34px;
      color: #68757D;
      cursor: pointer;
      transition: all .3s ease;
      border-radius: 2px;
      &:hover{
        color: #fff;
      }
      &.active{
        background-color: #49AFF7;
        color: #fff;
      }
    }
  }
  .mask{
    position: absolute;
    width: 100%;
    height: 100%;
    left: 0;
    top: 0;
    background-color: rgba(37, 43, 48, .5);
    z-index: 10;
    .pop-up{
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%) scale(0);
      opacity: 0;
      width: 710px;
      background-color: #394048;
      border: 1px solid #515B65;
      color: #828991;
      .title{
        line-height: 40px;
        font-size: 18px;
        padding-left: 10px;
        border-bottom: 1px solid #515B65;
        i.close{
          display: block;
          width: 40px;
          height: 40px;
          background-image: url('~static/img/close.png');
          background-repeat: no-repeat;
          background-position: center;
          float: right;
          cursor: pointer;
        }
      }
      .main{
        padding: 20px 30px;
        input{
          padding: 3px 6px;
          &::-webkit-input-placeholder { /* WebKit browsers */
            color: #848D94;
          }
          &:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
            color: #848D94;
          }
          &::-moz-placeholder { /* Mozilla Firefox 19+ */
            color: #848D94;
          }
          &:-ms-input-placeholder { /* Internet Explorer 10+ */
            color: #848D94;
          }
        }
      }
      .btns{
        text-align: center;
        margin-bottom: 40px;
        button{
          &:nth-of-type(n + 2){
            margin-left: 70px;
          }
        }
      }
    }
  }
  .dateInput{
    cursor: pointer;
  }
  .lengthLimit{
    display: inline-block;
    line-height: 1;
    max-width: 250px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .lengthLimit-new{
    display: inline-block;
    line-height: 1;
    max-width: 130px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    z-index: 1;
    &:hover{
      background-color: #c0c7cd;
      color: #333;
      padding: 3px;
      border-radius: 3px;
      overflow: visible;
      white-space: nowrap;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      max-width: 9999px!important;
    }
  }

  /*定义滚动条高宽及背景 高宽分别对应横竖滚动条的尺寸*/
  ::-webkit-scrollbar{
    width: 8px;
    height: 8px;
    background-color: #F5F5F5;
  }
  /*定义滚动条轨道 内阴影+圆角*/
  ::-webkit-scrollbar-track{
    // border-radius: 10px;
    background-color: #394048;
    // border-left: 1px solid #292E34;
  }
  /*定义滑块 内阴影+圆角*/
  ::-webkit-scrollbar-thumb{
    border-radius: 2px;
    background-color: rgba(73,175,247,0.3);
    &:hover{
      background-color: rgba(73,175,247,0.7);
    }
  }
</style>
