<template>
  <div class="layout">
    <div class="top">
      <p>河南博物院文物展示平台后台</p>
      <div class="top_right" @mouseenter="cut = true" @mouseleave="cut = false">
        <div class="user">
          <span>{{ userName }}</span>
          <!-- 下箭头 -->
          <div class="pull_down" v-if="cut"></div>
          <div class="pull_up" v-else></div>
        </div>
        <!-- 点击箭头出来的ul -->
        <ul class="user_handle" v-show="cut">
          <li @click="isShow = true">修改密码</li>
          <li @click="outLogin">退出登录</li>
        </ul>
      </div>
    </div>
    <div class="con">
      <div class="left">
        <div
          class="biaoji el-icon-message"
          :class="{ biaojiAc: $route.meta.myInd === 2 }"
        >
          内容管理
        </div>
        <ul>
          <li
            v-for="item in tab1"
            :key="item.id"
            :class="{ active: $route.meta.myInd === item.id }"
            @click="push(item.url)"
          >
            {{ item.name }}
          </li>
        </ul>

        <div
          class="biaoji el-icon-s-data"
          :class="{ biaojiAc: $route.meta.myInd === 1 }"
        >
          统计管理
        </div>
        <ul>
          <li
            v-for="item in tab3"
            :key="item.id"
            :class="{ active: $route.meta.myInd === item.id }"
            @click="push(item.url)"
          >
            {{ item.name }}
          </li>
        </ul>

        <div
          class="biaoji el-icon-setting"
          :class="{ biaojiAc: $route.meta.myInd === 3 }"
        >
          系统管理
        </div>
        <ul>
          <li
            v-for="item in tab2"
            :key="item.id"
            :class="{ active: $route.meta.myInd === item.id }"
            @click="push(item.url)"
          >
            {{ item.name }}
          </li>
        </ul>
      </div>
      <!-- 右侧内容 -->
      <div class="right" @click="cut = false">
        <Router-view />
      </div>
    </div>
    <!-- 点击修改密码出现弹窗 -->
    <el-dialog title="修改密码" :visible.sync="isShow" @close="btnX()">
      <el-form :model="form" label-width="100px" :rules="rules" ref="ruleForm">
        <el-form-item label="旧密码：" prop="oldPassword">
          <el-input
            v-model="form.oldPassword"
            placeholder="请输入"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item label="新密码：" prop="newPassword">
          <el-input
            v-model="form.newPassword"
            placeholder="请输入"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item label="确定新密码：" prop="checkPass">
          <el-input
            v-model="form.checkPass"
            placeholder="请输入"
            show-password
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="btnX">取 消</el-button>
        <el-button type="primary" @click="btnOk">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { encodeStr } from '../../utils/pass'
