<template>
  <BaseContainer>
    <template slot="search-container">
      <div style="margin-left:auto;">
        <!-- 提交 -->
        <el-button
          type="primary"
          :disabled="submitIdArr.length==0"
          icon="el-icon-check"
          :loading="loading"
          @click="handleSubmit"
        >
          {{ $t('trials:seletctedReviews:button:submit') }}
        </el-button>
        <!-- 下载所有 -->
        <el-button
          v-if="hasPermi(['role:pm'])"
          type="primary"
          icon="el-icon-download"
          :disabled="!total"
          :loading="loading"
          @click="handleDownloadAll"
        >
          {{ $t('trials:seletctedReviews:button:submissionDownAll') }}
        </el-button>
      </div>
    </template>
    <template slot="main-container">
      <el-table
        v-loading="listLoading"
        v-adaptive="{bottomOffset:65}"
        height="100"
        :data="list"
        class="table"
        :row-class-name="handleHighLighRow"
        @sort-change="handleSortChange"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" :selectable="handleSelectTable" />
        <el-table-column type="index" width="40" />
        <!-- Name -->
        <el-table-column
          prop="LastName"
          :label="$t('trials:seletctedReviews:table:name')"
          show-overflow-tooltip
          sortable="custom"
          min-width="120"
        >
          <template slot-scope="scope">
            <router-link
              style="color: #428bca;"
              tag="a"
              :to="{
                path: `/trialsResume?doctorId=${scope.row.Id}&token=${token}`,
              }"
              target="_blank"
            >{{ scope.row.LastName }} / {{ scope.row.FirstName }}</router-link>
          </template>
        </el-table-column>
        <!-- Name CN -->
        <el-table-column
          prop="ChineseName"
          :label="$t('trials:seletctedReviews:table:nameCN')"
          show-overflow-tooltip
          sortable="custom"
          min-width="120"
        />
        <!-- ID -->
        <el-table-column
          prop="Code"
          :label="$t('trials:seletctedReviews:table:id')"
          show-overflow-tooltip
          sortable="custom"
          min-width="100"
        />
        <!-- Status -->
        <el-table-column
          prop="DoctorTrialState"
          :label="$t('trials:seletctedReviews:table:status')"
          min-width="100"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.DoctorTrialState === 5" type="primary">{{ $fd('DoctorTrialState', scope.row.DoctorTrialState) }}</el-tag>
            <el-tag v-else type="danger">{{ $fd('DoctorTrialState', 16) }}</el-tag>
          </template>
        </el-table-column>
        <!-- Submitter -->
        <el-table-column
          prop="OptUserName"
          :label="$t('trials:seletctedReviews:table:submitter')"
          min-width="120"
        />
        <!-- Submission Time -->
        <el-table-column
          prop="OptTimeStr"
          :label="$t('trials:seletctedReviews:table:submissionTime')"
          min-width="150"
        />
        <el-table-column
          :label="$t('common:action:action')"
          fixed="right"
          width="200"
        >
          <template slot-scope="scope">
            <!-- <el-button type="primary" icon="el-icon-view" @click="handleDetail(scope.row)">Detail</el-button> -->
            <el-button
              icon="el-icon-download"
              circle
              :title="$t('trials:seletctedReviews:button:submissionDown')"
              @click="handleDownload(scope.row)"
            />
          </template>
        </el-table-column>
      </el-table>
      <pagination class="page" :total="total" :page.sync="listQuery.PageIndex" :limit.sync="listQuery.PageSize" @pagination="getList" />

      <el-dialog
        :title="$t('trials:seletctedReviews:title:language')"
        :visible.sync="dialogVisible"
        width="30%"
      >
        <!-- 语言 -->
        <span>{{ $t('trials:seletctedReviews:label:language') }}: </span>
        <el-radio-group v-model="language">
          <el-radio :label="2">English</el-radio>
          <el-radio :label="1">中文</el-radio>
        </el-radio-group>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">{{ $t('common:button:cancel') }}</el-button>
          <el-button type="primary" @click="handleDownloadResumes">{{ $t('common:button:save') }}</el-button>
        </span>
      </el-dialog>
    </template>
  </BaseContainer>
</template>
<script>
import BaseContainer from '@/components/BaseContainer'
import Pagination from '@/components/Pagination'
import { getSubmissionOrApprovalReviewerList, submitReviewer, downloadResume } from '@/api/trials'
import store from '@/store'
const enrollState = 1
export default {
  name: 'Submission',
  components: { BaseContainer, Pagination },
  data() {
    return {
      list: [],
      listQuery: {
        TrialId: '',
        IntoGroupSearchState: enrollState,
        PageIndex: 1,
        PageSize: 20,
        Asc: false,
        SortField: ''
      },
      loading: false,
      total: 0,
      listLoading: false,
      submitIdArr: [],
      dialogVisible: false,
      language: 2,
      token: store.getters.token
    }
  },
  created() { this.initPage() },
  methods: {
    initPage() {
      this.getList()
    },
    getList() {
      this.listLoading = true
      this.listQuery.TrialId = this.$route.query.trialId
      getSubmissionOrApprovalReviewerList(this.listQuery).then(res => {
        this.listLoading = false
        this.list = res.Result.CurrentPageData
        this.total = res.Result.TotalCount
      // eslint-disable-next-line handle-callback-err
      }).catch(() => { this.listLoading = false })
    },
    handleSubmit() {
      // 是否确认提交？
      this.$confirm(this.$t('trials:seletctedReviews:message:msg2'), {
        type: 'warning'
      }).then(() => {
        this.loading = true
        const trialId = this.$route.query.trialId
        submitReviewer(trialId, this.submitIdArr, 1).then(res => {
          this.loading = false
          if (res.IsSuccess) {
            this.getList()
            // 保存成功
            this.$message.success(this.$t('common:message:savedSuccessfully'))
            this.$emit('nextStep', 'approval')
          }
        }).catch(() => {
          this.loading = false
        })
      })
    },
    handleSelectionChange(val) {
      const arr = []
      for (let index = 0; index < val.length; index++) {
        arr.push(val[index].Id)
      }
      this.submitIdArr = arr
    },
    handleSortChange(column) {
      if (column.order === 'ascending') {
        this.listQuery.Asc = true
      } else {
        this.listQuery.Asc = false
      }
      this.listQuery.SortField = column.prop
      this.listQuery.PageIndex = 1
      this.getList()
    },
    handleHighLighRow({ row, rowIndex }) {
      if (row.DoctorTrialState === 5) {
        return 'selected'
      }
    },
    handleSelectTable(row) { return row.DoctorTrialState !== 5 },
    handleDetail(row) {
      const { href } = this.$router.resolve({ path: `/trialsResume?doctorId=${row.Id}` })
      window.open(href, '_blank')
    },
    handleDownloadAll() {
      this.dialogVisible = true
    },
    handleDownloadResumes() {
      this.dialogVisible = false
      const arr = []
      for (let index = 0; index < this.list.length; index++) {
        arr.push(this.list[index].Id)
      }
      this.downloadResume(arr)
    },
    downloadResume(arr) {
      downloadResume(this.$route.query.trialId, this.language, arr).then(res => { window.open(res.Result) })
    },
    handleDownload(row) { this.downloadResume([row.Id]) }
  }
}
</script>

