<template>
  <div class="layout">
    <div class="top">
      <img src="@/assets/img/logo.png" alt="" />
      <p>石壁龙智慧园区</p>
      <div class="top_right">
        <div class="user" @click="cut = !cut">
          <img src="@/assets/img/user.jpg" alt="" />
          <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">数据管理</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">系统管理</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">
        <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: 1, url: '/layout/tab1' },
        { name: '鹏城云脑-能耗管理', id: 2, url: '/layout/tab2' },
        { name: '鹏城云脑-安防管理', id: 3, url: '/layout/tab3' },
        { name: '鹏城云脑-物联网设备', id: 4, url: '/layout/tab4' },
        { name: '工地监控', id: 5, url: '/layout/tab5' },
        { name: '无人机巡检', id: 6, url: '/layout/tab6' }
      ],
      tab2: [
        { name: '用户管理', id: 7, url: '/layout/tab7' },
        { name: '操作日志', id: 8, url: '/layout/tab8' }
      ]
    }
  },
  // 监听属性 类似于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))
        }
        await updatePwd(data)
        this.$message.success('修改成功')
        localStorage.clear('SWKK_token')
        localStorage.clear('SWKK_userInfo')
        this.$router.push('/')
      } catch (error) {
        this.$message.error('旧密码错误')
      }
    },
    push (url) {
      this.$router.push(url).catch(() => {})
    },
    // 点击退出登录
    outLogin () {
      this.cut = false
      this.$confirm('确定退出吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          localStorage.clear('SZSBL_token')
          localStorage.clear('SZSBL_userName')
          this.$router.push('/')
          this.$message.success('退出成功!')
        })
        .catch(() => {
          this.$message.info('已取消')
        })
    }
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created () {
    // 获取用户名
    const res = localStorage.getItem('SZSBL_userName')
    this.userName = res
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted () {},
  beforeCreate () {}, // 生命周期 - 创建之前
  beforeMount () {}, // 生命周期 - 挂载之前
  beforeUpdate () {}, // 生命周期 - 更新之前
  updated () {}, // 生命周期 - 更新之后
  beforeDestroy () {}, // 生命周期 - 销毁之前
  destroyed () {}, // 生命周期 - 销毁完成
  activated () {} // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style lang='less' scoped>
.layout {
  .top {
    position: relative;
    width: 100%;
    min-width: 1800px;
    box-shadow: 1px 3px 3px 3px rgb(196, 177, 177);
    height: 70px;
    background-color: #1482b4;
    display: flex;
    align-items: center;
    & > img {
      width: 35px;
      height: 35px;
      border-radius: 50%;
      overflow: hidden;
      margin: 0 30px;
    }
    & > 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;
        img {
          margin-right: 15px;
          width: 34px;
          height: 34px;
          border-radius: 50%;
          overflow: hidden;
        }
        .pull_down {
          position: absolute;
          right: -25px;
          bottom: 10px;
          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: 10px;
          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: #1482b4;
        li {
          color: #fff;
          cursor: pointer;
          margin: 10px 0;
        }
        li:hover {
          color: #dc3545;
        }
      }
    }
  }
  .con {
    display: flex;
    width: 100%;
    padding: 30px 30px 0;
    height: calc(100vh - 100px);
  }
  .left {
    min-width: 220px;
    width: 220px;
    height: 100%;
    background-color: #fff;
    padding-top: 15px;
    .biaoji {
      height: 40px;
      background-color: #1482b4;
      width: 90%;
      border-radius: 0 40px 40px 0;
      color: #fff;
      font-weight: 700;
      font-size: 20px;
      line-height: 40px;
      padding-left: 50px;
    }
    ul {
      li {
        cursor: pointer;
        padding-left: 50px;
        margin: 40px 0;
        &:hover {
          color: #1482b4;
        }
      }
      .active {
        color: #1482b4;
      }
    }
  }
  .right {
    padding: 20px;
    min-width: 1500px;
    flex: 1;
    margin-left: 30px;
    background-color: #fff;
    height: 100%;
  }
}
</style>
