<template>
  <div class="glReview_wrapper">
    <el-card shadow="never" :body-style="{ padding: '10px' }" style="margin-bottom:10px">
      <h4 v-if="isReadingShowSubjectInfo">
        {{ $t('trials:auditRecord:table:subject') }}：
        <span style="font-weight:normal">{{ subjectCode }} </span>
        <span style="font-weight:normal">({{ taskBlindName }})</span>
      </h4>
    </el-card>

    <el-card :body-style="{ padding: '10px' }" shadow="never">
      <div slot="header" class="clearfix">
        <span style="font-weight: bold;">{{ $t('trials:globalReview:table:evaluationRes') }}</span>
      </div>

      <div v-if="taskList.length > 0 && readingTaskState < 2" style="text-align:right;margin:5px 0;">
        <el-button size="small" type="primary" @click="handleConfirm">
          {{ $t('common:button:confirm') }}
        </el-button>
      </div>
      <el-table
        v-loading="loading"
        :data="taskList"
      >
        <el-table-column
          prop="VisitName"
          label="访视名称"
          show-overflow-tooltip
          width="150"
        />
        <!-- 评估结果 -->
        <el-table-column
          label="评估结果"
          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">
                {{ scope.row.BeforeQuestionList.length>index?scope.row.BeforeQuestionList[index].Answer:'' }}
              </template>
            </el-table-column>
          </template>
        </el-table-column>

        <!-- 评估结果 -->
        <el-table-column
          label="调整后结果"
          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">
                {{ scope.row.AfterQuestionList.length>index?scope.row.AfterQuestionList[index].Answer:'' }}
              </template>
            </el-table-column>
          </template>
        </el-table-column>

        <el-table-column
          :label="$t('common:action:action')"
          width="200"
        >
          <template slot-scope="scope">
            <el-button
              circle
              title="查看详情"
              icon="el-icon-view"
              @click="handleView(scope.row)"
            />
            <el-button
              v-if="readingTaskState < 2"
              circle
              title="调整"
              icon="el-icon-edit"
              @click="handleEdit(scope.row)"
            />
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-card v-if="isReadingShowPreviousResults" :body-style="{ padding: '10px' }" shadow="never">
      <div slot="header" class="clearfix">
        <span style="font-weight: bold;">既往全局阅片结果</span>
      </div>
      <el-table
        v-loading="historyLoading"
        :data="historyTaskList"
      >
        <el-table-column
          prop="TaskBlindName"
          label="全局"
          show-overflow-tooltip
          width="200"
        />
        <el-table-column
          :label="$t('common:action:action')"
          width="200"
        >
          <template slot-scope="scope">
            <el-button
              circle
              title="查看详情"
              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>

    <!-- 签名框 -->
    <el-dialog
      v-if="editVisible"
      :visible.sync="editVisible"
      :close-on-click-modal="false"
      width="600px"
      custom-class="base-dialog-wrapper"
      title="调整"
    >

      <EditGlobalForm
        :global-task-id="visitTaskId"
        :visit-task-id="rowData.VisitTaskId"
        :question-list="rowData.AfterQuestionList"
        :subject-id="subjectId"
        :trial-id="trialId"
        :before-question-list="rowData.BeforeQuestionList"
        @getList="getGlInfo"
        @close="editVisible = false"
      />
    </el-dialog>
  </div>
</template>
<script>
import { getGlobalReadingInfo, getReadingPastResultList, submitGlobalReadingInfo } from '@/api/trials'
import { getToken } from '@/utils/auth'
import const_ from '@/const/sign-code'
import EditGlobalForm from './EditGlobalForm'
import SignForm from '@/views/trials/components/newSignForm'
export default {
  name: 'GlobalReview',
  components: { EditGlobalForm, 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
    }
  },
  data() {
    return {
      loading: false,
      maxLength: 2,
      evaluationQsList: [],
      adjustedQsList: [],
      taskList: [],
      historyLoading: false,
      historyTaskList: [],
      currentUser: zzSessionStorage.getItem('userName'),
      signVisible: false,
      signCode: null,
      editVisible: false,
      rowData: {},
      readingTaskState: 0
    }
  },
  mounted() {
    this.getGlInfo()
    if (this.isReadingShowPreviousResults) {
      this.getHistoryGlobalInfo()
    }
  },
  methods: {
    getGlInfo() {
      this.loading = true
      getGlobalReadingInfo({ visitTaskId: this.visitTaskId }).then(res => {
        var evaluationQsList = []
        var adjustedQsList = []
        if (res.Result.TaskList.length > 0) {
          var task = res.Result.TaskList[0]
          task.BeforeQuestionList.map(qs => {
            evaluationQsList.push(qs.QuestionName)
          })
          task.AfterQuestionList.map(qs => {
            adjustedQsList.push(qs.QuestionName)
          })
        }

        this.evaluationQsList = evaluationQsList
        this.adjustedQsList = adjustedQsList
        this.taskList = res.Result.TaskList
        this.readingTaskState = res.Result.ReadingTaskState
        this.loading = false
      }).catch(() => { this.loading = false })
    },
    getHistoryGlobalInfo() {
      this.historyLoading = true
      getReadingPastResultList({ visitTaskId: this.visitTaskId }).then(res => {
        this.historyTaskList = res.Result
        this.historyLoading = false
      }).catch(() => { this.historyLoading = false })
    },
    handleEdit(row) {
      this.rowData = { ...row }
      this.editVisible = true
    },
    handleConfirm() {
      const { ImageAssessmentReportConfirmation } = const_.processSignature
      this.signCode = ImageAssessmentReportConfirmation
      this.signVisible = true
    },
    // 关闭签名框
    closeSignDialog(isSign, signInfo) {
      if (isSign) {
        this.signConfirm(signInfo)
      } else {
        this.signVisible = false
      }
    },
    // 签名并确认
    signConfirm(signInfo) {
      this.loading = true
      var params = {
        data: {
          globalTaskId: this.visitTaskId
        },
        signInfo: signInfo
      }
      submitGlobalReadingInfo(params).then(res => {
        if (res.IsSuccess) {
          this.$message.success(this.$t('common:message:savedSuccessfully'))
          this.$refs['signForm'].btnLoading = false
          this.signVisible = false
          window.location.reload()
          window.opener.postMessage('refreshTaskList', window.location)
        }
        this.loading = false
      }).catch(_ => {
        this.loading = false
        this.$refs['signForm'].btnLoading = false
      })
    },
    handleView(row) {
      var token = getToken()
      var visitTaskId = row.VisitTaskId
      const routeData = this.$router.resolve({
        path: `/readingPage?subjectId=${this.subjectId}&trialId=${this.trialId}&visitTaskId=${visitTaskId}&TokenKey=${token}&isReadingShowPreviousResults=false`
      })
      window.open(routeData.href, '_blank')
    }
  }
}
</script>
<style lang="scss" scoped>
.glReview_wrapper{
  flex: 1;
  padding: 10px;
  width: 100%;
  height: 100%;
  overflow-y: auto;
  .box-mr{
    margin:10px 0;
  }
}
</style>
