<template>
  <div class="app-container">
    <!--表单组件-->
    <el-dialog append-to-body :close-on-click-modal="false" :before-close="crud.cancelCU" :visible="crud.status.cu > 0" :title="crud.status.title" width="500px">
      <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="80px">
        <el-form-item label="编码" prop="name">
          <el-input v-model="form.code" style="width: 370px;" />
        </el-form-item>
        <el-form-item label="名称">
          <el-input v-model="form.name" 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="11" :xl="11" style="margin-bottom: 10px">
        <el-card class="box-card">
          <!--工具栏-->
          <div class="head-container">
            <div v-if="crud.props.searchToggle">
              <!-- 搜索 -->
              <el-input v-model="query.blurry" clearable size="mini" placeholder="输入编码或者名称搜索" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
              <rrOperation />
            </div>
            <crudOperation :permission="permission" />
          </div>
          <!--表格渲染-->
          <el-table ref="table" v-loading="crud.loading" :data="crud.data" highlight-current-row style="width: 100%;" @selection-change="crud.selectionChangeHandler" @current-change="handleCurrentChange">
            <el-table-column type="selection" width="55" />
            <el-table-column show-overflow-tooltip prop="code" width="80" label="编码" />
            <el-table-column show-overflow-tooltip prop="name" width="100" label="名称" />
            <el-table-column show-overflow-tooltip prop="demo" width="150" label="测试值" />
            <el-table-column show-overflow-tooltip prop="remark" label="备注" />
            <el-table-column v-permission="['admin','dict:edit','dict:del']" label="操作" width="130px" align="center" fixed="right">
              <template slot-scope="scope">
                <udOperation
                  :data="scope.row"
                  :permission="permission"
                />
              </template>
            </el-table-column>
          </el-table>
          <!--分页组件-->
          <pagination />
        </el-card>
      </el-col>
      <!-- 字典详情列表 -->
      <el-col :xs="24" :sm="24" :md="14" :lg="13" :xl="13">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>编码详情</span>
            <el-button
              v-if="checkPermission(['admin','genCode:add']) && this.$refs.codeGenDetail && this.$refs.codeGenDetail.query.dictName"
              class="filter-item"
              size="mini"
              style="float: right;padding: 4px 10px"
              type="primary"
              icon="el-icon-plus"
              @click="$refs.codeGenDetail && $refs.codeGenDetail.crud.toAdd()"
            >新增</el-button>
          </div>
          <codeGenDetail ref="codeGenDetail" :permission="permission" />
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import codeGenDetail from './codeDetail'
import crudDict from '@/api/system/genCode'

import CRUD, { presenter, header, form } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import rrOperation from '@crud/RR.operation'
import udOperation from '@crud/UD.operation'

const defaultForm = { id: null, name: null, description: null, codeGenDetails: [], code: null }

export default {
  name: 'GenCode',
  components: { crudOperation, pagination, rrOperation, udOperation, codeGenDetail },
  cruds() {
    return [
      CRUD({ title: '编码', url: 'api/genCode', crudMethod: { ...crudDict }, sort: ['create_time,desc'],
        optShow: {
          add: true,
          edit: true,
          del: true,
          download: false
        }})
    ]
  },
  mixins: [presenter(), header(), form(defaultForm)],
  data() {
    return {
      queryTypeOptions: [
        { key: 'code', display_name: '编码' },
        { key: 'name', display_name: '名称' }
      ],
      rules: {
        code: [
          { required: true, message: '请输入编码', trigger: 'blur' }
        ]
      },
      permission: {
        add: ['admin', 'dict:add'],
        edit: ['admin', 'dict:edit'],
        del: ['admin', 'dict:del'],
        download: [null]
      }
    }
  },
  methods: {
    checkPermission,
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      if (this.$refs.codeGenDetail) {
        this.$refs.codeGenDetail.query.dictName = ''
      }
      return true
    },
    // 选中字典后，设置字典详情数据
    handleCurrentChange(val) {
      if (val) {
        this.$refs.codeGenDetail.query.dictName = val.code
        this.$refs.codeGenDetail.query.id = val.id
        this.$refs.codeGenDetail.dictId = val.id
        this.$refs.codeGenDetail.crud.toQuery()
      }
    }
  }
}
</script>

<style scoped>
</style>
