<template>
  <el-form
    ref="organForm"
    v-loading="loading"
    :model="form"
    label-width="120px"
    size="small"
    :rules="rules"
  >
    <div class="base-dialog-body">
      <!-- 类型 -->
      <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>
      <!-- 分类 -->
      <el-form-item :label="$t('trials:readingUnit:baseDataCfg:title:classification')" prop="Classification">
        <el-input v-model="form.Classification" />
      </el-form-item>
      <!-- 分类（英文） -->
      <el-form-item :label="$t('trials:readingUnit:baseDataCfg:title:classificationEn')">
        <el-input v-model="form.ClassificationEN" />
      </el-form-item>
      <!-- 部位 -->
      <el-form-item :label="$t('trials:readingUnit:baseDataCfg:title:bodyPart')" prop="Part">
        <el-input v-model="form.Part" />
      </el-form-item>
      <el-form-item :label="$t('trials:readingUnit:baseDataCfg:title:bodyPartEn')">
        <el-input v-model="form.PartEN" />
      </el-form-item>
      <!-- 器官 -->
      <el-form-item :label="$t('trials:readingUnit:baseDataCfg:title:organ')" prop="TULOC">
        <el-input v-model="form.TULOC" />
      </el-form-item>
      <el-form-item :label="$t('trials:readingUnit:baseDataCfg:title:TULOCEN')" prop="TULOCEN">
        <el-input v-model="form.TULOCEN" />
      </el-form-item>
      <!-- 位置 -->
      <el-form-item :label="$t('trials:readingUnit:baseDataCfg:title:TULAT')">
        <el-input v-model="form.TULAT" />
      </el-form-item>
      <el-form-item :label="$t('trials:readingUnit:baseDataCfg:title:TULATEn')">
        <el-input v-model="form.TULATEN" />
      </el-form-item>
      <!-- 是否淋巴结 -->
      <el-form-item :label="$t('trials:readingUnit:baseDataCfg:title:isLymphNodes')" prop="IsLymphNodes">
        <el-radio-group v-model="form.IsLymphNodes">
          <el-radio
            v-for="item of $d.ReadingYesOrNo"
            :key="'form.IsLymphNodes' + item.value"
            :label="item.value"
          >{{ item.label }}</el-radio>
        </el-radio-group>
      </el-form-item>

      <!-- 是否可以编辑 -->
      <el-form-item :label="$t('trials:readingUnit:baseDataCfg:title:isCanEditPosition')" prop="IsCanEditPosition">
        <el-radio-group v-model="form.IsCanEditPosition">
          <el-radio
            v-for="item of $d.YesOrNo"
            :key="'IsCanEditPosition' + item.value"
            :label="item.value"
          >{{ item.label }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="$t('trials:readingUnit:baseDataCfg:title:showOrder')" prop="ShowOrder">
        <el-input v-model="form.ShowOrder" type="number" />
      </el-form-item>
      <!-- 描述 -->
      <el-form-item :label="$t('trials:readingUnit:baseDataCfg:title:remark')" prop="Remark">
        <el-input
          v-model="form.Remark"
          type="textarea"
          :autosize="{ minRows: 5, maxRows: 6}"
          maxlength="500"
          show-word-limit
        />
      </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 { addOrUpdateOrganInfo, getCriterionNidusList, getCriterionDictionary } from '@/api/dictionary'
export default {
  name: 'OrganForm',
  props: {
    data: {
      type: Object,
      default() { return {} }
    },
    criterionId: {
      type: String,
      required: true
    }
  },

  data() {
    return {
      form: {
        Id: '',
        OrganType: '',
        SystemCriterionId: '',
        Classification: '',
        ClassificationEN: '',
        Part: '',
        PartEN: '',
        TULOC: '',
        TULOCEN: '',
        TULAT: '',
        TULATEN: '',
        Remark: '',
        IsLymphNodes: null,
        ShowOrder: null,
        IsCanEditPosition: false
      },
      rules: {
        OrganType: [{ required: true, message: this.$t('common:ruleMessage:select'), trigger: ['blur'] }],
        Classification: [{ required: true, message: this.$t('common:ruleMessage:specify'), trigger: ['blur'] }],
        Part: [{ required: true, message: this.$t('common:ruleMessage:specify'), trigger: ['blur'] }],
        TULOC: [{ required: true, message: this.$t('common:ruleMessage:specify'), trigger: ['blur'] }],
        Remark: [{ max: 500, message: `${this.$t('common:ruleMessage:maxLength')} 500`, trigger: ['blur', 'change'] }],
        IsLymphNodes: [{ required: true, message: this.$t('common:ruleMessage:select'), trigger: ['blur'] }],
        ShowOrder: [{ required: true, message: this.$t('common:ruleMessage:specify'), trigger: ['blur'] }]
      },
      lesionTypes: [],
      loading: false,
      CriterionDictionaryOrganTypeList: null
    }
  },
  mounted() {
    this.initForm()
    this.getCriterionDictionaryOrganType()
  },
  methods: {
    getCriterionDictionaryOrganType() {
      getCriterionDictionary({
        ReadingCriterionId: this.criterionId,
        DictionaryCode: 'OrganType'
      }).then(res => {
        this.CriterionDictionaryOrganTypeList = res.Result
      })
    },
    // 保存
    handleSave() {
      this.$refs.organForm.validate(valid => {
        if (!valid) return

        this.loading = true
        this.form.SystemCriterionId = this.criterionId
        addOrUpdateOrganInfo(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')
    },
    async initForm() {
      this.loading = true
      // await this.getLesionTypes()
      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
    },
    getLesionTypes() {
      return new Promise((resolve, reject) => {
        getCriterionNidusList({ criterionId: this.criterionId }).then(res => {
          this.lesionTypes = res.Result
          resolve()
        }).catch(() => {
          reject()
          this.loading = false
        })
      })
    }
  }
}
</script>
