<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-date-picker
            v-model="time"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>

          <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>
        <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="userName" label="操作者"></el-table-column>
          <el-table-column prop="createTime" label="操作时间"></el-table-column>
          <el-table-column prop="ip" label="ip地址"> </el-table-column>
          <el-table-column prop="type" label="操作类型"> </el-table-column>
          <el-table-column prop="description" label="操作记录"> </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>
  </div>
</template>

<script>
import { getLogList } from '@/apis/tab4'
export default {
  name: 'tab3',
  data () {
    // 这里存放数据
    return {
      total: 0,
      time: '',
      form: {
        startTime: '',
        endTime: '',
        pageNum: 1,
        pageSize: 10,
        searchKey: ''
      },
      tableData: []
    }
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {
    time (val) {
      this.handleSelect(val)
    }
  },
  // 方法集合
  methods: {
    // 点击搜索
    search () {
      this.form.pageNum = 1
      this.getLogList(this.form)
    },
    // 分页器方法
    currentChange (val) {
      // console.log('当前页改变了', val)
      this.form.pageNum = val
      this.getLogList(this.form)
    },
    sizeChange (val) {
      // console.log('条数改变了', val)
      this.form.pageNum = 1
      this.form.pageSize = val
      this.getLogList(this.form)
    },
    // 封装获取列表方法
    async getLogList (data) {
      const res = await getLogList(data)
      console.log(999, res)
      this.tableData = res.data.list
      this.total = res.data.total
    },
    // 时间处理----------------
    handleSelect (e) {
      const date = []
      for (const i in e) {
        date.push(this.gettime(e[i]))
      }
      this.form.startTime = date[0]
      if (date[1]) this.form.endTime = date[1].replace('00:00:00', '23:59:59')
    },
    gettime (data) {
      const value =
        data.getFullYear() +
        '-' +
        this.checkTime(data.getMonth() + 1) +
        '-' +
        this.checkTime(data.getDate()) +
        ' ' +
        this.checkTime(data.getHours()) +
        ':' +
        this.checkTime(data.getMinutes()) +
        ':' +
        this.checkTime(data.getSeconds())
      return value
    },
    checkTime (i) {
      if (i < 10) {
        i = '0' + i
      }
      return i
    }
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created () {
    this.getLogList(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: 8px 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;
      }
    }
    .paging {
      position: absolute;
      bottom: 15px;
      right: 50px;
    }
  }
}
</style>
