<template>
  <div class="template">
    <div class="top">
      <div class="location">系统管理 / <span>用户管理</span></div>
    </div>
    <div class="conten">
      <div class="search">
        <div class="txt">筛选查询</div>
        <div class="row">
          <span>用户名：</span>
          <el-input v-model="form.searchKey" placeholder="请输入"></el-input>
        </div>
        <div class="btn" @click="search">搜 索</div>
      </div>
      <!-- 表格 -->
      <div class="table">
        <div class="title">
          <div class="txt">用户列表</div>
          <div class="addBtn" @click="addUser">+ 新增</div>
        </div>
        <el-table
          :header-cell-style="{ background: '#e3e6e9' }"
          :data="tableData"
          stripe
          style="width: 100%"
        >
          <el-table-column label="序号" width="80">
            <template slot-scope="scope">
              {{ scope.$index + (form.pageNum - 1) * form.pageSize + 1 }}
            </template>
          </el-table-column>
          <el-table-column prop="realName" label="用户名"></el-table-column>
          <el-table-column prop="roleName" label="用户角色"></el-table-column>
          <el-table-column prop="dept" label="部门"> </el-table-column>
          <el-table-column prop="createTime" label="创建时间"> </el-table-column>
          <el-table-column prop="remark" label="备注"> </el-table-column>
          <el-table-column label="操作">
            <template #default='{row}'>
              <el-button type="text" @click="editUser(row.id)">编 辑</el-button>
              <el-button type="text" @click="delUserById(row.id)" v-if="row.roleId!==1">删 除</el-button>
              <el-button type="text" @click="resetPass(row.id)" v-if="row.roleId!==1">重置密码</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页 -->
      <div class="paging">
        <el-pagination layout="prev, pager, next,sizes,jumper"
        :total="total"
          :current-page='form.pageNum'
          @current-change="currentChange"
          @size-change="sizeChange"
        >
        </el-pagination>
      </div>
    </div>
    <!-- 点击新增的弹窗 -->
    <Tab4Add :isShow.sync="isShow" @update='getUserList(form)' ref="myUser"/>
  </div>
</template>

<script>
import Tab4Add from './tab4_add.vue'
import { getUserList, delUserById, resetPass } from '@/apis/tab4'
export default {
  name: 'tab3',
  components: { Tab4Add },
  data () {
    // 这里存放数据
    return {
      isShow: false,
      total: 0,
      form: {
        pageNum: 1,
        pageSize: 10,
        searchKey: ''
      },
      tableData: []
    }
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 方法集合
  methods: {
    // 点击搜索
    search () {
      this.form.pageNum = 1
      this.getUserList(this.form)
    },

    // 点击重置密码
    resetPass (id) {
      this.$confirm('确定重置密码吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await resetPass(id)
          if (res.code === 0) {
            this.getUserList(this.form)
            this.$message.success('重置密码成功!')
          }
          console.log(999, res)
        })
        .catch(() => {
          this.$message.info('已取消重置!')
        })
    },
    // 点击删除
    delUserById (id) {
      this.$confirm('确定删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await delUserById(id)
          if (res.code === 0) {
            this.getUserList(this.form)
            this.$message.success('删除成功!')
          }
          console.log(999, res)
        })
        .catch(() => {
          this.$message.info('已取消删除!')
        })
    },
    // 点击编辑
    editUser (id) {
      this.$refs.myUser.getUserDetailById(id)
      this.isShow = true
    },
    // 点击新增
    addUser () {
      this.isShow = true
    },
    // 封装获取文物列表方法
    async getUserList (data) {
      const res = await getUserList(data)
      this.tableData = res.data.list

      this.total = res.data.total
    },
    // 分页器方法
    currentChange (val) {
      // console.log('当前页改变了', val)
      this.form.pageNum = val
      this.getUserList(this.form)
    },
    sizeChange (val) {
      // console.log('条数改变了', val)
      this.form.pageNum = 1
      this.form.pageSize = val
      this.getUserList(this.form)
    }

  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created () {
    this.getUserList(this.form)
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted () {},
  beforeCreate () {}, // 生命周期 - 创建之前
  beforeMount () {}, // 生命周期 - 挂载之前
  beforeUpdate () {}, // 生命周期 - 更新之前
  updated () {}, // 生命周期 - 更新之后
  beforeDestroy () {}, // 生命周期 - 销毁之前
  destroyed () {}, // 生命周期 - 销毁完成
  activated () {} // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style lang='less' scoped>
.template {
  /deep/.el-table__body-wrapper {
    max-height: 450px;
    overflow-y: auto;
  }
  /deep/.cell {
    padding-left: 20px !important;
  }
  /deep/.el-table__row .el-table__cell {
    padding:  0 !important;
  }
  /deep/.el-pager li {
    background: transparent !important;
  }
  /deep/.btn-prev {
    background: transparent !important;
  }
  /deep/.btn-next {
    background: transparent !important;
  }
  padding: 0px 30px;
  .top {
    display: flex;
    align-items: center;
    height: 58px;
    width: 100%;

    .location {
      font-size: 22px;
      color: #999;
      & > span {
        color: black;
      }
    }
  }
  .conten {
    position: relative;
    width: 100%;
    height: 780px;
    .txt {
      font-weight: 700;
      height: 40px;
      border-bottom: 1px solid #A3A2A9;
    }
    .search {
      position: relative;
      padding: 15px 20px 0;
      border-radius: 5px;
      box-shadow: 0px 0px 5px 0px;
      height: 130px;
      background-color: #d8dadc;
      .row {
        width: 100%;
        margin-top: 20px;
        display: flex;
        align-items: center;
        & > div {
          margin-right: 50px;
          width: 20%;
          display: flex;
          align-items: center;
        }
      }
      .btn {
        border-radius: 5px;
        display: flex;
        justify-content: center;
        align-items: center;
        color: #fff;
        cursor: pointer;
        width: 170px;
        height: 40px;
        background-color: #dc3545;
        position: absolute;
        right: 50px;
        top: 75px;
      }
    }
        .table {
      border-radius: 8px;
      overflow: hidden;
      margin-top: 25px;
      .title {
        position: relative;
        padding: 15px 20px 0;
        background-color: #e3e6e9;
        .addBtn {
          cursor: pointer;
          display: flex;
          justify-content: center;
          align-items: center;
          color: #fff;
          width: 80px;
          height: 30px;
          background-color: #dc3545;
          border-radius: 12px;
          z-index: 999;
          position: absolute;
          right: 56px;
          top: 62px;
        }
      }
    }
    .paging {
      position: absolute;
      bottom: 15px;
      right: 50px;
    }
  }
}
</style>
