<template>
  <box-content>
    <!-- 搜索框 -->
    <div class="search">
      <el-form :inline="true" size="mini" class="base-search-form">
        <el-form-item label="字典表名称:">
          <el-input v-model="searchData.Code" clearable style="width:120px;" />
        </el-form-item>
        <el-form-item label="字典分组:">
          <el-select v-model="searchData.ConfigTypeId" placeholder="字典分组" clearable size="small">
            <el-option v-for="item of basicDicList" :key="item.Id" :value="item.Id" :label="item.Code" />
          </el-select>
        </el-form-item>
        <el-form-item label="关键字:">
          <el-input v-model="searchData.KeyInfo" clearable style="width:120px;" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleReset">Reset</el-button>
          <el-button type="primary" @click="handleSearch">Search</el-button>
        </el-form-item>
      </el-form>
      <span style="margin-left:auto">
        <el-button type="primary" size="mini" @click="handleAdd">新建</el-button>
        <el-button type="primary" size="mini" @click="handleBatchAdd">批量新建</el-button>
      </span>
    </div>
    <el-table
      v-loading="loading"
      v-adaptive="{bottomOffset:45}"
      :data="list"
      stripe
      size="small"
      height="100"
      @sort-change="handleSortByColumn"
    >
      <el-table-column type="index" width="60" />
      <el-table-column
        prop="Code"
        label="字典表名称"
        sortable="custom"
        min-width="180"
        show-overflow-tooltip
      />
      <el-table-column
        prop="DataTypeEnum"
        label="类型"
        sortable="custom"
        min-width="180"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{ DataTypeEnumList[scope.row.DataTypeEnum] }}
        </template>
      </el-table-column>
      <el-table-column
        prop="ConfigType"
        label="字典分组"
        sortable="custom"
        min-width="180"
        show-overflow-tooltip
      />
      <el-table-column
        prop="Description"
        label="描述"
        sortable="custom"
        min-width="180"
        show-overflow-tooltip
      />
      <el-table-column
        prop="ShowOrder"
        label="显示顺序"
        sortable="custom"
        min-width="180"
        show-overflow-tooltip
      />
      <el-table-column label="是否可用" width="100" fixed="right">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.IsEnable"
            :active-value="true"
            :inactive-value="false"
            @change="(event) => {return switchChange(event, scope.row)}"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="handleEdit(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            type="success"
            size="mini"
            @click="handleChild(scope.row)"
          >
            子项
          </el-button>
          <el-button
            type="danger"
            size="mini"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination" style="text-align: right;margin-top:5px;">
      <pagination :total="total" :page.sync="searchData.PageIndex" :limit.sync="searchData.PageSize" @pagination="getList" />
    </div>
    <DictionaryTypeConfigForm ref="DictionaryTypeConfigForm" :basic-dic-list="basicDicList" @getList="getList" />
    <el-drawer
      :title="drawer_cfg.title"
      :visible.sync="drawer_cfg.drawerChild"
      direction="rtl"
      size="80%"
    >
      <DictionaryChild v-if="drawer_cfg.drawerChild" ref="DictionaryChild" :parent="drawer_cfg.parent" />
    </el-drawer>

    <BatchAddForm ref="batchAddForm" :basic-dic-list="basicDicList" @getList="getList" />
  </box-content>
</template>
<script>
import { getBasicDicList, deleteDictionary, addOrUpdateBasicDic } from '@/api/dictionary'

import BoxContent from '@/components/BoxContent'
import DictionaryTypeConfigForm from '../components/DictionaryTypeConfigForm'
import DictionaryChild from '../components/DictionaryChild'
import BatchAddForm from './components/BatchAddForm'
import Pagination from '@/components/Pagination'
const searchDataDefault = () => {
  return {
    Asc: true,
    SortField: '',
    Code: '',
    KeyName: '',
    PageIndex: 1,
    PageSize: 20,
    KeyInfo: null,
    ConfigTypeId: null
  }
}
export default {
  components: { BoxContent, Pagination, DictionaryTypeConfigForm, DictionaryChild, BatchAddForm },
  data() {
    return {
      searchData: searchDataDefault(),
      basicDicList: [],
      DataTypeEnumList: ['config', 'select', 'enum', 'bool'],
      list: [],
      total: 0,
      loading: false,
      drawer_cfg: { drawerChild: false, parentId: '', title: '' },
      rowData: {},
      model_cfg: { visible: false, showClose: true, width: '600px', title: '', appendToBody: true }
    }
  },
  mounted() {
    this.getList()
    this.getDictionaryTypeConfig()
  },
  methods: {
    handleSortByColumn(column) {
      if (column.order === 'ascending') {
        this.searchData.Asc = true
      } else {
        this.searchData.Asc = false
      }
      this.searchData.SortField = column.prop
      this.searchData.PageIndex = 1
      this.getList()
    },
    handleChild(row) {
      this.drawer_cfg = { drawerChild: true, parentId: row.Id, title: row.Code, parent: row }
    },
    handleSetting() {
      this.$router.push({ path: '/dictionary/newDictionary/setting' })
    },
    switchChange(event, item) {
      this.loading = true
      addOrUpdateBasicDic(item).then(res => {
        this.$message.success('Saved successfully!')
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    handleEdit(row) {
      this.$nextTick(() => {
        this.$refs['DictionaryTypeConfigForm'].openDialog('DictionaryTypeConfig', row)
      })
    },
    handleAdd() {
      this.$nextTick(() => {
        this.$refs['DictionaryTypeConfigForm'].openDialog('DictionaryTypeConfig', {})
      })
    },
    handleBatchAdd() {
      this.$nextTick(() => {
        this.$refs['batchAddForm'].openDialog('批量新建', {})
      })
    },
    handleDelete(row) {
      this.$confirm('Sure to delete?', {
        type: 'warning',
        distinguishCancelAndClose: true,
        confirmButtonText: 'Ok',
        cancelButtonText: 'Cancel'
      })
        .then(() => {
          this.loading = true
          deleteDictionary(row.Id)
            .then(res => {
              this.loading = false
              if (res.IsSuccess) {
                this.list.splice(this.list.findIndex(item => item.Id === row.Id), 1)
                this.$message.success('Deleted successfully!')
              }
            }).catch(() => { this.loading = false })
        })
    },
    getList() {
      this.loading = true
      getBasicDicList(this.searchData).then(res => {
        this.loading = false
        this.list = res.Result.CurrentPageData
        this.total = res.Result.TotalCount
      }).catch(() => { this.loading = false })
    },
    getDictionaryTypeConfig() {
      getBasicDicList({ DataTypeEnum: 0, SortField: 'Code', PageSize: 500 }).then(res => {
        this.basicDicList = res.Result.CurrentPageData
        console.log(this.basicDicList)
      }).catch(() => { })
    },
    // 查询
    handleSearch() {
      this.searchData.PageIndex = 1
      this.getList()
    },
    // 重置
    handleReset() {
      this.searchData = searchDataDefault()
      this.getList()
    }
  }
}
</script>
