<template>
  <el-form
    ref="addSubjectPR"
    v-loading="loading"
    :model="form"
    size="small"
    :rules="rules"
    label-width="180px"
  >
    <div class="base-dialog-body">
      <!-- 中心编号 -->
      <el-form-item :label="$t('trials:readingPeriod:table:siteCode')">
        <el-input v-model="form.TrialSiteCode" disabled />
      </el-form-item>
      <!-- 受试者编号 -->
      <el-form-item :label="$t('trials:readingPeriod:table:subjectCode')">
        <el-input v-model="form.SubjectCode" disabled />
      </el-form-item>
      <el-form-item :label="$t('trials:addSubjectPR:table:rpType')">
        <el-input v-model="form.ReadingSetTypeName" disabled />
      </el-form-item>
      <!-- 阅片期名称 -->
      <el-form-item :label="$t('trials:addSubjectPR:table:rpName')" prop="Name">
        <el-input v-model="form.Name" />
      </el-form-item>
      <el-form-item :label="$t('trials:addSubjectPR:table:deadlineVisit')" prop="VisitStageId">
        <el-select
          v-model="form.VisitStageId"
          clearable
          style="width:100%;"
          @change="handleVisitChange"
        >
          <el-option
            v-for="(item, index) of visitPlanOptions"
            :key="index"
            :label="item.VisitName"
            :value="item.SubjectVisitId"
          >
            <span style="float: left">{{ item.VisitName }}</span>
          </el-option>
        </el-select>
      </el-form-item>
    </div>
    <div class="base-dialog-footer" style="text-align:right;padding: 5px 0;">
      <el-form-item>
        <!-- 取消 -->
        <el-button
          :disabled="btnLoading"
          size="small"
          type="primary"
          @click="close"
        >
          {{ $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 { addReadModule, getSubjectReadVisitList, getCanChangeReadVisitList, changeCutOffVisit } from '@/api/trials'
export default {
  name: 'AddSubjectPR',
  props: {
    trialReadingCriterionId: {
      type: String,
      default: ''
    },
    data: {
      type: Object,
      default() { return {} }
    },
    type: {
      type: String,
      default() {
        return 'add'
      }
    }
  },
  data() {
    return {
      loading: false,
      btnLoading: false,
      visitPlanOptions: [],
      form: {
        Id: '',
        TrialId: '',
        SubjectId: '',
        SubjectCode: '',
        TrialSiteCode: '',
        Name: '',
        ReadingSetType: null,
        ReadingSetTypeName: '',
        VisitStageId: '',
        // ExpirationDate: '',
        ExpirationVisitNum: null
      },
      rules: {
        Name: [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' },
          { max: 50, message: `${this.$t('common:ruleMessage:maxLength')} 50` }
        ],
        ReadingSetType: [{ required: true, message: this.$t('common:ruleMessage:select'), trigger: ['blur', 'change'] }],
        VisitStageId: [{ required: true, message: this.$t('common:ruleMessage:select'), trigger: ['blur', 'change'] }]
      },
      trialId: '',
      readingSetTypes: []
    }
  },
  mounted() {
    this.trialId = this.$route.query.trialId
    this.initForm()
  },
  methods: {
    async initForm() {
      this.loading = true
      await this.getReadingSetType()
      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.ReadingSetTypeName = this.$fd('ReadingSetType', this.form.ReadingSetType)
      }
      this.form.TrialId = this.trialId
      this.loading = false
    },
    handleSave() {
      this.$refs.addSubjectPR.validate(valid => {
        if (!valid) return
        this.btnLoading = true
        this.form.TrialReadingCriterionId = this.trialReadingCriterionId
        if (this.type === 'add') {
          addReadModule(this.form).then(res => {
            this.btnLoading = false
            this.$emit('getList')
            this.$emit('close')
            this.form.Name = null
            this.form.VisitStageId = null
            this.$message.success(this.$t('common:message:savedSuccessfully'))
          })
            .catch(() => {
              this.btnLoading = false
            })
        } else {
          this.form.SubjectVisitIdId = this.form.VisitStageId
          changeCutOffVisit(this.form).then(res => {
            this.btnLoading = false
            this.$emit('getList')
            this.$emit('close')
            this.form.Name = null
            this.form.VisitStageId = null
            this.$message.success(this.$t('common:message:savedSuccessfully'))
          })
            .catch(() => {
              this.btnLoading = false
            })
        }
      })
    },
    handleVisitChange(val) {
      const selectArr = this.visitPlanOptions.filter(item => item.SubjectVisitId === val)
      if (selectArr.length > 0) {
        this.form.ExpirationVisitNum = selectArr[0].VisitNum
      } else {
        this.form.ExpirationVisitNum = null
      }
    },
    getReadingSetType() {
      this.visitPlanOptions = []
      return new Promise((resolve, reject) => {
        if (this.type === 'add') {
          getSubjectReadVisitList(this.data.ReadingSetType, this.data.SubjectId, this.trialReadingCriterionId).then(res => {
            this.visitPlanOptions = res.Result
            resolve()
          }).catch(() => {
            reject()
          })
        } else {
          getCanChangeReadVisitList({
            SubjectId: this.data.SubjectId,
            Id: this.data.Id,
            TrialReadingCriterionId: this.trialReadingCriterionId
          }).then(res => {
            this.visitPlanOptions = res.Result
            resolve()
          }).catch(() => {
            reject()
          })
        }
      })
    },
    close() {
      this.$emit('close')
    }
  }
}
</script>
