<template>
  <BaseContainer>
    <template slot="search-container">
      <div style="margin-left:auto;">
        <!-- Approve -->
      </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="index" width="40" />
        <!-- Name -->
        <el-table-column
          prop="LastName"
          :label="$t('trials:seletctedReviews:table:name')"
          show-overflow-tooltip
          min-width="120"
          sortable="custom"
        >
          <template slot-scope="scope">
            <!-- <el-button type="text" @click="go(`/trialsResume?doctorId=${scope.row.Id}&token=${token}`)">{{ scope.row.LastName }} / {{ scope.row.FirstName }}</el-button> -->
             <span style="color: #428bca;cursor: pointer;" @click="go(`/trialsResume?doctorId=${scope.row.Id}&token=${token}`)">{{ scope.row.LastName }} / {{ scope.row.FirstName }}</span>
          </template>
        </el-table-column>
        <!-- Name CN -->
        <el-table-column
          prop="ChineseName"
          :label="$t('trials:seletctedReviews:table:nameCN')"
          show-overflow-tooltip
          min-width="120"
          sortable="custom"
        />
        <!-- ID -->
        <el-table-column
          prop="Code"
          :label="$t('trials:seletctedReviews:table:id')"
          min-width="100"
          show-overflow-tooltip
          sortable="custom"
        />
        <!-- Status -->
        <el-table-column
          prop="DoctorTrialState"
          :label="$t('trials:seletctedReviews:table:status')"
          min-width="120"
        >
          <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', scope.row.DoctorTrialState) }}</el-tag>
          </template>
        </el-table-column>
        <!-- Approver -->
        <el-table-column
          prop="OptUserName"
          :label="$t('trials:seletctedReviews:table:approver')"
          min-width="120"
        />
        <!-- Approval Time -->
        <el-table-column
          prop="OptTimeStr"
          :label="$t('trials:seletctedReviews:table:approvalTime')"
          min-width="150"
        />
      </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: [],
      loading: false,
      listQuery: {
        TrialId: '',
        IntoGroupSearchState: enrollState,
        PageIndex: 1,
        PageSize: 20,
        Asc: false,
        SortField: ''
      },
      total: 0,
      listLoading: false,
      approveIdArr: [],
      token: store.getters.token
    }
  },
  created() { this.initPage() },
  methods: {
    go(path) {
      window.open(path)
    },
    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() {
      // Confirm the approval?
      this.$confirm(this.$t('trials:seletctedReviews:message:msg2'), {
        type: 'warning'
      }).then(() => {
        this.loading = true
        const trialId = this.$route.query.trialId
        approveReviewer(trialId, this.approveIdArr, 1).then(res => {
          this.loading = false
          if (res.IsSuccess) {
            this.getList()
            this.$message.success(this.$t('common:message:savedSuccessfully'))
            this.$emit('nextStep', 'confirmation')
          }
        }).catch(() => {this.loading = 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>