import { Base64 } from 'js-base64'
import { updatePwd } from '@/apis/login'
export default {
  name: 'layout',
  components: {},
  data () {
    // 这里存放数据
    const validatePass2 = (rule, value, callback) => {
      if (value !== this.form.newPassword) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      // 修改密码
      form: {
        oldPassword: '',
        newPassword: '',
        checkPass: ''
      },
      rules: {
        checkPass: [{ validator: validatePass2, trigger: 'blur' }],
        oldPassword: [{ required: true, message: '不能为空', trigger: 'blur' }],
        newPassword: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { max: 15, message: '不能超过15个字', trigger: 'blur' }
        ]
      },
      userName: '',
      cut: false,
      isShow: false,
      tab1: [{ name: '典藏管理', id: 2, url: '/layout/tab2' }],
      tab2: [{ name: '操作日志', id: 3, url: '/layout/tab3' }],
      tab3: [{ name: '文物统计', id: 1, url: '/layout/tab1' }]
    }
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 方法集合
  methods: {
    // 修改密码点击取消
    btnX () {
      this.$refs.ruleForm.resetFields()
      this.cut = false
      this.isShow = false
      this.form = {
        oldPassword: '',
        newPassword: '',
        checkPass: ''
      }
    },
    // 修改密码点击确定
    async btnOk () {
      await this.$refs.ruleForm.validate()
      try {
        const data = {
          oldPassword: encodeStr(Base64.encode(this.form.oldPassword)),
          newPassword: encodeStr(Base64.encode(this.form.newPassword))
        }
        const res = await updatePwd(data)
        if (res.code === 0) {
          this.$message.success('修改成功')
          localStorage.clear('HNBWY_token')
          localStorage.clear('HNBWY_userInfo')
          this.$router.push('/')
        } else this.$message.error(res.msg)
      } catch (error) {
        console.log(error)
      }
    },
    push (url) {
      this.$router.push(url).catch(() => {})
    },
    // 点击退出登录
    outLogin () {
      this.cut = false
      this.$confirm('确定退出吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          localStorage.clear('HNBWY_token')
          localStorage.clear('HNBWY_userName')
          this.$router.push('/')
          this.$message.success('退出成功!')
        })
        .catch(() => {
          this.$message.info('已取消')
        })
    }
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created () {
    // 获取用户名
    const res = localStorage.getItem('HNBWY_userName')
    this.userName = res
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted () {},
  beforeCreate () {}, // 生命周期 - 创建之前
  beforeMount () {}, // 生命周期 - 挂载之前
  beforeUpdate () {}, // 生命周期 - 更新之前
  updated () {}, // 生命周期 - 更新之后
  beforeDestroy () {}, // 生命周期 - 销毁之前
  destroyed () {}, // 生命周期 - 销毁完成
  activated () {} // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style lang='less' scoped>
.layout {
  .top {
    padding-left: 50px;
    position: relative;
    width: 100%;
    min-width: 1860px;
    box-shadow: 1px 3px 3px 3px rgb(196, 177, 177);
    height: 70px;
    background-color: #3a80d2;
    display: flex;
    align-items: center;
    & > P {
      color: #fff;
      font-size: 24px;
      font-weight: 700;
    }
    .top_right {
      width: 150px;
      position: absolute;
      right: 30px;
      top: 0;
      height: 100%;
      display: flex;
      align-items: center;
      justify-content: space-between;
      .user {
        color: #fff;
        cursor: pointer;
        position: relative;
        display: flex;
        align-items: center;
        font-weight: 700;
        img {
          margin-right: 15px;
          width: 34px;
          height: 34px;
          border-radius: 50%;
          overflow: hidden;
        }
        .pull_down {
          position: absolute;
          right: -25px;
          bottom: 4px;
          display: inline-block;
          width: 0;
          height: 0;
          border-left: 8px solid transparent;
          border-right: 8px solid transparent;
          border-bottom: 8px solid #fff;
        }
        .pull_up {
          cursor: pointer;
          position: absolute;
          right: -25px;
          bottom: 4px;
          display: inline-block;
          width: 0;
          height: 0;
          border-left: 8px solid transparent;
          border-right: 8px solid transparent;
          border-top: 8px solid #fff;
        }
      }
      .user_handle {
        z-index: 999;
        padding: 10px 40px;
        position: absolute;
        right: 0;
        bottom: -90px;
        background-color: #3a80d2;
        li {
          color: #fff;
          cursor: pointer;
          margin: 10px 0;
        }
        li:hover {
          color: orange;
        }
      }
    }
  }
  .con {
    min-width: 1860px;
    min-height: 807px;
    background-color: #f2ecde;
    display: flex;
    width: 100%;
    padding: 30px;
    height: calc(100vh - 70px);
  }
  .left {
    min-width: 180px;
    width: 180px;
    height: 100%;
    min-height: 770px;
    background-color: #fff;
    padding-top: 30px;
    .biaoji {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 40px;
      width: 90%;
      border-radius: 0 40px 40px 0;
      font-weight: 700;
      font-size: 20px;
      padding-left: 20px;
      padding-right: 30px;
    }
    .biaojiAc {
      background-color: #3a80d2;
      color: #fff;
    }
    ul {
      li {
        cursor: pointer;
        padding-left: 55px;
        margin: 30px 0;
        &:hover {
          color: #3a80d2;
        }
      }
      .active {
        color: #3a80d2;
      }
    }
  }
  .right {
    position: relative;
    padding: 20px 0 0 0;
    min-width: 1500px;
    flex: 1;
    margin-left: 30px;
    background-color: #fff;
    height: 100%;
    min-height: 770px;
  }
}
</style>
