<template lang='pug'>
    el-container(style="padding:0px;height:77.6vh;")
        el-header(style='height:42px;line-height:42px;border: 1px solid rgba(195, 195, 195, 1);background: #f4f4f4;')
            span.title-bold.title-left-color() 系统日志
            <el-popover placement="bottom" width="150" >
                    <el-button slot="reference" size='mini' type="primary" icon="el-icon-s-tools" style='float:right;transform:translateY(6px)'> 筛选</el-button>
                    <el-checkbox-group v-model="checkList">
                        <el-col :span="24">
                            <el-checkbox v-for="(item,index) in tableHeader" :label="item" :key="index"  >
                              span {{item.label}}
                            </el-checkbox>
                        </el-col>
                    </el-checkbox-group>
            </el-popover>
        el-main.box_main
            el-card()
                div.tableCard()
                    el-form( :inline="true" :model="dataForm" size="mini" @keyup.enter.native="getDataList()" )
                        el-form-item( label="操作员:")
                            el-input(  v-model="dataForm.username" placeholder="操作员" clearable)
                        el-form-item( label="操作类型:")
                            el-select( v-model="dataForm.operation" placeholder="请选择" clearable)
                                el-option( v-for="(item,index) in operationList" :key="index" :label="item" :value="item")
                        el-form-item( label="操作时间:")
                            el-date-picker(type="datetime" value-format="yyyy-MM-dd HH:mm:ss" style="width:175px;" v-model="dataForm.startTime" placeholder="选择开始时间"  clearable)
                        el-form-item( label="至")
                            el-date-picker( type="datetime" value-format="yyyy-MM-dd HH:mm:ss" style="width:175px;" v-model="dataForm.endTime" placeholder="选择结束时间" clearable)
                        el-button( type="primary" size='mini' icon="el-icon-search" @click="getDataList()") 查询
                        el-button(@click="reSet()" size='mini' type="primary" icon="el-icon-refresh-right") 重置
                //- <drag-table :data="dataList" :header="checkList" :option="tableOption"  @getDataList="getDataList" v-loading="dataListLoading"  :operationNum="3"  :isShowOperate="true">
                <drag-table :data="dataList" :header="checkList" :option="tableOption"  @getDataList="getDataList" v-loading="dataListLoading"  >

                  //- -- 插槽
                  <el-table-column slot="fixed"  fixed  prop=""  label="序号"  width="80"  header-align="center" align="center" >
                      <template slot-scope="scope">
                        span   {{scope.$index+(pageIndex - 1) * pageSize + 1}}
                      </template>
                  </el-table-column>

                  //- 操作项
                  <template slot-scope="scope">
                    <el-button  class="el-table-btn" type="text">编辑</el-button>
                    <el-button  class="el-table-btn" type="text">查看</el-button>
                    <el-button  class="el-table-btn" type="text">相关用户</el-button>
                  </template>

                </drag-table>
        el-footer.box_footer
            el-pagination(@size-change="sizeChangeHandle" background  @current-change="currentChangeHandle" :current-page="pageIndex" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" layout="total, sizes, prev, pager, next, jumper")
</template>

<script>
import Sortable from 'sortablejs'
import dragTable from '../../components/tab'
let that
export default {
  filters: {
    ellipsis (value) {
      if (!value) return ''
      if (value.length > 12) {
        return value.slice(0, 12) + '...'
      }
      return value
    }
  },
  data () {
    return {
      checkList: [], //筛选数据
      tableHeader: [
        { label: '操作员', prop: 'username' }, //是否插槽
        { label: '操作时间', prop: 'createDate' },
        { label: '主机ip', prop: 'ip' },
        { label: '模块名称', prop: 'blockName' },
        { label: '操作类型', prop: 'operation' },
        { label: '描述', prop: 'detail' }
        // {label:'操作',prop:''},
      ],
      tableOption: {
        border: false, //是否边框
        maxHeight: 500 //高度
      },
      dataForm: {
        username: '',
        operation: '',
        startTime: '',
        endTime: ''
      },
      operationList: ['登录', '注销', '新增', '修改', '删除', '查看', '上传'],
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false
    }
  },
    components: {
    dragTable
  },
  watch: {
    totalPage() { //注意这个函数的名字必须和你监听data中的属性的名字一样，这样才能当你data中的属性发生变化时，触发这个函数
      let pages = Math.ceil(this.totalPage / this.pageSize)//新数据总页数
      //总页数小于当前页数则重新加载列表数据
      if (pages < this.pageIndex) {
          this.pageIndex = pages || 1
          this.getDataList()//获取表格数据的方法
      }
    }
  },
  created () {
    that = this
    this.pageIndex = 1
    this.getDataList()
  },
  mounted(){
    this.checkList = [...this.tableHeader]
  },
  methods: {
    toLine(name) {
      return name.replace(/([A-Z])/g, '_$1').toLowerCase()
    },
    //重置
    reSet() {
        this.dataForm = {}
        this.pageIndex = 1
        this.getDataList()
    },
    // 获取数据列表
    getDataList (sort, order) {
      // console.log(sort,'---',order)
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/sys/log/list'),
        method: 'get',
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          username: this.dataForm.username,
          operation: this.dataForm.operation,
          startTime: this.dataForm.startTime,
          endTime: this.dataForm.endTime,
          sort: sort,
          order: order,
          stationId: localStorage.getItem('stationId')
        })
      }).then(data => {
        //console.log('data', data)
        if (data && data.code === 0) {
          this.dataList = data.page.records
          // console.log(this.dataList)
          this.totalPage = data.page.total
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    //排序
    sortChange (column) {
      if (column.order === 'descending') {
        this.order = 'desc'
      } else {
        this.order = 'asc'
      }
      if (column.column.columnKey) {
        this.sort = column.column.columnKey
      } else {
        this.sort = column.prop
      }
      this.getDataList()
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    }
  }
}
</script>
