<template>
  <BaseContainer>
    <template slot="search-container">
      <el-form :inline="true">
        <el-form-item :label="$t('trials:downloadRecord:table:SubjectCode')" prop="SubjectCode" v-if="!isSystem">
          <el-input v-model="searchData.SubjectCode" size="small" clearable style="width: 120px" />
        </el-form-item>
        <el-form-item :label="$t('trials:downloadRecord:table:IP')" prop="IP">
          <el-input v-model="searchData.IP" size="small" clearable style="width: 120px" />
        </el-form-item>
        <el-form-item :label="$t('trials:downloadRecord:table:VisitName')" prop="VisitName" v-if="!isSystem">
          <el-input v-model="searchData.VisitName" size="small" clearable style="width: 120px" />
        </el-form-item>
        <el-form-item :label="$t('trials:downloadRecord:table:Name')" prop="LoginUserName">
          <el-input v-model="searchData.Name" size="small" clearable style="width: 120px" />
        </el-form-item>
        <el-form-item :label="$t('trials:downloadRecord:table:userType')" prop="UserTypeEnum">
          <el-select v-model="searchData.UserTypeEnum" clearable style="width: 120px">
            <el-option v-for="item of userTypeOptions" :key="item.Id" :label="item.UserType"
              :value="item.UserTypeEnum" />
          </el-select>
        </el-form-item>
        <!-- 下载日期 -->
        <el-form-item :label="$t('trials:downloadRecord:table:DownloadTime')">
          <el-date-picker v-model="dateValue" type="datetimerange" range-separator="-"
            :start-placeholder="$t('trials:uploadClinicalData:table:beginDate')"
            :end-placeholder="$t('trials:uploadClinicalData:table:endDate')" :default-time="['00:00:00', '23:59:59']">
          </el-date-picker>
        </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>
    </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: 'DownloadTime', order: 'descending' }">
        <el-table-column type="index" width="50" />
        <!--受试者-->
        <el-table-column :label="$t('trials:downloadRecord:table:SubjectCode')" prop="SubjectCode" min-width="90"
          show-overflow-tooltip sortable="custom" v-if="!isSystem">
        </el-table-column>
        <!--ip-->
        <el-table-column :label="$t('trials:downloadRecord:table:IP')" prop="IP" min-width="90" show-overflow-tooltip
          sortable="custom" />
        <!--访视名-->
        <el-table-column :label="$t('trials:downloadRecord:table:VisitName')" prop="VisitName" min-width="90"
          show-overflow-tooltip sortable="custom" v-if="!isSystem" />
        <!--下载用户名称-->
        <el-table-column :label="$t('trials:downloadRecord:table:DownloadUserName')" prop="DownloadUserName"
          min-width="90" show-overflow-tooltip sortable="custom">
        </el-table-column>
        <!--下载用户别名--->
        <el-table-column :label="$t('trials:downloadRecord:table:DownLoadUserFullName')" prop="DownLoadUserFullName"
          min-width="90" show-overflow-tooltip sortable="custom" />
        <!--用户类型--->
        <el-table-column :label="$t('trials:downloadRecord:table:userType')" prop="UserTypeEnum" min-width="90"
          show-overflow-tooltip sortable="custom">
          <template slot-scope="scope">
            {{ $fd('UserType', scope.row.UserTypeEnum) }}
          </template>
        </el-table-column>
        <!--检查数量--->
        <el-table-column :label="$t('trials:downloadRecord:table:StudyCount')" prop="StudyCount" min-width="90"
          show-overflow-tooltip sortable="custom" />
        <!--下载图像数量--->
        <el-table-column :label="$t('trials:downloadRecord:table:VisitImageFileCount')" prop="ImageCount" min-width="90"
          show-overflow-tooltip sortable="custom" />
        <!--下载图像大小--->
        <el-table-column :label="$t('trials:downloadRecord:table:VisitImageFileSize')" prop="ImageSize" min-width="90"
          show-overflow-tooltip sortable="custom">
          <template slot-scope="scope">
            <span>{{
              scope.row.ImageSize && scope.row.ImageSize > 0
                ? `${(scope.row.ImageSize / 1024 / 1024).toFixed(2)}MB`
                : 0
            }}</span>
          </template>
        </el-table-column>
        <!--下载时间--->
        <el-table-column :label="$t('trials:downloadRecord:table:DownloadStartTime')" prop="DownloadStartTime"
          min-width="130" show-overflow-tooltip sortable="custom" />
        <!-- 下载结束时间 -->
        <el-table-column prop="DownloadEndTime" min-width="130"
          :label="$t('trials:downloadRecord:table:downloadEndTime')" show-overflow-tooltip sortable="custom" />
        <!-- 总共用时 -->
        <el-table-column prop="UploadIntervalStr" min-width="100" :label="$t('trials:downloadRecord:table:totalTime')"
          show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.UploadIntervalStr }}
          </template>
        </el-table-column>
        <!--操作-->
        <el-table-column :label="$t('common:action:action')" min-width="100" fixed="right">
          <template slot-scope="scope">
            <el-button circle icon="el-icon-info" :title="$t('trials:downloadRecord:button:view')"
              @click.stop="view(scope.row)" />
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <pagination class="page" :total="total" :page.sync="searchData.PageIndex" :limit.sync="searchData.PageSize"
        @pagination="getList" />
    </template>
    <base-model v-if="detail_model.visible" :config="detail_model">
      <template slot="dialog-body">
        <el-table :data="detailData" style="width: 100%">
          <el-table-column prop="PatientId" :label="$t('trials:downloadRecord:table:PatientId')" show-overflow-tooltip>
          </el-table-column>
          <el-table-column prop="StudyInstanceUid" :label="$t('trials:downloadRecord:table:StudyInstanceUid')"
            show-overflow-tooltip>
          </el-table-column>
          <el-table-column prop="ImageCount" :label="$t('trials:downloadRecord:table:ImageCount')"
            show-overflow-tooltip>
          </el-table-column>
        </el-table>
      </template>
      <template slot="dialog-footer">
        <!-- 保存 -->
        <el-button size="small" type="primary" @click="detail_model.visible = false">
          {{ $t("common:button:confirm") }}
        </el-button>
      </template>
    </base-model>
  </BaseContainer>
