<template>
    <div class="app-container">
        <!-- 查询表单区域 -->
        <el-form :model="listQuery" size="small" :inline="true" label-position="top">
          <el-row :gutter="20">
            <el-col :span="6">
              <el-form-item label="字典名称">
                <el-select @change="changeDictType" v-model="listQuery.dictType" clearable filterable placeholder="请选择字典名称" style="width: 100%">
                  <el-option :key="item.dictType" v-for="item in dictNameList" :label="item.dictName" :value="item.dictType">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="字典标签">
                <el-input v-model="listQuery.dictLabel" placeholder="请输入字典标签" style="width: 100%" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="状态">
                <el-select v-model="listQuery.status" clearable placeholder="请选择状态" style="width: 100%">
                  <el-option :key="item.type" v-for="item in typeList" :label="item.name" :value="item.type">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="6">
              <el-form-item label="搜索">
                <el-button style="width: 100%;" type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="重置">
                <el-button style="width: 100%;" icon="el-icon-refresh" type="primary" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>

        <!-- 操作按钮区域 -->
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" class="editButton" size="small" icon="el-icon-plus" @click="handleCreate">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="small" @click="delAll">批量删除</el-button>
          </el-col>
        </el-row>

        <!-- 表格区域 -->
        <div class="border-card">
          <el-table height="450" size="small" :data="list" v-loading="listLoading" @selection-change="handleSelectionChange" element-loading-text="正在查询中。。。" border fit highlight-current-row>
            <el-table-column type="selection" width="55px" align="center"></el-table-column>
            <el-table-column type="index" label="序号" header-align="center" align="center" width="55"></el-table-column>
            <el-table-column align="center" min-width="60px" label="字典编码" prop="dictCode"></el-table-column>
            <el-table-column align="center" min-width="100px" label="字典标签" prop="dictLabel"></el-table-column>
            <el-table-column align="center" min-width="60px" label="字典键值" prop="dictValue"></el-table-column>
            <el-table-column align="center" min-width="60px" label="字典排序" prop="dictSort"></el-table-column>
            <el-table-column align="center" min-width="80px" label="状态">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.status == '0'">正常</el-tag>
                <el-tag v-else>停用</el-tag>
              </template>
            </el-table-column>
            <el-table-column align="center" min-width="200px" label="备注" prop="remark"></el-table-column>
            <el-table-column align="center" min-width="100px" label="创建时间" prop="createTime"></el-table-column>
            <el-table-column align="center" label="操作" width="240px" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button type="text" icon="el-icon-edit" class="editText" size="small" @click="handleUpdate(scope.row, false)">编辑</el-button>
                <el-button type="text" icon="el-icon-delete" class="deleteText" size="small" @click="handleDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>

          <!-- 分页 -->
          <div class="pagination-container">
            <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
              :current-page="listQuery.page" :page-sizes="[10, 20, 30, 50]" :page-size="listQuery.limit"
              layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
          </div>
        </div>

        <!-- 添加或修改对话框 -->
        <el-dialog :close-on-click-modal="false" :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="40%">
          <el-form :rules="rules" ref="dataForm" :model="dataForm" status-icon label-position="left" label-width="80px">
            <el-form-item label="字典类型" prop="dictType">
              <el-input disabled v-model="dataForm.dictType" placeholder="字典类型"></el-input>
            </el-form-item>
            <el-form-item label="字典标签" prop="dictLabel">
              <el-input v-model="dataForm.dictLabel" placeholder="请输入字典标签"></el-input>
            </el-form-item>
            <el-form-item label="字典键值" prop="dictValue">
              <el-input v-model="dataForm.dictValue" placeholder="请输入字典键值"></el-input>
            </el-form-item>
            <el-form-item label="显示排序" prop="dictSort">
              <el-input-number :precision="0" :step="1" v-model="dataForm.dictSort"></el-input-number>
            </el-form-item>
            <el-form-item label="状态">
              <el-radio-group v-model="dataForm.status">
                <el-radio :label="'0'">正常</el-radio>
                <el-radio :label="'1'">停用</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="备注">
              <el-input type="textarea" :rows="2" v-model="dataForm.remark" placeholder="请输入备注"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
            <el-button v-if="dialogStatus == 'create'" type="primary" @click="createData">确定</el-button>
            <el-button v-else type="primary" @click="updateData">确定</el-button>
          </div>
        </el-dialog>

    </div>
