<template>
  <div v-if="query.tableName === ''">
    <div class="my-code">点击查看详情</div>
  </div>
  <div v-else>
    <div class="app-container">
      <!--工具栏-->
      <div class="head-container">
        <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
        <crudOperation :permission="permission" :tableKey="this.$options.name"/>
        <!--表单组件-->
        <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="600px">
          <el-form :inline="true" ref="form" :model="form" :rules="rules" size="small" label-width="100px">
            <el-form-item label="数据库表名" prop="tableName">
<!--              <el-input v-model="form.tableName" style="width: 370px;" />-->
              <el-select v-model="form.tableName" style="width: 370px" placeholder="请选择" clearable>
                <el-option
                    v-for="item in dict.per_table_config"
                    :key="item.value"
                    :label="item.value"
                    :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="字段名" prop="tableColumnName">
              <el-select v-model="form.tableColumnName" filterable style="width: 270px" placeholder="请选择">
                <el-option
                    v-for="item in columnsList"
                    :key="item.columnName"
                    :label="item.columnName"
                    :value="item.columnName"
                >
                  <span style="float: left">{{ item.columnName }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.remark }}</span>
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="onlyFlag">
              <el-checkbox v-model="form.onlyFlag">唯一标识</el-checkbox>
            </el-form-item>
            <el-form-item label="字段类型" prop="tableColumnType">
              <el-select v-model="form.tableColumnType" style="width: 370px" placeholder="请选择" clearable>
                <el-option
                    v-for="item in dict.excel_columns_type"
                    :key="item.value"
                    :label="item.value"
                    :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="Excel列名" prop="excelColumnName">
              <el-input v-model="form.excelColumnName" style="width: 370px;" />
            </el-form-item>
            <el-form-item label="默认值">
              <el-input v-model="form.defaultValue" style="width: 370px;" />
            </el-form-item>
            <el-form-item label="关联表">
              <!--            <el-input v-model="form.keyFlag" style="width: 370px;" />-->
              <el-radio-group v-model="form.glbType">
                <el-radio :label="1" v-model="form.glbType"  @click.native.prevent="clickitem(1)">外键</el-radio>
                <el-radio :label="2" v-model="form.glbType"  @click.native.prevent="clickitem(2)">字典</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item v-if="form.glbType==1" label="外键表名" prop="keyTableName">
              <el-select v-model="form.ecKey.tableName" @change="getKeyColumns(form.ecKey.tableName)" filterable style="width: 370px" placeholder="请选择">
                <el-option
                    v-for="item in tablesList"
                    :key="item.tableName"
                    :label="item.tableName"
                    :value="item.tableName"
                >
                  <span style="float: left">{{ item.tableName }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.remark }}</span>
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item v-if="form.glbType==1" label="表字段" prop="keyColumnName">
              <el-select v-model="form.ecKey.tableColumnName" filterable style="width: 370px" placeholder="请选择">
                <el-option
                    v-for="item in keyColumnsList"
                    :key="item.columnName"
                    :label="item.columnName"
                    :value="item.columnName"
                >
                  <span style="float: left">{{ item.columnName }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.remark }}</span>
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item v-if="form.glbType==1" label="字段类型" prop="keyColumnType">
              <el-select v-model="form.ecKey.tableColumnType" style="width: 370px" placeholder="请选择" clearable>
                <el-option
                    v-for="item in dict.excel_columns_type"
                    :key="item.value"
                    :label="item.value"
                    :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item v-if="form.glbType==1" label="映射字段" prop="keyName">
              <el-select v-model="form.ecKey.keyName" filterable style="width: 370px" placeholder="请选择">
                <el-option
                    v-for="item in keyColumnsList"
                    :key="item.columnName"
                    :label="item.columnName"
                    :value="item.columnName"
                >
                  <span style="float: left">{{ item.columnName }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.remark }}</span>
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item v-if="form.glbType==2" label="关联字典">
              <el-select filterable v-model="form.dictName"  style="width: 370px" placeholder="请选择" clearable>
                <el-option
                    v-for="item in keyDictList"
                    :key="item.name"
                    :label="item.name"
                    :value="item.name"
                >
                  <span style="float: left">{{ item.name }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.description }}</span>
                </el-option>
              </el-select>
            </el-form-item>

          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="text" @click="crud.cancelCU">取消</el-button>
            <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
          </div>
        </el-dialog>
        <!--表格渲染-->
        <el-table ref="table" v-loading="crud.loading" :data="crud.data" :height="crud.tableHeight" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
          <el-table-column prop="tableColumnName" label="字段名" />
          <el-table-column prop="tableColumnType" label="字段类型" />
          <el-table-column prop="excelColumnName" label="列名" />
          <el-table-column prop="onlyFlag" label="是否唯一">
            <template slot-scope="scope">
              <span v-if="scope.row.onlyFlag==true">是</span>
              <span v-if="scope.row.onlyFlag==false">否</span>
            </template>
          </el-table-column>

          <el-table-column prop="dictName" label="关联字典" />
          <el-table-column prop="ecKey.tableName" label="关联表名" />
          <el-table-column prop="ecKey.keyName" label="映射字段" />
          <el-table-column prop="ecKey.tableColumnName" label="关联字段" />
          <el-table-column prop="ecKey.tableColumnType" label="类型" />
          <el-table-column fixed="right" v-if="checkPer(['admin','excelConfig:edit','excelConfig:del'])" label="操作" width="150px" align="center">
            <template slot-scope="scope">
              <udOperation
                  :data="scope.row"
                  :permission="permission"
              />
            </template>
          </el-table-column>
        </el-table>
        <!--分页组件-->
        <pagination />
      </div>
    </div>
  </div>
