<template>
  <box-content>
    <!-- 搜索框 -->
    <div class="search">
      <el-form :inline="true" class="base-search-form">
        <!-- 临床数据名称 -->
        <el-form-item :label="$t('trials:processCfg:title:clinicalDataCfg')">
          <el-input v-model="searchData.ClinicalDataSetName" />
        </el-form-item>
        <!-- 数据级别 -->
        <el-form-item :label="$t('trials:processCfg:title:dataLevel')">
          <el-select v-model="searchData.ClinicalDataLevel" clearable style="width:120px;">
            <el-option
              v-for="(item,index) of $d.ClinicalLevel"
              :key="index"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <!-- 传输方式 -->
        <el-form-item :label="$t('trials:processCfg:title:transferType')">
          <el-select v-model="searchData.ClinicalUploadType" clearable style="width:120px;">
            <el-option
              v-for="(item,index) of $d.ClinicalUploadType"
              :key="index"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </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"
    >
      <el-table-column type="index" width="40" />
      <!-- 临床数据名称 -->
      <el-table-column
        prop="ClinicalDataSetName"
        :label="$t('trials:processCfg:title:clinicalDataName')"
        show-overflow-tooltip
        min-width="160"
      />
      <!-- 临床数据名称（EN） -->
      <el-table-column
        prop="ClinicalDataSetEnName"
        :label="$t('dictionary:clinicalData:label:clinicalDataNameEn')"
        show-overflow-tooltip
        min-width="190"
      />
      <!-- 上传人 -->
      <el-table-column
        prop="UploadRole"
        :label="$t('trials:processCfg:title:uploader')"
        show-overflow-tooltip
        min-width="120"
      >
        <template slot-scope="scope">
          {{ $fd('ClinicalDataUploadRole',scope.row.UploadRole) }}
        </template>
      </el-table-column>
      <!-- 数据级别 -->
      <el-table-column
        prop="ClinicalDataLevel"
        :label="$t('trials:processCfg:title:dataLevel')"
        show-overflow-tooltip
        min-width="120"
      >
        <template slot-scope="scope">
          {{ $fd('ClinicalLevel',scope.row.ClinicalDataLevel) }}
        </template>
      </el-table-column>
      <!-- 传输方式 -->
      <el-table-column
        prop="ClinicalUploadType"
        :label="$t('trials:processCfg:title:transferType')"
        show-overflow-tooltip
        min-width="120"
      >
        <template slot-scope="scope">
          {{ $fd('ClinicalUploadType',scope.row.ClinicalUploadType) }}
        </template>
      </el-table-column>
      <!-- 匹配的阅片标准 -->
      <el-table-column
        prop="CriterionEnumList"
        :label="$t('trials:processCfg:label:criterion')"
        show-overflow-tooltip
        min-width="200"
      >
        <template slot-scope="scope">
          {{ scope.row.CriterionEnumList.map(v => $fd('CriterionType', v)).toString() }}
        </template>
      </el-table-column>
      <!-- 模板名称 -->
      <el-table-column
        prop="FileName"
        :label="$t('trials:processCfg:title:module')"
        show-overflow-tooltip
        min-width="120"
      >
        <template slot-scope="scope">
          {{ scope.row.FileName }}
        </template>
      </el-table-column>
      <!-- 模板英文名称 -->
      <el-table-column
        prop="EnFileName"
        :label="$t('trials:processCfg:title:enModule')"
        show-overflow-tooltip
        min-width="140"
      >
        <template slot-scope="scope">
          {{ scope.row.EnFileName }}
        </template>
      </el-table-column>
      <!-- 状态 -->
      <el-table-column
        prop="IsEnable"
        :label="$t('dictionary:clinicalData:label:status')"
        show-overflow-tooltip
        min-width="120"
      >
        <template slot-scope="scope">
          <!-- <el-tag v-if="scope.row.IsEnable">启用</el-tag>
          <el-tag v-else type="danger">禁用</el-tag> -->
          <el-tag v-if="!scope.row.IsEnable" type="danger">{{ $fd('IsEnable', scope.row.IsEnable) }}</el-tag>
          <el-tag v-else>{{ $fd('IsEnable', scope.row.IsEnable) }}</el-tag>
        </template>
      </el-table-column>
      <!-- 创建时间 -->
      <el-table-column
        prop="CreateTime"
        :label="$t('dictionary:clinicalData:label:CreateTime')"
        show-overflow-tooltip
        min-width="140"
      />

      <el-table-column :label="$t('common:action:action')" min-width="250" fixed="right">
        <template slot-scope="scope">
          <!-- 问题配置 -->
          <el-button
            type="text"
            :disabled="scope.row.ClinicalUploadType !== 2"
            @click="handleConfigQuestion(scope.row)"
          >
            {{ $t('dictionary:clinicalData:button:qsConfig') }}
          </el-button>
          <!-- 下载 -->
          <el-button
            v-if="$i18n.locale === 'zh'"
            type="text"
            :disabled="!scope.row.FileName"
            @click="handleDownloadTpl(scope.row.Path)"
          >
            {{ $t('common:button:download') }}
          </el-button>
          <!-- 下载 -->
           <el-button
            v-else
            type="text"
            :disabled="!scope.row.EnFileName"
            @click="handleDownloadTpl(scope.row.EnPath)"
          >
            {{ $t('common:button:download') }}
          </el-button>
          <!-- 编辑 -->
          <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>
    <el-dialog
      v-if="addOrEditCD.visible"
      :visible.sync="addOrEditCD.visible"
      :close-on-click-modal="false"
      :title="addOrEditCD.title"
      width="600px"
      append-to-body
      custom-class="base-dialog-wrapper"
    >
      <clinical-data-form ref="addOrEditCD" :data="currentRow" @close="addOrEditCD.visible = false" @getList="getList" />
    </el-dialog>
    <el-dialog
      v-if="QuestionConfigVisible.visible"
      :visible.sync="QuestionConfigVisible.visible"
      :close-on-click-modal="false"
      :title="QuestionConfigVisible.title"
      width="1600px"
      append-to-body
      custom-class="base-dialog-wrapper"
    >
      <clinical-question-config ref="QuestionConfigVisible" :data="currentRow" @close="QuestionConfigVisible.visible = false" @getList="getList" />
    </el-dialog>
  </box-content>
