<template>
  <el-form ref="researchBSForm" size="small" :model="form" :rules="rules" style="width:80%">
    <!-- 项目编号 -->
    <el-form-item :label="$t('trials:researchForm:form:trialId')">
      <el-input :maxLength="$inputMax" v-model="form.TrialCode" disabled />
    </el-form-item>
    <!-- 试验方案号 -->
    <el-form-item :label="$t('trials:researchForm:form:researchNo')">
      <el-input :maxLength="$inputMax" v-model="form.ResearchProgramNo" disabled />
    </el-form-item>
    <!-- 试验名称 -->
    <el-form-item :label="$t('trials:researchForm:form:researchName')">
      <el-input :maxLength="$inputMax" v-model="form.ExperimentName" disabled />
    </el-form-item>
    <!-- 适应症类型 -->
    <el-form-item :label="$t('trials:researchForm:form:decleareType')">
      <el-input :maxLength="$inputMax" v-model="form.IndicationType" disabled />
    </el-form-item>
    <!-- 中心名称 -->
    <el-form-item :label="$t('trials:researchForm:form:siteName')" prop="SiteId">
      <el-select
        v-model="form.SiteId"
        filterable
        style="width:100%;"
        :disabled="!(state === 0 && userTypeEnumInt === 0)"
        @change="handleSiteChange"
      >
        <el-option
          v-for="(item,index) of siteOptions"
          :key="index"
          :label="item.TrialSiteAliasName"
          :value="item.SiteId"
        />
      </el-select>
    </el-form-item>
    <!-- 中心编号 -->
    <el-form-item v-if="form.SiteId" :label="$t('trials:researchForm:form:siteId')">
      <el-input :maxLength="$inputMax" v-model="form.TrialSiteCode" disabled />
    </el-form-item>
    <!-- 联系人 -->
    <el-form-item :label="$t('trials:researchForm:form:contactor')" prop="UserName">
      <el-input :maxLength="$inputMax" v-model="form.UserName" :disabled="!(state === 0 && userTypeEnumInt === 0)" />
    </el-form-item>
    <!-- 联系电话 -->
    <el-form-item :label="$t('trials:researchForm:form:contactorPhone')" prop="Phone">
      <el-input :maxLength="$inputMax" v-model="form.Phone" :disabled="!(state === 0 && userTypeEnumInt === 0)" />
    </el-form-item>
    <!-- 联系邮箱 -->
    <el-form-item :label="$t('trials:researchForm:form:contactorEmail')">
      <el-input :maxLength="$inputMax" v-model="form.Email" disabled />
    </el-form-item>

    <!-- <el-divider /> -->
    <!-- 平均刻盘周期（天） -->
    <el-form-item :label="$t('trials:researchForm:form:engravingCycle')">
      <el-input-number v-model="form.AverageEngravingCycle" :disabled="!(state === 0 && userTypeEnumInt === 0)" controls-position="right" :min="0" />
    </el-form-item>
    <!-- 请确认参与本项目影像采集的影像技师具备对应的资质（如：“技师证”，对应设备的“大型设备上岗证”） -->
    <el-form-item :label="$t('trials:researchForm:form:isQualified')">
      <el-radio-group v-model="form.IsConfirmImagingTechnologist" :disabled="!(state === 0 && userTypeEnumInt === 0)">
        <el-radio :label="true">是</el-radio>
        <el-radio :label="false">否</el-radio>
      </el-radio-group>
    </el-form-item>
    <!-- 原因 -->
    <el-form-item
      v-if="form.IsConfirmImagingTechnologist === false"
      :label="$t('trials:researchForm:form:notQualifiedReason')"
    >
      <el-input :maxLength="$inputMax"
        v-model="form.NotConfirmReson"
        type="textarea"
        :autosize="{ minRows: 2, maxRows: 4}"
        :disabled="!(state === 0 && userTypeEnumInt === 0)"
      />
    </el-form-item>
    <!-- 研究单位疗效评估人员类型 -->
    <el-form-item :label="$t('trials:researchForm:form:staffType')">
      <el-radio-group v-model="form.EfficacyEvaluatorType" :disabled="!(state === 0 && userTypeEnumInt === 0)">
        <el-radio :label="0">影像科医生</el-radio>
        <el-radio :label="1">临床医生</el-radio>
      </el-radio-group>
    </el-form-item>
    <!-- 是否严格按照研究单位影像手册参数完成图像采集 -->
    <el-form-item :label="$t('trials:researchForm:form:isFollowStudyParam')">
      <el-radio-group v-model="form.IsFollowStudyParameters" :disabled="!(state === 0 && userTypeEnumInt === 0)">
        <el-radio :label="true">是</el-radio>
        <el-radio :label="false">否</el-radio>
      </el-radio-group>
    </el-form-item>
    <!-- 不能严格按照研究单位影像手册参数采集图像原因 -->
    <el-form-item
      v-if="!form.IsFollowStudyParameters"
      :label="$t('trials:researchForm:form:notFollowStudyParam')"
    >
      <el-input :maxLength="$inputMax"
        v-model="form.NotFollowReson"
        type="textarea"
        :autosize="{ minRows: 2, maxRows: 4}"
        :disabled="!(state === 0 && userTypeEnumInt === 0)"
      />
    </el-form-item>
    <el-form-item>
      <!-- 保存 -->
      <el-button
        v-if="state === 0 && userTypeEnumInt === 0"
        type="primary"
        :loading="btnLoading"
        size="small"
        @click="handleSave(false)"
      >
        {{ $t('common:button:save') }}
      </el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { getTrialSiteSelect } from '@/api/trials'
