<template>
  <el-dialog
    title="选择码值"
    :visible.sync="dialogVisible"
    :modal="false"
  >
    <el-form :inline="true" :model="searchForm" class="demo-form-inline" ref="searchForm">
      <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-item>-->
      <!--        <el-button @click="resetForm('searchForm')">重置</el-button>-->
      <!--      </el-form-item>-->
    </el-form>
    <el-table
      :data="tableData"
      @row-click="singleElection"
    >
      <el-table-column align="center" width="55" label="选择">
        <template slot-scope="scope">
          <!-- 可以手动的修改label的值，从而控制选择哪一项 -->
          <el-radio class="radio" v-model="selectVal" :label="scope.row.dictType"
          >&nbsp;
          </el-radio
          >
        </template>
      </el-table-column>
      <el-table-column align="center" prop="name" label="字段名称"></el-table-column>
      <el-table-column align="center" prop="dictType" label="字段类型"></el-table-column>
      <el-table-column align="center" prop="usable" label="状态">
        <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
        @current-change="currentChange"
        :page-size="page.pageSize"
        layout="total, prev, pager, next"
        :total="page.total">
      </el-pagination>
    </div>
    <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="choose">确 定</el-button>
  </span>
  </el-dialog>
</template>

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

export default {
  name: "ChooseDict",
  data() {
    return {
      dialogVisible: false,
      //列表显示数据
      tableData: [],
      //前端分页,这里是所有数据
      tableList: [],
      page: {total: 0, pageSize: 10},
      //选择的数据
      selectVal: '',
      searchForm: {type: ''}
    }
  },
  methods: {
    show() {
      this.loadData()
      this.dialogVisible = true
    },
    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)
      }
    },
    singleElection(row) {
      this.selectVal = row.dictType
    },
    choose() {
      if (!this.selectVal) {
        this.$message.error("请选择一行数据")
        return
      }
      this.dialogVisible = false
      this.$emit('choose', this.selectVal)
    }
  },
}
</script>

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