<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="small" label-width="80px">
        <el-form-item label="字典名称" prop="dic_name">
          <el-input v-model="form.dic_name" style="width: 370px;" />
        </el-form-item>
        <el-form-item label="字典代码" prop="dic_code">
          <el-input v-model="form.dic_code" style="width: 370px;" />
        </el-form-item>
        <el-form-item label="内容说明">
          <el-input v-model="form.dic_explain" style="width: 370px;" type="textarea" :rows="2" />
        </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 class="container-main" :gutter="10">
      <el-col :xs="24" :sm="24" :md="6" :lg="6" :xl="5">
        <el-card class="box-card">
          <span class="right-top-line" />
          <span class="left-bottom-line" />
          <crudOperation :permission="permission" />
          <!--字典树状结构-->
          <el-tree ref="tree" v-loading="crud.loading" :data="crud.data" :props="defaultProps" node-key="id" :default-expand-all="true" highlight-current @node-click="handleNodeClick" />
        </el-card>
      </el-col>
      <!-- 字典详情列表 -->
      <el-col :xs="24" :sm="24" :md="18" :lg="18" :xl="19">
        <el-card class="box-card">
          <span class="right-top-line" />
          <span class="left-bottom-line" />
          <dictDetail ref="dictDetail" :permission="permission" :active-add-btn="activeAddBtn" @treeRefresh="treeRefresh" />
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import dictDetail from './dictDetail'
import crudDict from '@/api/archivesConfig/dict'
import CRUD, { presenter, header, form } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'

const defaultForm = { id: null, dic_name: null, dic_code: null, dic_explain: null, dic_type: true }

export default {
  name: 'Dict',
  components: { crudOperation, dictDetail },
  cruds() {
    return [
      CRUD({
        title: '字典', url: 'api/dictrionary/menu',
        crudMethod: { ...crudDict },
        optShow: {
          add: true,
          edit: true,
          del: true,
          download: false,
          group: false
        }
      })
    ]
  },
  mixins: [presenter(), header(), form(defaultForm)],
  data() {
    return {
      queryTypeOptions: [
        { key: 'name', display_name: '字典名称' },
        { key: 'description', display_name: '描述' }
      ],
      rules: {
        dic_name: [
          { required: true, message: '请输入字典名称', trigger: 'blur' }
        ],
        dic_code: [
          { required: true, message: '请输入字典代码', trigger: 'blur' }
        ]
      },
      permission: {
        add: ['admin', 'dict:add'],
        edit: ['admin', 'dict:edit'],
        del: ['admin', 'dict:del']
      },
      defaultProps: {
        children: 'child_menus',
        label: 'dic_name'
      },
      activeAddBtn: false
    }
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      if (this.$refs.dictDetail) {
        this.$refs.dictDetail.query.id = ''
      }
      return true
    },
    // 选中字典后，设置字典详情数据
    handleNodeClick(val) {
      if (val) {
        if (val.dic_type === 'true') {
          this.crud.selectionChangeHandler([val])
        } else {
          this.crud.selectionChangeHandler([])
        }
        this.$refs.dictDetail.query.id = val.id
        this.$refs.dictDetail.dicPid = val.id
        this.$refs.dictDetail.crud.toQuery()
        this.activeAddBtn = true
      }
    },
    // 编辑前将字典明细临时清空，避免日志入库数据过长
    [CRUD.HOOK.beforeToEdit](crud, form) {
      // 将角色的菜单清空，避免日志入库数据过长
      form.dictDetails = null
    },
    treeRefresh() {
      this.crud.refresh()
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card{
  min-height: calc(100vh - 210px);
}

.el-tree--highlight-current .el-tree-node.is-current > .el-tree-node__content {
  /* background-color: rgb(158, 213, 250) !important; */
}
.el-card__body .crud-opts {
  justify-content: space-around;
}
</style>
