<template>
  <BaseContainer>
    <!-- 搜索框 -->
    <template slot="search-container">
      <el-form :inline="true">
        <!-- 中心编号 -->
        <el-form-item :label="$t('trials:researchStaff:table:siteId')">
          <el-select v-model="searchData.TrialSiteId" clearable filterable style="width:120px;">
            <el-option
              v-for="(item,index) of siteOptions"
              :key="index"
              :label="item.TrialSiteCode"
              :value="item.TrialSiteId"
            />
          </el-select>
        </el-form-item>
        <!-- 中心名称 -->
        <el-form-item :label="$t('trials:researchStaff:table:siteName')">
          <el-input v-model="searchData.TrialSiteName" class="mr" 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-form-item>
        <!-- 重置 -->
        <el-form-item>
          <el-button type="primary" icon="el-icon-refresh-left" @click="handleReset">
            {{ $t('common:button:reset') }}
          </el-button>
        </el-form-item>
        <!-- 导出 -->
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-download"
            :disabled="list.length === 0"
          >
            {{ $t('common:button:export') }}
          </el-button>
        </el-form-item>
      </el-form>
    </template>

    <template slot="main-container">
      <el-table
        ref="list"
        v-loading="loading"
        v-adaptive="{ bottomOffset: 55 }"
        :data="list"
        stripe
        height="100"
        @sort-change="handleSortByColumn"
      >
        <el-table-column type="index" width="50" />
        <!-- 中心编号 -->
        <el-table-column
          prop="TrialSiteCode"
          :label="$t('trials:researchStaff:table:siteId')"
          show-overflow-tooltip
          sortable="custom"
          min-width="100"
        >
        </el-table-column>
        <!-- 中心名称 -->
        <el-table-column
          prop="TrialSiteName"
          :label="$t('trials:researchStaff:table:siteName')"
          show-overflow-tooltip
          sortable="custom"
          min-width="120"
        >
        </el-table-column>
        <!-- 扫描设备 -->
        <el-table-column 
          v-show="equipmentControlFieldList.includes('EquipmentTypeEnum')" 
          prop="EquipmentTypeEnum"
          :label="$t('trials:equiptResearch:form:equipment')" 
          min-width="120" 
          show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.OtherEquipmentType ? scope.row.OtherEquipmentType :
              $fd('SiteSurvey_ScanEquipmentType', scope.row.EquipmentTypeEnum) }}
          </template>
        </el-table-column>
        <!-- 扫描参数 -->
        <el-table-column 
          v-show="equipmentControlFieldList.includes('Parameters')" 
          prop="Parameters"
          :label="$t('trials:equiptResearch:form:param')" 
          min-width="100" 
          show-overflow-tooltip />
        <!-- 扫描仪器制造商名称 -->
        <el-table-column 
          v-show="equipmentControlFieldList.includes('ManufacturerType')" 
          min-width="120"
          prop="ManufacturerType" 
          :label="$t('trials:equiptResearch:form:manufacturer')" 
          show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.ManufacturerName ? scope.row.ManufacturerName : $fd('ManufacturerType',
              scope.row.ManufacturerType) }}
          </template>
        </el-table-column>
        <!-- 扫描仪型号 -->
        <el-table-column 
          v-show="equipmentControlFieldList.includes('ScannerType')" 
          min-width="120" 
          prop="ScannerType"
          :label="$t('trials:equiptResearch:form:model')" 
          show-overflow-tooltip />
        <!-- 磁场强度 -->
        <el-table-column 
          v-show="equipmentControlFieldList.includes('MagneticFieldStrengthType')" 
          min-width="120"
          prop="MagneticFieldStrengthType" 
          :label="$t('trials:equiptResearch:form:MagneticFieldStrengthType')"
          show-overflow-tooltip >
          <template slot-scope="scope">
            {{ $fd('MagneticFieldStrengthType', scope.row.MagneticFieldStrengthType) }}
          </template>
        </el-table-column>
        <!-- 体部线圈通道数 -->
        <el-table-column 
          v-show="equipmentControlFieldList.includes('BodyCoilChannelCount')" 
          min-width="120"
          prop="BodyCoilChannelCount" 
          :label="$t('trials:equiptResearch:form:BodyCoilChannelCount')"
          show-overflow-tooltip >
          <template slot-scope="scope">
            {{ $fd('BodyCoilChannelCount', scope.row.BodyCoilChannelCount) }}
          </template>
        </el-table-column>
        <!-- 是否具备专用的PDFF脂肪定量序列（CSE-MRI序列） -->
        <el-table-column 
          v-show="equipmentControlFieldList.includes('HasDedicatedPdfFatQuantificationSequence')"
          min-width="120" 
          prop="HasDedicatedPdfFatQuantificationSequence"
          :label="$t('trials:equiptResearch:form:HasDedicatedPdfFatQuantificationSequence')" 
          show-overflow-tooltip >
          <template slot-scope="scope">
            {{ $fd('YesOrNo', scope.row.HasDedicatedPdfFatQuantificationSequence) }}
          </template>
        </el-table-column>
        <!-- 专用的PDFF脂肪定量序列类型 -->
        <el-table-column 
          v-show="equipmentControlFieldList.includes('PdfFatQuantificationSequenceType')" 
          min-width="120"
          prop="PdfFatQuantificationSequenceType"
          :label="$t('trials:equiptResearch:form:PdfFatQuantificationSequenceType')" 
          show-overflow-tooltip >
          <template slot-scope="scope">
            {{ scope.row.OtherSequenceSpecification ? scope.row.OtherSequenceSpecification :
              $fd('PdfFatQuantificationSequenceType', scope.row.PdfFatQuantificationSequenceType) }}
          </template>
        </el-table-column>
        <!-- 是否包含 T2/R2 校正（用于铁沉积校正） -->
        <el-table-column 
          v-show="equipmentControlFieldList.includes('HasT2R2Correction')" 
          min-width="120"
          prop="HasT2R2Correction" 
          :label="$t('trials:equiptResearch:form:HasT2R2Correction')" 
          show-overflow-tooltip >
          <template slot-scope="scope">
            {{ $fd('YesOrNo', scope.row.HasT2R2Correction) }}
          </template>
        </el-table-column>
        <!-- 是否可完整导出 PDFF 参数图及全部原始 DICOM 数据 -->
        <el-table-column 
          v-show="equipmentControlFieldList.includes('CanFullyExportPdfParameterMapsAndRawDicom')"
          prop="CanFullyExportPdfParameterMapsAndRawDicom"
          :label="$t('trials:equiptResearch:form:CanFullyExportPdfParameterMapsAndRawDicom')" 
          min-width="120"
          show-overflow-tooltip >
          <template slot-scope="scope">
            {{ $fd('YesOrNo', scope.row.CanFullyExportPdfParameterMapsAndRawDicom) }}
          </template>
        </el-table-column>
        <!-- 备注 -->
        <el-table-column 
          v-show="equipmentControlFieldList.includes('Note')" 
          min-width="120" 
          prop="Note"
          :label="$t('trials:equiptResearch:form:precautions')" 
          show-overflow-tooltip />
      </el-table>

      <!-- 分页组件 -->
      <pagination class="page" :total="total" :page.sync="searchData.PageIndex" :limit.sync="searchData.PageSize" @pagination="getList" />
    </template>
  </BaseContainer>
