<template>
  <el-container class="content-height-fit">
    <inner-sidebar>
      <dict-tree @node-click="handleTreeClick" />
    </inner-sidebar>
    <el-container class="resize-fit-container">
      <div class="app-container blue-top-border">
        <div class="filter-container">
          <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查询</el-button>
          <el-button
            :disabled="!listQuery.dictId || !$requirePermission('XT.ZD.C')"
            class="filter-item"
            type="primary"
            icon="el-icon-edit"
            @click="handleCreate"
          >新增</el-button>
        </div>
        <div class="content-container">
          <el-table
            v-loading="listLoading"
            row-key="id"
            :empty-text="listQuery.dictId ? '暂无数据' : '请选择字典类型'"
            :data="list"
            border
            fit
            highlight-current-row
            style="width: 100%"
            height="calc(100vh - 216px)"
          >
            <el-table-column label="序号" type="index" align="center" width="80" />
            <el-table-column label="条目名称" prop="name" align="center" />
            <el-table-column label="条目编码" prop="code" align="center" />
            <el-table-column label="字典名称" prop="dictName" align="center" />
            <el-table-column label="上级字典名称" prop="dictParentName" align="center" />
            <el-table-column label="上级字典条目" prop="dictParentEntryName" align="center" />
            <el-table-column label="备注" prop="remark" align="center" />
            <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
              <template slot-scope="{ row }">
                <el-button :disabled="!$requirePermission('XT.ZD.U')" type="primary" size="mini" @click="handleUpdate(row)">修改</el-button>
                <el-button :disabled="!$requirePermission('XT.ZD.D')" size="mini" type="danger" @click="handleDelete(row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <el-dialog :close-on-click-modal="false" class="dialogForm" :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
          <el-form ref="dataForm" v-loading="formLoading" :rules="rules" :model="dict" label-position="top" label-width="120px">
            <el-row :gutter="16">
              <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
                <el-form-item label="条目名称" prop="name" class="form-item">
                  <el-input v-model="dict.name" />
                </el-form-item>
              </el-col>
              <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
                <el-form-item label="条目编码" prop="code" class="form-item">
                  <el-input v-model="dict.code" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
                <el-form-item label="备注" prop="remark">
                  <el-input v-model="dict.remark" type="textarea" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button :disabled="formLoading" @click="dialogFormVisible = false">取消</el-button>
            <el-button :disabled="formLoading" type="primary" @click="dialogStatus === 'create' ? createData() : updateData()">确认</el-button>
          </div>
        </el-dialog>
      </div>
    </el-container>
  </el-container>
</template>

<script>
import * as entryApi from '@/api/dict-entry'
import waves from '@/directive/waves' // waves directive
import DictTree from './components/DictTree'
import InnerSidebar from '@/components/InnerSidebar'

export default {
  name: 'DictEntryMng',
  components: {
    DictTree,
    InnerSidebar
  },
  directives: { waves },
  data() {
    return {
      list: [],
      dict: {
        id: undefined,
        dictId: undefined
      },
      total: 0,
      listLoading: true,
      listQuery: {
        dictId: undefined
      },
      formLoading: false,
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '修改',
        create: '创建'
      },
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' },
          {
            min: 2,
            max: 64,
            message: '长度在 2 到 64 个字符',
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '请输入编码', trigger: 'blur' },
          {
            min: 1,
            max: 32,
            message: '长度在 1 到 32 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleTreeClick(data) {
      this.listQuery.dictId = data.id
      this.getList()
    },
    getList() {
      this.listLoading = true
      entryApi
        .getList(this.listQuery)
        .then(res => {
          this.list = res
        })
        .finally(() => {
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.skipCount = 0
      this.getList()
    },
    resetDict() {
      this.dict = {
        id: undefined,
        dictId: this.listQuery.dictId
      }
    },
    handleCreate() {
      this.resetDict()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.formLoading = false
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.formLoading = true
          entryApi.create(this.dict).then(() => {
            this.getList()
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleUpdate(row) {
      this.dict = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.formLoading = false
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          const updateData = Object.assign({}, this.dict)
          this.formLoading = true
          entryApi.update(updateData.id, updateData).then(() => {
            this.getList()
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '修改成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('此操作将删除该项, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        entryApi
          .remove(row.id)
          .then(() => {
            this.getList()
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
          })
          .finally(() => {
            this.listLoading = false
          })
      })
    }
  }
}
</script>
