<template>
  <box-content>
    <!-- 搜索框 -->
    <div class="search">
      <el-form :inline="true" size="mini" class="base-search-form" />
      <span style="margin-left:auto">
        <el-button type="primary" size="mini" @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"
      @sort-change="handleSortByColumn">
      <el-table-column type="index" width="60" />
      <!-- 字典键值 -->
      <el-table-column v-if="!~$route.path.indexOf('select')" prop="Code" :label="$t('dic:all:label:keyCode')" min-width="180"
        show-overflow-tooltip sortable='custom' />
      <!-- 中文值 -->
      <el-table-column prop="ValueCN" :label="$t('dic:all:label:valueCN')" min-width="180" show-overflow-tooltip sortable='custom' />
      <!-- 英文值 -->
      <el-table-column prop="Value" :label="$t('dic:all:label:valueEN')" min-width="180" show-overflow-tooltip sortable='custom' />
      <!-- 分组 -->
      <el-table-column prop="ChildGroup" :label="$t('dic:all:label:childGroup')" min-width="180" show-overflow-tooltip sortable='custom' />
      <!-- 显示顺序 -->
      <el-table-column prop="ShowOrder" :label="$t('dic:all:label:showOrder')" min-width="180" show-overflow-tooltip sortable='custom' />
      <!-- 描述 -->
      <el-table-column prop="Description" :label="$t('dic:all:label:desc')" min-width="180" show-overflow-tooltip sortable='custom' />
      <!-- 是否可用 -->
      <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="150" 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>
    <DictionaryChildAddOrUpdateForm ref="DictionaryChildAddOrUpdateForm" @getList="getList" />
  </box-content>
</template>

<script>
import { deleteDictionary, addOrUpdateBasicDic, getDictionaryChildList } from '@/api/dictionary'
import BoxContent from '@/components/BoxContent'
import DictionaryChildAddOrUpdateForm from './DictionaryChildAddOrUpdateForm'

const searchDataDefault = () => {
  return {
    Asc: true,
    SortField: 'ShowOrder',
  }
}
export default {
  components: { BoxContent, DictionaryChildAddOrUpdateForm },
  props: {
    parent: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      searchData: searchDataDefault(),
      basicDicList: [],
      list: [],
      total: 0,
      loading: false,
      drawerChild: false,
      rowData: {},
      childList: [],
      model_cfg: { visible: false, showClose: true, width: '600px', title: '', appendToBody: true }
    }
  },
  mounted() {
    this.getList()
  },
  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.drawerChild = true
      this.getDictionaryChildList(row.Id)
    },
    getDictionaryChildList(id) {
      getDictionaryChildList(id).then((res) => {
        this.childList = res.Result.CurrentPageData
      }).catch(() => {
        this.loading = false
      })
    },
    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['DictionaryChildAddOrUpdateForm'].openDialog(this.$t('common:action:edit'), row, { DataTypeEnum: this.parent.DataTypeEnum })
      })
    },
    handleAdd() {
      this.$nextTick(() => {
        this.$refs['DictionaryChildAddOrUpdateForm'].openDialog(this.$t('common:button:new'), {}, { ConfigTypeId: this.parent.ConfigTypeId, parentId: this.parent.Id, DataTypeEnum: this.parent.DataTypeEnum })
      })
    },
    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
      this.searchData.ParentId = this.parent.Id
      getDictionaryChildList(this.searchData).then(res => {
        this.loading = false
        this.list = res.Result
      }).catch(() => { this.loading = false })
    },
    // 查询
    handleSearch() {
      this.getList()
    },
    // 重置
    handleReset() {
      this.searchData = searchDataDefault()
      this.getList()
    }
  }
}
</script>

<style scoped></style>
