<template>
  <div>
    <div>
      <el-row v-if="head.show" class="table-title">
        <el-col :span="6">
          <div class="title-header">
            <el-popover
              v-model="visible"
              placement="bottom-start"
              trigger="click">
              <div class="popper-div">
                <div v-for="(column,index) in columnsChecked" :key="index">
                  <el-checkbox v-model="column.show" >{{ column.text }}</el-checkbox>
                </div>
                <div class="popper-btns">
                  <el-button size="mini" type="" @click="initColumnsState">取消</el-button>
                  <el-button size="mini" type="primary" @click="changeColumns">确定</el-button>
                </div>
              </div>
              <i slot="reference" ref="icon" :class="animateClass" class="el-icon-menu" @mouseenter="iconRotate" @mouseleave="removeAnimateClass"/>
            </el-popover>
            {{ head.text }}
          </div>
        </el-col>
        <el-col :span="18" class="edit_btns">
          <slot name="btns"/>
        </el-col>
      </el-row>
      <el-table v-loading="listLoading" :data="data" :size="size" class="table" row-class-name="small-row-class" border>
        <!--序号列-->
        <el-table-column v-if="options && options.needIndex" :index="indexMethod" label="#" align="center" type="index"/>
        <!--内容列, 若columns存在且长度大于0-->
        <template v-if="columnsFiltered && columnsFiltered.length>0">
          <el-table-column
            v-for="column in columnsFiltered"
            :key="column.value"
            :label="column.text"
            :width="column.width"
            :align="column.align?column.align:'center'"
            :show-overflow-tooltip="column.showOverflow?column.showOverflow:true">
            <template slot-scope="scope">
              <span>{{ scope.row[column.value] }}</span>
            </template>
          </el-table-column>
        </template>
        <!--自定义的放这里-->
        <slot name="columns"/>
      </el-table>
    </div>
    <!--分页-->
    <div class="pagination-container">
      <el-pagination
        :current-page="query.offset"
        :page-sizes="pageSizes"
        :page-size="query.limit"
        :total="total"
        align="center"
        layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>

  </div>
</template>

<script>
export default {
  name: 'NormalTable',
  props: {
    query: {
      type: Object,
      default: function() {
        return {
          offset: 1,
          limit: 20
        }
      }
    },
    head: {
      type: Object,
      default: function() {
        return {
          show: true,
          text: '数据列表'
        }
      }
    }, // 是否显示头
    listLoading: {
      type: Boolean,
      default: false
    }, // loading状态
    data: {
      type: Array,
      default: function() {
        return []
      }
    }, // 数据
    total: {
      type: Number,
      default: 0
    },
    columns: {
      type: Array,
      default: function() {
        return []
      }
    }, // 列
    options: {
      type: Object,
      default: function() {
        return {
          needIndex: true
        }
      }
    }, // 配置项
    pageSizes: {
      type: Array,
      default: function() {
        return [10, 20, 30]
      }
    },
    size: {
      type: String,
      default: ''
    }// 表格大小
  },
  data() {
    return {
      columnsFiltered: [], // 过滤后的列请款
      columnsChecked: [], // 显示列选择情况
      visible: false, // 弹窗显示情况
      animateClass: '' // 按钮动画效果
    }
  },
  created() {
    // 初始化状态值为全部显示
    this.initColumnsState()
    this.changeColumns()
  },
  methods: {
    // 初始化显示列状态
    initColumnsState() {
      this.columnsChecked = []
      for (const column of this.columns) {
        if (column.show === false) {
          this.columnsChecked.push({ text: column.text, show: false })
        } else {
          this.columnsChecked.push({ text: column.text, show: true })
        }
      }
      this.visible = false
    },
    // 修改显示列
    changeColumns() {
      this.columnsFiltered = []
      for (const i in this.columns) {
        if (this.columnsChecked[i].show === true) {
          this.columnsFiltered.push(this.columns[i])
        }
      }
      this.visible = false
    },
    // 序号计算
    indexMethod(index) {
      return this.query.limit * (this.query.offset - 1) + index + 1
    },
    // 改变页容量
    handleSizeChange(val) {
      this.query.limit = val
      this.$emit('change', { size: val })
    },
    // 改变当前页
    handleCurrentChange(val) {
      this.query.offset = val
      this.$emit('change', { page: val })
    },
    // 按钮旋转动画
    iconRotate() {
      this.animateClass = 'animated swing'
    },
    // 移除动画
    removeAnimateClass() {
      this.animateClass = ''
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  $tableTitleHeight:46px;
  .table{
    margin-bottom: 20px;
  }
  .pagination-container{
    padding-bottom: 50px;
  }
  .table-title{
    background-color:rgba(243, 243, 243, 1);
    height: $tableTitleHeight;
    .title-header{
      line-height:$tableTitleHeight;
      color: #606266;
      font-size: 15px;
      i{
        margin-left: 10px;
        margin-right: 5px;
      }
      i:focus{
        outline: none;
      }
    }
  }
  .edit_btns{
    padding-top:7px;
    padding-right:5px;
    text-align: right;
  }
  .popper-div{
    line-height: 1.5;
    .popper-btns{
      margin-top:10px;
    }
  }

</style>

