<template>
  <BaseContainer>
    <template slot="search-container">
      <el-form :inline="true">
        <!-- 受试者编号 -->
        <el-form-item label="受试者编号" prop="SubjectCode">
          <el-input v-model="searchData.SubjectCode" size="small" clearable style="width: 120px" />
        </el-form-item>
        <!-- 访视名称 -->
        <el-form-item label="访视名称">
          <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="检查编号" prop="StudyCode">
          <el-input v-model="searchData.StudyCode" size="small" clearable style="width: 120px" />
        </el-form-item>
        <!-- 源区域 -->
        <el-form-item label="源区域" 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="目标区域" 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="是否同步完成" 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="受试者编号" prop="SubjectCode" min-width="90" show-overflow-tooltip sortable="custom"/>
        <el-table-column label="访视名称" prop="VisitName" min-width="90" show-overflow-tooltip sortable="custom"/>
        <el-table-column label="检查编号" prop="StudyCode" min-width="90" show-overflow-tooltip sortable="custom"/>
        <el-table-column label="文件数" prop="FileCount" min-width="90" show-overflow-tooltip sortable="custom"/>
        <el-table-column label="源区域" prop="UploadRegion" min-width="90" show-overflow-tooltip sortable="custom" />
        <el-table-column label="源可用时间" prop="CreateTime" min-width="90" show-overflow-tooltip sortable="custom" />
        <el-table-column label="目标区域" prop="TargetRegion" min-width="90" show-overflow-tooltip sortable="custom" />
        <el-table-column label="目标可用时间" prop="TargetRegion" min-width="90" show-overflow-tooltip sortable="custom" />
        <el-table-column label="是否同步完成" prop="IsSync" min-width="90" show-overflow-tooltip sortable="custom">
          <template slot-scope="scope">
            {{ $fd('YesOrNo', scope.row.IsSync) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="80" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-button type="text" @click="openDetailTable(scope.row)">
              详情
            </el-button>
            <el-button type="text">
              同步
            </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
      :visible.sync="detailDialog.visible"
      fullscreen
      append-to-body
      :close-on-click-modal="false"
      class="detail-dialog"
    >
      <span slot="title">{{ detailDialog.title }}</span>
      <span v-if="detailDialog.currentRow">{{`${detailDialog.currentRow.SubjectCode} / ${detailDialog.currentRow.VisitName} ${detailDialog.currentRow.StudyCode ? ' / ' + detailDialog.currentRow.StudyCode : ''}`}}</span>
      <el-tabs class="detail-tabs" v-model="detailDialog.activeTab" @tab-click="handleDetailTabClick">
        <el-tab-pane label="文件级别" name="file">
          <FileList 
            v-if="detailDialog.activeTab === 'file'" 
            :rowInfo="detailDialog.currentRow"
            @openTaskTable="openTaskTable"
          />
        </el-tab-pane>
        <el-tab-pane label="任务级别" 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 './components/FileList'
import TaskList from './components/TaskList'
import moment from 'moment'
const searchDataDefault = () => {
  return {
    TrialId: '',
    SubjectCode: '',
    VisitName: null,
    StudyCode: '',
    UploadRegion: '',
    TargetRegion: '',
    IsSync: null,
    PageIndex: 1,
    PageSize: 20,
    Asc: true,
    SortField: 'StudyCode'
  }
}
export default {
  components: { BaseContainer, Pagination, FileList, TaskList },
  data() {
    return {
      trialId: '',
      moment,
      searchData: searchDataDefault(),
      list: [],
      total: 0,
      loading: false,
      visitPlanOptions: [],
      detailDialog: {
        visible: false,
        title: '详情',
        activeTab: 'file',
        currentRow: null
      },
      regionOptions: [
        {
          value: 'CN',
          label: 'CN'
        }, {
          value: 'US',
          label: 'US'
        }
      ],
      fileUploadRecordId: '',
      path: ''
    }
  },
  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)
      }
    },
    handleSearch() {
      this.searchData.PageIndex = 1
      this.getList()
    },
    // 重置列表查询
    handleReset() {
      this.datetimerange = null
      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>

