<template>
  <div class="template-data-panel">
    <el-row :gutter="10" class="mb8 toolbar-row">
      <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" border size="small" @selection-change="handleSelectionChange" class="dict-table">
      <el-table-column type="selection" width="45" align="center" />
      <el-table-column label="标签" prop="dictLabel" align="center" min-width="120" :show-overflow-tooltip="true" />
      <el-table-column label="键值" prop="dictValue" align="center" min-width="100" />
      <el-table-column label="排序" prop="dictSort" width="70" align="center" />
      <el-table-column label="平台管控" width="88" 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="76" align="center">
        <template slot-scope="scope"><dict-tag :options="statusOptions" :value="scope.row.status"/></template>
      </el-table-column>
      <el-table-column label="操作" width="130" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['tenant:dict:template:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" style="color:#f5222d" @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" style="width: 100%" /></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" :rows="3" /></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
} from '@/api/tenant/dict'

export default {
  name: 'TemplateDataPanel',
  props: {
    dictType: { type: String, required: true }
  },
  data() {
    return {
      loading: false,
      ids: [],
      single: true,
      multiple: true,
      total: 0,
      dataList: [],
      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' }]
      }
    }
  },
  watch: {
    dictType: {
      immediate: true,
      handler(val) {
        if (val) {
          this.queryParams.dictType = val
          this.getList()
        }
      }
    }
  },
  created() {
    this.getDicts('sys_normal_disable').then(r => { this.statusOptions = r.data })
  },
  methods: {
    getList() {
      if (!this.queryParams.dictType) return
      this.loading = true
      listTemplateDictData(this.queryParams).then(r => {
        this.dataList = r.rows
        this.total = r.total
        this.loading = false
      }).catch(() => { 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>
.template-data-panel { min-height: 200px; }
.mb8 { margin-bottom: 8px; }
.toolbar-row { display: flex; flex-wrap: wrap; align-items: center; }
.dict-table { width: 100%; }
</style>

