<template>
  <BaseContainer>
    <template slot="search-container">
      <el-form :inline="true">
        <!-- 受试者编号 -->
        <el-form-item :label="$t('trials:data-sync:studyList:subjectCode')" prop="SubjectCode">
          <el-input v-model="searchData.SubjectCode" size="small" clearable style="width: 120px" />
        </el-form-item>
        <!-- 访视名称 -->
        <el-form-item :label="$t('trials:data-sync:studyList:visitName')">
          <el-select v-model="searchData.VisitName" style="width: 140px" clearable>
            <el-option v-for="(item, index) of visitPlanOptions" :key="index" :label="item.VisitName"
              :value="item.VisitNum">
              <span style="float: left">{{ item.VisitName }}</span>
            </el-option>
            <!-- <el-option key="Other" label="Out of Plan" value="1.11" /> -->
          </el-select>
        </el-form-item>
        <!-- 检查编号 -->
        <el-form-item :label="$t('trials:data-sync:studyList:studyCode')" prop="StudyCode">
          <el-input v-model="searchData.StudyCode" size="small" clearable style="width: 120px" />
        </el-form-item>
        <!-- 源区域 -->
        <el-form-item :label="$t('trials:data-sync:studyList:uploadRegion')" prop="UploadRegion">
          <el-select v-model="searchData.UploadRegion" style="width: 120px">
            <el-option
              v-for="item in regionOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      <!-- 源可用时间 -->
      <!-- <el-form-item label="源可用时间">
        <el-date-picker
          v-model="uploadTimeRange"
          :default-time="['00:00:00', '23:59:59']"
          value-format="yyyy-MM-dd HH:mm:ss"
          @change="handleUploadtimeChange"
          style="width: 250px"
          type="datetimerange">
        </el-date-picker>
      </el-form-item> -->
        <!-- 目标区域 -->
        <el-form-item :label="$t('trials:data-sync:studyList:targetRegion')" prop="TargetRegion">
          <el-select v-model="searchData.TargetRegion" style="width: 120px">
            <el-option
              v-for="item in regionOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <!-- 目标可用时间 -->
      <!-- <el-form-item label="目标可用时间">
        <el-date-picker
          v-model="SyncTimeRange"
          :default-time="['00:00:00', '23:59:59']"
          value-format="yyyy-MM-dd HH:mm:ss"
          @change="handleSynctimeChange"
          style="width: 250px"
          type="datetimerange">
        </el-date-picker>
      </el-form-item> -->
        <!-- 是否同步完成 -->
        <el-form-item :label="$t('trials:data-sync:studyList:visitName')" prop="IsSync">
          <el-select v-model="searchData.IsSync" clearable style="width: 120px">
            <el-option 
              v-for="item of $d.YesOrNo"
              :key="'IsSync' + item.label" 
              :value="item.value" 
              :label="item.label" 
            />
          </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-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: 'CreateTime', order: 'descending' }">
        <el-table-column type="index" width="50" />
        <el-table-column 
          :label="$t('trials:data-sync:studyList:subjectCode')" 
          prop="SubjectCode" 
          min-width="90" 
          show-overflow-tooltip 
          sortable="custom"
        />
        <el-table-column 
          :label="$t('trials:data-sync:studyList:visitName')" 
          prop="VisitName" 
          min-width="90" 
          show-overflow-tooltip 
          sortable="custom"
        />
        <el-table-column 
          :label="$t('trials:data-sync:studyList:studyCode')" 
          prop="StudyCode" 
          min-width="90" 
          show-overflow-tooltip 
          sortable="custom"
        />
        <el-table-column 
          :label="$t('trials:data-sync:studyList:fileCount')" 
          prop="FileCount" 
          min-width="90" 
          show-overflow-tooltip
          sortable="custom"
        />
        <el-table-column 
          :label="$t('trials:data-sync:studyList:uploadRegion')" 
          prop="UploadRegion" 
          min-width="60" 
          show-overflow-tooltip 
          sortable="custom"
        />
        <el-table-column 
          :label="$t('trials:data-sync:studyList:createTime')" 
          prop="CreateTime" 
          min-width="90" 
          show-overflow-tooltip 
          sortable="custom"
        />
        <el-table-column 
          :label="$t('trials:data-sync:studyList:targetRegion')" 
          prop="TargetRegion" 
          min-width="60" 
          show-overflow-tooltip 
          sortable="custom"
        />
        <el-table-column 
          :label="$t('trials:data-sync:studyList:syncFinishedTime')" 
          prop="SyncFinishedTime" 
          min-width="90" 
          show-overflow-tooltip
          sortable="custom"
        />
        <el-table-column 
          :label="$t('trials:data-sync:studyList:isSync')" 
          prop="IsSync" 
          min-width="90" 
          show-overflow-tooltip 
          sortable="custom"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.IsSync" type="success">
              {{ $fd('YesOrNo', scope.row.IsSync) }}
            </el-tag>
            <el-tag v-else type="danger">
              {{ $fd('YesOrNo', scope.row.IsSync) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column :label="$t('common:action:action')" min-width="80" show-overflow-tooltip>
          <template slot-scope="scope">
            <!-- 详情 -->
            <el-button type="primary" size="small" @click="openDetailTable(scope.row)">
              {{ $t('trials:data-sync:studyList:detail') }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <pagination class="page" :total="total" :page.sync="searchData.PageIndex" :limit.sync="searchData.PageSize"
        @pagination="getList" />
    </template>
    <el-dialog
      v-if="detailDialog.visible"
      :visible.sync="detailDialog.visible"
      fullscreen
      append-to-body
      :close-on-click-modal="false"
      class="detail-dialog"
    >
      <span slot="title">{{ detailDialog.title }}</span>
      <!-- （源：${detailDialog.currentRow.UploadRegion} -> 目标：${detailDialog.currentRow.TargetRegion}） -->
      <span v-if="detailDialog.currentRow">{{`${detailDialog.currentRow.SubjectCode} / ${detailDialog.currentRow.VisitName} ${detailDialog.currentRow.StudyCode ? ' / ' + detailDialog.currentRow.StudyCode : ''} ${$t('trials:data-sync:studyList:msg1').replace('xxx', detailDialog.currentRow.UploadRegion).replace('yyy', detailDialog.currentRow.TargetRegion)}`}}</span>
      <el-tabs class="detail-tabs" v-model="detailDialog.activeTab" @tab-click="handleDetailTabClick">
        <!-- 文件级别 -->
        <el-tab-pane :label="$t('trials:data-sync:label:fileList')" name="file">
          <FileList 
            v-if="detailDialog.activeTab === 'file'" 
            :dataFileType="1"
            :rowInfo="detailDialog.currentRow"
            @openTaskTable="openTaskTable"
          />
        </el-tab-pane>
        <!-- 任务级别 -->
        <el-tab-pane :label="$t('trials:data-sync:label:taskList')" name="task">
          <TaskList 
            v-if="detailDialog.activeTab === 'task'" 
            :rowInfo="detailDialog.currentRow"
            :fileUploadRecordId="fileUploadRecordId"
            :path="path"
          />
        </el-tab-pane>
      </el-tabs>
    </el-dialog> 
  </BaseContainer>
</template>
<script>
import { getSubjectUploadRecordList } from '@/api/file'
import { getTrialVisitStageSelect } from '@/api/trials'
import BaseContainer from '@/components/BaseContainer'
import Pagination from '@/components/Pagination'
import FileList from './FileList'
import TaskList from './TaskList'
import moment from 'moment'
const searchDataDefault = () => {
  return {
    TrialId: '',
    SubjectCode: '',
    VisitName: null,
    StudyCode: '',
    UploadRegion: '',
    TargetRegion: '',
    IsSync: null,
    // UploadStartTime: null,
    // UploadEndTime: null,
    // SyncFinishedStartTime: null,
    // SyncFinishedEndTime: null,
    PageIndex: 1,
    PageSize: 20,
    Asc: false,
    SortField: 'CreateTime'
  }
} 
export default {
  components: { BaseContainer, Pagination, FileList, TaskList },
  data() {
    return {
      trialId: '',
      moment,
      searchData: searchDataDefault(),
      list: [],
      total: 0,
      loading: false,
      visitPlanOptions: [],
      detailDialog: {
        visible: false,
        title: this.$t('trials:data-sync:studyList:detail'),
        activeTab: 'file',
        currentRow: null
      },
      regionOptions: [
        {
          value: 'CN',
          label: 'CN'
        }, {
          value: 'US',
          label: 'US'
        }
      ],
      fileUploadRecordId: '',
      path: '',
      uploadTimeRange: null,
      SyncTimeRange: null,
    }
  },
  mounted() {
    this.trialId = this.$route.query.trialId
    this.getList()
    this.getVisitPlanOptions()
  },
  methods: {
    async getList() {
      try {
        this.loading = true
        this.searchData.TrialId = this.trialId
        let res = await getSubjectUploadRecordList(this.searchData)
        this.loading = false
        this.list = res.Result.CurrentPageData
        this.total = res.Result.TotalCount
      } catch(e) {
        this.loading = false
        console.log(e)
      }
    },
    openDetailTable(row) {
      this.detailDialog.currentRow = row || null
      this.detailDialog.activeTab = 'file'
      this.detailDialog.visible = true
    },
    openTaskTable(row) {
      this.fileUploadRecordId = row.Id
      this.path = row.Path
      this.detailDialog.activeTab = 'task'

    },
    handleDetailTabClick(tab) {
      const name = tab.name
      if (name === 'file') {
        this.fileUploadRecordId = ''
        this.path = ''
      }
      // if (name !== 'upload' && name !== 'sync') return
      // if (this.detailDialog[name].list && this.detailDialog[name].list.length) return
      // this.fetchDetailList(name)
    },
    // 获取访视下拉框数据
    async getVisitPlanOptions() {
      try {
        let res = await getTrialVisitStageSelect(this.trialId)
        this.visitPlanOptions = res.Result
      } catch(e) {
        console.log(e)
      }
    },
    handleUploadtimeChange(val) {
      if (val) {
        this.searchData.UploadStartTime = val[0]
        this.searchData.UploadEndTime = val[1]
      } else {
        this.searchData.UploadStartTime = ''
        this.searchData.UploadEndTime = ''
      }
    },
    handleSynctimeChange(val) {
      if (val) {
        this.searchData.SyncFinishedStartTime = val[0]
        this.searchData.SyncFinishedEndTime = val[1]
      } else {
        this.searchData.SyncFinishedStartTime = ''
        this.searchData.SyncFinishedEndTime = ''
      }
    },
    handleSearch() {
      this.searchData.PageIndex = 1
      this.getList()
    },
    // 重置列表查询
    handleReset() {
      // this.uploadTimeRange = null
      // this.handleUploadtimeChange()
      // this.SyncTimeRange = null
      // this.handleSynctimeChange()
      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()
    },
  },
}
</script>
<style lang="scss">
.detail-dialog {
  margin: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;

  .el-dialog__header {
    flex: 0 0 auto;
    padding-bottom: 0px;
  }

  .el-dialog__body {
    padding-top: 10px;
    flex: 1 1 auto;
    overflow: hidden;
  }

  &.is-fullscreen {
    .el-dialog__body {
      margin-top: 10px;
      height: calc(100% - 80px);
    }
  }
}

.detail-tabs {
  height: 100%;
  display: flex;
  flex-direction: column;
  .el-tabs__header {
    flex: 0 0 auto;
    margin: 0 0 8px;
  }
  .el-tabs__content {
    flex: 1 1 auto;
    overflow: hidden;
  }
  .el-tab-pane {
    height: 100%;
  }
}

</style>

