<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="$t('trials:subject:table:site')" prop="SiteId">
        <el-select
          v-model="form.SiteId"
          clearable
          filterable
          style="width:100%"
          :disabled="form.StudyCount>0 || isEdit"
        >
          <el-option
            v-for="item of siteOptions"
            :key="item.Id"
            :label="item.TrialSiteCode"
            :value="item.SiteId"
          />
        </el-select>
      </el-form-item>
      <!-- 受试者编号 -->
      <el-form-item :label="$t('trials:subject:table:subjectId')" prop="Code">
        <el-input v-model="form.Code" :disabled="form.StudyCount > 0 || isEdit" />
        <span v-show="otherInfo.IsNoticeSubjectCodeRule" style="color:#F56C6C;font-size:12px;">{{ otherInfo.SubjectCodeRule }}</span>
      </el-form-item>
      <!-- 受试者第二编号 -->
      <el-form-item v-if="otherInfo.IsSubjectSecondCodeView" :label="$t('trials:subject:table:medicalNo')" prop="MedicalNo">
        <el-input v-model="form.MedicalNo" :disabled="form.StudyCount > 0 || isEdit" />
      </el-form-item>
      <!-- 访视基准日期 -->
      <el-form-item
        v-show="otherInfo.IsHaveFirstGiveMedicineDate"
        :label="$t('trials:subject:table:firstGiveMedicineTime')"
      >
        <el-date-picker
          v-model="form.FirstGiveMedicineTime"
          type="date"
          :picker-options="pickerOption"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
        />
      </el-form-item>
      <!-- Age -->
      <el-form-item
        v-if="otherInfo.IsHaveSubjectAge"
        :label="$t('trials:subject:table:age')"
        prop="Age"
      >
        <el-input-number v-model="form.Age" controls-position="right" :min="0" />
      </el-form-item>
      <!-- Gender -->
      <el-form-item v-if="otherInfo.IsSubjectSexView" :label="$t('trials:subject:table:gender')">
        <el-radio-group v-model="form.Sex">
          <el-radio label="F">F</el-radio>
          <el-radio label="M">M</el-radio>
          <el-radio label="O">O</el-radio>
        </el-radio-group>
      </el-form-item>
      <!-- 是否加急 -->
      <el-form-item v-if="otherInfo.IsSubjectExpeditedView" :label="$t('trials:subject:table:isUrgent')">
        <el-radio-group v-model="form.IsUrgent">
          <el-radio v-for="item of $d.YesOrNo" :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
          :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: '',
        Code: '',
        ShortName: '',
        Height: '',
        Weight: '',
        Age: '',
        Sex: '',
        BirthDate: '',
        SiteId: '',
        MedicalNo: '',
        Status: 1,
        FirstGiveMedicineTime: '',
        OutEnrollmentTime: '',
        VisitOverTime: '',
        Reason: '',
        StudyCount: '',
        SignDate: '',
        IsUrgent: false
      },
      isHaveSubjectAge: false,
      rules: {
        Code: [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' },
          { max: 50, message: `${this.$t('common:ruleMessage:maxLength')} 50` }
        ],
        // ShortName: [{ required: true, message: 'Please specify', trigger: 'blur' },
        //   { max: 50, message: 'The maximum length is 50' }],
        Age: [{ required: this.otherInfo.IsHaveSubjectAge || false, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' }],
        // Sex: [
        //   { required: true, message: 'Please specify', trigger: 'blur' }
        // ],
        SiteId: [{ required: true, message: this.$t('common:ruleMessage:select'), trigger: ['blur', 'change'] }],
        Reason: [{ max: 500, message: `${this.$t('common:ruleMessage:maxLength')} 500` }]
      },
      pickerOption: {
        disabledDate: time => {
          return time.getTime() > Date.now()
        }
      },
      btnLoading: false,
      siteOptions: [],
      loading: false,
      trialId: ''
    }
  },
  computed: {
    isEdit() {
      return (!!this.form.Id) && this.hasPermi(['role:crc'])
    }
  },
  mounted() {
    this.trialId = this.$route.query.trialId
    this.initForm()
  },
  methods: {
    async initForm() {
      this.loading = true
      const res = await getTrialSiteSelect(this.trialId)
      this.loading = false
      this.siteOptions = res.Result
      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
        addOrUpdateSubject(this.form).then(res => {
          this.btnLoading = false
          this.$emit('getList')
          this.$emit('close')
          this.$message.success(this.$t('common:message:savedSuccessfully'))
        })
          .catch(() => {
            this.btnLoading = false
          })
      })
    },
    handleCancel() {
      this.$emit('close')
    },
    getSite() {
      getTrialSiteSelect(this.trialId).then(res => { this.siteList = res.Result })
    }
  }
}
</script>
