<template>
  <el-form
    ref="organForm"
    v-loading="loading"
    :model="form"
    label-width="100px"
    size="small"
    :rules="rules"
  >
    <div class="base-dialog-body">
      <!-- 病灶类型 -->
      <el-form-item :label="$t('dictionary:template:basicData:lesionType')" prop="LesionType">
        <el-select v-model="form.LesionType">
          <el-option
            v-for="item of CriterionDictionaryList.LesionType"
            :key="item.Id"
            :value="parseInt(item.Code)"
            :label="item.ValueCN"
          />
        </el-select>
      </el-form-item>
      <!-- 器官类型 -->
      <el-form-item :label="$t('trials:readingUnit:baseDataCfg:title:organType')" prop="OrganType">
        <el-select v-model="form.OrganType">
          <el-option
            v-for="item of CriterionDictionaryOrganTypeList.OrganType"
            :key="item.Id"
            :value="parseInt(item.Code)"
            :label="item.ValueCN"
          />
        </el-select>
      </el-form-item>
    </div>
    <div class="base-dialog-footer" style="text-align:right;margin-top:10px;">
      <el-form-item style="text-align:right;">
        <el-button type="primary" @click="handleCancel"> {{ $t('common:button:cancel') }}</el-button>
        <!-- Save -->
        <el-button size="small" type="primary" @click="handleSave">
          {{ $t('common:button:save') }}
        </el-button>
      </el-form-item>
    </div>
  </el-form>
</template>
<script>
import { addOrUpdateCriterionNidus, getCriterionDictionary } from '@/api/dictionary'
export default {
  name: 'LesionTypeForm',
  props: {
    data: {
      type: Object,
      default() { return {} }
    }
  },

  data() {
    return {
      form: {
        Id: '',
        CriterionId: '',
        LesionType: '',
        OrganType: ''
      },
      rules: {
        LesionType: [{ required: true, message: this.$t('common:ruleMessage:select'), trigger: ['blur'] }],
        OrganType: [{ required: true, message: this.$t('common:ruleMessage:select'), trigger: ['blur'] }]
      },
      loading: false,
      CriterionDictionaryList: [],
      CriterionDictionaryOrganTypeList: []
    }
  },
  mounted() {
    this.initForm()
    this.getCriterionDictionary()
    this.getCriterionDictionaryOrganType()
  },
  methods: {
    getCriterionDictionary() {
      getCriterionDictionary({
        ReadingCriterionId: this.form.CriterionId,
        DictionaryCode: 'LesionType'
      }).then(res => {
        this.CriterionDictionaryList = res.Result
      })
    },
    getCriterionDictionaryOrganType() {
      getCriterionDictionary({
        ReadingCriterionId: this.form.CriterionId,
        DictionaryCode: 'OrganType'
      }).then(res => {
        this.CriterionDictionaryOrganTypeList = res.Result
      })
    },
    // 保存
    handleSave() {
      this.$refs.organForm.validate(valid => {
        if (!valid) return

        this.loading = true
        addOrUpdateCriterionNidus(this.form).then(res => {
          this.loading = false
          this.$emit('close')
          this.$emit('getList')
          if (this.form.Id) {
            this.$message.success(this.$t('common:message:savedSuccessfully'))
          } else {
            this.$message.success(this.$t('common:message:addedSuccessfully'))
          }
        }).catch(() => {
          this.loading = false
        })
      })
    },
    handleCancel() {
      this.$emit('close')
    },
    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.loading = false
    }
  }
}
</script>
