<template>
  <box-content>
    <!-- 搜索框 -->
    <div class="search">
      <el-form :inline="true" class="base-search-form">
        <el-form-item :label="$t('dic:all:label:group')">
          <el-input v-model="searchData.Code" clearable style="width:120px;" />
        </el-form-item>
        <el-form-item :label="$t('dic:all:label:keyWord')">
          <el-input v-model="searchData.KeyName" 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>
      </span>
    </div>
    <el-table
      v-loading="loading"
      v-adaptive="{bottomOffset:45}"
      :data="list"
      stripe
      size="small"
      height="100"
    >
      <el-table-column type="index" width="60" />
      <el-table-column
        prop="Code"
        :label="$t('dic:all:label:group')"
        show-overflow-tooltip
      />
      <el-table-column
        prop="Description"
        :label="$t('dic:all:label:desc')"
        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="200" 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="handleDelete(scope.row)"
          >
            {{ $t('common:button:delete') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <DictionaryTypeConfigForm ref="DictionaryTypeConfigForm" @getList="getList" />
  </box-content>
</template>
<script>
import { getBasicDicList, deleteDictionary, addOrUpdateBasicDic } from '@/api/dictionary'
import DictionaryTypeConfigForm from './DictionaryTypeConfigForm'
import BoxContent from '@/components/BoxContent'

const searchDataDefault = () => {
  return {
    Asc: true,
    SortField: 'Code',
    Code: '',
    KeyName: '',
    DataTypeEnum: 0,
    PageIndex: 1,
    PageSize: 500
  }
}
export default {
  name: 'DictionaryConfig',
  components: { BoxContent, DictionaryTypeConfigForm },
  data() {
    return {
      searchData: searchDataDefault(),
      list: [],
      total: 0,
      loading: false,
      rowData: {},
      model_cfg: { visible: false, showClose: true, width: '600px', title: '' }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    switchChange(event, item) {
      this.loading = true
      addOrUpdateBasicDic(item).then(res => {
        this.$message.success(this.$t('common:message:savedSuccessfully'))
        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'), {})
      })
    },
    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
      }).catch(() => { this.loading = false })
    },
    // 查询
    handleSearch() {
      this.getList()
    },
    // 重置
    handleReset() {
      this.searchData = searchDataDefault()
      this.getList()
    }
  }
}
</script>
