<template>
  <el-form
    ref="previousOther"
    :model="form"
    label-width="100px"
    size="small"
    :rules="rules"
    class="previousOther-wrapper"
  >
    <div class="base-dialog-body">
      <!-- 治疗类型 -->
      <el-form-item :label="$t('trials:uploadClinicalData:table:treatmentType')" prop="TreatmentType">
        <el-input :maxLength="$inputMax" v-model="form.TreatmentType" />
      </el-form-item>
      <!-- 开始日期 -->
      <el-form-item :label="$t('trials:uploadClinicalData:table:treatmentbeginDate')" prop="StartTime">
        <el-date-picker
          v-model="form.StartTime"
          type="date"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          :picker-options="beginPickerOption"
          style="width:100%;"
        />
      </el-form-item>
      <!-- 结束日期 -->
      <el-form-item :label="$t('trials:uploadClinicalData:table:treatmentendDate')" prop="EndTime">
        <el-date-picker
          v-model="form.EndTime"
          type="date"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          :picker-options="endpickerOption"
          style="width:100%;"
        />
      </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="saveBtnLoading" @click="cancel">
          {{ $t('common:button:cancel') }}
        </el-button>
        <el-button size="small" type="primary" :loading="saveBtnLoading" @click="handleSave">
          {{ $t('common:button:save') }}
        </el-button>
      </el-form-item>
    </div>
  </el-form>
</template>
<script>
import { addOrUpdatePreviousOther } from '@/api/trials'
export default {
  name: 'PreviousOther',
  props: {
    parentData: {
      type: Object,
      default() { return {} }
    },
    data: {
      type: Object,
      default() { return {} }
    },
    subjectVisitId: {
      type: String,
      required: true
    }
  },

  data() {
    return {
      trialId: this.$route.query.trialId,
      form: {
        Id: '',
        TreatmentType: '',
        StartTime: '',
        EndTime: '',
        ClinicalDataTrialSetId: ''
        // IsSubjectLevel: true
      },
      rules: {
        TreatmentType: [{ required: true, message: this.$t('common:ruleMessage:specify'), trigger: ['blur', 'change'] }]
      },
      saveBtnLoading: false,
      beginPickerOption: {
        disabledDate: time => {
          if (this.form.EndTime) {
            return time.getTime() >= new Date(this.form.EndTime).getTime()
          } else {
            return time.getTime() > Date.now()
          }
        }
      },
      endpickerOption: {
        disabledDate: time => {
          if (this.form.StartTime) {
            return time.getTime() > Date.now() || time.getTime() <= new Date(this.form.StartTime).getTime() - 86400000
          } else {
            return time.getTime() > Date.now()
          }
        }
      }
    }
  },
  mounted() {
    if (Object.keys(this.data).length) {
      for (const k in this.form) {
        if (this.data.hasOwnProperty(k)) {
          this.form[k] = this.data[k]
        }
      }
    }
  },
  methods: {
    handleSave() {
      this.$refs.previousOther.validate(valid => {
        if (!valid) return
        this.saveBtnLoading = true
        this.form.SubjectVisitId = this.subjectVisitId
        addOrUpdatePreviousOther(this.trialId, this.form).then(res => {
          this.saveBtnLoading = false
          this.$emit('closePODialog')
          this.$emit('refresh')
          this.$message.success(this.$t('common:message:savedSuccessfully'))
        }).catch(() => {
          this.saveBtnLoading = false
        })
      })
    },
    cancel() {
      this.$emit('closePODialog')
    }
  }
}
</script>