</template>
  
<style>
.demo-table-expand {
    font-size: 0;
}

.demo-table-expand label {
    width: 200px;
    color: #99a9bf;
}

.demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
}
</style>
  
<script>
import { dataList, dataAdd, dataEdit, dataRemove, optionSelect } from '@/api/dictManage'
import waves from '@/directive/waves' // 水波纹指令
import Tinymce from '@/components/Tinymce'

export default {
  components: { Tinymce },
  directives: { waves },
  data() {
    return {
      dictType: '',
      dictNameList: [],
      typeList: [
        {
          type: '0',
          name: '正常'
        },
        {
          type: '1',
          name: '停用'
        }
      ],
      list: [
      ],
      delarr: [],
      multipleSelection: [],
      total: 0,
      listLoading: false,
      listQuery: {
        page: 1,
        limit: 10,
        dictType: '',
        dictLabel: '',
        status: ''
      },
      dataForm: {
        dictType: undefined,
        dictLabel: undefined,
        dictValue: undefined,
        dictSort: undefined,
        status: '0',
        remark: undefined
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      imageUrl: undefined,
      rules: {
        dictLabel: [{ required: true, message: '字典标签不能为空', trigger: 'blur' }],
        dictValue: [{ required: true, message: '字典键值不能为空', trigger: 'blur' }],
        dictSort: [{ required: true, message: '显示排序不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.dictType = this.$route.params.id
    this.getOptionSelect()
  },
  methods: {
    changeDictType(val) {
      this.dictType = val
      this.listQuery.dictType = val
      this.getList()
    },
    getOptionSelect() {
      optionSelect().then(response => {
        this.dictNameList = response.data.data
        this.listQuery.dictType = this.dictType
        this.getList()
      }).catch(() => {})
    },
    resetForm() {
      this.dataForm = {
        dictType: undefined,
        dictLabel: undefined,
        dictValue: undefined,
        dictSort: undefined,
        status: '0',
        remark: undefined
      }
    },
    handleCreate() {
      this.resetForm()
      this.dataForm.dictType = this.dictType
      this.dialogFormVisible = true
      this.dialogStatus = 'create'
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          dataAdd(this.dataForm)
            .then(() => {
              this.getList()
              this.dialogFormVisible = false

              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              })
              this.getList()
            })
        }
      })
    },
    getList() {
      this.listLoading = true
      dataList(this.listQuery).then(response => {
        this.list = response.data.data.items
        this.total = response.data.data.total
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },

    handleQuery() {
      this.listQuery.page = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.listQuery = {
        dictType: '',
        dictLabel: '',
        status: ''
      }
      this.handleQuery()
    },

    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },

    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },

    handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },

    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          dataEdit(this.dataForm).then(() => {
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        }
      })
    },

    handleDelete(row) {
      this.$confirm('确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        dataRemove({ dictCodes: row.dictCode }).then(response => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          const index = this.list.indexOf(row)
          this.list.splice(index, 1)
        })
      }).catch(() => {

      })
    },
    delAll() {
      this.$confirm('确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          const length = this.multipleSelection.length

          if (length > 0) {
            for (let i = 0; i < length; i++) {
              this.delarr.push(this.multipleSelection[i].dictCode)
            }
            const dictCodes = this.delarr.join(',')
            dataRemove({ dictCodes: dictCodes })
              .then(() => {
                this.$notify({
                  title: '成功',
                  message: '删除成功',
                  type: 'success',
                  duration: 2000
                })
                this.getList()
              })
              .catch(() => { })
          } else {
            this.$notify({
              title: '警告提示',
              message: '请选择要删除的信息！',
              type: 'warning'
            })
          }
        })
        .catch(() => { })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    }
  }
}
</script>
<style>
.ad-avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.ad-avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.ad-avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}

.ad-avatar {
    display: block;
}
</style>