<template>
  <div class="app-container">
    <el-alert title="平台字典模板：维护标准字典定义，可通过「字典同步」下发到各租户库" type="info" show-icon :closable="false" class="mb8" />

    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="字典名称" prop="dictName">
        <el-input v-model="queryParams.dictName" placeholder="请输入字典名称" clearable size="small" style="width: 200px" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="字典类型" prop="dictType">
        <el-input v-model="queryParams.dictType" placeholder="请输入字典类型" clearable size="small" style="width: 200px" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <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>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="typeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="dictId" width="80" />
      <el-table-column label="字典名称" align="center" prop="dictName" />
      <el-table-column label="字典类型" align="center">
        <template slot-scope="scope">
          <router-link :to="'/system/tenant/dict-template-data/index/' + scope.row.dictId" class="link-type">
            <span>{{ scope.row.dictType }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="平台管控" align="center" width="90">
        <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="默认同步模式" align="center" prop="syncMode" width="110" />
      <el-table-column label="状态" align="center" width="80">
        <template slot-scope="scope">
          <dict-tag :options="statusOptions" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="140">
        <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" @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="560px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="字典名称" prop="dictName"><el-input v-model="form.dictName" /></el-form-item>
        <el-form-item label="字典类型" prop="dictType"><el-input v-model="form.dictType" :disabled="form.dictId != null" /></el-form-item>
        <el-form-item label="平台管控" prop="isManaged">
          <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="默认同步模式" prop="syncMode">
          <el-select v-model="form.syncMode" style="width: 100%">
            <el-option label="MERGE - 合并更新" value="MERGE" />
            <el-option label="APPEND - 仅追加" value="APPEND" />
            <el-option label="OVERWRITE - 覆盖平台项" value="OVERWRITE" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictValue">{{ dict.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 {
  listTemplateDictType, getTemplateDictType, addTemplateDictType,
  updateTemplateDictType, delTemplateDictType
} from '@/api/tenant/dict'

export default {
  name: 'TenantDictTemplate',
  data() {
    return {
      loading: true, ids: [], single: true, multiple: true, showSearch: true, total: 0, typeList: [],
      title: '', open: false, statusOptions: [],
      queryParams: { pageNum: 1, pageSize: 10, dictName: undefined, dictType: undefined },
      form: {},
      rules: {
        dictName: [{ required: true, message: '字典名称不能为空', trigger: 'blur' }],
        dictType: [{ required: true, message: '字典类型不能为空', trigger: 'blur' }],
        syncMode: [{ required: true, message: '请选择同步模式', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getList()
    this.getDicts('sys_normal_disable').then(r => { this.statusOptions = r.data })
  },
  methods: {
    getList() {
      this.loading = true
      listTemplateDictType(this.queryParams).then(r => {
        this.typeList = r.rows; this.total = r.total; this.loading = false
      })
    },
    cancel() { this.open = false; this.reset() },
    reset() {
      this.form = { dictId: undefined, dictName: undefined, dictType: undefined, status: '0', isManaged: 0, syncMode: 'MERGE', remark: undefined }
      this.resetForm('form')
    },
    handleQuery() { this.queryParams.pageNum = 1; this.getList() },
    resetQuery() { this.resetForm('queryForm'); this.handleQuery() },
    handleAdd() { this.reset(); this.open = true; this.title = '添加模板字典类型' },
    handleSelectionChange(s) { this.ids = s.map(i => i.dictId); this.single = s.length !== 1; this.multiple = !s.length },
    handleUpdate(row) {
      this.reset()
      getTemplateDictType(row.dictId || 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.dictId != null ? updateTemplateDictType(this.form) : addTemplateDictType(this.form)
        req.then(() => { this.msgSuccess('保存成功'); this.open = false; this.getList() })
      })
    },
    handleDelete(row) {
      const ids = row.dictId || this.ids.join(',')
      this.$confirm('确认删除?', '警告', { type: 'warning' }).then(() => delTemplateDictType(ids))
        .then(() => { this.getList(); this.msgSuccess('删除成功') }).catch(() => {})
    }
  }
}
</script>

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