</template>
<script>
import { getTrialSiteSelect } from '@/api/trials'
import { getSiteSurveyEquipmentList } from '@/api/research'
import BaseContainer from '@/components/BaseContainer'
import Pagination from '@/components/Pagination'
const searchDataDefault = () => {
  return {
    SortField: '',
    Asc: true,
    PageIndex: 1,
    PageSize: 20,
    TrialId: '',
    TrialSiteId: '',
    TrialSiteName: ''
  }
}
export default {
  name: 'Equipments',
  components: { BaseContainer, Pagination },
  data() {
    return {
      searchData: searchDataDefault(),
      loading: false,
      list: [],
      total: 0,
      trialId: '',
      otherInfo: {},
      siteOptions: [],
      equipmentControlFieldList: []
    }
  },
  mounted() {
    this.trialId = this.$route.query.trialId
    this.getList()
    this.getSite()
  },
  methods: {
    async getList() {
      try {
        this.loading = true
        this.searchData.TrialId = this.trialId
        let res = await getSiteSurveyEquipmentList(this.searchData)
        if (res.IsSuccess) {
          this.list = res.Result.CurrentPageData
          this.total = res.Result.TotalCount
          this.otherInfo = res.OtherInfo
          const { EquipmentControlFieldList } = res.OtherInfo
          this.equipmentControlFieldList = []
          EquipmentControlFieldList.forEach(item => {
            this.equipmentControlFieldList.push(item.FiledName)
          })
        }
      } catch(e) {
        console.log(e)
      } finally {
        this.loading = false
      }
    },
    // 导出
    async handleExport() {
      try {
        this.searchData.TrialId = this.trialId
        this.loading = true
        const { SortField, Asc, PageIndex, PageSize, ...param } = { ...this.searchData }
        await trialSiteUserSummaryListExport({ ...param })
      } catch (e) {
        console.log(e)
      } finally {
        this.loading = false
      }
    },
    // 获取site下拉框数据
    async getSite() {
      try {
        let res = await getTrialSiteSelect(this.trialId)
        this.siteOptions = res.Result
      } catch (e) {
        console.log(e)
      }
    },
    // 重置
    handleReset() {
      this.searchData = searchDataDefault()
      this.getList()
      this.$nextTick(() => {
        this.$refs.list.clearSort()
      })
    },
    // 查询
    handleSearch() {
      this.getList()
    },
    // 排序
    handleSortByColumn(column) {
      if (column.order === 'ascending') {
        this.searchData.Asc = true
      } else {
        this.searchData.Asc = false
      }
      this.searchData.SortField = column.prop
      this.getList()
    }
  }
}
</script>
