<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 :label="$t('trials:researchRecord:table:status')">
          <el-select v-model="searchData.State" clearable filterable style="width: 120px">
            <el-option v-for="(item, index) of $d.ResearchRecord" :key="index" :label="item.label"
              :value="item.value" />
          </el-select>
        </el-form-item>
        <!-- 是否废除 -->
        <!-- <el-form-item :label="$t('trials:researchRecord:table:isDeleted')">
          <el-select v-model="searchData.IsDeleted" clearable filterable style="width: 120px">
            <el-option v-for="item of $d.YesOrNo" v-show="item.raw.ValueCN !== '无'" :key="`IsDeleted${item.value}`"
              :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-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" @click="handleExport">
            {{ $t('common:button:export') }}
          </el-button>
        </el-form-item>
      </el-form>
    </template>

    <template slot="main-container">
      <el-table ref="equipmentsList" 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 prop="State" :label="$t('trials:researchRecord:table:status')" min-width="150"
          sortable="custom" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-tag v-if="scope.row.State === 0" type="primary">{{
              $fd('ResearchRecord', scope.row.State)
            }}</el-tag>
            <el-tag v-if="scope.row.State === 1" type="info">{{
              $fd('ResearchRecord', scope.row.State)
            }}</el-tag>
            <el-tag v-if="scope.row.State === 2" type="warning">{{
              $fd('ResearchRecord', scope.row.State)
            }}</el-tag>
            <el-tag v-if="scope.row.State === 3" type="danger">{{
              $fd('ResearchRecord', scope.row.State)
            }}</el-tag>
          </template>
        </el-table-column>
        <!-- 是否废除 -->
        <!-- <el-table-column prop="IsDeleted" :label="$t('trials:researchRecord:table:isDeleted')" min-width="100"
          sortable="custom" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-tag v-if="scope.row.IsDeleted" type="danger">{{
              $fd('YesOrNo', scope.row.IsDeleted)
            }}</el-tag>
            <el-tag v-else type="primary">{{
              $fd('YesOrNo', scope.row.IsDeleted)
            }}</el-tag>
          </template>
        </el-table-column> -->
        <!-- 联系人 -->
        <el-table-column prop="UserName" :label="$t('trials:researchForm:form:contactor')" show-overflow-tooltip
          sortable="custom" min-width="100" />
        <!-- 联系电话 -->
        <el-table-column prop="Phone" :label="$t('trials:researchForm:form:contactorPhone')" show-overflow-tooltip
          sortable="custom" min-width="100" />
        <!-- 联系邮箱 -->
        <el-table-column prop="Email" :label="$t('trials:researchForm:form:contactorEmail')" show-overflow-tooltip
          sortable="custom" min-width="100" />
        <template v-if="equipmentControlFieldList.includes('EquipmentTypeEnum')">
          <!-- 扫描设备 -->
          <el-table-column 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>
        </template>
        <template v-if="equipmentControlFieldList.includes('Parameters')">
          <!-- 扫描参数 -->
          <el-table-column prop="Parameters" :label="$t('trials:equiptResearch:form:param')" min-width="100"
            show-overflow-tooltip />
        </template>

        <template v-if="equipmentControlFieldList.includes('ManufacturerType')">
          <!-- 扫描仪器制造商名称 -->
          <el-table-column 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>
        </template>

        <template v-if="equipmentControlFieldList.includes('ScannerType')">
          <!-- 扫描仪型号 -->
          <el-table-column min-width="120" prop="ScannerType" :label="$t('trials:equiptResearch:form:model')"
            show-overflow-tooltip />
        </template>

        <template v-if="equipmentControlFieldList.includes('MagneticFieldStrengthType')">
          <!-- 磁场强度 -->
          <el-table-column 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>
        </template>

        <template v-if="equipmentControlFieldList.includes('BodyCoilChannelCount')">
          <!-- 体部线圈通道数 -->
          <el-table-column 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>
        </template>

        <template v-if="equipmentControlFieldList.includes('HasDedicatedPdfFatQuantificationSequence')">
          <!-- 是否具备专用的PDFF脂肪定量序列（CSE-MRI序列） -->
          <el-table-column 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>
        </template>

        <template v-if="equipmentControlFieldList.includes('PdfFatQuantificationSequenceType')">
          <!-- 专用的PDFF脂肪定量序列类型 -->
          <el-table-column 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>
        </template>

        <template v-if="equipmentControlFieldList.includes('HasT2R2Correction')">
          <!-- 是否包含 T2/R2 校正（用于铁沉积校正） -->
          <el-table-column 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>
        </template>

        <template v-if="equipmentControlFieldList.includes('CanFullyExportPdfParameterMapsAndRawDicom')">
          <!-- 是否可完整导出 PDFF 参数图及全部原始 DICOM 数据 -->
          <el-table-column 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>
        </template>

        <template v-if="equipmentControlFieldList.includes('Note')">
          <!-- 备注 -->
          <el-table-column min-width="120" prop="Note" :label="$t('trials:equiptResearch:form:remark')"
            show-overflow-tooltip />
        </template>

      </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'
import { getSiteSurveyEquipmentList_Export } from '@/api/export'
const searchDataDefault = () => {
  return {
    SortField: '',
    State: "",
    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 handleExport() {
      await getSiteSurveyEquipmentList_Export(this.searchData)
    },
    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)
          })
          this.$nextTick(() => {
            this.$refs.equipmentsList.doLayout()
          })
        }
      } 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>