</template>
<script>
import { getUserTypeListByUserType } from '@/api/admin'
import { getTrialSubjectVisitDownloadList } from '@/api/inspection'
import BaseContainer from '@/components/BaseContainer'
import Pagination from '@/components/Pagination'
import BaseModel from '@/components/BaseModel'
const searchDataDefault = () => {
  return {
    TrialId: '',
    SubjectCode: null,
    IP: '',
    VisitName: '',
    Name: '',
    BeginDownloadTime: null,
    EndDownloadTime: null,
    Asc: false,
    SortField: 'DownloadTime',
    PageIndex: 1,
    PageSize: 20,
  }
}
export default {
  components: { BaseContainer, Pagination, BaseModel },
  name: "downloadRecord",
  props: {
    isSystem: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      userTypeOptions: [],
      searchData: searchDataDefault(),
      list: [],
      total: 0,
      loading: false,
      dateValue: [],
      detail_model: {
        visible: false,
        title: this.$t('trials:inspection:dialogTitle:detail'),
        width: '800px',
        appendToBody: true
      },
      detailData: []
    }
  },
  mounted() {
    this.getList()
    this.getUserType()
  },
  methods: {
    view(row) {
      this.detailData = JSON.parse(row.DownloadListStr).DownLoadList
      this.detail_model.visible = true
    },
    // 获取用户类型下拉数据
    getUserType() {
      getUserTypeListByUserType(0).then((res) => {
        let arr = []
        if (this.hasPermi(['role:admin'])) {
          arr = [1]
        }
        if (this.hasPermi(['role:sa'])) {
          arr = [1, 2]
        }
        if (this.hasPermi(['role:pm'])) {
          arr = [1, 2, 14]
        }
        this.userTypeOptions = res.Result.map((item) => {
          if (!arr.includes(item.UserTypeEnum)) {
            return item
          }
        }).filter((item) => item)
      })
    },
    getList() {
      this.loading = true
      if (!this.isSystem) {
        this.searchData.TrialId = this.$route.query.trialId
      }
      if (this.dateValue && this.dateValue[0] && this.dateValue[1]) {
        this.searchData.BeginDownloadTime = this.$moment(
          this.dateValue[0]
        ).format('YYYY-MM-DD HH:mm:ss')
        this.searchData.EndDownloadTime = this.$moment(
          this.dateValue[1]
        ).format('YYYY-MM-DD HH:mm:ss')
      } else {
        this.searchData.BeginDownloadTime = null
        this.searchData.EndDownloadTime = null
      }
      getTrialSubjectVisitDownloadList(this.searchData)
        .then((res) => {
          this.loading = false
          this.list = res.Result.CurrentPageData
          this.total = res.Result.TotalCount
        })
        .catch(() => {
          this.loading = false
        })
    },
    handleSearch() {
      this.searchData.PageIndex = 1
      this.getList()
    },
    // 重置列表查询
    handleReset() {
      this.searchData = searchDataDefault()
      this.dateValue = []
      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()
    },
  },
}
</script>
