<template>
  <BaseContainer>
    <template slot="search-container">
      <el-form :inline="true">
        <!--病历号-->
        <el-form-item
          :label="$t('trials:imageInspect:table:patientIdStr')"
          prop="PatientIdStr"
        >
          <el-input
            v-model="searchData.PatientIdStr"
            size="small"
            clearable
            style="width: 120px"
          />
        </el-form-item>
        <!--患者姓名-->
        <el-form-item
          :label="$t('trials:imageInspect:table:patientName')"
          prop="PatientName"
        >
          <el-input
            v-model="searchData.PatientName"
            size="small"
            clearable
            style="width: 120px"
          />
        </el-form-item>
        <!--接收端AE-->
        <el-form-item
          :label="$t('trials:imageInspect:table:calledAE')"
          prop="CalledAE"
        >
          <el-select
            v-model="searchData.CalledAE"
            clearable
            style="width: 120px"
          >
            <el-option
              v-for="item of calledAEList"
              :key="item"
              :value="item"
              :label="item"
            />
          </el-select>
        </el-form-item>
        <!--发送端AE-->
        <el-form-item
          :label="$t('trials:imageInspect:table:callingAE')"
          prop="CallingAE"
        >
          <el-select
            v-model="searchData.CallingAE"
            clearable
            style="width: 120px"
          >
            <el-option
              v-for="item of callingAEList"
              :key="item"
              :value="item"
              :label="item"
            />
          </el-select>
        </el-form-item>
        <!--所在中心-->
        <el-form-item
          :label="$t('trials:imageInspect:table:trialSiteKeyInfo')"
          prop="TrialSiteKeyInfo"
        >
          <el-input
            v-model="searchData.TrialSiteKeyInfo"
            size="small"
            clearable
            style="width: 150px"
            :placeholder="
              $t('trials:imageInspect:placeholder:trialSiteKeyInfo')
            "
          />
        </el-form-item>
        <!--最新接受日期-->
        <el-form-item :label="$t('trials:imageInspect:table:createTime')">
          <el-date-picker
            v-model="datetimerange"
            type="datetimerange"
            :default-time="['00:00:00', '23:59:59']"
            :range-separator="$t('baseForm:daterange:rangeSeparator')"
            :start-placeholder="$t('imageInspect:daterange:startPlaceholder')"
            :end-placeholder="$t('imageInspect:daterange:endPlaceholder')"
            value-format="yyyy-MM-dd HH:mm:ss"
            @change="handleDatetimeChange"
          />
        </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-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
        v-loading="loading"
        v-adaptive="{ bottomOffset: 60 }"
        height="100"
        :data="list"
        class="table"
        @sort-change="handleSortByColumn"
        :default-sort="{ prop: 'LatestPushTime', order: 'descending' }"
      >
        <el-table-column type="index" width="50" />
        <!--病历号-->
        <el-table-column
          :label="$t('trials:imageInspect:table:patientIdStr')"
          prop="PatientIdStr"
          min-width="90"
          show-overflow-tooltip
          sortable="custom"
        />
        <!--患者姓名-->
        <el-table-column
          :label="$t('trials:imageInspect:table:patientName')"
          prop="PatientName"
          min-width="90"
          show-overflow-tooltip
          sortable="custom"
        />
        <!--出生日期-->
        <el-table-column
          :label="$t('trials:imageInspect:table:patientBirthDate')"
          prop="PatientBirthDate"
          min-width="90"
          show-overflow-tooltip
          sortable="custom"
        />
        <!--性别-->
        <el-table-column
          :label="$t('trials:imageInspect:table:patientSex')"
          prop="PatientSex"
          min-width="90"
          show-overflow-tooltip
          sortable="custom"
        />
        <!--接收端AE-->
        <el-table-column
          :label="$t('trials:imageInspect:table:calledAEList')"
          prop="CalledAEList"
          min-width="90"
          show-overflow-tooltip
          ><template slot-scope="scope">
            {{ scope.row.CalledAEList.join(', ') }}
          </template>
        </el-table-column>
        <!--发送端AE-->
        <el-table-column
          :label="$t('trials:imageInspect:table:callingAEList')"
          prop="CallingAEList"
          min-width="90"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{ scope.row.CallingAEList.join(', ') }}
          </template>
        </el-table-column>
        <!--检查-->
        <el-table-column
          :label="$t('trials:imageInspect:table:studyCount')"
          prop="StudyCount"
          min-width="160"
          show-overflow-tooltip
          sortable="custom"
        >
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.StudyCount >= 1"
              type="text"
              @click="handleOpenDialog(scope.row, 'study')"
            >
              <span>{{ scope.row.StudyCount }}</span>
            </el-button>
            <span v-else>{{ scope.row.StudyCount }}</span>
          </template>
        </el-table-column>
        <!--所在中心-->
        <el-table-column
          :label="$t('trials:imageInspect:table:trialSiteCodeAndName')"
          prop="TrialSiteCode"
          min-width="160"
          show-overflow-tooltip
          sortable="custom"
        >
          <template slot-scope="scope">
            {{ scope.row.TrialSiteCode }}, {{ scope.row.TrialSiteAliasName }}
          </template>
        </el-table-column>
        <!--受试者ID-->
        <el-table-column
          :label="$t('trials:imageInspect:table:subjectCode')"
          prop="SubjectCode"
          min-width="90"
          show-overflow-tooltip
          sortable="custom"
        />
        <!--最新接收时间-->
        <el-table-column
          :label="$t('trials:imageInspect:table:lastestPushTime')"
          prop="LatestPushTime"
          min-width="90"
          show-overflow-tooltip
          sortable="custom"
        />
      </el-table>
      <!-- 分页组件 -->
      <pagination
        class="page"
        :total="total"
        :page.sync="searchData.PageIndex"
        :limit.sync="searchData.PageSize"
        @pagination="getList"
      />
    </template>
    <!--查看检查列表-->
    <viewStudy
      v-if="visible"
      :visible.sync="visible"
      :Patient="selectPatient"
      @getList="getList"
    />
  </BaseContainer>
