<template>
  <BaseContainer>
    <template slot="search-container">
      <el-form :inline="true">
        <!-- 中心编号 -->
        <el-form-item :label="$t('trials:reviewTrack:table:siteCode')">
          <el-select v-model="searchData.SiteId" clearable filterable style="width:120px;">
            <el-option
              v-for="(item,index) of siteOptions"
              :key="index"
              :label="item.TrialSiteCode"
              :value="item.SiteId"
            />
          </el-select>
        </el-form-item>
        <!-- 受试者编号 -->
        <el-form-item :label="$t('trials:reviewTrack:table:subjectCode')">
          <el-input v-model="searchData.SubjectCode" style="width:100px;" />
        </el-form-item>
        <!-- 任务名称 -->
        <el-form-item style="margin-bottom:10px" :label="$t('trials:reviewTrack:table:taskName')">
          <el-input
            v-model="searchData.TaskName"
            style="width:100px;"
            clearable
          />
        </el-form-item>
        <!-- 阅片人 -->
        <el-form-item style="margin-bottom:10px" :label="$t('trials:reviewTrack:table:reader')">
          <el-select v-model="searchData.DoctorUserId" clearable style="width:120px;">
            <el-option v-for="item of DoctorUserList" :key="'DoctorUserId' + item.DoctorUserId" :value="item.DoctorUserId" :label="`${item.UserName}(${item.FullName})`" />
          </el-select>
        </el-form-item>
        <!-- 阅片标准 -->
        <el-form-item style="margin-bottom:10px" :label="$t('trials:reviewTrack:table:criterionName')">
          <el-select v-model="searchData.TrialReadingCriterionId" clearable style="width:120px;">
            <el-option v-for="item of trialCriterionList" :key="'TrialReadingCriterionId' + item.TrialReadingCriterionId" :value="item.TrialReadingCriterionId" :label="item.TrialReadingCriterionName" />
          </el-select>
        </el-form-item>
        <!-- 是否加急 -->
        <el-form-item style="margin-bottom:10px" :label="$t('trials:reviewTrack:table:isUrgent')">
          <el-select v-model="searchData.IsUrgent" clearable style="width:120px;">
            <el-option v-for="item of $d.YesOrNo" :key="'IsUrgent' + item.label" :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item>
        <!-- 任务状态 -->
        <el-form-item style="margin-bottom:10px" :label="$t('trials:reviewTrack:table:taskStatus')">
          <el-select v-model="searchData.TaskState" clearable style="width:120px;">
            <el-option v-for="item of $d.TaskState" :key="'TaskState' + item.label" :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item>
        <!-- 阅片状态 -->
        <!-- <el-form-item style="margin-bottom:10px" :label="$t('trials:reviewTrack:table:readingStatus')">
          <el-select v-model="searchData.ReadingTaskState" clearable style="width:120px;">
            <el-option v-for="item of $d.ReadingTaskState" :key="'ReadingTaskState' + item.label" :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item> -->
        <el-form-item style="margin-bottom:10px" :label="$t('trials:rereadTrack:table:ReReadingApplyState')">
          <el-select v-model="searchData.ReReadingApplyState" clearable style="width:120px;">
            <el-option v-for="item of $d.ReReadingApplyState" :key="'ReReadingApplyState' + item.label" :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item>
        <!-- 审核状态 -->
        <el-form-item style="margin-bottom:10px" :label="$t('trials:reviewTrack:table:auditStatus')">
          <el-select v-model="searchData.PIAuditState" clearable style="width:120px;">
            <el-option v-for="item of $d.PIAuditState" :key="'PIAuditState' + item.label" :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item>
        <el-form-item style="margin-bottom:10px">
          <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-adaptive="{bottomOffset:60}"
        v-loading="loading"
        :data="list"
        stripe
        height="100"
        @sort-change="handleSortChange"
      >
        <!-- 是否加急 -->
        <el-table-column
          prop="IsUrgent"
          :label="$t('trials:reviewTrack:table:isUrgent')"
          width="120"
          sortable="custom"
          align="center"
        >
          <template slot-scope="scope">
            <!-- <el-tag v-if="scope.row.IsUrgent" type="danger">{{ $fd('YesOrNo', scope.row.IsUrgent) }}</el-tag>
            <el-tag v-else type="primary">{{ $fd('YesOrNo', scope.row.IsUrgent) }}</el-tag> -->
            <el-tooltip placement="top">
              <div slot="content">
                <span>{{ `${$t('trials:pendingReview:title:urgentReason')}${$fd('TaskUrgentType',scope.row.TaskUrgentType)}` }}</span><br>
                <span>{{ scope.row.TaskUrgentRemake?`${$t('trials:pendingReview:title:urgentNote')}${scope.row.TaskUrgentRemake}`:'' }}</span>
              </div>
              <div>
                <el-button v-if="scope.row.IsUrgent" :disabled="!(!scope.row.TaskState && (scope.row.ReadingTaskState === 0 || scope.row.ReadingTaskState === 1)) || !scope.row.IsCanEditUrgentState" type="danger" size="mini" @click="changeUrgentStatus(scope.row)">{{ $fd('YesOrNo', scope.row.IsUrgent) }}</el-button>
              </div>
            </el-tooltip>
            <el-button v-if="!scope.row.IsUrgent" :disabled="!(!scope.row.TaskState && (scope.row.ReadingTaskState === 0 || scope.row.ReadingTaskState === 1)) || !scope.row.IsCanEditUrgentState" type="primary" size="mini" @click="changeUrgentStatus(scope.row)">{{ $fd('YesOrNo', scope.row.IsUrgent) }}</el-button>

          </template>
        </el-table-column>
        <!-- 任务编号 -->
        <el-table-column
          prop="TaskCode"
          :label="$t('trials:reviewTrack:table:taskCode')"
          width="120"
          sortable="custom"
          show-overflow-tooltip
        />
        <!-- 中心编号 -->
        <el-table-column
          prop="TrialSiteCode"
          :label="$t('trials:reviewTrack:table:siteCode')"
          width="100"
          sortable="custom"
          show-overflow-tooltip
        />
        <!-- 受试者编号 -->
        <el-table-column
          prop="SubjectCode"
          :label="$t('trials:reviewTrack:table:subjectCode')"
          min-width="120"
          sortable="custom"
          show-overflow-tooltip
        />
        <!-- 任务名称 -->
        <el-table-column
          prop="VisitTaskNum"
          :label="$t('trials:reviewTrack:table:taskName')"
          width="140"
          sortable="custom"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{ scope.row.TaskName }}
          </template>
        </el-table-column>
        <!-- 盲态任务标识 -->
        <el-table-column
          prop="TaskBlindName"
          :label="$t('trials:reviewTrack:table:blindName')"
          width="160"
          sortable="custom"
          show-overflow-tooltip
        />
        <!-- 任务创建时间 -->
        <el-table-column
          prop="CreateTime"
          :label="$t('trials:reviewTrack:table:createTime')"
          width="180"
          sortable="custom"
          show-overflow-tooltip
        />
        <!-- 任务状态 -->
        <el-table-column
          prop="TaskState"
          :label="$t('trials:reviewTrack:table:taskStatus')"
          width="160"
          sortable="custom"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.TaskState === 0" type="primary">{{ $fd('TaskState', scope.row.TaskState) }}</el-tag>
            <el-tag v-if="scope.row.TaskState === 1" type="info">{{ $fd('TaskState', scope.row.TaskState) }}</el-tag>
            <el-tag v-if="scope.row.TaskState === 3" type="danger">{{ $fd('TaskState', scope.row.TaskState) }}</el-tag>
            <el-tag v-if="scope.row.TaskState === 4" type="warning">{{ $fd('TaskState', scope.row.TaskState) }}</el-tag>
            <el-tag v-if="scope.row.TaskState === 5" type="danger">{{ $fd('TaskState', scope.row.TaskState) }}</el-tag>
          </template>
        </el-table-column>
        <!-- 角色 -->
        <el-table-column
          prop="ArmEnum"
          :label="$t('trials:reviewTrack:table:role')"
          width="100"
          sortable="custom"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.ArmEnum === 0" type="primary">{{ $fd('ArmEnum', scope.row.ArmEnum) }}</el-tag>
            <el-tag v-if="scope.row.ArmEnum === 1" type="primary">{{ $fd('ArmEnum', scope.row.ArmEnum) }}</el-tag>
            <el-tag v-if="scope.row.ArmEnum === 2" type="success">{{ $fd('ArmEnum', scope.row.ArmEnum) }}</el-tag>
            <el-tag v-if="scope.row.ArmEnum === 3" type="info">{{ $fd('ArmEnum', scope.row.ArmEnum) }}</el-tag>
            <el-tag v-if="scope.row.ArmEnum === 4" type="danger">{{ $fd('ArmEnum', scope.row.ArmEnum) }}</el-tag>
            <el-tag v-if="scope.row.ArmEnum === 5" type="warning">{{ $fd('ArmEnum', scope.row.ArmEnum) }}</el-tag>
          </template>
        </el-table-column>
        <!-- 阅片人 -->
        <el-table-column
          prop="UserName"
          :label="$t('trials:reviewTrack:table:reader')"
          width="130"
          sortable="custom"
          show-overflow-tooltip
        >
          <template v-if="scope.row.UserName" slot-scope="scope">
            {{ scope.row.UserName }}({{ scope.row.FullName }})
          </template>
        </el-table-column>
        <!-- 阅片标准 -->
        <el-table-column
          prop="TrialReadingCriterionName"
          :label="$t('trials:reviewTrack:table:criterionName')"
          min-width="180"
          sortable="custom"
          show-overflow-tooltip
        />
        <!-- 建议完成时间 -->
        <el-table-column
          prop="SuggesteFinishedTime"
          :label="$t('trials:reviewTrack:table:suggesteFinishedTime')"
          width="210"
          sortable="custom"
          show-overflow-tooltip
        />
        <!-- 阅片状态 -->
        <el-table-column
          prop="ReadingTaskState"
          :label="$t('trials:reviewTrack:table:readingStatus')"
          width="160"
          sortable="custom"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.ReadingTaskState === 2" type="primary">{{ $fd('ReadingTaskState', scope.row.ReadingTaskState) }}</el-tag>
            <el-tag v-else type="danger">{{ $fd('ReadingTaskState', scope.row.ReadingTaskState) }}</el-tag>
          </template>
        </el-table-column>
        <!-- 阅片完成时间 -->
        <el-table-column
          prop="SignTime"
          :label="$t('trials:reviewTrack:table:signTime')"
          min-width="180"
          sortable="custom"
          show-overflow-tooltip
        />
        <!-- 申请状态 -->
        <el-table-column
          prop="ReReadingApplyState"
          :label="$t('trials:reviewTrack:table:applyStatus')"
          min-width="140"
          sortable="custom"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.ReReadingApplyState === 4 || scope.row.ReReadingApplyState === 5" type="danger">{{ $fd('ReReadingApplyState', scope.row.ReReadingApplyState) }}</el-tag>
            <el-tag v-if="scope.row.ReReadingApplyState === 2" type="primary">{{ $fd('ReReadingApplyState', scope.row.ReReadingApplyState) }}</el-tag>
            <el-tag v-if="scope.row.ReReadingApplyState === 3" type="warning">{{ $fd('ReReadingApplyState', scope.row.ReReadingApplyState) }}</el-tag>
          </template>
        </el-table-column>
        <!-- 审核状态 -->
        <el-table-column
          prop="PIAuditState"
          :label="$t('trials:reviewTrack:table:auditStatus')"
          min-width="110"
          sortable="custom"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.PIAuditState === 0" type="danger">{{ $fd('PIAuditState', scope.row.PIAuditState) }}</el-tag>
            <el-tag v-else-if="scope.row.PIAuditState === 1" type="primary">{{ $fd('PIAuditState', scope.row.PIAuditState) }}</el-tag>
            <el-tag v-else-if="scope.row.PIAuditState === 2" type="warning">{{ $fd('PIAuditState', scope.row.PIAuditState) }}
            </el-tag>
            <span v-else>{{ $fd('PIAuditState', scope.row.PIAuditState) }}</span>
          </template>
        </el-table-column>
        <!-- 是否入组 -->
        <el-table-column
          prop="IsEnrollment"
          :label="$t('trials:auditRecord:table:isEnrollment')"
          min-width="110"
          sortable="custom"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.IsEnrollment === true" type="primary">
              {{ $fd('YesOrNo', scope.row.IsEnrollment) }}
            </el-tag>
            <el-tag v-else-if="scope.row.IsEnrollment === false" type="danger">{{ $fd('YesOrNo', scope.row.IsEnrollment) }}</el-tag>
          </template>
        </el-table-column>
        <!-- 是否PD -->
        <el-table-column
          prop="IsPDConfirm"
          :label="$t('trials:auditRecord:table:isPDConfirm')"
          min-width="110"
          sortable="custom"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.IsPDConfirm === true" type="primary">
              {{ $fd('YesOrNo', scope.row.IsPDConfirm) }}
            </el-tag>
            <el-tag v-else-if="scope.row.IsPDConfirm === false" type="danger">{{ $fd('YesOrNo', scope.row.IsPDConfirm) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          :label="$t('common:action:action')"
          width="200"
        >
          <template slot-scope="scope">
            <!-- 阅片结果 -->
            <el-button
              circle
              icon="el-icon-view"
              :title="$t('trials:auditRecord:table:readingResult')"
              @click="handleView(scope.row)"
            />
            <!-- 入组确认 -->
            <el-button
              v-if="scope.row.VisitTaskNum === 0"
              circle
              icon="el-icon-s-claim"
              :title="$t('trials:pendingReview:button:enrollmentConfirm')"
              :disabled="!(scope.row.TaskState === 0 && scope.row.PIAuditState === 2 && scope.row.IsEnrollmentConfirm && otherInfo.EnrollConfirmDefaultUserType === 10)"
              @click="handleEnrollConfirm(scope.row)"
            />
            <!-- PD确认 -->
            <el-button
              v-if="scope.row.VisitTaskNum !== 0"
              circle
              icon="el-icon-s-claim"
              :title="$t('trials:pendingReview:button:pdConfirm')"
              :disabled="!(scope.row.TaskState === 0 && scope.row.PIAuditState === 2 && scope.row.PDState && otherInfo.PDProgressDefaultUserType === 10)"
              @click="handlePDConfirm(scope.row)"
            />
          </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="enrollVisible"
      :visible.sync="enrollVisible"
      :close-on-click-modal="false"
      custom-class="base-dialog-wrapper"
      :title="`${$t('trials:pendingReview:button:enrollmentConfirm')}（${currentRow.SubjectCode} | ${currentRow.TaskName} | ${currentRow.UserName}）`"
      width="400px"
    >
      <EnrollForm
        :row-data="currentRow"
        @viewReaingResult="viewReaingResult"
        @getList="getList"
        @close="enrollVisible=false"
      />
    </el-dialog>
    <!-- PD确认 -->
    <el-dialog
      v-if="pdVisible"
      :visible.sync="pdVisible"
      :close-on-click-modal="false"
      custom-class="base-dialog-wrapper"
      :title="`${$t('trials:pendingReview:button:pdConfirm')}（${currentRow.SubjectCode} | ${currentRow.TaskName} | ${currentRow.UserName}）`"
      width="400px"
    >
      <PdForm
        :row-data="currentRow"
        @viewReaingResult="viewReaingResult"
        @getList="getList"
        @close="pdVisible=false"
      />
    </el-dialog>
  </BaseContainer>
</template>
<script>
import { getPIReadingAuditList } from '@/api/reading'
import { getTrialCriterionList, getDoctorUserSelectList } from '@/api/trials/reading'
import { getTrialSiteSelect } from '@/api/trials'
import { getToken } from '@/utils/auth'
import BaseContainer from '@/components/BaseContainer'
import Pagination from '@/components/Pagination'
import EnrollForm from '@/views/trials/trials-panel/reading/pending-review/components/EnrollForm'
import PdForm from '@/views/trials/trials-panel/reading/pending-review/components/PdForm'
const searchDataDefault = () => {
  return {
    PageIndex: 1,
    PageSize: 20,
    Asc: true,
    SortField: '',
    TrialId: null,
    SiteId: null,
    SubjectId: null,
    SubjectCode: null,
    IsUrgent: null,
    TaskName: null,
    TaskState: null,
    DoctorUserId: null,
    ReadingTaskState: 2,
    PIAuditState: null,
    IsWaitPIAudit: false
  }
}
export default {
  name: 'Confirmation',
  components: { BaseContainer, Pagination, EnrollForm, PdForm },
  data() {
    return {
      searchData: searchDataDefault(),
      list: [],
      total: 0,
      loading: false,
      userListLoading: '',
      trialId: '',
      trialCriterionList: [],
      siteOptions: [],
      DoctorUserList: [],
      enrollVisible: false,
      pdVisible: false,
      currentRow: {},
      otherInfo: {},
      openWindow: null
    }
  },
  mounted() {
    this.trialId = this.$route.query.trialId
    this.getTrialCriterionList()
    this.getList()
    this.getSite()
    this.getDoctorUserSelectList()
  },
  methods: {
    getList() {
      this.loading = true
      this.searchData.TrialId = this.trialId
      getPIReadingAuditList(this.searchData).then(res => {
        this.loading = false
        this.list = res.Result.CurrentPageData
        this.total = res.Result.TotalCount
        this.otherInfo = res.OtherInfo
      }).catch(() => { this.loading = false })
    },
    // 入组确认
    handleEnrollConfirm(row) {
      this.handleView(row)
      this.currentRow = { ...row }
      this.enrollVisible = true
    },
    // PD确认
    handlePDConfirm(row) {
      this.handleView(row)
      this.currentRow = { ...row }
      this.pdVisible = true
    },
    viewReaingResult() {
      if (!this.currentRow) return
      this.handleView(this.currentRow)
    },
    // 查看阅片结果
    handleView(row) {
      if (this.openWindow) {
        this.openWindow.close()
      }
      var token = getToken()
      var path
      if (row.ReadingTool === 0) {
        path = `/readingDicoms?TrialReadingCriterionId=${row.TrialReadingCriterionId}&trialId=${this.trialId}&subjectCode=${row.SubjectCode}&subjectId=${row.SubjectId}&visitTaskId=${row.Id}&isReadingTaskViewInOrder=${row.IsReadingTaskViewInOrder}&criterionType=${row.CriterionType}&readingTool=${row.ReadingTool}&TokenKey=${token}`
      } else {
        path = `/noneDicomReading?TrialReadingCriterionId=${row.TrialReadingCriterionId}&trialId=${this.trialId}&subjectCode=${row.SubjectCode}&subjectId=${row.SubjectId}&visitTaskId=${row.Id}&isReadingTaskViewInOrder=${row.IsReadingTaskViewInOrder}&criterionType=${row.CriterionType}&readingTool=${row.ReadingTool}&TokenKey=${token}`
      }
      this.openWindow = window.open(path, '_blank')
    },
    getTrialCriterionList() {
      getTrialCriterionList(this.trialId).then(res => {
        this.trialCriterionList = res.Result
      }).catch(() => {})
    },
    getDoctorUserSelectList() {
      getDoctorUserSelectList(this.$route.query.trialId).then(res => {
        this.DoctorUserList = res.Result
      })
    },
    getSite() {
      getTrialSiteSelect(this.trialId).then(res => {
        this.siteOptions = res.Result
      })
    },
    handleSearch() {
      this.searchData.PageIndex = 1
      this.getList()
    },
    handleReset() {
      this.searchData = searchDataDefault()
      this.getList()
    },
    // 排序
    handleSortChange(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>
