<template>
  <BaseContainer>
    <template slot="search-container">
      <el-form :inline="true">
        <!--所在中心-->
        <el-form-item :label="$t('trials:pushRecord:table:trialSiteKeyInfo')" prop="TrialSiteKeyInfo">
          <el-input v-model="searchData.TrialSiteKeyInfo" size="small" clearable style="width: 150px"
            :placeholder="$t('trials:pushRecord:placeholder:trialSiteKeyInfo')" />
        </el-form-item>
        <!--接收端AE-->
        <el-form-item :label="$t('trials:pushRecord: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:pushRecord: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:pushRecord:table:createTime')">
          <el-date-picker v-model="datetimerange" type="datetimerange" :default-time="['00:00:00', '23:59:59']"
            :start-placeholder="$t('trials:loginLog:table:beginTime')"
            :end-placeholder="$t('trials:loginLog:table:endTime')" 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: 'StartTime', order: 'descending' }">
        <el-table-column type="index" width="50" />
        <!--中心编号-->
        <el-table-column :label="$t('trials:pushRecord:table:trialSiteCode')" prop="TrialSiteCode" min-width="160"
          show-overflow-tooltip sortable="custom" />
        <!--中心名称-->
        <el-table-column :label="$t('trials:pushRecord:table:trialSiteAliasName')" prop="TrialSiteAliasName"
          min-width="160" show-overflow-tooltip sortable="custom" />
        <!--发送端AE-->
        <el-table-column :label="$t('trials:pushRecord:table:callingAEList')" prop="CallingAE" min-width="90"
          show-overflow-tooltip />
        <!--发送端IP-->
        <el-table-column :label="$t('trials:pushRecord:table:callingAEIP')" prop="CallingAEIP" min-width="160"
          show-overflow-tooltip sortable="custom" />
        <!--接收端AE-->
        <el-table-column :label="$t('trials:pushRecord:table:calledAEList')" prop="CalledAE" min-width="90"
          show-overflow-tooltip />

        <!--检查数-->
        <el-table-column :label="$t('trials:pushRecord:table:studyCount')" prop="StudyCount" min-width="160"
          show-overflow-tooltip sortable="custom" />
        <!--图像数量-->
        <el-table-column :label="$t('trials:pushRecord:table:fileCount')" prop="FileCount" min-width="160"
          show-overflow-tooltip sortable="custom" />
        <!--图像大小-->
        <el-table-column :label="$t('trials:pushRecord:table:fileSize')" prop="FileSize" min-width="160"
          show-overflow-tooltip sortable="custom">
          <template slot-scope="scope">
            <span>{{
              $FormatSize(scope.row.FileSize)
            }}</span>
          </template>
        </el-table-column>
        <!--推送开始时间-->
        <el-table-column :label="$t('trials:pushRecord:table:startTime')" prop="StartTime" min-width="160"
          show-overflow-tooltip sortable="custom" />
        <!--推送结束时间-->
        <el-table-column :label="$t('trials:pushRecord:table:endTime')" prop="EndTime" min-width="160"
          show-overflow-tooltip sortable="custom" />
        <!-- 总共用时 -->
        <el-table-column prop="UploadIntervalStr" min-width="100" :label="$t('trials:pushRecord:table:totalTime')"
          show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.UploadIntervalStr }}
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <pagination class="page" :total="total" :page.sync="searchData.PageIndex" :limit.sync="searchData.PageSize"
        @pagination="getList" />
    </template>
  </BaseContainer>
</template>
<script>
import {
  getSCPImageUploadList,
  getDicomCalledAEList,
  getDicomCallingAEList,
} from '@/api/trials'
import BaseContainer from '@/components/BaseContainer'
import Pagination from '@/components/Pagination'
import moment from 'moment'
import { getSCPImageUploadList_Export } from '@/api/export'
const searchDataDefault = () => {
  return {
    CallingAE: null,
    CalledAE: null,
    StartTime: null,
    EndTime: null,
    TrialSiteKeyInfo: null,
    PageIndex: 1,
    PageSize: 20,
    Asc: false,
    SortField: 'StartTime',
  }
}
export default {
  components: { BaseContainer, Pagination },
  data() {
    return {
      moment,
      searchData: searchDataDefault(),
      list: [],
      total: 0,
      loading: false,
      datetimerange: [],
      calledAEList: [],
      callingAEList: [],
    }
  },
  created() {
    this.getCalledAEList()
    this.getCallingAEList()
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 获取接收端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
      getSCPImageUploadList(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.StartTime = val[0]
        this.searchData.EndTime = val[1]
      } else {
        this.searchData.StartTime = ''
        this.searchData.EndTime = ''
      }
    },
    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 getSCPImageUploadList_Export(this.searchData)
      } catch (err) {
        consol.log(err)
      }
    },
  },
}
</script>