import { addOrUpdateTrialSiteSurvey } from '@/api/research'
export default {
  name: 'QuestionForm',

  data() {
    var checkPhone = (rule, value, callback) => {
      const phoneReg = /^1[3|4|5|7|8][0-9]{9}$/
      if (!value) {
        return callback(new Error(this.$t('trials:researchForm:formRule:specify')))
      }
      setTimeout(() => {
        if (!Number.isInteger(+value)) {
          callback(new Error(this.$t('trials:researchForm:formRule:phone')))
        } else {
          if (phoneReg.test(value)) {
            callback()
          } else {
            callback(new Error(this.$t('trials:researchForm:formRule:phone')))
          }
        }
      }, 100)
    }
    return {
      form: {
        Id: '',
        Sponsor: '', // 申办方
        ResearchProgramNo: '', // 方案号
        ExperimentName: '',
        TrialCode: '', // 项目编号
        IndicationType: '', // 适应症
        SiteId: '', // 研究单位名称
        TrialSiteCode: '',
        UserName: '', // 联系人
        Phone: '', // 联系人电话
        Email: '', // 联系人邮箱
        AverageEngravingCycle: '',
        IsConfirmImagingTechnologist: '',
        NotConfirmReson: '',
        EfficacyEvaluatorType: '',
        IsFollowStudyParameters: '',
        NotFollowReson: ''
      },
      rules: {
        SiteId: [
          { required: true, message: this.$t('trials:researchForm:formRule:specify'), trigger: 'blur' }
        ],
        UserName: [
          { required: true, message: this.$t('trials:researchForm:formRule:specify'), trigger: 'blur' },
          { min: 0, max: 50, message: this.$t('trials:researchForm:formRule:maxLength'), trigger: 'blur' }
        ],
        Phone: [
          { required: true, validator: checkPhone, trigger: 'blur' }
        ]
      },
      siteOptions: [],
      btnLoading: false,
      state: null,
      userTypeEnumInt: zzSessionStorage.getItem('userTypeEnumInt') * 1
    }
  },
  methods: {
    // 保存基本信息
    handleSave(isAutoCommit) {
      return new Promise((resolve, reject) => {
        this.$refs['researchBSForm'].validate(valid => {
          if (valid) {
            this.btnLoading = true
            const param = {
              id: this.form.Id,
              trialId: this.$route.query.trialId,
              siteId: this.form.SiteId,
              userName: this.form.UserName,
              phone: this.form.Phone,
              email: this.form.Email,
              averageEngravingCycle: this.form.AverageEngravingCycle,
              isConfirmImagingTechnologist: this.form.IsConfirmImagingTechnologist,
              notConfirmReson: this.form.NotConfirmReson,
              efficacyEvaluatorType: this.form.EfficacyEvaluatorType,
              isFollowStudyParameters: this.form.IsFollowStudyParameters,
              notFollowReson: this.form.NotFollowReson
            }
            addOrUpdateTrialSiteSurvey(param).then(res => {
              this.btnLoading = false
              if (res.IsSuccess && !isAutoCommit) {
                this.$message.success(this.$t('common:message:savedSuccessfully'))
              }
              resolve(true)
            }).catch(() => {
              this.btnLoading = false
              reject(false)
            })
          } else {
            reject(false)
          }
        })
      })
    },

    // 初始化
    async initForm(trialInfo, trialSiteSurvey) {
      // 获取项目下的site
      const { Result } = await getTrialSiteSelect(this.$route.query.trialId)
      this.siteOptions = Result
      this.form.Id = trialSiteSurvey.Id
      this.form.Sponsor = trialInfo.Sponsor // 申办方
      this.form.ResearchProgramNo = trialInfo.ResearchProgramNo // 方案号
      this.form.ExperimentName = trialInfo.ExperimentName
      this.form.TrialCode = trialInfo.TrialCode // 项目编号
      this.form.TrialSiteCode = trialSiteSurvey.TrialSiteCode
      this.form.TrialSiteAliasName = trialSiteSurvey.TrialSiteAliasName // 项目别称
      this.form.IndicationType = trialInfo.IndicationType // 适应症
      this.form.SiteId = trialSiteSurvey.SiteId
      this.form.UserName = trialSiteSurvey.UserName // 联系人
      this.form.Phone = trialSiteSurvey.Phone // 联系人电话
      this.form.Email = trialSiteSurvey.Email // 联系人邮箱
      this.form.AverageEngravingCycle = trialSiteSurvey.AverageEngravingCycle
      this.form.IsConfirmImagingTechnologist = trialSiteSurvey.IsConfirmImagingTechnologist
      this.form.NotConfirmReson = trialSiteSurvey.NotConfirmReson
      this.form.EfficacyEvaluatorType = trialSiteSurvey.EfficacyEvaluatorType
      this.form.IsFollowStudyParameters = trialSiteSurvey.IsFollowStudyParameters
      this.form.NotFollowReson = trialSiteSurvey.NotFollowReson
      this.state = trialSiteSurvey.State
    },
    handleSiteChange(val) {
      var selected = this.siteOptions.find(item => item.SiteId === val)
      if (selected) {
        this.form.TrialSiteCode = selected.TrialSiteCode
      }
    }
  }
}
</script>
