<template>
  <div>
    <el-form ref="searchForm" :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="字典编号" prop="dataSourceName">
        <el-input v-model="searchForm.type" placeholder="字典编号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="loadData">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" highlight-current-row @row-click="handlerRowClick">
      <el-table-column align="center" label="字段名称" prop="name"></el-table-column>
      <el-table-column align="center" label="字段类型" prop="dictType"></el-table-column>
      <el-table-column align="center" label="状态" prop="usable">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.usable">可用</el-tag>
          <el-tag v-else>禁用</el-tag>
        </template>
      </el-table-column>
    </el-table>
    <div style="float: right">
      <el-pagination
          :page-size="page.pageSize"
          :total="page.total"
          layout="total, prev, pager, next"
          @current-change="currentChange">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {getDictList} from '../../../lib/api/easyData'

export default {
  name: "DiyCompDictChoose",
  props: {
    control: {
      type: Object, default: () => {
      }
    },
    controlProp: {
      type: Object, default: () => {
      }
    },
    // 值
    value: {type: [Object, Array], default: () => []},
    // 标题
    title: {type: String, default: "表格选择器"},
    // 查询时附带的额外参数
    params: {type: Object},
    // 表格行选中事件
    rowSelect: {
      type: Function,
      default: function () {
      }
    },
  },
  data() {
    return {
      //列表显示数据
      tableData: [],
      //前端分页,这里是所有数据
      tableList: [],
      page: {total: 0, pageSize: 10},
      //选择的数据
      currentRow: {},
      searchForm: {type: ''}
    }
  },
  mounted() {
    this.loadData();
  },
  methods: {
    loadData() {
      this.tableData = []
      getDictList(this.searchForm).then((resp) => {
        if (resp.data) {
          this.tableList = resp.data
          if (this.tableList.length > 10) {
            this.tableData = this.tableList.slice(0, 10)
          } else {
            this.tableData = this.tableList
          }
          this.page.total = resp.data.length
        }
      })
    },
    currentChange(val) {
      const index = (val - 1) * 10
      if (this.tableList.length >= val * 10) {
        this.tableData = this.tableList.slice(index, val * 10)
      } else {
        this.tableData = this.tableList.slice(index, this.tableList.length)
      }
    },
    handlerRowClick(row, column, event) {
      this.currentRow = row
      this.rowSelect({value: row.dictType, label: row.name})
    },
    getData() {
      return {value: this.currentRow.dictType, label: this.currentRow.name}
    }
  },
}
</script>

<style scoped>
:deep(.el-dialog) {
  height: 85vh;
}
</style>
