<template>
  <box-content>
    <!-- 搜索框 -->
    <div class="search">
      <el-form :inline="true" size="mini" class="base-search-form">
        <el-form-item label="Group:">
          <el-input v-model="searchData.Group" clearable style="width:120px;" />
        </el-form-item>
        <el-form-item label="Element:">
          <el-input v-model="searchData.Element" clearable style="width:120px;" />
        </el-form-item>
        <el-form-item label="Tag Description:">
          <el-input v-model="searchData.TagDescription" clearable style="width:120px;" />
        </el-form-item>
        <el-form-item label="Tag DescriptionCN:">
          <el-input v-model="searchData.TagDescriptionCN" 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">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="Group"
        label="Group"
        show-overflow-tooltip
      />
      <el-table-column
        prop="Element"
        label="Element"
        show-overflow-tooltip
        min-width="110"
      />
      <el-table-column
        prop="TagDescription"
        label="Tag Description"
        min-width="110"
      />
      <el-table-column
        prop="TagDescriptionCN"
        label="Tag DescriptionCN"
        min-width="110"
      />
      <el-table-column
        prop="ValueRepresentation"
        label="Value Representation"
        min-width="110"
      />
      <el-table-column label="Is Fixed" 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>
      <el-table-column
        prop="ReplaceValue"
        label="Value Replace"
        min-width="110"
      />
      <el-table-column label="Is Enable" 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="Action" width="200" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="handleEdit(scope.row)"
          >
            Edit
          </el-button>
          <el-button
            type="danger"
            size="mini"
            @click="handleDelete(scope.row)"
          >
            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: '',
    TagDescriptionCN: '',
    IsAdd: true,
    PageIndex: 1,
    PageSize: 500
  }
}
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: {
    switchChange(event, item) {
      this.loading = true
      addOrUpdateSystemAnonymization(item).then(res => {
        this.$message.success('Saved successfully!')
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    handleEdit(row) {
      this.$nextTick(() => {
        this.$refs['AddDICOMConfig'].openDialog('Edit Anonymization', row)
      })
    },
    handleAdd() {
      this.$nextTick(() => {
        this.$refs['AddDICOMConfig'].openDialog('New IncreaseFields', {
          Group: '',
          Element: '',
          TagDescription: '',
          TagDescriptionCN: '',
          ValueRepresentation: '',
          ReplaceValue: '',
          IsEnable: false,
          IsAdd: true
        })
      })
    },
    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>
