<template>
  <div>
    <div style="text-align:right">

      <el-button v-if="auditInfo.IsExistsClinicalData" type="text" @click="previewCD">
        {{ $t('trials:adReview:title:clinicalData') }}
      </el-button>
      <el-button v-if="auditInfo.GlobalInfo.OtherGlobalTaskId" type="text" @click="handleView(auditInfo.GlobalInfo.OtherGlobalTaskId)">{{ $t('trials:medicalFeedback:title:otherReader') }}</el-button>

      <!-- 关联裁判任务 -->
      <el-button v-if="auditInfo.GlobalInfo.JudgeTaskName" type="text" @click="handleView(auditInfo.GlobalInfo.JudgeTaskId)">{{ auditInfo.GlobalInfo.JudgeTaskName }}</el-button>
    </div>

    <el-table
      v-if="rowData.CriterionType === 10"
      :data="taskList"
    >
      <el-table-column
        prop="BlindName"
        :label="$t('trials:medicalFeedback:table:taskBlindName')"
        show-overflow-tooltip
        width="150"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleView(scope.row.VisitTaskId)"
          >
            {{ scope.row.BlindName }}
          </el-button>
        </template>
      </el-table-column>
      <!-- 访视点计数 -->
      <el-table-column
        :label="$t('trials:medicalFeedback:table:visitPointCount')"
        align="center"
        prop=""
        show-overflow-tooltip
      >
        <template>
          <el-table-column
            v-for="(qs,index) in evaluationQsList"
            :key="index"
            prop=""
            :label="qs"
            show-overflow-tooltip
            width="150"
          >
            <template slot-scope="scope">
              <div v-if="scope.row.BeforeQuestionList.length>index && scope.row.BeforeQuestionList[index].Answer">
                <span v-if="scope.row.BeforeQuestionList[index].DictionaryCode">
                  {{ $fd(scope.row.BeforeQuestionList[index].DictionaryCode,parseInt(scope.row.BeforeQuestionList[index].Answer)) }}
                </span>
                <span v-else-if="scope.row.BeforeQuestionList[index].QuestionType === 22">{{ scope.row.BeforeQuestionList[index].Answer === '-1' ? $t('trials:medicalFeedback:table:unKnow') : scope.row.BeforeQuestionList[index].Answer }}</span>
                <span v-else>{{ scope.row.BeforeQuestionList[index].Answer }}</span>
              </div>
            </template>
          </el-table-column>
        </template>
      </el-table-column>

      <!-- 是否同意访视结果 -->
      <el-table-column
        v-for="(qs, index) in agreeOrNotList"
        :key="index"
        prop=""
        :label="$t('trials:medicalFeedback:table:agreeToVisitPointCount')"
        show-overflow-tooltip
        width="170"
      >
        <template slot-scope="scope">
          <el-tag v-if="parseInt(scope.row.AgreeOrNot[0].Answer) === 1" type="primary">
            {{ $fd('ReadingYesOrNo',parseInt(scope.row.AgreeOrNot[0].Answer)) }}
          </el-tag>
          <el-tag v-else-if="parseInt(scope.row.AgreeOrNot[0].Answer) === 0" type="danger"> {{ $fd('ReadingYesOrNo',parseInt(scope.row.AgreeOrNot[0].Answer)) }}</el-tag>
          <span v-else />
        </template>
      </el-table-column>

      <!-- 评估结果 -->
      <el-table-column
        :label="$t('trials:medicalFeedback:table:assessmentResults')"
        align="center"
        prop=""
        show-overflow-tooltip
      >
        <template>
          <el-table-column
            v-for="(qs,index) in adjustedQsList"
            :key="index"
            prop=""
            :label="qs"
            show-overflow-tooltip
            width="150"
          >
            <template slot-scope="scope">
              <!-- {{ scope.row.AfterQuestionList.length>index?scope.row.AfterQuestionList[index].Answer:'' }} -->
              <div v-if="scope.row.AfterQuestionList.length>index && scope.row.AfterQuestionList[index].Answer">

                <span v-if="scope.row.AfterQuestionList[index].GlobalAnswerType === 3">
                  {{ getAssessType(scope.row.AfterQuestionList[index].Answer) }}
                </span>
                <span v-else-if="scope.row.AfterQuestionList[index].DictionaryCode">
                  {{ $fd(scope.row.AfterQuestionList[index].DictionaryCode,parseInt(scope.row.AfterQuestionList[index].Answer)) }}
                </span>
                <span v-else>{{ scope.row.AfterQuestionList[index].Answer }}</span>
              </div>
            </template>
          </el-table-column>
        </template>
      </el-table-column>

      <el-table-column
        :label="$t('common:action:action')"
        width="100"
      >
        <template slot-scope="scope">
          <el-button
            circle
            :title="$t('trials:medicalFeedback:table:view')"
            icon="el-icon-view"
            @click="handleView(scope.row.VisitTaskId)"
          />
        </template>
      </el-table-column>
    </el-table>

    <el-table
      v-else
      :data="taskList"
    >
      <!-- 访视名称 -->
      <el-table-column
        prop="BlindName"
        :label="$t('trials:globalReview:table:visitName')"
        show-overflow-tooltip
        width="150"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleView(scope.row.VisitTaskId)"
          >
            {{ scope.row.BlindName }}
          </el-button>
        </template>
      </el-table-column>
      <!-- 评估结果 -->
      <el-table-column
        :label="$t('trials:globalReview:table:evaluationRes')"
        align="center"
        prop=""
      >
        <template>
          <el-table-column
            v-for="(qs,index) in evaluationQsList"
            :key="index"
            prop=""
            :label="qs"
            show-overflow-tooltip
            width="150"
          >
            <template slot-scope="scope">
              <div v-if="scope.row.BeforeQuestionList.length>index && scope.row.BeforeQuestionList[index].Answer">
                <span v-if="scope.row.BeforeQuestionList[index].DictionaryCode">
                  {{ $fd(scope.row.BeforeQuestionList[index].DictionaryCode,parseInt(scope.row.BeforeQuestionList[index].Answer)) }}
                </span>
                <span v-else>{{ scope.row.BeforeQuestionList[index].Answer }}</span>
              </div>
            </template>
          </el-table-column>
        </template>
      </el-table-column>
      <!-- 是否同意访视结果 -->
      <el-table-column
        v-for="(qs,index) in agreeOrNotList"
        :key="index"
        prop=""
        :label="$t('trials:globalReview:table:isAgreeEvaluationRes')"
        show-overflow-tooltip
        width="170"
      >
        <template slot-scope="scope">
          <el-tag v-if="parseInt(scope.row.AgreeOrNot[0].Answer) === 1" type="primary">
            {{ $fd('ReadingYesOrNo',parseInt(scope.row.AgreeOrNot[0].Answer)) }}
          </el-tag>
          <el-tag v-else-if="parseInt(scope.row.AgreeOrNot[0].Answer) === 0" type="danger"> {{ $fd('ReadingYesOrNo',parseInt(scope.row.AgreeOrNot[0].Answer)) }}</el-tag>
          <span v-else />
        </template>
      </el-table-column>

      <!-- 调整后结果 -->
      <el-table-column
        :label="$t('trials:globalReview:table:adjustedRes')"
        align="center"
        prop=""
      >
        <template>
          <el-table-column
            v-for="(qs,index) in adjustedQsList"
            :key="index"
            prop=""
            :label="qs"
            show-overflow-tooltip
            width="150"
          >
            <template slot-scope="scope">
              <div v-if="scope.row.AfterQuestionList.length>index && scope.row.AfterQuestionList[index].Answer">

                <span v-if="scope.row.AfterQuestionList[index].GlobalAnswerType === 3">
                  {{ getAssessType(scope.row.AfterQuestionList[index].Answer) }}
                </span>
                <span v-else-if="scope.row.AfterQuestionList[index].DictionaryCode">
                  {{ $fd(scope.row.AfterQuestionList[index].DictionaryCode,parseInt(scope.row.AfterQuestionList[index].Answer)) }}
                </span>
                <span v-else>{{ scope.row.AfterQuestionList[index].Answer }}</span>
              </div>
            </template>
          </el-table-column>
        </template>
      </el-table-column>

      <el-table-column
        :label="$t('common:action:action')"
        width="120"
        :fixed="adjustedQsList.length > 4 ? 'right' : false"
      >
        <template slot-scope="scope">
          <el-button
            circle
            :title="$t('trials:globalReview:table:view')"
            icon="el-icon-view"
            @click="handleView(scope.row.VisitTaskId)"
          />
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import { getToken } from '@/utils/auth'
export default {
  name: 'GReview',
  props: {
    auditInfo: {
      type: Object,
      default() {
        return {}
      }
    },
    rowData: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      taskList: [],
      evaluationQsList: [],
      agreeOrNotList: [],
      adjustedQsList: [],
      judgeQuestion: []
    }
  },
  mounted() {
    this.initPage()
  },
  methods: {
    initPage() {
      var evaluationQsList = []
      var adjustedQsList = []
      var agreeOrNotList = []
      if (this.auditInfo.GlobalInfo.TaskList.length > 0) {
        var task = this.auditInfo.GlobalInfo.TaskList[0]
        task.BeforeQuestionList.map(qs => {
          evaluationQsList.push(qs.QuestionName)
        })
        task.AfterQuestionList.map(qs => {
          if (qs.GlobalAnswerType === 1 && this.CriterionType === 10) {
            // 访视阅片备注
            adjustedQsList.push(this.$t('trials:globalReview:table:visitRemark'))
          } else if (qs.GlobalAnswerType === 1 && this.CriterionType !== 10) {
            // 全局阅片备注
            adjustedQsList.push(this.$t('trials:globalReview:table:globalRemark'))
          } else if (qs.GlobalAnswerType === 3 && this.CriterionType !== 10) {
            // 评估更新类型
            adjustedQsList.push(this.$t('trials:globalReview:table:updateType'))
          } else {
            adjustedQsList.push(qs.QuestionName)
          }
        })
        task.AgreeOrNot.map(qs => {
          agreeOrNotList.push(qs.QuestionName)
        })
      }
      this.globalUpdateType = this.auditInfo.GlobalInfo.GlobalUpdateType ? this.auditInfo.GlobalInfo.GlobalUpdateType.split('|') : []
      this.evaluationQsList = evaluationQsList
      this.adjustedQsList = adjustedQsList
      this.agreeOrNotList = agreeOrNotList

      this.taskList = this.auditInfo.GlobalInfo.TaskList
      this.assessTypeList = this.auditInfo.GlobalInfo.AssessTypeList
    },
    getAssessType(code) {
      var idx = this.assessTypeList.findIndex(item => item.Code === code)
      if (idx > -1) {
        return this.assessTypeList[idx].ValueCN
      } else {
        return ''
      }
    },
    handleView(visitTaskId) {
      var token = getToken()
      var trialId = this.$route.query.trialId
      var criterionType = this.rowData.CriterionType
      var readingTool = this.rowData.ReadingTool
      readingTool = isNaN(parseInt(readingTool)) ? null : parseInt(readingTool)
      var isReadingTaskViewInOrder = this.rowData.IsReadingTaskViewInOrder
      var trialReadingCriterionId = this.rowData.TrialReadingCriterionId
      var path = ''
      if (readingTool === 0) {
        path = `/readingDicoms?TrialReadingCriterionId=${trialReadingCriterionId}&trialId=${trialId}&subjectCode=${this.rowData.SubjectCode}&subjectId=${this.rowData.SubjectId}&visitTaskId=${visitTaskId}&isReadingTaskViewInOrder=${isReadingTaskViewInOrder}&criterionType=${criterionType}&readingTool=${readingTool}&TokenKey=${token}&key=${new Date().getTime()}`
      } else {
        path = `/noneDicomReading?TrialReadingCriterionId=${trialReadingCriterionId}&trialId=${trialId}&subjectCode=${this.rowData.SubjectCode}&subjectId=${this.rowData.SubjectId}&visitTaskId=${visitTaskId}&isReadingTaskViewInOrder=${isReadingTaskViewInOrder}&criterionType=${criterionType}&readingTool=${readingTool}&TokenKey=${token}&key=${new Date().getTime()}`
      }
      const routeData = this.$router.resolve({ path })
      window.open(routeData.href, '_blank')
    },

    previewCD() {
      var token = getToken()
      var trialId = this.$route.query.trialId
      const routeData = this.$router.resolve({
        path: `/clinicalData?subjectId=${this.auditInfo.SubjectId}&trialId=${trialId}&visitTaskId=${this.auditInfo.VisitTaskId}&TokenKey=${token}`
      })
      window.open(routeData.href, '_blank')
    }
  }
}
</script>
