<template>
  <el-form ref="equipmentForm" :model="form" :rules="rules" label-width="380px">
    <div class="base-dialog-body">
      <!-- 扫描设备 -->
      <el-form-item :label="$t('trials:equiptResearch:form:equipment')" prop="EquipmentTypeEnum"
        v-if="EquipmentControlFieldList.includes('EquipmentTypeEnum')">
        <div style="display: flex;align-items: center;">
          <el-select v-model="form.EquipmentTypeEnum" style="width:100%" @change="form.OtherEquipmentType = null">
            <el-option v-for="item of $d.SiteSurvey_ScanEquipmentType" :key="item.id" :label="item.label"
              :value="item.value" />
          </el-select>
          <el-input placeholder="" v-model="form.OtherEquipmentType" style="margin-left: 10px;"
            v-if="form.EquipmentTypeEnum == '-1'" clearable>
          </el-input>
        </div>
      </el-form-item>
      <!-- 扫描参数 -->
      <el-form-item :label="$t('trials:equiptResearch:form:param')"
        v-if="EquipmentControlFieldList.includes('Parameters')" prop="Parameters">
        <el-input v-model="form.Parameters" />
      </el-form-item>
      <!-- 扫描仪器制造商名称 -->
      <el-form-item :label="$t('trials:equiptResearch:form:manufacturer')"
        v-if="EquipmentControlFieldList.includes('ManufacturerType')" prop="ManufacturerType">
        <div style="display: flex;align-items: center;">
          <el-select v-model="form.ManufacturerType" style="width:100%" @change="form.ManufacturerName = null">
            <el-option v-for="item of $d.ManufacturerType" :key="item.id" :label="item.label" :value="item.value" />
          </el-select>
          <el-input placeholder="" v-model="form.ManufacturerName" style="margin-left: 10px;"
            v-if="form.ManufacturerType == '-1'" clearable>
          </el-input>
        </div>
      </el-form-item>
      <!-- 扫描仪型号 -->
      <el-form-item :label="$t('trials:equiptResearch:form:model')"
        v-if="EquipmentControlFieldList.includes('ScannerType')" prop="ScannerType">
        <el-input v-model="form.ScannerType" />
      </el-form-item>
      <!-- 磁场强度 -->
      <el-form-item :label="$t('trials:equiptResearch:form:MagneticFieldStrengthType')"
        v-if="EquipmentControlFieldList.includes('MagneticFieldStrengthType')" prop="MagneticFieldStrengthType">
        <el-select v-model="form.MagneticFieldStrengthType" style="width:100%">
          <el-option v-for="item of $d.MagneticFieldStrengthType" :key="item.id" :label="item.label"
            :value="item.value" />
        </el-select>
      </el-form-item>
      <!-- 体部线圈通道数 -->
      <el-form-item :label="$t('trials:equiptResearch:form:BodyCoilChannelCount')"
        v-if="EquipmentControlFieldList.includes('BodyCoilChannelCount')" prop="BodyCoilChannelCount">
        <el-select v-model="form.BodyCoilChannelCount" style="width:100%">
          <el-option v-for="item of $d.BodyCoilChannelCount" :key="item.id" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <!-- 是否具备专用的PDFF脂肪定量序列（CSE-MRI序列） -->
      <el-form-item :label="$t('trials:equiptResearch:form:HasDedicatedPdfFatQuantificationSequence')"
        v-if="EquipmentControlFieldList.includes('HasDedicatedPdfFatQuantificationSequence')"
        prop="HasDedicatedPdfFatQuantificationSequence">
        <el-select v-model="form.HasDedicatedPdfFatQuantificationSequence" style="width:100%"
          @change="form.PdfFatQuantificationSequenceType = null, form.OtherSequenceSpecification = null">
          <el-option v-for="item of $d.YesOrNo" :key="item.id" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <!-- PDFF脂肪定量序列 -->
      <el-form-item :label="$t('trials:equiptResearch:form:PdfFatQuantificationSequenceType')"
        prop="PdfFatQuantificationSequenceType"
        v-if="form.HasDedicatedPdfFatQuantificationSequence && EquipmentControlFieldList.includes('PdfFatQuantificationSequenceType')">
        <div style="display: flex;align-items: center;">
          <el-select v-model="form.PdfFatQuantificationSequenceType" style="width:100%"
            @change="form.OtherSequenceSpecification = null">
            <el-option v-for="item of $d.PdfFatQuantificationSequenceType" :key="item.id" :label="item.label"
              :value="item.value" />
          </el-select>
          <el-input placeholder="" v-model="form.OtherSequenceSpecification" style="margin-left: 10px;"
            v-if="form.PdfFatQuantificationSequenceType == '-1'" clearable>
          </el-input>
        </div>
      </el-form-item>
      <!-- 是否包含 T2/R2 校正（用于铁沉积校正） -->
      <el-form-item :label="$t('trials:equiptResearch:form:HasT2R2Correction')" prop="HasT2R2Correction"
        v-if="EquipmentControlFieldList.includes('HasT2R2Correction')">
        <el-select v-model="form.HasT2R2Correction" style="width:100%">
          <el-option v-for="item of $d.YesOrNo" :key="item.id" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <!-- 是否可完整导出 PDFF 参数图及全部原始 DICOM 数据 -->
      <el-form-item :label="$t('trials:equiptResearch:form:CanFullyExportPdfParameterMapsAndRawDicom')"
        prop="CanFullyExportPdfParameterMapsAndRawDicom"
        v-if="EquipmentControlFieldList.includes('CanFullyExportPdfParameterMapsAndRawDicom')">
        <el-select v-model="form.CanFullyExportPdfParameterMapsAndRawDicom" style="width:100%">
          <el-option v-for="item of $d.YesOrNo" :key="item.id" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <!-- 备注 -->
      <el-form-item :label="$t('trials:equiptResearch:form:remark')" prop="Note"
        v-if="EquipmentControlFieldList.includes('Note')">
        <el-input v-model="form.Note" />
      </el-form-item>
    </div>
    <div class="base-dialog-footer" style="text-align:right;margin-top:10px;">
      <el-form-item>
        <!-- 取消 -->
        <el-button size="small" type="primary" :disabled="btnLoading" @click="handleCancel">
          {{ $t('common:button:cancel') }}
        </el-button>
        <!-- 保存 -->
        <el-button size="small" type="primary" :loading="btnLoading" @click="handleSave">
          {{ $t('common:button:save') }}
        </el-button>
      </el-form-item>
    </div>
  </el-form>
