<template>
  <div class="measurement-wrapper" :style="{'height':height+10+'px'}" style="position: relative">

    <div class="container" :style="{'height':height+'px'}" style="padding-bottom: 50px;overflow-y: auto;">
      <h3 style="color: #ddd;padding: 5px 0px;margin: 0;" v-if="isReadingShowSubjectInfo">
        <span v-if="subjectCode">{{ subjectCode }} </span>
        <span style="margin-left:5px;">{{ taskBlindName }}</span>
      </h3>
      <QuestionsPreview
        ref="QuestionsPreview"
        v-if="ecrfShow"
        :visitTaskId="visitTaskId"
        :criterionId="TrialReadingCriterionId">
      </QuestionsPreview>
    </div>
    <div style="position: absolute;bottom:0;left: 0;z-index: 10;background: #000;width: 100%;display: flex;justify-content: right;align-items: center;height: 50px">
      <el-button size="mini" v-if="readingTaskState<2" style="margin-right: 10px" @click="handleSave(true)">
        保存
      </el-button>
      <el-button size="mini" style="margin-right: 10px"  v-if="readingTaskState<2 && IseCRFShowInDicomReading" @click="handleConfirm">提交</el-button>
    </div>
    <!-- 签名框 -->
    <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 { submitDicomVisitTask } from '@/api/trials'
