<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="650px"
    >
      <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="170px">
        <el-form-item :label="$t('dict.table_value.Code')" prop="code">
          <el-input v-model="form.code" style="width: 370px;" />
        </el-form-item>
        <el-form-item :label="$t('dict.table_value.Name')">
          <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">{{ $t('task.select.Cancel') }}</el-button>
        <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">{{ $t('task.select.Confirm') }}</el-button>
      </div>
    </el-dialog>
    <!-- 字典列表 -->
    <el-row :gutter="10">
      <el-col :xs="13" :sm="13" :md="13" :lg="13" :xl="13" style="margin-bottom: 10px">
        <el-card class="box-card">
          <!--工具栏-->
          <div class="head-container">
            <div v-if="crud.props.searchToggle">
              <!-- 搜索 -->
              <el-form
                :inline="true"
                class="demo-form-inline"
                label-position="right"
                label-suffix=":"
              >
                <el-form-item  :label="$t('dict.table_title.code_Name')">
                  <el-input
                    v-model="query.blurry"
                    clearable
                    size="mini"
                    :placeholder="$t('dict.table_title.enter_code_name')"
                    style="width: 200px;"
                    class="filter-item"
                    @keyup.enter.native="crud.toQuery"
                  />
                </el-form-item>
                <rrOperation />
              </el-form>
            </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" :label="$t('dict.table_value.Code')"  />
            <el-table-column show-overflow-tooltip prop="name" :label="$t('dict.table_value.Name')" />
            <el-table-column
              v-permission="['admin','dict:edit','dict:del']"
              :label="$t('dict.table_value.Operate')"
              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="11" :sm="11" :md="11" :lg="11" :xl="11">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>{{ $t('dict.other.Dict_detail') }}</span>
            <el-button
              v-if="checkPermission(['admin','dict:add']) && this.$refs.dictDetail && this.$refs.dictDetail.query.code"
              class="filter-item"
              size="mini"
              style="float: right;padding: 4px 10px"
              type="primary"
              icon="el-icon-plus"
              @click="$refs.dictDetail && $refs.dictDetail.crud.toAdd()"
            >{{ $t('common.Create') }}
            </el-button>
          </div>
          <dictDetail ref="dictDetail" :permission="permission" />
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import dictDetail from './dictDetail.vue'
import crudDict from '@/views/system/dict/dict'

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'
import i18n from '@/i18n'

const defaultForm = {
  dict_id: null,
  code: null,
  name: null,
  label: null,
  value: null,
  dict_sort: null,
  dict_type: null,
  para1: null,
  para2: null,
  para3: null
}

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

<style scoped>
</style>
