<template>
  <div class="glReview_wrapper">
    <!--    <el-card v-if="isReadingShowSubjectInfo" shadow="never" :body-style="{ padding: '10px' }" style="margin-bottom:10px">-->
    <!--      <h4>-->
    <!--        &lt;!&ndash; 受试者： &ndash;&gt;-->
    <!--        {{ $t('trials:globalReview:title:subject') }}-->
    <!--        <span style="font-weight:normal">{{ subjectCode }} </span>-->
    <!--        <span style="font-weight:normal">({{ taskBlindName }})</span>-->
    <!--      </h4>-->
    <!--    </el-card>-->

    <el-card v-loading="loading" :body-style="{ padding: '10px' }" shadow="never">
      <div slot="header" style="display: flex;flex-direction: row;justify-content: space-between;">
        <!-- 访视评估结果 -->
        <div style="font-weight: bold;">
          {{ $t('trials:globalReview:title:result') }}
          <span v-if="isReadingShowSubjectInfo">
            （{{ subjectCode }} | {{ taskBlindName }}）
          </span>
        </div>
        <div v-if="isExistsClinicalData">
          <!-- 临床数据 -->
          <el-button type="text" @click="previewCD">{{ $t('trials:globalReview:title:clinicalData') }}</el-button>
        </div>
      </div>

      <div v-if=" readingTaskState < 2" style="text-align:right;margin:5px 0;">
        <el-button
            type="primary"
            size="small"
            @click="skipTask"
          >
            <!-- 跳过 -->
            {{ $t('trials:readingReport:button:skip') }}
          </el-button>
        <el-button size="small" type="primary" @click="handleSave">
          {{ $t('common:button:save') }}
        </el-button>
        <!-- 提交 -->
        <el-button size="small" type="primary" @click="handleConfirm">
          {{ $t('trials:globalReview:button:submit') }}
        </el-button>
      </div>
      <CustomizeTable
        v-if="CriterionType === 0 && Object.keys(globalInfo).length !== 0"
        ref="globalTbl"
        :global-info="globalInfo"
        :global-form="globalInfo.globalForm"
        :reading-task-state="readingTaskState"
        @getGlInfo="getGlInfo"
        @setOpenWindow="setOpenWindow"
      />
      <RecistTable
        v-if="CriterionType === 1 && Object.keys(globalInfo).length !== 0"
        ref="globalTbl"
        :global-info="globalInfo"
        :global-form="globalInfo.globalForm"
        :reading-task-state="readingTaskState"
        @getGlInfo="getGlInfo"
        @setOpenWindow="setOpenWindow"
      />
      <LuganoTable
        v-if="CriterionType === 2 && Object.keys(globalInfo).length !== 0"
        ref="globalTbl"
        :global-info="globalInfo"
        :global-form="globalInfo.globalForm"
        :reading-task-state="readingTaskState"
        @getGlInfo="getGlInfo"
        @handleView="handleView"
        @setOpenWindow="setOpenWindow"
      />
      <PCWG3Table
        v-if="CriterionType === 10 && Object.keys(globalInfo).length !== 0"
        ref="globalTbl"
        :global-info="globalInfo"
        :global-form="globalInfo.globalForm"
        :reading-task-state="readingTaskState"
        @getGlInfo="getGlInfo"
        @handleView="handleView"
        @setOpenWindow="setOpenWindow"
      />
      <IRecistTable
        v-if="CriterionType === 3 && Object.keys(globalInfo).length !== 0"
        ref="globalTbl"
        :global-info="globalInfo"
        :global-form="globalInfo.globalForm"
        :reading-task-state="readingTaskState"
        @getGlInfo="getGlInfo"
        @setOpenWindow="setOpenWindow"
      />
      <RecistBMTable
        v-if="CriterionType === 17 && Object.keys(globalInfo).length !== 0"
        ref="globalTbl"
        :global-info="globalInfo"
        :global-form="globalInfo.globalForm"
        :reading-task-state="readingTaskState"
        @getGlInfo="getGlInfo"
        @setOpenWindow="setOpenWindow"
      />

    </el-card>

    <el-card v-if="isReadingShowPreviousResults" :body-style="{ padding: '10px' }" shadow="never">
      <div slot="header" class="clearfix">
        <!-- 既往全局阅片结果 -->
        <span style="font-weight: bold;">{{ $t('trials:globalReview:title:previousRes') }}</span>
      </div>
      <el-table
        v-loading="historyLoading"
        :data="historyTaskList"
      >
        <!-- 全局 -->
        <el-table-column
          prop="TaskBlindName"
          :label="$t('trials:globalReview:table:globalReading')"
          show-overflow-tooltip
          width="200"
        />
        <el-table-column
          prop="VisitBlindName"
          :label="$t('trials:globalReview:table:cutOffVisitName')"
          show-overflow-tooltip
          width="200"
        />
        <el-table-column
          :label="$t('common:action:action')"
          width="200"
        >
          <template slot-scope="scope">
            <!-- 查看详情 -->
            <el-button
              circle
              :title="$t('trials:globalReview:table:view')"
              icon="el-icon-view"
              @click="handleView(scope.row)"
            />
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 签名框 -->
    <el-dialog
      v-if="signVisible"
      :visible.sync="signVisible"
      :close-on-click-modal="false"
      width="600px"
      custom-class="base-dialog-wrapper"
    >
      <div slot="title">
        <span style="font-size:18px;">{{ $t('common:dialogTitle:sign') }}</span>
        <span style="font-size:12px;margin-left:5px">{{ `(${$t('common:label:sign')}${ currentUser })` }}</span>
      </div>
      <SignForm ref="signForm" :sign-code-enum="signCode" @closeDialog="closeSignDialog" />
    </el-dialog>
  </div>