import DicomEvent from '../components/DicomEvent'
import SignForm from '@/views/trials/components/newSignForm'
import QuestionsPreview from './CustomizeQuestionsPreview'
import const_ from '@/const/sign-code'
import store from '@/store'
import { mapGetters } from 'vuex'
export default {
  name: 'CustomizeMeasurementList',
  components: {
    QuestionsPreview, SignForm
  },
  props: {
    isReadingShowSubjectInfo: {
      type: Boolean,
      required: true
    },
    IseCRFShowInDicomReading: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      currentUser: zzSessionStorage.getItem('userName'),
      height: window.innerHeight - 140,
      visitTaskId: '',
      loading: false,
      CriterionType: null,
      subjectCode: '',
      taskBlindName: '',
      TrialReadingCriterionId: null,
      ecrfShow: false,
      signCode: null,
      signVisible: false,
      readingTaskState: null
    }
  },
  computed: {
    ...mapGetters(['visitTaskList'])
  },
  mounted() {
    this.subjectCode = this.$router.currentRoute.query.subjectCode
    this.CriterionType = parseInt(localStorage.getItem('CriterionType'))
    this.TrialReadingCriterionId = this.$router.currentRoute.query.TrialReadingCriterionId
    DicomEvent.$on('loadMeasurementList', async obj => {
      console.log(11111, this.$refs['QuestionsPreview'])
      if (this.visitTaskId !== obj.visitTaskId) {
        this.visitTaskId = obj.visitTaskId
        this.taskBlindName = obj.taskBlindName
        this.$refs['QuestionsPreview'].getCustomTableQuestionAnswer(obj.visitTaskId)
      }
    })
    DicomEvent.$on('getCustomTableQuestionAnswer', (visitTaskId) => {
      this.$refs['QuestionsPreview'].getCustomTableQuestionAnswer(visitTaskId)
    })
    DicomEvent.$on('setCollapseActive', measureData => {
    })
    DicomEvent.$on('changeLesionType', (questionsObj) => {
    })
    DicomEvent.$on('getAllUnSaveLesions', (callback) => {
    })

    DicomEvent.$on('split', measureData => {
    })
    DicomEvent.$on('setReadingState', readingTaskState => {
      console.log(readingTaskState)
      this.readingTaskState = readingTaskState
    })

    window.addEventListener('resize', this.setHeight)
  },
  beforeDestroy() {
    DicomEvent.$off('loadMeasurementList')
    DicomEvent.$off('setCollapseActive')
    DicomEvent.$off('changeLesionType')
    DicomEvent.$off('getUnSaveTarget')
    DicomEvent.$off('setReadingState')
    DicomEvent.$off('getCustomTableQuestionAnswer')
  },
  methods: {
    // 关闭签名框
    closeSignDialog(isSign, signInfo) {
      if (isSign) {
        this.signConfirm(signInfo)
      } else {
        this.signVisible = false
      }
    },
    // 签名并确认
    signConfirm(signInfo) {
      this.loading = true
      var params = {
        data: {
          visitTaskId: this.visitTaskId
        },
        signInfo: signInfo
      }
      this.signVisible = false
      submitDicomVisitTask(params).then(res => {
        if (res.IsSuccess) {
          try {
            this.$message.success(this.$t('common:message:savedSuccessfully'))
            if (this.$refs['signForm']) {
              this.$refs['signForm'].btnLoading = false
            }
            this.signVisible = false
            // 设置当前任务阅片状态为已读
            this.readingTaskState = 2
            store.dispatch('reading/setVisitTaskReadingTaskState', { visitTaskId: this.visitTaskId, readingTaskState: 2 })
            DicomEvent.$emit('setReadingState', 2)
            window.opener.postMessage('refreshTaskList', window.location)
            this.$confirm('当前阅片任务已完成，是否进入下一个阅片任务？', {
              type: 'warning',
              distinguishCancelAndClose: true
            })
              .then(() => {
                try {DicomEvent.$emit('getNextTask')} catch (e) {console.log(e)}
              })
              .catch(action => {
              })
          } catch (e) {
            console.log(e)
          }
        }
        this.loading = false
      }).catch(() => {
        this.loading = false
        if (this.$refs['signForm'] && this.$refs['signForm'].btnLoading) {
          this.$refs['signForm'].btnLoading = false
        }
      })
    },
    async handleConfirm() {
      var res = await this.$refs['QuestionsPreview'].handleSave(false)
      if (res) {
        const { ImageAssessmentReportConfirmation } = const_.processSignature
        this.signCode = ImageAssessmentReportConfirmation
        this.signVisible = true
      }
    },
    handleSave(isMsg) {
      this.$refs['QuestionsPreview'].handleSave(isMsg)
    },
    setHeight() {
      this.height = window.innerHeight - 140
    },
    initPage(obj) {
      if (this.visitTaskId !== obj.visitTaskId) {
        this.visitTaskId = obj.visitTaskId
        this.taskBlindName = obj.taskBlindName
        this.activeName = ''
        this.ecrfShow = true
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.measurement-wrapper{
  overflow-y: auto;
  // overflow: hidden;

  .container{
    padding: 10px;
  }
  .title{
    padding: 5px;
    font-weight: bold;
    color: #ddd;
    font-size: 15px;

  }
  .add-icon{
    padding: 5px;
    font-weight: bold;
    color: #ddd;
    font-size: 15px;
    border: 1px solid #938b8b;
    margin-bottom: 2px;
    cursor: pointer;
  }
  .add-icon:hover{
    background-color: #607d8b;
  }

  .flex-row{
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    background-color: #424242;

  }
  .lesion_list{
    position: relative;
  }
  .el-collapse{
    border-bottom:none;
    >>>.el-collapse-item{
      background-color: #000!important;
      color: #ddd;

    }
    >>>.el-collapse-item__header{
      background-color: #000!important;
      color: #ddd;
      border-bottom-color:#5a5a5a;
      padding-left: 5px;
      height: 35px;
      line-height: 35px;
    }
    >>>.el-collapse-item__wrap{
      background-color: #000!important;
      color: #ddd;
    }
    >>>.el-collapse-item__content{
      width:260px;
      position: absolute;
      top: 0px;
      right: 0px;
      // border: 1px solid #ffeb3b;
      border: 1px solid #fff;
      z-index: 1;
      color: #ddd;
      padding: 5px;
      background-color:#1e1e1e;
    }

  }
}
</style>
