<template>
  <div class="layout">
    <div class="top">
      <div class="logo">
        <img src="@/assets/img/logo.png" alt="" />
      </div>
      <div class="right">
        <div class="txt">项目资料管理系统</div>
        <div class="user" @click="cut = !cut">
          <span>{{ userInfo.realName }}</span>
          <img src="@/assets/img/user.jpg" alt="" />
          <!-- 下箭头 -->
          <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="conten">
      <div class="leftTab">
        <div class="title">数据管理</div>
        <ul>
          <li
            v-for="item in tabList1"
            :key="item.id"
            :class="{ active: $route.meta.myInd === item.id }"
            @click="skip(item.id, item.path)"
          >
            {{ item.name }}
          </li>
        </ul>
        <div class="title" v-if="userInfo.roleId===1">系统管理</div>
        <ul v-if="userInfo.roleId===1">
          <li
            v-for="item in tabList2"
            :key="item.id"
            :class="{ active: $route.meta.myInd === item.id }"
            @click="skip(item.id, item.path)"
          >
            {{ item.name }}
          </li>
        </ul>
      </div>
      <!-- 右侧内容 -->
      <Router-view style="width: 1630px" @click.native="cut = false" />
    </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>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// 例如：import 《组件名称》 from '《组件路径》';
import { updatePwd } from '@/apis/tab4'
export default {
  // import引入的组件需要注入到对象中才能使用
  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' }
        ]
      },
      isShow: false,
      cut: false,
      userInfo: {},
      tabList1: [
        { id: 0, name: '项目资料', path: '/layout/tab1' },
        { id: 1, name: '文物案例', path: '/layout/tab2' },
        { id: 2, name: '场景案例', path: '/layout/tab3' }
      ],
      tabList2: [
        { id: 3, name: '用户管理', path: '/layout/tab4' },
        { id: 4, name: '操作日志', path: '/layout/tab5' }
      ]
    }
  },
  // 监听属性 类似于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 {
        await updatePwd(this.form)
        this.$message.success('修改成功')
        localStorage.clear('SWKK_token')
        localStorage.clear('SWKK_userInfo')
        this.$router.push('/')
      } catch (error) {
        this.$message.error('旧密码错误')
      }
    },
    skip (id, url) {
      this.$router.push(url).catch(() => {})
    },
    // 点击退出登录
    outLogin () {
      this.cut = false
      this.$confirm('确定退出吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          localStorage.clear('SWKK_token')
          localStorage.clear('SWKK_userInfo')
          this.$router.push('/')
          this.$message.success('退出成功!')
        })
        .catch(() => {
          this.$message.info('已取消')
        })
    }
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created () {
    // 获取用户信息
    const res = localStorage.getItem('SWKK_userInfo')
    // console.log(999, JSON.parse(res))
    this.userInfo = JSON.parse(res)
    // 如果是普通用户---删掉场景案例
    // if (this.userInfo.roleId === 2) {
    //   this.tabList1.pop()
    // }
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted () {
  },
  beforeCreate () {}, // 生命周期 - 创建之前
  beforeMount () {}, // 生命周期 - 挂载之前
  beforeUpdate () {}, // 生命周期 - 更新之前
  updated () {}, // 生命周期 - 更新之后
  beforeDestroy () {}, // 生命周期 - 销毁之前
  destroyed () {}, // 生命周期 - 销毁完成
  activated () {} // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style lang='less' scoped>
.layout {
  .top {
    display: flex;
    padding: 10px 0;
    height: 60px;
    background-color: #f6f8f9;
    box-shadow: 0px 0px 5px 0px;
  }
  .logo {
    width: 290px;
    border-right: 2px solid #A3A2A9;
    text-align: center;
  }
  .right {
    position: relative;
    flex: 1;
    padding: 0 60px;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .txt {
      font-size: 24px;
      font-weight: 700;
    }
    .user {
      cursor: pointer;
      position: relative;
      display: flex;
      align-items: center;
      img {
        margin-left: 15px;
        width: 34px;
        height: 34px;
        border-radius: 50%;
        overflow: hidden;
      }
      .pull_down {
        position: absolute;
        right: -24px;
        bottom: 5px;
        display: inline-block;
        width: 0;
        height: 0;
        border-left: 8px solid transparent;
        border-right: 8px solid transparent;
        border-bottom: 8px solid black;
      }
      .pull_up {
        cursor: pointer;
        position: absolute;
        right: -24px;
        bottom: 5px;
        display: inline-block;
        width: 0;
        height: 0;
        border-left: 8px solid transparent;
        border-right: 8px solid transparent;
        border-top: 8px solid black;
      }
    }
    .user_handle {
      z-index: 999;
      padding: 10px 40px;
      position: absolute;
      right: 0;
      bottom: -102px;
      background-color: #f6f8f9;
      li {
        cursor: pointer;
        margin: 10px 0;
      }
      li:hover {
        color: #dc3545;
      }
    }
  }
}
.leftTab {
  font-size: 24px;
  color: #fff;
  padding: 30px 25px;
  width: 290px;
  height: calc(100vh - 60px);
  min-height: 870px;
  background-color: rgba(0, 0, 0, 0.8);
  .title {
    border-radius: 5px;
    margin-top: 30px;
    width: 230px;
    height: 60px;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #dc3545;
  }
  ul {
    margin-top: 10px;
    li {
      background-color: rgba(0,0,0,.2);
      margin: 5px 0;
      cursor: pointer;
      width: 230px;
      height: 60px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    li:hover {
      background-color: rgba(220, 53, 69, 0.6);
    }
    .active {
      background-color: rgba(220, 53, 69, 0.6);
    }
  }
}
.conten {
  display: flex;
}
</style>
