<template>
  <el-form
    ref="subjectForm"
    v-loading="loading"
    :model="form"
    :rules="rules"
    size="small"
    label-width="200px"
  >
    <div class="base-dialog-body">
      <!-- Site -->
      <el-form-item label="AE Title" prop="aet">
        <el-input v-model="form.aet" />
      </el-form-item>
      <el-form-item label="IP" prop="host">
        <el-input v-model="form.host" />
      </el-form-item>
      <!-- 受试者编号 -->
      <el-form-item label="Port" prop="port">
        <el-input v-model="form.port" />
      </el-form-item>
      <!-- 受试者编号 -->
      <el-form-item label="Modality" prop="mode">
        <el-input v-model="form.mode" />
      </el-form-item>
      <!-- 受试者编号 -->
      <el-form-item label="Description" prop="description">
        <el-input v-model="form.description" />
      </el-form-item>
    </div>
    <div class="base-dialog-footer" style="text-align:right;margin-top:10px;">
      <el-form-item>
        <!-- 取消 -->
        <el-button
          :disabled="btnLoading"
          size="small"
          type="primary"
          @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 { getTrialSiteSelect, addOrUpdateSubject } from '@/api/trials'
export default {
  name: 'SubjectForm',
  props: {
    data: {
      type: Object,
      default() { return {} }
    },
    otherInfo: {
      type: Object,
      default() { return {} }
    }
  },

  data() {
    return {
      form: {
        _id: '',
        host: '',
        port: '',
        aet: '',
        mode: '',
        description: ''
      },
      rules: {
        host: [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' },
        ],
        port: [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' },
        ],
        aet: [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' },
        ],
      },
      btnLoading: false,
      loading: false,
      trialId: ''
    }
  },
  mounted() {
    this.trialId = this.$route.query.trialId
    this.initForm()
  },
  methods: {
    async initForm() {
      if (Object.keys(this.data).length > 0) {
        for (const k in this.form) {
          if (this.data.hasOwnProperty(k)) {
            this.form[k] = this.data[k]
          }
        }
      }
      this.form.trialId = this.trialId
    },
    handleSave() {
      this.$refs.subjectForm.validate(valid => {
        if (!valid) return
        this.btnLoading = true
        if(!this.form._id) {
          delete this.form._id
        }
        this.form.trialId = this.trialId
        this.$axios({
          method: this.form._id ? 'put' : 'post',
          url: process.env.VUE_APP_NODE_SERVER_PATH + 'pacs',
          data: this.form
        }).then(res => {
          if (res.data.code !== 1) {
            this.$message.error(res.data.message)
          } else {
            this.$message.success('保存成功')
            this.handleCancel()
            this.$emit('getList')
          }
          this.btnLoading = false
        })
      })
    },
    handleCancel() {
      this.$emit('close')
    },
  }
}
</script>
