<template>
  <div class="app-container">
    <!--表单组件-->
    <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
      <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
        <el-form-item label="权限" prop="label">
          <el-input v-model="form.label" style="width: 370px;" />
        </el-form-item>
        <el-form-item label="表名" prop="value">
          <el-select v-model="form.value" 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 label="描述" prop="description">
          <el-input v-model="form.description" style="width: 370px;" />
        </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-row :gutter="10">
      <el-col :xs="24" :sm="24" :md="10" :lg="10" :xl="11" style="margin-bottom: 10px">
        <el-card class="box-card">
          <!--工具栏-->
          <div class="head-container">
            <!--工具栏-->
            <div class="head-container">
              <div v-if="crud.props.searchToggle">
                <!-- 搜索 -->
                <el-input v-model="query.blurry" clearable size="small" placeholder="输入名称或者描述搜索" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
                <rrOperation />
              </div>
              <crudOperation :permission="permission" :tableKey="this.$options.name"/>
            </div>
            <!--表格渲染-->
            <el-table ref="table" v-loading="crud.loading" :data="crud.data" :height="crud.tableHeight" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler" @current-change="handleCurrentChange">
              <el-table-column prop="label" label="权限" />
              <el-table-column prop="value" label="表名" />
              <el-table-column prop="description" label="描述" />
              <el-table-column fixed="right" v-if="checkPer(['admin','dictDetail:edit','dictDetail:del'])" label="操作" width="150px" align="center">
                <template slot-scope="scope">
                  <udOperation
                      :data="scope.row"
                      :permission="permission"
                  />
                </template>
              </el-table-column>
            </el-table>
            <!--分页组件-->
            <pagination />
          </div>
        </el-card>
      </el-col>
      <el-col :xs="24" :sm="24" :md="14" :lg="14" :xl="13">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>字段配置</span>
            <el-button
                v-if="checkPer(['admin','excelConfig:add']) && this.$refs.excelConfig && this.$refs.excelConfig.tableName"
                class="filter-item"
                size="mini"
                style="float: right;padding: 8px 12px"
                type="primary"
                icon="el-icon-plus"
                @click="$refs.excelConfig && $refs.excelConfig.crud.toAdd()"
            >新增</el-button>
          </div>
          <excelConfig ref="excelConfig" :permission="permission" />
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import crudDictDetail from '@/api/system/dictDetail'
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 excelConfig from './excelConfig'
import {getTables} from "@/api/excelConfig";

const defaultForm = { id: null,description:null, dict: {name:"per_table_config"}, label: null, value: null, dictSort: null, createBy: null, updateBy: null, createTime: null, updateTime: null }
export default {
  name: 'DictDetail',
  components: { pagination, crudOperation, rrOperation, udOperation, excelConfig },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '权限关联', url: 'api/dictDetail',query: { dictName: 'per_table_config' }, sort: 'id,desc',
      crudMethod: { ...crudDictDetail },
      optShow: {
        add: true,
        edit: false,
        del: false,
        reset: false
      }})
  },
  data() {
    return {
      tablesList: [],
      permission: {
        add: ['admin', 'dictDetail:add'],
        edit: ['admin', 'dictDetail:edit'],
        del: ['admin', 'dictDetail:del']
      },
      rules: {
        label: [
          { required: true, message: '字典标签不能为空', trigger: 'blur' }
        ],
        value: [
          { required: true, message: '字典值不能为空', trigger: 'blur' }
        ]
      }    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      if (this.$refs.excelConfig) {
        this.$refs.excelConfig.query.tableName = ''
      }
      return true
    },
    // 选中数据后将选中数据的信息传到另一张表 查询
    handleCurrentChange(val) {
      if (val) {
        this.$refs.excelConfig.query.tableName = val.value
        this.$refs.excelConfig.tableName = val.value
        this.$refs.excelConfig.crud.toQuery()
        this.$refs.excelConfig.getColumns(val.value)
      }
    },
    // 编辑前将子表明细临时清空，避免日志入库数据过长
    [CRUD.HOOK.beforeToEdit](crud, form) {
      // 将角色的菜单清空，避免日志入库数据过长
      form.excelConfig = null
    },
    // 查询suo数据库表
    getTables() {
      getTables().then(res => {
        this.tablesList = res.content
      })
    }
  },
  mounted() {
    this.getTables();
  }
}
</script>

<style scoped>

</style>
