<template>
  <BaseContainer>
    <template slot="search-container">
      <div style="margin-left:auto;">
        <!-- Approve -->
        <el-button
          icon="el-icon-check"
          type="primary"
          :disabled="approveIdArr.length==0"
          @click="handleApprove()"
          v-hasPermi="['trials:trials-panel:attachments:SPMEnrollment:button-agree']"
        >
          {{ $t('trials:seletctedReviews:button:approve') }}
        </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" />
        <!-- 英文名 -->
        <el-table-column
          prop="BlindName"
          :label="$t('trials:spmEnroll:table:blindName')"
          show-overflow-tooltip
          min-width="120"
          sortable="custom"
        />
        <!-- 中文名 -->
        <!-- <el-table-column
          prop="BlindNameCN"
          :label="$t('trials:spmEnroll:table:blindNameCN')"
          show-overflow-tooltip
          min-width="120"
          sortable="custom"
        /> -->
        <!-- 专业 -->
        <el-table-column
          prop="SpecialityId"
          :label="$t('trials:spmEnroll:table:specialityId')"
          show-overflow-tooltip
          min-width="120"
        >
          <template slot-scope="scope">
            {{ scope.row.SpecialityId === otherId?(scope.row.SpecialityOther?scope.row.SpecialityOther:'Other'): scope.row.Speciality }}
          </template>
        </el-table-column>
        <!-- 亚专业 -->
        <el-table-column
          prop="SubspecialityIds"
          :label="$t('trials:spmEnroll:table:subspecialityIds')"
          show-overflow-tooltip
          min-width="120"
        >
          <template slot-scope="scope">
            {{ scope.row.SubspecialityIds.map(v => {return $fd('Subspeciality', v, 'id')}).join(", ") }}
          </template>
        </el-table-column>
        <el-table-column
          prop="HospitalName"
          :label="$t('trials:seletctedReviews:table:institution')"
          show-overflow-tooltip
          width="150"
        />

        <!-- 提交人 -->
        <el-table-column
          prop="SubmmitUserName"
          :label="$t('trials:spmEnroll:table:submmitUserName')"
          min-width="100"
        />
        <!-- 提交时间 -->
        <el-table-column
          prop="SubmmitTime"
          :label="$t('trials:spmEnroll:table:submmitTime')"
          min-width="150"
        />
        <!-- 审核人 -->
        <el-table-column
          prop="OptUserName"
          :label="$t('trials:spmEnroll:table:optUserName')"
          min-width="100"
        />
        <!-- 审核时间 -->
        <el-table-column
          prop="OptTimeStr"
          :label="$t('trials:spmEnroll:table:optTimeStr')"
          min-width="150"
        />
        <el-table-column
          fixed="right"
          :label="$t('common:action:action')"
          width="210"
        >
          <template slot-scope="scope">
            <!-- 查看阅片人简历 -->
            <el-button
              icon="el-icon-view"
              circle
              :title="$t('trials:spmEnroll:button:view')"
              @click="lookResumeInfo(scope.row)"
            />
            <!-- 同意 -->
            <el-button
              v-hasPermi="['trials:trials-panel:attachments:SPMEnrollment:button-agree']"
              :disabled="scope.row.DoctorTrialState === 8"
              icon="el-icon-check"
              circle
              :title="$t('trials:spmEnroll:button:agree')"
              @click="handleApprove(scope.row)"
            />
          </template>
        </el-table-column>
      </el-table>
      <pagination class="page" :total="total" :page.sync="listQuery.PageIndex" :limit.sync="listQuery.PageSize" @pagination="getList" />
    </template>
  </BaseContainer>
</template>
<script>
import BaseContainer from '@/components/BaseContainer'
import Pagination from '@/components/Pagination'
import { getSubmissionOrApprovalReviewerList, approveReviewer } from '@/api/trials'
import store from '@/store'
const enrollState = 4
export default {
  name: 'Approval',
  components: { BaseContainer, Pagination },
  data() {
    return {
      list: [],
      listQuery: {
        TrialId: '',
        IntoGroupSearchState: enrollState,
        PageIndex: 1,
        PageSize: 20,
        Asc: false,
        SortField: ''
      },
      otherId: 'ef84e9cb-f1a6-49d7-b6da-34be2c12abd5',
      total: 0,
      listLoading: false,
      approveIdArr: [],
      token: store.getters.token
    }
  },
  created() { this.initPage() },
  methods: {
    lookResumeInfo(row) {
      console.log(row)
      window.open(`/blindResumeInfo?doctorId=${row.Id}&token=${this.token}`)
    },
    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 })
    },
    handleApprove(row) {
      // Confirm the approval?
      var params
      if (row) {
        params = [row.Id]
      } else {
        params = Object.assign([], this.approveIdArr)
      }
      // '是否确认审核?'
      this.$confirm(this.$t('trials:spmEnroll:title:msg1'), {
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        const trialId = this.$route.query.trialId
        approveReviewer(trialId, params, 1).then(res => {
          if (res.IsSuccess) {
            this.getList()
            this.$message.success(this.$t('common:message:savedSuccessfully'))
            // this.$emit('nextStep', 'confirmation')
          }
          this.listLoading = false
        }).catch(() => { this.listLoading = false })
      })
    },
    handleSelectionChange(val) {
      const arr = []
      for (let index = 0; index < val.length; index++) {
        arr.push(val[index].Id)
      }
      this.approveIdArr = 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 === 8) {
        return 'selected'
      }
    },
    handleSelectTable(row) { return row.DoctorTrialState !== 8 },
    handleDetail(row) {
      const { href } = this.$router.resolve({ path: `/trialsResume?doctorId=${row.Id}` })
      window.open(href, '_blank')
    }
  }
}
</script>

