<template>
  <box-content>
    <!-- 搜索框 -->
    <div class="search">
      <el-form :inline="true" class="base-search-form">
        <!-- Group -->
        <el-form-item :label="$t('template:anonymization:label:group')">
          <el-input v-model="searchData.Group" clearable style="width:120px;" />
        </el-form-item>
        <!-- Element -->
        <el-form-item :label="$t('template:anonymization:label:element')">
          <el-input v-model="searchData.Element" clearable style="width:120px;" />
        </el-form-item>
        <!-- Tag Description -->
        <el-form-item :label="$t('template:anonymization:label:tagDescription')">
          <el-input v-model="searchData.TagDescription" clearable style="width:120px;" />
        </el-form-item>
        <!-- Tag DescriptionCN -->
        <el-form-item :label="$t('template:anonymization:label:tagDescriptionCN')">
          <el-input v-model="searchData.TagDescriptionCN" 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
      height="100"
      @sort-change="handleSortByColumn"
    >
      <el-table-column type="index" width="60" />
      <!-- Group -->
      <el-table-column
        prop="Group"
        :label="$t('template:anonymization:label:group')"
        show-overflow-tooltip
        sortable="custom"
      />
      <!-- Element -->
      <el-table-column
        prop="Element"
        :label="$t('template:anonymization:label:element')"
        show-overflow-tooltip
        min-width="110"
        sortable="custom"
      />
      <!-- Tag Description -->
      <el-table-column
        prop="TagDescription"
        :label="$t('template:anonymization:label:tagDescription')"
        min-width="110"
        sortable="custom"
      />
      <!-- Tag DescriptionCN -->
      <el-table-column
        prop="TagDescriptionCN"
        :label="$t('template:anonymization:label:tagDescriptionCN')"
        min-width="110"
        sortable="custom"
      />
      <!-- Value Representation -->
      <el-table-column
        prop="ValueRepresentation"
        :label="$t('template:anonymization:label:valueRepresentation')"
        min-width="110"
        sortable="custom"
      />
      <!-- Is Fixed -->
      <el-table-column :label="$t('template:anonymization:label:isFixed')" width="100">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.IsFixed"
            :active-value="true"
            :inactive-value="false"
            @change="(event) => {return switchChange(event, scope.row)}"
          />
        </template>
      </el-table-column>
      <!-- Value Replace -->
      <el-table-column
        prop="ReplaceValue"
        :label="$t('template:anonymization:label:valueReplace')"
        min-width="110"
        sortable="custom"
      />
      <!-- Is Enable -->
      <el-table-column :label="$t('template:anonymization: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:button:edit') }}
          </el-button>
          <el-button
            type="text"
            @click="handleDelete(scope.row)"
          >
            {{ $t('common:button:delete') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--<QcQuestionForm ref="qcQuestionForm" @getList="getList" />-->
    <AddDICOMConfig ref="AddDICOMConfig" @getList="getList" />

  </box-content>
</template>
<script>
import { getSystemAnonymizationList, addOrUpdateSystemAnonymization, deleteSystemAnonymization } from '@/api/dictionary'
import AddDICOMConfig from './AddDICOMConfig'
import BoxContent from '@/components/BoxContent'

const searchDataDefault = () => {
  return {
    Group: '',
    Element: '',
    TagDescription: '',
    IsAdd: false,
    TagDescriptionCN: '',
    PageIndex: 1,
    PageSize: 500,
    Asc: false,
    SortField: ''
  }
}
export default {
  name: 'Anonymization',
  components: { BoxContent, AddDICOMConfig },
  data() {
    return {
      searchData: searchDataDefault(),
      list: [],
      total: 0,
      loading: false,
      rowData: {},
      model_cfg: { visible: false, showClose: true, width: '600px', title: '' }
    }
  },
  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()
    },
    switchChange(event, item) {
      this.loading = true
      addOrUpdateSystemAnonymization(item).then(res => {
        this.$message.success(this.$t('common:message:savedSuccessfully'))
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    handleAdd() {
      this.$nextTick(() => {
        this.$refs['AddDICOMConfig'].openDialog(this.$t('common:button:new'), {})
      })
    },
    handleEdit(row) {
      this.$nextTick(() => {
        this.$refs['AddDICOMConfig'].openDialog(this.$t('common:action:edit'), row)
      })
    },
    handleDelete(row) {
      this.$confirm(this.$t('trials:uploadedDicoms:message:deleteMes'), {
        type: 'warning',
        distinguishCancelAndClose: true,

      })
        .then(() => {
          this.loading = true
          deleteSystemAnonymization(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
      getSystemAnonymizationList(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>
