<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="病灶类型" prop="OrganType">
        <el-select
          v-model="form.OrganType"
        >
          <el-option
            v-for="item of $d.OrganType"
            :key="item.id"
            :value="item.value"
            :label="item.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="分类" prop="Classification">
        <el-input :maxLength="$inputMax" v-model="form.Classification" />
      </el-form-item>
      <el-form-item label="分类（英文）">
        <el-input :maxLength="$inputMax" v-model="form.ClassificationEN" />
      </el-form-item>
      <!-- 部位 -->
      <el-form-item label="部位" prop="Part">
        <el-input :maxLength="$inputMax" v-model="form.Part" />
      </el-form-item>
      <el-form-item label="部位（英文）">
        <el-input :maxLength="$inputMax" v-model="form.PartEN" />
      </el-form-item>
      <!-- TULOC -->
      <el-form-item label="器官" prop="TULOC">
        <el-input :maxLength="$inputMax" v-model="form.TULOC" />
      </el-form-item>
      <el-form-item label="器官（英文）" prop="TULOCEN">
        <el-input :maxLength="$inputMax" v-model="form.TULOCEN" />
      </el-form-item>
      <!-- TULAT -->
      <el-form-item label="位置">
        <el-input :maxLength="$inputMax" v-model="form.TULAT" />
      </el-form-item>
      <el-form-item label="位置（英文）">
        <el-input :maxLength="$inputMax" v-model="form.TULATEN" />
      </el-form-item>
      <!-- 是否淋巴结 -->
      <el-form-item label="是否淋巴结" 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="是否可以编辑" 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="序号">
        <el-input :maxLength="$inputMax" v-model="form.ShowOrder" type="number" />
      </el-form-item>
      <!-- 描述 -->
      <el-form-item label="描述" prop="Remark">
        <el-input :maxLength="$inputMax"
          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 } 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'] }]
      },
      lesionTypes: [],
      loading: false
    }
  },
  mounted() {
    this.initForm()
  },
  methods: {
    // 保存
    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>
