<template>
  <div>
    <!--表格-->
    <div style="position: relative">
      <div>
        <el-table
          ref="moveTable"
          v-loading="loading"
          :stripe="stripe === undefined"
          border
          size="small"
          highlight-current-row
          :data="getPage.records"
          style="width: 100%"
          :height="tableHeight"
          :default-sort="{prop: 'date', order: 'descending'}"
          element-loading-text="拼命加载中..."
          element-loading-spinner="el-icon-loading"
          :row-class-name="tableRowClassName"
          @select-all="_selectAll"
          @selection-change="_selectRowChange"
          @row-dblclick="_dbClickRow"
          @cell-click="_cellClick"
        >
          <el-table-column
            v-if="showCheckBox!==undefined&&showCheckBox!==false"
            type="selection"
            width="55"
            fixed="left"
            align="center"
          />
          <slot />
          <el-table-column
            v-if="(iconMsg !== undefined &&iconMsg.length) > 0?(optColWidth?true:false):false"
            label="操作"
            align="center"
            :width="optColWidth"
            fixed="right"
          >
            <template slot-scope="scope">
              <icon-list :msg="iconMsg" :row-data="scope.row" @on-result-change="_iconClick" />
            </template>
          </el-table-column>
          <slot name="col" />
        </el-table>
      </div>
      <!--自定义表格列-->
      <div style="position: absolute;right:0;top:0;z-index:2000">
        <slot name="setting" />
      </div>
    </div>
    <div v-if="statusNum !== undefined" style="position: absolute;bottom: 9px;font-weight: bold">已选择 {{statusNum}} 条</div>
    <Page
      v-if="hidePage===undefined"
      :total="getPage.total"
      :page-size="getPage.size"
      :current="getPage.current"
      :page-size-opts="pageSizeOpts"
      placement="top"
      :size="pageSizeSmall===undefined?'':'small'"
      :show-elevator="hideElevator===undefined"
      :show-total="hideTotal===undefined"
      :show-sizer="hideSizer===undefined"
      :style="statusNum !== undefined?'margin:10px 0 0 100px': 'margin-top:10px'"
      @on-change="_pageChange"
      @on-page-size-change="_pageSizeChange"
    />
  </div>
</template>

<script>
import { sysTableColumn } from '../../api'

export default {
  props: [
    'pageColumns',
    'tableHeight',
    'showCheckBox',
    'optColWidth',
    'getPage',
    'iconMsg',
    'warnKey',
    'selectData',
    'hideElevator',
    'hideTotal',
    'hidePage',
    'hideSizer',
    'click',
    'tableName',
    'statusNum',
    'stripe',
    'pageSizeSmall'
  ],
  data() {
    return {
      formId: '', // 表单id
      loading: true,
      pageParams: {
        page: 1,
        rows: this.$pageParams()
      },
      rowData: {},
      pageSizeOpts: this.$pageSizeOpts()
    }
  },
  methods: {
    // 行内样式
    tableRowClassName({ row, rowIndex }) {
      if (this.warnKey && row.manualJudge === 1) {
        return 'warning-row'
      }
      if (this.warnKey && row[this.warnKey] === 't') return 'warning-bg-yellow'
      return ''
    },
    // 点击单元格触发
    _cellClick(row, event, column) {
      if (event.label !== '操作') {
        this._rowClickChange(row)
      }
    },
    _rowClickChange(row) {
      if (this.click === undefined) {
        // 默认选中该行数据
        this.$refs.moveTable.toggleRowSelection(row)
      } else {
        // 有click参数时执行
        this.$emit('on-result-change', 'click', row)
      }
    },
    _clearSelection() {
      this.$refs.moveTable.clearSelection()
    },
    // 初始化加载
    _initTable() {
      this.loading = false
      if (this.tableName) {
        // tableName存在-----支持自定义表格
        this._getByCompanyTable()
      }
    },
    // 查询企业下得表格数据
    _getByCompanyTable: async function() {
      const result = await sysTableColumn.getByCompanyTable(this.tableName)
      this._getByUserTable(result)
    },
    // 查询用户下的表格数据
    _getByUserTable: async function(companyTableCode) {
      const userTableCode = await sysTableColumn.getByUserTable(this.tableName)
      this.$emit(
        'on-result-change',
        'table-col',
        this.$tableColumns(this.pageColumns, companyTableCode, userTableCode)
      )
    },
    _sortChange({ prop }) {
      const { records } = this.getPage
      records.forEach(item => console.log(item[prop]))
    },
    _pageChange(page) {
      this.pageParams.page = page
      this._pageParamsChange()
    },
    _pageSizeChange(rows) {
      if (rows !== this.pageParams.rows) {
        this.pageParams.rows = rows
        this._pageParamsChange()
      }
    },
    _pageParamsChange() {
      this.$emit('on-result-change', 'page', '')
    },
    _searchParams() {
      this.loading = true
      const data = {}
      if (this.hidePage !== undefined) {
        // 不分页
        return data
      } else {
        // 分页
        return this.pageParams
      }
    },
    // 多选
    _selectRowChange(data) {
      // 默认返回的是id数组
      if (this.showCheckBox !== undefined) {
        // 有checkbox 默认返回选中的id数组
        if (this.selectData === undefined) {
          const idList = []
          for (let i = 0; i < data.length; i++) {
            idList.push(data[i].id)
          }
          this.$emit('on-result-change', 'selectIds', idList)
        } else {
          // 有selectData参数时执行
          this.$emit('on-result-change', 'selectData', data)
        }
      }
    },
    _selectAll: function(data) {
      this._selectRowChange(data)
    },
    // 操作列回调
    _iconClick(name, rowData) {
      this.$emit('on-result-change', 'iconClick', {
        name: name,
        rowData: rowData
      })
    },
    // 双击行操作
    _dbClickRow(row, event) {
      this.$emit('on-result-change', 'dbSelect', row)
    },
    _hideLoading() {
      this.loading = false
    }
  }
}
</script>
<style>
.warning-row {
  color: red;
}
.el-table .warning-bg-yellow {
  background: yellow;
}
</style>
