<template>
  <div class="app-container calendar-list-container" v-if="hasPerm('/system-management/dict:list')">
    <div class="filter-container">
      <el-input
        style="width: 150px;margin-left: 5px"
        class="filter-item"
        v-model="filterData.code"
        placeholder=""
        clearable
      ></el-input>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
        style="vertical-align: top;margin-left: 5px"
        >搜索</el-button
      >
      <el-button
        type="primary"
        @click="handleCreate"
        v-if="hasPerm('/system-management/dict:add')"
        style="vertical-align: top;margin-left: 5px"
        icon="el-icon-edit"
        >添加</el-button
      >
    </div>

    <el-table :data="tableData" style="width: 100%" border fit stripe>
      <!-- <el-table-column type="selection" align="center" width="55"></el-table-column> -->
      <el-table-column label="字典编号" prop="code" width="160"></el-table-column>
      <el-table-column label="字典名称" prop="dictValue" width="160"></el-table-column>
      <el-table-column label="封存" width="120">
        <template v-slot="scope">
          <el-tag type="info">{{ scope.row.isSealed === 0 ? '否' : '是' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button type="text" @click="handleUpdate(scope.row)" v-if="hasPerm('/system-management/dict:update')"
            >编辑</el-button
          >
          <el-button
            v-if="hasPerm('/system-management/dict:del')"
            type="text"
            class="dangertext"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!--翻页-->
    <div class="pagination-container" style="float: left">
      <!-- <el-button
            class="filter-item"
            type="danger"
            plain
            :disabled="multipleSelection.length === 0"
            @click="handleBatchDel"
            style="margin-left: 10px;"
            >批量删除</el-button
          > -->
    </div>
    <div class="pagination-container" style="float: right">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handlepageNumChange"
        :pageNum-page="filterData.pageNum"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="filterData.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
    <el-dialog
      :visible.sync="dialogFormVisible"
      append-to-body
      :close-on-click-modal="false"
      width="550px"
      :title="dialogStatus == 'create' ? '添加' : '编辑'"
    >
      <el-form
        :rules="rules"
        ref="rulesForm"
        :model="rulesForm"
        label-width="120px"
        class="demo-form-inline"
        label-position="left"
        style="margin-left: 20px"
      >
        <el-form-item label="字典编号" prop="code">
          <el-input v-model="rulesForm.code" clearable style="width: 300px"> </el-input>
        </el-form-item>
        <el-form-item label="字典名称" prop="dictValue">
          <el-input v-model="rulesForm.dictValue" clearable style="width: 300px"> </el-input>
        </el-form-item>
        <el-form-item label="字典排序" prop="sort">
          <el-input v-model="rulesForm.sort" clearable type="number" style="width: 300px"> </el-input>
        </el-form-item>
        <el-form-item label="封存" prop="isSealed">
          <el-radio v-model="rulesForm.isSealed" :label="0">否</el-radio>
          <el-radio v-model="rulesForm.isSealed" :label="1">是</el-radio>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="rulesForm.remark" clearable style="width: 300px"> </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button v-if="dialogStatus == 'create'" type="primary" @click="createDict('rulesForm')">确认</el-button>
        <el-button v-if="dialogStatus == 'update'" type="primary" @click="updateDict('rulesForm')">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getDictByPage, addDict, updateDict, delDict } from '../../api/dict'
export default {
  name: 'dict',
  data() {
    return {
      filterData: {
        code: '',
        name: '',
        pageNum: 1,
        pageSize: 10,
        parentId: 0,
      },
      total: 0,
      tableData: [],
      rulesForm: {
        code: '',
        dictValue: '',
        sort: 1,
        remark: '',
        isSealed: 0,
      },
      rules: {
        code: [{ required: true, message: '请输入字典编号', trigger: 'blur' }],
        dictValue: [{ required: true, message: '请输入字典名称', trigger: 'change' }],
        sort: [{ required: true, message: '请输入字典排序', trigger: 'blur' }],
        isSealed: [{ required: true, message: '请输入字典排序', trigger: 'blur' }],
        remark: [{ min: 0, max: 30, message: '长度为30个字符以内', trigger: 'blur' }],
      },
      dialogStatus: 'create',
      dialogFormVisible: false,
    }
  },
  created() {
    this.getDictList()
  },
  mounted() {},
  watch: {
    parentId(val) {
      this.dictChildFilterData.parentId = val
      this.dictChildForm.parentId = val //选中父字典后 子表单和子筛选项 的parentId要设置进去
      this.getDictChildList()
    },
  },
  methods: {
    getDictList() {
      getDictByPage(this.filterData).then((res) => {
        console.log('created -> res', res)
        const resData = res.data.data
        this.tableData = resData.list
        this.total = resData.total
      })
    },
    handleFilter() {
      this.getDictList()
    },
    handleCreate() {
      this.dialogFormVisible = true
      this.dialogStatus = 'create'
    },
    handleUpdate(row) {
      console.log(row)
      this.dialogFormVisible = true
      this.dialogStatus = 'update'
      this.rulesForm = row
    },
    handleCancel() {
      this.rulesForm = {}
      this.$refs['rulesForm'].resetFields()
      this.dialogFormVisible = false
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除该字典, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          delDict({ id: row.id }).then((res) => {
            console.log('handleDelete -> res', res)
            if (res.data.code === 1000) {
              this.getDictList()
              this.$message({
                type: 'success',
                message: '删除成功!',
              })
            } else {
              this.$message({
                type: 'error',
                message: res.data.msg,
              })
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    handleSizeChange(val) {
      this.filterData.pageSize = val
      this.filterData.pageNum = 1
      this.getDictList()
    },
    handlepageNumChange(val) {
      this.filterData.pageNum = val
      this.getDictList()
    },
    dealWithParentRes(res) {
      if (res.data.code === 1000) {
        this.handleCancel()
        this.getDictList()
      } else {
        this.$message({
          type: 'error',
          message: res.data.msg,
        })
      }
    },
    createDict() {
      addDict(this.rulesForm).then((res) => {
        this.dealWithParentRes(res)
      })
    },
    updateDict() {
      updateDict(this.rulesForm).then((res) => {
        this.dealWithParentRes(res)
      })
    },
  },
}
</script>

<style scoped></style>