</template>
<script>
import { getGlobalReadingInfo, getReadingPastResultList, submitGlobalReadingInfo, saveGlobalReadingInfo } from '@/api/trials'
import { getAutoCutNextTask } from '@/api/user'
import { setSkipReadingCache } from '@/api/reading'
import { getToken } from '@/utils/auth'
import const_ from '@/const/sign-code'
import DicomEvent from '@/views/trials/trials-panel/reading/dicoms/components/DicomEvent'
import CustomizeTable from './components/CustomizeTable'
import RecistTable from './components/RecistTable'
import PCWG3Table from './components/PCWG3Table'
import IRecistTable from './components/IRecistTable'
import RecistBMTable from './components/RecistBMTable'
import LuganoTable from './components/LuganoTable'
import SignForm from '@/views/trials/components/newSignForm'
import { changeURLStatic } from '@/utils/history.js'
import store from '@/store'
export default {
  name: 'GlobalReview',
  components: { CustomizeTable, RecistTable, PCWG3Table, IRecistTable, RecistBMTable,LuganoTable, SignForm },
  props: {
    trialId: {
      type: String,
      required: true
    },
    subjectId: {
      type: String,
      required: true
    },
    visitTaskId: {
      type: String,
      required: true
    },
    readingCategory: {
      type: Number,
      required: true
    },
    subjectCode: {
      type: String,
      required: true
    },
    taskBlindName: {
      type: String,
      required: true
    },
    isReadingShowSubjectInfo: {
      type: Boolean,
      required: true
    },
    isReadingShowPreviousResults: {
      type: Boolean,
      required: true
    },
    isExistsClinicalData: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      loading: false,
      maxLength: 2,
      evaluationQsList: [],
      adjustedQsList: [],
      agreeOrNotList: [],
      taskList: [],
      historyLoading: false,
      historyTaskList: [],
      currentUser: zzSessionStorage.getItem('userName'),
      signVisible: false,
      signCode: null,
      rowData: {},
      readingTaskState: 0,
      globalUpdateType: [],
      assessTypeList: [],
      CriterionType: 0,
      globalInfo: {},
      openWindow: null
    }
  },
  // watch: {
  //   visitTaskId: {
  //     immediate: true,
  //     handler(val) {
  //       this.$nextTick(() => {
  //         this.getGlInfo()
  //         if (this.isReadingShowPreviousResults) {
  //           this.getHistoryGlobalInfo()
  //         }
  //       })
  //     }
  //   }
  // },
  mounted() {
    this.CriterionType = parseInt(this.$route.query.criterionType)
    this.getGlInfo()
    if (this.isReadingShowPreviousResults) {
      this.getHistoryGlobalInfo()
    }
    DicomEvent.$on('resetOpenWindow', () => {
      if (this.openWindow) {
        this.openWindow.close()
      }
    })
  },
  beforeDestroy() {
    DicomEvent.$off('resetOpenWindow')
    if (this.openWindow) {
      this.openWindow.close()
    }
  },
  methods: {
    async getGlInfo() {
      this.loading = true
      try {
        let res = await getGlobalReadingInfo({ visitTaskId: this.visitTaskId })
        var evaluationQsList = []
        var adjustedQsList = []
        var agreeOrNotList = []
        var globalForm = {}
        if (res.Result.TaskList.length > 0) {
          res.Result.TaskList.map((qs, index) => {
            if (index === 0) {
              qs.BeforeQuestionList.map(qs => {
                evaluationQsList.push(qs.QuestionName)
              })
              qs.AfterQuestionList.map(qs => {
                console.log(qs)
                if (qs.GlobalAnswerType === 1 && (this.CriterionType === 10)) {
                  // 访视阅片备注
                  adjustedQsList.push({questionName:this.$t('trials:globalReview:table:visitRemark'),isShow:true})
                } else if (qs.GlobalAnswerType === 1 && (this.CriterionType !== 10)) {
                  // 全局阅片备注
                  adjustedQsList.push({questionName:this.$t('trials:globalReview:table:globalRemark'),isShow:true})
                } else if (qs.GlobalAnswerType === 3 && (this.CriterionType !== 10)) {
                  // 评估更新类型
                  adjustedQsList.push({questionName:this.$t('trials:globalReview:table:updateType'),isShow:true})
                } else {
                  var isShow = true
                  if(this.CriterionType === 2 && (qs.QuestionType === 39 || qs.QuestionType === 40 || qs.QuestionType === 41)){
                    isShow = false
                  }
                  adjustedQsList.push({questionName:qs.QuestionName,isShow:isShow})
                }
              })
              qs.AgreeOrNot.map(qs => {
                agreeOrNotList.push(qs.QuestionName)
              })
            }
            qs.AgreeOrNot.map(qs => {
              var answer = qs.Answer
              this.$set(globalForm, `${index}${qs.GlobalAnswerType}`, answer)
            })
            qs.AfterQuestionList.map(q => {
              if (q.Answer !== q.VisitAnswer) {
                this.$set(q, 'isChange', true)
              }
              var answer = q.Answer

              if (!q.QuestionId) {
                this.$set(globalForm, `${index}${q.GlobalAnswerType}`, answer)
              } else {
                this.$set(globalForm, `${index}${q.QuestionId}`, answer)
              }
            })
            if (this.CriterionType === 1 || this.CriterionType === 3 || this.CriterionType === 17 || this.CriterionType === 0) {
              this.$set(globalForm, `${index}${qs.AgreeOrNot[0].GlobalAnswerType}`, qs.AgreeOrNot[0].Answer)
            }
          })
        }
        var globalUpdateType = res.Result.GlobalUpdateType ? res.Result.GlobalUpdateType.split('|') : []

        this.readingTaskState = res.Result.ReadingTaskState
        globalForm.taskList = res.Result.TaskList
        this.globalInfo = {
          trialId: this.trialId, subjectId: this.subjectId, subjectCode: this.subjectCode, visitTaskId: res.Result.GlobalTaskId, globalForm, globalUpdateType, evaluationQsList, adjustedQsList, agreeOrNotList, readingTaskState: this.readingTaskState, assessTypeList: res.Result.AssessTypeList
        }
        this.loading = false
      } catch (e) {
        this.loading = false
      }
    },
    async getHistoryGlobalInfo() {
      this.historyLoading = true
      try {
        let res = await getReadingPastResultList({ visitTaskId: this.visitTaskId })
        this.historyTaskList = res.Result
        this.historyLoading = false
      } catch (e) {
        this.historyLoading = false 
      }
    },
    handleSave() {
      this.$refs['globalTbl'].handleSave(true)
    },
    async skipTask() {
      try {
        // 是否确认跳过？
        const confirm = await this.$confirm(
          this.$t('trials:readingReport:message:skipConfirm'),
          {
            type: 'warning',
            distinguishCancelAndClose: true
          }
        )
        if (confirm !== 'confirm') return
        this.loading = true
        const res = await setSkipReadingCache({ visitTaskId: this.visitTaskId })
        this.loading = false
        if (res.IsSuccess) {
          window.location.reload()
        }
      } catch (e) {
        this.loading = false
        console.log(e)
      }
    },
    async handleConfirm() {
      // 判断是否存在访视评估结果未确认
      var idx = this.taskList.findIndex(i => !i.AgreeOrNotAnswer)
      if (idx > -1) {
        // 存在访视评估结果未确认！
        this.$confirm(this.$t('trials:globalReview:message:msg1'), {
          type: 'warning',
          distinguishCancelAndClose: true
        })
          .then(() => {})
          .catch(() => {})
      } else {
        // 自动保存
        await this.$refs['globalTbl'].handleSave(false)
        const { ImageAssessmentReportConfirmation } = const_.processSignature
        this.signCode = ImageAssessmentReportConfirmation
        this.signVisible = true
      }
    },
    // 关闭签名框
    closeSignDialog(isSign, signInfo) {
      if (isSign) {
        this.signConfirm(signInfo)
      } else {
        this.signVisible = false
      }
    },
    // 签名并确认
    async signConfirm(signInfo) {
      this.loading = true
      var params = {
        data: {
          globalTaskId: this.visitTaskId
        },
        signInfo: signInfo
      }
      try{
        let res = await submitGlobalReadingInfo(params)
        if (res.IsSuccess) {
          this.$message.success(this.$t('common:message:savedSuccessfully'))
          this.$refs['signForm'].btnLoading = false
          this.signVisible = false
          // 设置当前任务阅片状态为已读
          this.readingTaskState = 2
          const res = await getAutoCutNextTask()
          var isAutoTask = res.Result.AutoCutNextTask
          if (isAutoTask) {
            // DicomEvent.$emit('getNextTask')
            window.location.reload()
          } else {
            // 当前阅片任务已完成，是否进入下一个阅片任务
            const confirm = await this.$confirm(
              this.$t('trials:globalReview:message:msg2'),
              {
                type: 'warning',
                distinguishCancelAndClose: true
              }
            )
            if (confirm === 'confirm') {
              // store.dispatch('reading/resetVisitTasks')
              // DicomEvent.$emit('getNextTask')
              window.location.reload()
            } else {
              changeURLStatic('visitTaskId', this.visitTaskId)
            }
          }
          window.opener.postMessage('refreshTaskList', window.location)
        }
        this.loading = false
      } catch (e) {
        this.loading = false
        this.$refs['signForm'].btnLoading = false
      }
    },
    handleView(row) {
      if (this.openWindow) {
        this.openWindow.close()
      }
      try {
        var token = getToken()
        var visitTaskId = row.VisitTaskId
        var trialReadingCriterionId = this.$router.currentRoute.query.TrialReadingCriterionId
        var criterionType = this.$router.currentRoute.query.criterionType
        var readingTool = this.$router.currentRoute.query.readingTool
        readingTool = isNaN(parseInt(readingTool)) ? null : parseInt(readingTool)
        var isReadingTaskViewInOrder = this.$router.currentRoute.query.isReadingTaskViewInOrder
        var path = ''
        if (readingTool === 0) {
          path = `/readingDicoms?TrialReadingCriterionId=${trialReadingCriterionId}&trialId=${this.trialId}&subjectCode=${this.subjectCode}&subjectId=${this.subjectId}&visitTaskId=${visitTaskId}&isReadingTaskViewInOrder=${isReadingTaskViewInOrder}&criterionType=${criterionType}&readingTool=${readingTool}&TokenKey=${token}`
        } else {
          path = `/noneDicomReading?TrialReadingCriterionId=${trialReadingCriterionId}&trialId=${this.trialId}&subjectCode=${this.subjectCode}&subjectId=${this.subjectId}&visitTaskId=${visitTaskId}&isReadingTaskViewInOrder=${isReadingTaskViewInOrder}&criterionType=${criterionType}&readingTool=${readingTool}&TokenKey=${token}`
        }
        const routeData = this.$router.resolve({ path })
        this.openWindow = window.open(routeData.href, '_blank')
      } catch (e) {
        console.log(e)
      }
    },

    previewCD() {
      var token = getToken()
      const routeData = this.$router.resolve({
        path: `/clinicalData?subjectId=${this.subjectId}&trialId=${this.trialId}&visitTaskId=${this.visitTaskId}&TokenKey=${token}`
      })

      window.open(routeData.href, '_blank')
    },
    setOpenWindow(window) {
      if (this.openWindow) {
        this.openWindow.close()
      }
      this.openWindow = window
    }
  }
}
</script>
<style lang="scss" scoped>
.glReview_wrapper{
  flex: 1;
  padding: 10px;
  width: 100%;
  height: 100%;
  overflow-y: auto;
  .box-mr{
    margin:10px 0;
  }
}
</style>
