<template>
  <box-content>
    <!-- 搜索框 -->
    <div class="search">
      <el-form :inline="true" class="base-search-form">
        <!-- 字典表名称 -->
        <el-form-item :label="$t('dic:all:label:code')">
          <el-input v-model="searchData.Code" clearable style="width:120px;" />
        </el-form-item>
        <!-- 字典分组 -->
        <el-form-item :label="$t('dic:all:label:group')">
          <el-select v-model="searchData.ConfigTypeId" 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="$t('dic:all:label:keyWord')">
          <el-input v-model="searchData.KeyInfo" clearable style="width:120px;" />
        </el-form-item>
        <el-form-item>
          <el-button 
            type="primary" 
            icon="el-icon-search" 
            @click="handleSearch"
          >
            {{ $t('common:button:search') }}
          </el-button>
          <el-button
            type="primary"
            icon="el-icon-refresh-left"
            @click="handleReset"
          >
            {{ $t('common:button:reset') }}
          </el-button>
        </el-form-item>
      </el-form>
      <span style="margin-left:auto">
        <!-- 新建 -->
        <el-button type="primary" @click="handleAdd">
          {{ $t('common:button:new') }}
        </el-button>
        <!-- 批量新建 -->
        <el-button type="primary" @click="handleBatchAdd">
          {{ $t('common:button:batchNew') }}
        </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="$t('dic:all:label:code')"
        sortable="custom"
        min-width="180"
        show-overflow-tooltip
      />
      <!-- 类型 -->
      <el-table-column
        prop="DataTypeEnum"
        :label="$t('dic:all:label:dataType')"
        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="$t('dic:all:label:group')"
        sortable="custom"
        min-width="180"
        show-overflow-tooltip
      />
      <!-- 描述 -->
      <el-table-column
        prop="Description"
        :label="$t('dic:all:label:desc')"
        sortable="custom"
        min-width="180"
        show-overflow-tooltip
      />
      <!-- 显示顺序 -->
      <el-table-column
        prop="ShowOrder"
        :label="$t('dic:all:label:showOrder')"
        sortable="custom"
        min-width="180"
        show-overflow-tooltip
      />
      <!-- 是否可用 -->
      <el-table-column :label="$t('dic:all:label:isEnable')" 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="$t('common:action:action')" width="300" fixed="right">
        <template slot-scope="scope">
          <!-- 编辑 -->
          <el-button
            type="text"
            @click="handleEdit(scope.row)"
          >
            {{ $t('common:action:edit') }}
          </el-button>
          <!-- 子项 -->
          <el-button
            type="text"
            @click="handleChild(scope.row)"
          >
            {{ $t('dic:all:label:childItem') }}
          </el-button>
          <!-- 删除 -->
          <el-button
            type="text"
            @click="handleDelete(scope.row)"
          >
            {{ $t('common:button:delete') }}
          </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(this.$t('common:action:edit'), row)
      })
    },
    handleAdd() {
      this.$nextTick(() => {
        this.$refs['DictionaryTypeConfigForm'].openDialog(this.$t('common:button:new'), {})
      })
    },
    handleBatchAdd() {
      this.$nextTick(() => {
        this.$refs['batchAddForm'].openDialog(this.$t('common:button:batchNew'), {})
      })
    },
    handleDelete(row) {
      this.$confirm(this.$t('trials:uploadedDicoms:message:deleteMes'), {
        type: 'warning',
        distinguishCancelAndClose: true,

      })
        .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(this.$t('common:message:deletedSuccessfully'))
              }
            }).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>
