<template>
  <base-model v-if="config.visible" :config="config">
    <template slot="dialog-body">
      <el-button
        type="primary"
        icon="el-icon-plus"
        @click="openAdd"
        style="float: right"
        v-if="status === 'add'"
      >
        {{ $t('common:button:add') }}
      </el-button>
      <el-table v-loading="loading" :data="list" stripe>
        <!-- AE 名称 -->
        <el-table-column
          prop="CallingAE"
          :label="$t('trials:sitesList:dicomAEList:table:AEName')"
          show-overflow-tooltip
          min-width="120"
        />
        <!-- IP地址 -->
        <el-table-column
          prop="IP"
          :label="$t('trials:sitesList:dicomAEList:table:ip')"
          show-overflow-tooltip
          min-width="120"
        />
        <!-- 端口 -->
        <el-table-column
          prop="Port"
          :label="$t('trials:sitesList:dicomAEList:table:port')"
          show-overflow-tooltip
          min-width="120"
        />
        <!-- 说明 -->
        <el-table-column
          prop="Description"
          :label="$t('trials:sitesList:dicomAEList:table:description')"
          show-overflow-tooltip
          min-width="120"
        />
        <!-- 操作 -->
        <el-table-column
          prop="action"
          :label="$t('trials:sitesList:dicomAEList:table:action')"
          min-width="120"
          v-if="
            hasPermi([
              'trials:trials-panel:setting:personnel-manage:edit-dicom',
            ])
          "
        >
          <template slot-scope="scope">
            <el-button
              circle
              :title="$t('trials:sitesList:dicomAEList:action:edit')"
              icon="el-icon-edit-outline"
              @click="editDicom(scope.row)"
            />
            <el-button
              circle
              :title="$t('trials:sitesList:dicomAEList:action:del')"
              icon="el-icon-delete"
              :disabled="scope.row.IsDeleted"
              @click="delDicom(scope.row)"
            />
          </template>
        </el-table-column>
      </el-table>
      <dicomForm ref="dicomForm" @getList="getList" @save="save" />
    </template>
  </base-model>
</template>
<script>
import BaseModel from '@/components/BaseModel'
import dicomForm from './dicomForm.vue'
import { getTrialSiteDicomAEList, deleteTrialSiteDicomAE } from '@/api/trials'
const defaultSearchData = () => {
  return {
    TrialSiteId: null,
    CallingAE: null,
    Ip: null,
    Port: null,
    Description: null,
  }
}
export default {
  name: 'dicomAE',
  components: { BaseModel, dicomForm },
  data() {
    return {
      config: {
        visible: false,
        title: null,
        width: '800px',
        appendToBody: true,
      },
      status: 'add',
      TrialSiteId: null,
      loading: false,
      list: [],
      searchData: defaultSearchData(),
      siteData: {},
    }
  },
  methods: {
    open(row, status = 'add') {
      this.status = status
      this.TrialSiteId = row.TrialSiteId
      this.config.title = this.$t(
        'trials:trials-panel:setting:personnel-manage:dicomAETitle'
      )
        .replace('xxx', `${row.TrialSiteCode}`)
        .replace('yyy', `${row.TrialSiteAliasName}`)

      this.siteData = row
      this.config.visible = true
      this.getList()
    },
    // 获取dicomAE列表
    async getList() {
      try {
        this.searchData.TrialSiteId = this.TrialSiteId
        this.loading = true
        let res = await getTrialSiteDicomAEList(this.searchData)
        this.loading = false
        if (res.IsSuccess) {
          this.list = res.Result
        }
      } catch (err) {
        console.log(err)
        this.loading = false
      }
    },
    // 新增
    openAdd() {
      this.$refs.dicomForm.open(this.siteData, null, 'add')
    },
    save() {
      this.$emit('getList')
    },
    // 修改dicom
    editDicom(row) {
      this.$refs.dicomForm.open(this.siteData, row, 'edit')
    },
    // 删除dicom
    async delDicom(row) {
      try {
        let confirm = await this.$confirm(
          this.$t('trials:sitesList:dicomAEList:confirm:delMessage'),
          {
            type: 'warning',
            distinguishCancelAndClose: true,
            confirmButtonText: this.$t('common:button:confirm'),
            cancelButtonText: this.$t('common:button:cancel'),
          }
        )
        if (confirm !== 'confirm') return
        let res = await deleteTrialSiteDicomAE(row.Id)
        if (res.IsSuccess) {
          this.getList()
          this.save()
          this.$t('common:message:deletedSuccessfully')
        }
      } catch (err) {
        console.log(err)
      }
    },
  },
}
</script>
<style lang="scss" scoped></style>