</template>
<script>
import { getClinicalDataSetList, deleteClinicalSetData, DownloadSystemClinicalFile } from '@/api/dictionary'
import BoxContent from '@/components/BoxContent'
import ClinicalDataForm from './ClinicalDataForm'
import ClinicalQuestionConfig from './ClinicalQuestionConfig'
const searchDataDefault = () => {
  return {
    ClinicalDataSetName: '',
    ClinicalDataLevel: null,
    ClinicalUploadType: null
  }
}

export default {
  name: 'ClinicalDataTemplate',
  components: { BoxContent, ClinicalDataForm, ClinicalQuestionConfig },
  data() {
    return {
      searchData: searchDataDefault(),
      list: [],
      total: 0,
      loading: false,
      currentRow: {},
      addOrEditCD: { visible: false, title: '' },
      QuestionConfigVisible: { visible: false, title: '' }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    handleConfigQuestion(row) {
      this.currentRow = { ...row }
      this.QuestionConfigVisible.visible = true
      // 问题配置
      this.QuestionConfigVisible.title = this.$t('dictionary:clinicalData:button:qsConfig')
    },
    // 获取列表信息
    getList() {
      this.loading = true
      getClinicalDataSetList(this.searchData).then(res => {
        this.loading = false
        this.list = res.Result
      }).catch(() => { this.loading = false })
    },
    // 新增
    handleAdd() {
      this.currentRow = {}
      this.addOrEditCD.title = this.$t('common:button:new')
      this.addOrEditCD.visible = true
    },
    // 编辑
    handleEdit(row) {
      this.currentRow = { ...row }
      this.addOrEditCD.title = this.$t('common:action:edit')
      this.addOrEditCD.visible = true
    },
    // 删除
    handleDelete(row) {
      this.$confirm(this.$t('trials:staffResearch:message:confirmDel'), {
        type: 'warning',
        distinguishCancelAndClose: true
      })
        .then(() => {
          this.loading = true
          deleteClinicalSetData(row.Id)
            .then(res => {
              this.loading = false
              if (res.IsSuccess) {
                this.getList()
                this.$message.success(this.$t('common:message:deletedSuccessfully'))
              }
            }).catch(() => { this.loading = false })
        })
    },
    handleDownloadTpl(path) {
      window.open(this.OSSclientConfig.basePath + path, '_blank')
    },
    // 排序
    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()
    },
    // 查询
    handleSearch() {
      this.getList()
    },
    // 重置
    handleReset() {
      this.searchData = searchDataDefault()
      this.getList()
    }
  }
}
</script>