</template>

<script>
import crudExcelConfig, {getColumns, getTables} from '@/api/excelConfig'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import {getPoints} from "@/api/point";
import crudDict, {getDicts} from '@/api/system/dict'

const defaultForm = { id: null, excelColumnName: null, tableColumnName: null, tableColumnType: null,glbType:0, onlyFlag: false,dictName: null,ecKey: { tableName:null,keyName:null,tableColumnName:null,tableColumnType: null }}
export default {
  name: 'ExcelColumnConfig',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(),
    form(function() {
      return Object.assign({ tableName: this.tableName}, defaultForm)
    }),
    crud()],
  dicts:['per_table_config','excel_columns_type'],
  cruds() {
    return CRUD({ title: '导入配置', url: 'api/excelConfig', idField: 'id', sort: 'id,desc', crudMethod: { ...crudExcelConfig }, optShow: {
        add: false,
        edit: false,
        del: false,
        reset: false,
        download: false,
        import: false
    },queryOnPresenterCreated: false})
  },
  data() {
    return {
      tableName: null,
      columnsList:[],
      keyColumnsList:[],
      tablesList:[],
      keyDictList:[],
      permission: {
        add: ['admin', 'excelConfig:add'],
        edit: ['admin', 'excelConfig:edit'],
        del: ['admin', 'excelConfig:del']
      },
      rules: {
        tableName: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        tableColumnName: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        tableColumnType: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      }}
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    // 查询数据库字段
    getColumns(tableName) {
      getColumns(tableName).then(res => {
        this.columnsList = res.content
      })
    },
    // 查询数据库字段
    getKeyColumns(tableName) {
      getColumns(tableName).then(res => {
        this.keyColumnsList = res.content
      })
    },
    // 查询suo数据库表
    getTables() {
      getTables().then(res => {
        this.tablesList = res.content
      })
    },
    // 查询全部字典
    getKeyDicts() {
      crudDict.getDicts().then(res => {
        this.keyDictList = res
      })
    },
    clickitem (e) {
      e === this.$refs.form.model.glbType ? this.$refs.form.model.glbType = 0 : this.$refs.form.model.glbType = e
    }
  },
  mounted() {
    this.getKeyDicts();
    this.getTables();
  }
}
</script>

<style scoped>

</style>
