<template>
  <div>
    <!--表格-->
    <div style="position: relative">
      <div>
        <el-table
          ref="moveTable"
          v-loading="loading"
          stripe
          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-click="_rowClickChange"
          @row-dblclick="_dbClickRow"
        >
          <el-table-column label="" width="60">
            <template slot-scope="scope">
              <div style="text-align: center !important;margin-right: -15px">
                <el-radio
                  v-model="templateRadio"
                  :label="scope.row.id"
                  @change.native="_getTemplateRow(scope.row)"
                >&ensp;</el-radio>
              </div>
            </template>
          </el-table-column>
          <slot />
        </el-table>
      </div>
      <!--自定义表格列-->
      <div style="position: absolute;right:0;top:0;z-index:2000">
        <slot name="setting" />
      </div>
    </div>
    <Page
      v-if="hidePage===undefined"
      :total="getPage.total"
      :page-size="getPage.size"
      :current="getPage.current"
      :page-size-opts="pageSizeOpts"
      placement="top"
      :show-elevator="hideElevator===undefined"
      :show-total="hideTotal===undefined"
      :show-sizer="hideSizer===undefined"
      style="margin-top: 10px;"
      @on-change="_pageChange"
      @on-page-size-change="_pageSizeChange"
    />
  </div>
</template>

<script>
import { sysTableColumn } from '../../api'
let clickTime
export default {
  props: [
    'pageColumns',
    'tableHeight',
    'showCheckBox',
    'optColWidth',
    'getPage',
    'iconMsg',
    'warnKey',
    'selectData',
    'hideElevator',
    'hideTotal',
    'hidePage',
    'hideSizer',
    'click',
    'tableName'
  ],
  data() {
    return {
      formId: '', // 表单id
      loading: true,
      pageParams: {
        page: 1,
        rows: 50
      },
      templateRadio: '',
      rowData: {},
      pageSizeOpts: [30, 50, 100, 200, 300, 500]
    }
  },
  methods: {
    _getTemplateRow(row) {
      this.$emit('on-result-change', 'radioSelect', 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)
      )
    },
    _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
      }
    },
    _hideLoading() {
      this.loading = false
    },
    // 点击单元格触发
    _rowClickChange(row, column, event) {
      clearTimeout(clickTime)
      clickTime = setTimeout(() => {
        this.templateRadio = row.id
        this.$emit('on-result-change', 'radioSelect', row)
      }, 260)
    },
    // 双击单元格触发
    _dbClickRow(row) {
      clearTimeout(clickTime)
      this.templateRadio = row.id
      this.$emit('on-result-change', 'dbRadioSelect', row)
    }
  }
}
</script>