</template>
  <script>
import {
  getPatientList,
  getDicomCalledAEList,
  getDicomCallingAEList,
} from '@/api/trials'
import BaseContainer from '@/components/BaseContainer'
import Pagination from '@/components/Pagination'
import moment from 'moment'
import viewStudy from './components/view-study'
import { getPatientList_Export } from '@/api/export'
const searchDataDefault = () => {
  return {
    PatientIdStr: null,
    PatientName: null,
    CallingAE: null,
    CalledAE: null,
    BeginPushTime: null,
    EndPushTime: null,
    SubejctCode: null,
    TrialSiteKeyInfo: null,
    PageIndex: 1,
    PageSize: 20,
    Asc: false,
    SortField: 'LatestPushTime',
  }
}
export default {
  components: { BaseContainer, Pagination, viewStudy },
  data() {
    return {
      moment,
      searchData: searchDataDefault(),
      list: [],
      total: 0,
      loading: false,
      datetimerange: [],
      calledAEList: [],
      callingAEList: [],
      visible: false,
      selectPatient: {},
    }
  },
  created() {
    this.getCalledAEList()
    this.getCallingAEList()
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 打开弹框
    handleOpenDialog(item) {
      this.visible = true
      this.selectPatient = item
    },
    // 获取接收端AE列表
    async getCalledAEList() {
      try {
        let params = {
          TrialId: this.$route.query.trialId,
        }
        let res = await getDicomCalledAEList(params)
        if (res.IsSuccess) {
          this.calledAEList = res.Result
        }
      } catch (err) {
        console.log(err)
      }
    },
    // 获取发送端端AE列表
    async getCallingAEList() {
      try {
        let params = {
          TrialId: this.$route.query.trialId,
        }
        let res = await getDicomCallingAEList(params)
        if (res.IsSuccess) {
          this.callingAEList = res.Result
        }
      } catch (err) {
        console.log(err)
      }
    },
    getList() {
      this.loading = true
      this.searchData.TrialId = this.$route.query.trialId
      getPatientList(this.searchData)
        .then((res) => {
          this.loading = false
          this.list = res.Result.CurrentPageData
          this.total = res.Result.TotalCount
        })
        .catch(() => {
          this.loading = false
        })
    },
    handleDatetimeChange(val) {
      if (val) {
        this.searchData.BeginPushTime = val[0]
        this.searchData.EndPushTime = val[1]
      } else {
        this.searchData.BeginPushTime = ''
        this.searchData.EndPushTime = ''
      }
    },
    handleSearch() {
      this.searchData.PageIndex = 1
      this.getList()
    },
    // 重置列表查询
    handleReset() {
      this.datetimerange = null
      this.handleDatetimeChange()
      this.searchData = searchDataDefault()
      this.getList()
    },
    // 排序
    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()
    },
    // 导出
    async handleExport() {
      try {
        return getPatientList_Export(this.searchData)
      } catch (err) {
        consol.log(err)
      }
    },
  },
}
</script>
  