<template>
  <div class="view-systemDataDictionary">
    <div class="common-box">
      <div class="common-box-button">
        <el-button type="success" @click="handleAdd" style="margin-bottom: 10px">新增</el-button>
        <el-button type="warning" @click="handleDelete" style="margin-bottom: 10px">删除</el-button>
      </div>
      <div class="common-box-search">
        <el-input placeholder="请输入字典名称/类型/备注" v-model="keyword" class="input-with-select" @keyup.enter.native="fetchTableData">
          <el-button slot="append" icon="el-icon-search" @click="fetchTableData(1)"></el-button>
        </el-input>
      </div>
    </div>
    <LTable :dataSource="tableData" ref="Ltable" :options="options" :fetch="fetchTableData" :columns="columns" @select-all="selectAll" @selection-change="selection" :pagination="pagination"></LTable>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body @close="cancel">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="字典名称" prop="dictName">
          <el-input v-model="form.dictName" placeholder="请输入字典名称" />
        </el-form-item>
        <el-form-item label="字典类型" prop="dictType">
          <el-input v-model="form.dictType" placeholder="请输入字典类型" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { compareObj } from '@/utils/index'
import { columns, options } from './index'
import { dictTypeList, dictTypeAdd, dictTypeUpdate, dictTypeRemove } from '@/api/system/systemDataDictionary'
import { deepClone } from '@/utils/index'

export default {
  name: 'systemDataDictionary',
  components: {},
  data() {
    // 这里存放数据
    return {
      columns: columns(this),
      options,
      roleFlag: true,
      pagination: {
        total: 0,
        pageIndex: 1,
        pageSize: 10
      },
      // 字典表格数据
      tableData: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dictName: undefined,
        dictType: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      removeArr: [],
      // 表单校验
      rules: {
        dictName: [{ required: true, message: '字典名称不能为空', trigger: 'blur' }],
        dictType: [{ required: true, message: '字典类型不能为空', trigger: 'blur' }]
      },
      keyword: ''
    }
  },
  mounted() {},
  methods: {
    async fetchTableData(pageIndex) {
      let postData = {
        keyword: this.keyword,
        pageIndex: pageIndex ? pageIndex : this.pagination.pageIndex,
        pageSize: this.pagination.pageSize
      }
      const { code, data } = await dictTypeList(postData)
      if (code == 200) {
        this.tableData = data.list
        this.pagination.total = data.totalCount
      }
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        dictId: undefined,
        dictName: undefined,
        dictType: undefined,
        status: '0',
        remark: undefined
      }
      this.resetForm('form')
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加字典类型'
    },
    /** 修改按钮操作 */
    handUpdate(row) {
      this.reset()
      this.form = deepClone(row)
      this.open = true
      this.title = '修改字典类型'
    },
    /** 调整到字典数据页面 */
    toDictData(row) {
      this.$router.push(`/system/systemDataDictionary/${row.dictId}`)
    },
    // 全选
    selectAll(data) {
      this.removeArr = []
      if (data.length > 0) {
        data.forEach((item) => {
          this.removeArr.push(item)
        })
      } else {
        this.removeArr = []
      }
    },
    // 单选
    selection(data) {
      this.removeArr = []
      data.forEach((item) => {
        this.removeArr.push(item)
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(async (valid) => {
        if (valid) {
          if (this.title == '添加字典类型') {
            const { code } = await dictTypeAdd(this.form)
            if (code == 200) {
              this.fetchTableData()
              this.reset()
              this.$message({
                type: 'success',
                message: '添加成功!'
              })
              this.open = false
            }
          } else {
            const { code } = await dictTypeUpdate(this.form)
            if (code == 200) {
              this.fetchTableData()
              this.reset()
              this.$message({
                type: 'success',
                message: '修改成功!'
              })
              this.open = false
            }
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete() {
      this.$confirm('是否确认删除该数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          let postArr = []
          this.removeArr.forEach((i) => {
            postArr.push(i.dictId)
          })
          const { code } = await dictTypeRemove(postArr)
          if (code == 200) {
            this.fetchTableData()
            this.$refs.Ltable.clearSelection()
            useMessage('success', 1000, '删除成功')
          }
        })
        .catch(() => {})
    },
    handRemove(row) {
      this.$confirm('是否确认删除该数据', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { code } = await dictTypeRemove([row.dictId])
          if (code == 200) {
            this.fetchTableData()
            this.$refs.Ltable.clearSelection()
            useMessage('success', 1000, '删除成功')
          }
        })
        .catch(() => {})
    }
  }
}
</script>

<style lang="scss">
@import './index.scss';
</style>
