<template>
  <div class="app-container  container-wrap">
    <span class="right-top-line" />
    <span class="left-bottom-line" />
    <!--工具栏-->
    <div class="head-container">
      <crudOperation :permission="permission" />
    </div>
    <!--表格渲染-->
    <el-table ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%;" @selection-change="selectionChangeHandler" @row-click="clickRowHandler">
      <el-table-column type="selection" width="55" />
      <el-table-column type="index" label="序号" width="100" />
      <el-table-column prop="field_cn_name" label="中文名称" />
      <el-table-column prop="field_name" label="字段标识" />
      <el-table-column label="数据类型">
        <template slot-scope="scope">
          <span v-if="scope.row.is_data_type === 1">字符</span>
          <span v-if="scope.row.is_data_type === 2">数字</span>
        </template>
      </el-table-column>
      <el-table-column prop="is_column_length" label="字段长度" />
      <el-table-column prop="is_default_value" label="默认值" />
    </el-table>
    <!--表单渲染-->
    <eForm />
  </div>
</template>

<script>
import crudFields from '@/api/archivesConfig/field'
import eForm from './module/form'
import CRUD, { presenter } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'

export default {
  name: 'CommonFields',
  components: { eForm, crudOperation },
  cruds() {
    return CRUD({
      title: '常用字段',
      url: 'api/field/findGroupType',
      crudMethod: { ...crudFields },
      optShow: {
        add: true,
        edit: true,
        del: false,
        download: false,
        group: false
      },
      query: {
        is_type: 2
      }
    })
  },
  mixins: [presenter()],
  data() {
    return {
      permission: {
        add: ['admin', 'commonFields:add'],
        edit: ['admin', 'commonFields:edit']
      }
    }
  },
  methods: {
    clickRowHandler(row) {
      this.$refs.table.clearSelection()
      this.$refs.table.toggleRowSelection(row)
    },
    selectionChangeHandler(val) {
      let finalVal
      if (val.length > 1) {
        // 取出最后val的最后一个返回出来
        finalVal = val.pop()
        // 清除所有选中
        this.$refs.table.clearSelection()
        // 给最后一个加上选中
        this.$refs.table.toggleRowSelection(finalVal)
      } else {
        finalVal = val
      }
      this.crud.selectionChangeHandler(finalVal)
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep thead .el-table-column--selection .cell {
  display: none;
}
</style>
