<template>
  <div v-loading="loading">
    <QuestionsForm v-if="clinicalUploadType === 2" :data="data" :trial-clinical-id="trialClinicalId"
      :is-viewer="isViewer" :visit-id="visitId" :subject-id="subjectId" :open-type="openType"
      :system-clinical-id="systemClinicalId" :trial-id="trialId" :reading-id="readingId"
      :clinical-form-id="clinicalFormId" :clinical-data-level="clinicalDataLevel" @close="close"></QuestionsForm>
    <uploadClinicalData v-else :subject-visit-id="subjectVisitId" :data="data" :enum-type="0" :allow-add-or-edit="true"
      @getList="() => { }">
    </uploadClinicalData>
    <div class="base-dialog-footer" v-if="!isViewer && openType !== 'look' && [0, 1].includes(clinicalDataLevel)"
      style="text-align:right;margin-top:10px;">
      <!-- 保存 -->
      <el-button size="small" type="primary" @click="submitClinicalForm">
        {{ $t('common:button:submit') }}
      </el-button>
    </div>
    <!-- 临床数据签名框 -->
    <el-dialog v-if="signVisible" :visible.sync="signVisible" :close-on-click-modal="false" width="600px" append-to-body
      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" :subject-visit-id="subjectVisitId"
        @closeDialog="closeSignDialog" />
    </el-dialog>
  </div>
</template>

<script>
import { CRCSignClinicalData } from '@/api/trials'
import SignForm from '@/views/trials/components/newSignForm'
import QuestionsForm from './components/QuestionsForm'
import uploadClinicalData from './components/uploadClinicalData'
import const_ from '@/const/sign-code'

export default {
  name: "index",
  components: { QuestionsForm, uploadClinicalData, SignForm },
  methods: {
    submitClinicalForm() {
      const { ClinicalDataConfirmation } = const_.processSignature
      this.signCode = ClinicalDataConfirmation
      this.signVisible = true
    },
    close() {
      this.$emit('close')
    },
    closeSignDialog(isSign, signInfo) {
      if (isSign) {
        this.submit(signInfo)
      } else {
        this.signVisible = false
      }
    },
    submit(signInfo) {
      this.loading = true
      var params = {
        data: { readingClinicalDataId: this.readingClinicalDataId }
      }
      if (signInfo) {
        params.signInfo = signInfo
      }
      params.signInfo.TrialId = this.trialId
      CRCSignClinicalData(params)
        .then(res => {
          this.loading = false
          this.$message.success(this.$t('common:message:signSuccessfully'))
          this.close()
        }).catch((res) => {
          this.$refs['signForm'].btnLoading = false
        })
    },
  },
  data() {
    return {
      loading: false,
      signVisible: false,
      signCode: null,
      currentUser: zzSessionStorage.getItem('userName'),
    }
  },
  mounted() {
    console.log(this.clinicalDataLevel)
  },
  props: {
    readingClinicalDataId: {
      type: String,
      default: () => ''
    },
    subjectVisitId: {
      type: String,
      default: () => ''
    },
    clinicalUploadType: {
      type: Number,
      default: () => {
        return 2
      }
    },
    clinicalDataLevel: {
      type: Number,
      default: () => {
        return 2
      }
    },
    openType: {
      type: String,
      default: () => 'add'
    },
    isViewer: {
      type: Boolean,
      default: () => true
    },
    visitId: {
      type: String,
      default: () => ''
    },
    subjectId: {
      type: String,
      default: () => ''
    },
    trialId: {
      type: String,
      default: () => ''
    },
    readingId: {
      type: String,
      default: () => ''
    },
    clinicalFormId: {
      type: String,
      default: () => ''
    },
    data: {
      type: Object,
      default: () => { }
    },
    trialClinicalId: {
      type: String,
      default: () => {
        return ''
      }
    },
    systemClinicalId: {
      type: String,
      default: () => {
        return ''
      }
    },
  }
}
</script>

<style scoped></style>