</template>
<script>
import { addOrUpdateTrialSiteEquipmentSurvey } from '@/api/research'
import { getQueryString } from '@/utils/history.js'
import { getBasicDataSelects } from '@/api/dictionary/dictionary'
export default {
  name: 'ResearchEquipmentForm',
  props: {
    data: {
      type: Object,
      default() {
        return {}
      }
    },
    trialSiteSurveyEquipmentType: {
      type: String,
      default: ''
    },
    EquipmentControlFieldList: {
      type: Array,
      default: () => {
        return []
      }
    },
    isPM: {
      type: Boolean,
      default: false
    },
  },
  data() {
    return {
      form: {
        Id: '',
        EquipmentTypeEnum: null,
        OtherEquipmentType: null,
        Parameters: null,
        Note: null,
        ManufacturerType: null,
        ManufacturerName: null,
        ScannerType: null,
        MagneticFieldStrengthType: null,
        BodyCoilChannelCount: null,
        HasDedicatedPdfFatQuantificationSequence: null,
        PdfFatQuantificationSequenceType: null,
        OtherSequenceSpecification: null,
        HasT2R2Correction: null,
        CanFullyExportPdfParameterMapsAndRawDicom: null,
        TrialSiteSurveyId: ''
      },
      rules: {
        EquipmentTypeEnum: [
          { required: true, message: this.$t('trials:researchForm:formRule:select'), trigger: ['blur', 'change'] },
          {
            validator: (rule, value, callback) => {
              if (this.form.EquipmentTypeEnum === -1 && !this.form.OtherEquipmentType) {
                callback(this.$t('common:ruleMessage:specify'));
              } else {
                callback()
              }
            }, message: this.$t('common:ruleMessage:specify'), trigger: ['blur', 'change']
          },
        ],
        Parameters: [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: ['blur', 'change'] }
        ],
        //  Note: [
        //   { required: true, message: this.$t('common:ruleMessage:specify'), trigger: ['blur', 'change'] }
        // ],
        ManufacturerType: [
          { required: true, message: this.$t('trials:researchForm:formRule:select'), trigger: ['blur', 'change'] },
          {
            validator: (rule, value, callback) => {
              if (this.form.ManufacturerType === -1 && !this.form.ManufacturerName) {
                callback(this.$t('common:ruleMessage:specify'));
              } else {
                callback()
              }
            }, message: this.$t('common:ruleMessage:specify'), trigger: ['blur', 'change']
          },
        ],
        ScannerType: [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: ['blur', 'change'] }
        ],
        MagneticFieldStrengthType: [
          { required: true, message: this.$t('trials:researchForm:formRule:select'), trigger: ['blur', 'change'] }
        ],
        BodyCoilChannelCount: [
          { required: true, message: this.$t('trials:researchForm:formRule:select'), trigger: ['blur', 'change'] }
        ],
        HasDedicatedPdfFatQuantificationSequence: [
          { required: true, message: this.$t('trials:researchForm:formRule:select'), trigger: ['blur', 'change'] }
        ],
        PdfFatQuantificationSequenceType: [
          { required: true, message: this.$t('trials:researchForm:formRule:select'), trigger: ['blur', 'change'] },
          {
            validator: (rule, value, callback) => {
              if (this.form.PdfFatQuantificationSequenceType === -1 && !this.form.OtherSequenceSpecification) {
                callback(this.$t('common:ruleMessage:specify'));
              } else {
                callback()
              }
            }, message: this.$t('common:ruleMessage:specify'), trigger: ['blur', 'change']
          },
        ],
        HasT2R2Correction: [
          { required: true, message: this.$t('trials:researchForm:formRule:select'), trigger: ['blur', 'change'] }
        ],
        CanFullyExportPdfParameterMapsAndRawDicom: [
          { required: true, message: this.$t('trials:researchForm:formRule:select'), trigger: ['blur', 'change'] }
        ],
      },
      btnLoading: false,
      dictionaryList: {}
    }
  },
  mounted() {
    this.initForm()
  },
  methods: {
    async initForm() {
      // await this.getDicData()
      Object.keys(this.data).forEach(key => {
        this.form[key] = this.data[key]
      })
    },
    // 保存参与者信息
    handleSave() {
      this.$refs['equipmentForm'].validate(valid => {
        if (!valid) return
        this.btnLoading = true
        if (!this.form.TrialSiteSurveyId) {
          this.form.TrialSiteSurveyId = getQueryString('trialSiteSurveyId')
        }
        const trialId = getQueryString('trialId')
        addOrUpdateTrialSiteEquipmentSurvey(trialId, this.form).then(res => {
          this.btnLoading = false
          if (res.IsSuccess) {
            this.$message.success(this.$t('common:message:savedSuccessfully'))
            this.$emit('getList')
            this.$emit('close')
          }
        }).catch(() => { this.btnLoading = false })
      })
    },
    // 取消保存
    handleCancel() {
      this.$emit('close')
    },
    // 获取下拉框
    getDicData() {
      getBasicDataSelects(['SiteSurvey_ScanEquipmentType']).then(res => {
        this.dictionaryList = { ...res.Result }
      }).catch(() => {

      })
    }

  }
}
</script>
