<!---->
<template>
  <div class="dict">
    <!-- <el-card shadow="never">
      adsf
    </el-card> -->
    <el-card shadow="never" style="margin-top:10px">
      <el-row style="margin-bottom:10px">
        <el-col :span="18" style="text-align: right;">
          1
        </el-col>
        <el-col :span="6" style="text-align: right;">
          <el-button type="primary" size="mini" icon="el-icon-plus" @click="addVisible = true">新增</el-button>
          <!-- <el-button type="primary" size="mini" icon="el-icon-close" title="清除业务接口的缓存记录" @click="beCleanCache">清除缓存</el-button> -->
        </el-col>
      </el-row>
      <el-table
        v-loading="loading"
        :data="list"
        :header-cell-style="{background:'#f5f5f5',color:'#262626',fontSize:'13px'}"
      >
        <el-table-column
          prop="no"
          label="序号"
          width="50"
          align="center"
        >
          <template slot-scope="scope">
            {{ scope.$index+(currentPage-1)*pageSize+1 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="名称"
          min-width="200"
          align="center"
        >
          <template slot-scope="scope">
            <el-link type="primary" @click="pushDictLabel(scope.row)">{{ scope.row.name }}</el-link>
          </template>
        </el-table-column>
        <el-table-column
          prop="type"
          min-width="200"
          label="类型"
          align="center"
        />
        <el-table-column
          prop="description"
          label="描述"
          min-width="200"
          align="center"
        />
        <el-table-column
          label="状态"
          width="140px"
          align="center"
          prop="isValid"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.isValid==='1'" type="success">正常</el-tag>
            <el-tag v-else type="info">停用</el-tag>
          </template>
        </el-table-column>

        <el-table-column
          prop="createTime"
          width="160px"
          label="创建时间"
          align="center"
        />
        <el-table-column
          width="150px"
          label="操作"
          align="center"
        >
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="editDict(scope.row)">
              修改
            </el-button>
            <!-- <el-button type="text" size="small" @click="deleteDict(scope.row)">
              删除
            </el-button> -->
          </template>
        </el-table-column>
      </el-table>
      <el-row style="margin: 20px auto">
        <el-col :span="24" style="text-align:right">
          <el-pagination
            background
            :current-page.sync="currentPage"
            :page-sizes="[10, 20, 30, 100]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="value=>{pageSize=value,getList()}"
            @current-change="value=>{currenPage=value,getList()}"
          />
        </el-col>
      </el-row>
    </el-card>
    <el-dialog
      title="添加字典"
      :visible.sync="addVisible"
      width="500px"
      destroy-on-close
      :close-on-click-modal="false"
    >
      <el-form ref="addDictForm" :model="addDictForm" label-width="80px" size="mini" :rules="dictFormRules">
        <el-form-item label="字典名称" prop="name">
          <el-input v-model.trim="addDictForm.name" />
        </el-form-item>
        <el-form-item label="字典类型" prop="type">
          <el-input v-model.trim="addDictForm.type" />
        </el-form-item>
        <el-form-item label="状态" prop="isValid">
          <el-radio-group v-model="addDictForm.isValid" size="mini">
            <el-radio label="1">正常</el-radio>
            <el-radio label="2">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="addDictForm.description" type="textarea" :rows="4" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="loading" @click="subAdd">创 建</el-button>
          <el-button @click="$refs.addDictForm.resetFields(),addDictForm={isValid: '1'}">重 置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-dialog
      title="修改字典"
      :visible.sync="updateVisible"
      width="500px"
      destroy-on-close
      :close-on-click-modal="false"
    >
      <el-form ref="updateDictForm" :model="updateDictForm" label-width="80px" size="mini" :rules="dictFormRules">
        <el-form-item label="字典名称" prop="name">
          <el-input v-model.trim="updateDictForm.name" />
        </el-form-item>
        <el-form-item label="字典类型" prop="type">
          <el-input v-model.trim="updateDictForm.type" />
        </el-form-item>
        <el-form-item label="状态" prop="isValid">
          <el-radio-group v-model="updateDictForm.isValid" size="mini">
            <el-radio label="1">正常</el-radio>
            <el-radio label="2">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="updateDictForm.description" type="textarea" :rows="4" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="loading" @click="subUpdate">修 改</el-button>
          <el-button @click="$refs.updateDictForm.resetFields(),updateDictForm = tempUpdateDictForm">重 置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { addDict, listDict, updateDict, removeDict, cleanCache } from '@/api/dict.js'
export default {
  name: 'DictList',
  data() {
    return {
      loading: false,
      list: [],
      addDictForm: {
        name: '',
        type: '',
        description: '',
        isValid: '1'
      },
      addVisible: false,
      dictFormRules: {
        name: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        isValid: [
          { required: true, message: '请输入', trigger: 'blur' }
        ]
      },
      searchFrom: {},
      currentPage: 1,
      pageSize: 10,
      total: undefined,
      updateVisible: false,
      updateDictForm: {},
      tempUpdateDictForm: {}
    }
  },
  created() {
  },
  mounted() {
    // this.getList()
  },
  methods: {
    beCleanCache() {
      // 清除业务接口缓存
      this.$confirm('进行刷新字典的业务接口缓存记录?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        cleanCache().then(res => {
          if (res.code === 0) {
            this.$message.success('清除缓存成功')
          } else {
            this.$message.warning(res.msg)
          }
        })
      }).catch(() => {
      })
    },
    subAdd() {
      this.$refs.addDictForm.validate((valid) => {
        if (valid) {
          this.loading = true
          addDict(this.addDictForm).then(res => {
            if (res.code === 0) {
              this.addVisible = false
              this.$message.success('创建成功')
              this.getList()
            } else {
              this.$message.warning(res.msg)
            }
            this.loading = false
          })
        }
      })
    },
    getList() {
      const data = {
        entity: {
          ...this.searchFrom
        },
        current: this.currentPage,
        size: this.pageSize
      }
      this.loading = true
      listDict(data).then(res => {
        if (res.code === 0) {
          this.addVisible = false
          this.list = res.data.records
          this.total = res.data.total
        } else {
          this.$message.warning(res.msg)
        }
        this.loading = false
      })
    },
    editDict(row) {
      this.updateDictForm = Object.assign({}, row)
      this.tempUpdateDictForm = Object.assign({}, row)
      this.updateVisible = true
    },
    deleteDict(row) {
      this.$confirm(`确认删除名称为 '${row.name}' ?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        removeDict({ uuid: row.uuid }).then(res => {
          if (res.code === 0) {
            this.$message.success('删除成功')
            this.getList()
          } else {
            this.$message.warning(res.msg)
          }

          this.getList()
        })
      })
    },
    subUpdate(row) {
      this.$refs.updateDictForm.validate((valid) => {
        if (valid) {
          this.loading = true
          updateDict(this.updateDictForm).then(res => {
            if (res.code === 0) {
              this.updateVisible = false
              this.$message.success('修改成功')
              this.getList()
            } else {
              this.$message.warning(res.msg)
            }
            this.loading = false
          })
        }
      })
    },
    pushDictLabel(row) {
      this.$router.push({ path: '/dict/dictLabel', query: { type: row.type }})
    }
  }
}
</script>
<!--style标签必须使用scoped属性-->
<style lang="scss" scoped>
.dict{
  margin: 10px;
}
</style>
