<template>
  <el-form
    ref="surgeryForm"
    :model="form"
    label-width="160px"
    size="small"
    :rules="rules"
    class="surgery-wrapper"
  >
    <div class="base-dialog-body">
      <!-- 手术名称 -->
      <el-form-item :label="$t('trials:uploadClinicalData:table:surgeryName')" prop="OperationName">
        <el-input v-model="form.OperationName" />
      </el-form-item>
      <!-- 手术日期 -->
      <el-form-item :label="$t('trials:uploadClinicalData:table:surgeryDate')" prop="OperationTime">
        <el-date-picker
          v-model="form.OperationTime"
          type="date"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          :picker-options="pickerOption"
          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 { addOrUpdatePreviousSurgery } from '@/api/trials'
export default {
  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: '',
        OperationName: '',
        OperationTime: '',
        ClinicalDataTrialSetId: ''
        // IsSubjectLevel: true
      },
      rules: {
        OperationName: [{ required: true, message: this.$t('common:ruleMessage:specify'), trigger: ['blur', 'change'] }]
      },
      saveBtnLoading: false,
      pickerOption: {
        disabledDate: time => {
          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.surgeryForm.validate(valid => {
        if (!valid) return
        this.saveBtnLoading = true
        this.form.SubjectVisitId = this.subjectVisitId
        // this.form.Type = '既往手术史'
        addOrUpdatePreviousSurgery(this.parentData.TrialId, this.form).then(res => {
          this.saveBtnLoading = false
          this.$emit('closePSDialog')
          this.$emit('refresh')
          this.$message.success(this.$t('common:message:savedSuccessfully'))
        }).catch(() => {
          this.saveBtnLoading = false
        })
      })
    },
    cancel() {
      this.$emit('closePSDialog')
    }
  }
}
</script>
