<template>
  <div class="app-container">
    <el-page-header @back="goBack" :content="'模板字典数据 - ' + (defaultDictType || '')" class="mb8" />

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5"><el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['tenant:dict:template:add']">新增</el-button></el-col>
      <el-col :span="1.5"><el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['tenant:dict:template:edit']">修改</el-button></el-col>
      <el-col :span="1.5"><el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['tenant:dict:template:remove']">删除</el-button></el-col>
    </el-row>

    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编码" prop="dictCode" width="80" align="center" />
      <el-table-column label="标签" prop="dictLabel" align="center" />
      <el-table-column label="键值" prop="dictValue" align="center" />
      <el-table-column label="排序" prop="dictSort" width="70" align="center" />
      <el-table-column label="平台管控" width="90" align="center">
        <template slot-scope="scope">
          <el-tag size="mini" :type="scope.row.isManaged === 1 ? 'danger' : 'info'">{{ scope.row.isManaged === 1 ? '是' : '否' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="80" align="center">
        <template slot-scope="scope"><dict-tag :options="statusOptions" :value="scope.row.status"/></template>
      </el-table-column>
      <el-table-column label="操作" width="140" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)" v-hasPermi="['tenant:dict:template:edit']">修改</el-button>
          <el-button size="mini" type="text" @click="handleDelete(scope.row)" v-hasPermi="['tenant:dict:template:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <el-dialog :title="title" :visible.sync="open" width="520px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="字典类型"><el-input v-model="form.dictType" disabled /></el-form-item>
        <el-form-item label="数据标签" prop="dictLabel"><el-input v-model="form.dictLabel" /></el-form-item>
        <el-form-item label="数据键值" prop="dictValue"><el-input v-model="form.dictValue" :disabled="form.dictCode != null" /></el-form-item>
        <el-form-item label="显示排序" prop="dictSort"><el-input-number v-model="form.dictSort" :min="0" controls-position="right" /></el-form-item>
        <el-form-item label="平台管控">
          <el-radio-group v-model="form.isManaged">
            <el-radio :label="1">是</el-radio><el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio v-for="d in statusOptions" :key="d.dictValue" :label="d.dictValue">{{ d.dictLabel }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注"><el-input v-model="form.remark" type="textarea" /></el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTemplateDictData, getTemplateDictData, addTemplateDictData,
  updateTemplateDictData, delTemplateDictData, getTemplateDictType
} from '@/api/tenant/dict'

export default {
  name: 'TenantDictTemplateData',
  data() {
    return {
      loading: true, ids: [], single: true, multiple: true, total: 0, dataList: [], defaultDictType: '',
      title: '', open: false, statusOptions: [],
      queryParams: { pageNum: 1, pageSize: 10, dictType: undefined },
      form: {},
      rules: {
        dictLabel: [{ required: true, message: '标签不能为空', trigger: 'blur' }],
        dictValue: [{ required: true, message: '键值不能为空', trigger: 'blur' }],
        dictSort: [{ required: true, message: '排序不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    const dictId = this.$route.params.dictId
    getTemplateDictType(dictId).then(r => {
      this.queryParams.dictType = r.data.dictType
      this.defaultDictType = r.data.dictType
      this.getList()
    })
    this.getDicts('sys_normal_disable').then(r => { this.statusOptions = r.data })
  },
  methods: {
    goBack() { this.$router.push({ path: '/admin/dict', query: { tab: 'template' } }) },
    getList() {
      this.loading = true
      listTemplateDictData(this.queryParams).then(r => { this.dataList = r.rows; this.total = r.total; this.loading = false })
    },
    cancel() { this.open = false; this.reset() },
    reset() {
      this.form = { dictCode: undefined, dictLabel: undefined, dictValue: undefined, dictSort: 0, status: '0', isManaged: 0, remark: undefined }
      this.resetForm('form')
    },
    handleAdd() { this.reset(); this.form.dictType = this.queryParams.dictType; this.open = true; this.title = '添加模板数据' },
    handleSelectionChange(s) { this.ids = s.map(i => i.dictCode); this.single = s.length !== 1; this.multiple = !s.length },
    handleUpdate(row) {
      this.reset()
      getTemplateDictData(row.dictCode || this.ids[0]).then(r => { this.form = r.data; this.open = true; this.title = '修改模板数据' })
    },
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (!valid) return
        const req = this.form.dictCode != null ? updateTemplateDictData(this.form) : addTemplateDictData(this.form)
        req.then(() => { this.msgSuccess('保存成功'); this.open = false; this.getList() })
      })
    },
    handleDelete(row) {
      const ids = row.dictCode || this.ids.join(',')
      this.$confirm('确认删除?', '警告', { type: 'warning' }).then(() => delTemplateDictData(ids))
        .then(() => { this.getList(); this.msgSuccess('删除成功') }).catch(() => {})
    }
  }
}
</script>

<style scoped>
.mb8 { margin-bottom: 12px; }
</style>
