<template>
  <el-form
    ref="radiotherapyForm"
    :model="form"
    label-width="100px"
    size="small"
    :rules="rules"
    class="radiotherapy-wrapper"
  >
    <div class="base-dialog-body">
      <!-- 放疗部位 -->
      <el-form-item :label="$t('trials:uploadClinicalData:table:bodyPart')" prop="Position">
        <el-input v-model="form.Position" />
      </el-form-item>
      <!-- 开始日期 -->
      <el-form-item :label="$t('trials:uploadClinicalData:table:beginDate')" 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:endDate')" 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>
      <!-- 病灶是否PD -->
      <el-form-item :label="$t('trials:uploadClinicalData:table:isPD')">
        <el-radio-group v-model="form.IsPD ">
          <el-radio v-for="item of $d.IsPdEnum" :key="item.value" :label="item.value">{{ item.label }}</el-radio>
        </el-radio-group>
      </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 { addOrUpdatePreviousHistory } from '@/api/trials'
export default {
  props: {
    data: {
      type: Object,
      default() { return {} }
    },
    subjectVisitId: {
      type: String,
      required: true
    }
  },

  data() {
    return {
      trialId: this.$route.query.trialId,
      form: {
        Id: '',
        Position: '',
        StartTime: '',
        EndTime: '',
        IsPD: 2,
        ClinicalDataTrialSetId: ''
        // IsSubjectLevel: true
      },
      rules: {
        Position: [{ 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.radiotherapyForm.validate(valid => {
        if (!valid) return
        this.saveBtnLoading = true
        this.form.SubjectVisitId = this.subjectVisitId
        addOrUpdatePreviousHistory(this.trialId, this.form).then(res => {
          this.saveBtnLoading = false
          this.$emit('closePRDialog')
          this.$emit('refresh')
          this.$message.success(this.$t('common:message:savedSuccessfully'))
        }).catch(() => {
          this.saveBtnLoading = false
        })
      })
    },
    cancel() {
      this.$emit('closePRDialog')
    }
  }
}
</script>
