<!--
  @description:数据字典
  @Modifier: haofengxian
  @routerPath:
  @date: 2020-7-31-10:40
-->
<template>
  <div>
    <!--标题-->
    <div class="title title_border p0-16">
      <i class="el-icon-s-home" />数据字典
    </div>
    <section class="p0-16">
      <!--查询条件-->
      <div class="crud_menu_title clearfix">
        <span class="icon-line fl">查询条件</span>
      </div>
      <!-- 搜索条件 -->
      <section class="clearfix SearchContent">
        <div class="SearchContent_leftInput fl">
          <el-form ref="searchForm" :model="searchForm" inline @submit.native.prevent>
            <el-form-item label="字典名称" prop="typegroupname">
              <el-input
                v-model="searchForm.typegroupname"
                placeholder="请输入字典名称"
                @keyup.enter.native="onSearch"
              />
            </el-form-item>
          </el-form>
        </div>
        <div class="fr">
          <el-button type="primary" @click="onSearch('searchForm')">
            搜 索
          </el-button>
          <el-button plain type="info" @click="resetForm('searchForm')">
            重 置
          </el-button>
        </div>
      </section>
      <!--查询结果-->
      <div class="crud_menu_title">
        <span class="icon-line">查询结果</span>
      </div>
      <div class="mb10">
        <el-button type="primary" plain @click="addDictionary">
          字典分类录入
        </el-button>
      </div>
      <!-- 列表 -->
      <section>
        <el-table
          ref="multipleTable"
          :data="tableData"
        >
          <el-table-column
            type="index"
            label="序号"
            align="center"
            width="60"
          />
          <el-table-column
            prop="typegroupname"
            label="字典名称"
          />
          <el-table-column
            prop="typegroupcode"
            label="字典code"
          />
          <el-table-column
            label="操作"
            align="center"
          >
            <template slot-scope="scope">
              <el-link
                type="primary"
                @click="handleUpdate(scope.row)"
              >
                [编辑]
              </el-link>
              <el-link
                type="primary"
                @click="handleDelete(scope.row.id,scope.$index)"
              >
                [删除]
              </el-link>
              <el-link
                type="primary"
                @click="toSeeType(scope.row)"
              >
                [查看类型]
              </el-link>
            </template>
          </el-table-column>
        </el-table>
        <!--分页组件-->
        <pagination :pagination-data="paginationData" @change="typeGroupLoadData" />
      </section>
    </section>
    <!-- 字典分类录入 、编辑 -->
    <dictionaryEditDialog ref="dictionaryClassificationEdit" @typeGroupLoadData="typeGroupLoadData" />
    <!-- 查看类型列表弹框 -->
    <dictionaryTypeDetailDialog ref="dictionaryTypeDetail" />
  </div>
</template>
<script>
// 接口
import { delTypeGroup, getTypeGroupList } from '@api/common/tools'
// 混入公共方法
import { ListMixin } from '@/mixins/ListMixin'
/* 组件*/
import pagination from '@comp/common/pagination/pagination'// 分页
import dictionaryEditDialog from './dictionaryEdit-dialog'
import dictionaryTypeDetailDialog from './dictionaryTypeDetail-dialog'
export default {
  name: 'Dictionary',
  components: {
    pagination, // 分页
    dictionaryEditDialog, // 字典分类录入 、编辑
    dictionaryTypeDetailDialog // 字典类型配置列表
  },
  mixins: [ListMixin],
  data() {
    return {
      tableData: [],
      searchForm: {
        typegroupname: '' // 字典名称
      }
    }
  },
  created() {
    this.typeGroupLoadData()
  },
  methods: {
    // 初始化页面
    typeGroupLoadData() {
      const params = {
        pageNo: this.paginationData.pageNo,
        pageSize: this.paginationData.pageSize,
        ...this.searchForm
      }
      getTypeGroupList(params).then(res => {
        const { data } = res
        this.tableData = data.records
        this.paginationData.total = data.total
      }).catch(() => {
      })
    },
    // 查看字典类型
    toSeeType(row) {
      // id 分类id,字典分类表主键id typegroupname 分类名称
      this.$refs.dictionaryTypeDetail.openDialogSeeType(row)
    },
    // 搜索loadData
    onSearch() {
      this.paginationData.pageNo = 1
      this.paginationData.pageSize = 10
      this.typeGroupLoadData()
    },
    // 重置
    resetForm(formName) {
      this.paginationData.pageNo = 1
      this.paginationData.pageSize = 10
      this.$refs[formName].resetFields()
      this.typeGroupLoadData()
    },

    // 字典分类录入
    addDictionary() {
      this.$refs.dictionaryClassificationEdit.openDictionaryEdit('create')
    },
    // 编辑
    handleUpdate(row) {
      this.$refs.dictionaryClassificationEdit.openDictionaryEdit('update', row)
    },

    // 删除
    handleDelete(id, index) {
      this.$confirm('确认要删除字典吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        delTypeGroup(id).then(() => {
          this.$message.success('删除成功')
          this.tableData.splice(index, 1)
          this.typeGroupLoadData()
        })
      })
    }
  }
}
</script>
<style lang="less" scoped>
</style>
