<template>
  <el-form
    ref="organForm"
    v-loading="loading"
    :model="form"
    label-width="130px"
    size="small"
    :rules="rules"
  >
    <div class="base-dialog-body">
      <el-form-item label="标准名称" prop="CriterionName">
        <el-input
          v-model="form.CriterionName"
        />
      </el-form-item>

      <el-form-item label="标准类型" prop="CriterionType">
        <el-select v-model="form.CriterionType">
          <el-option
            v-for="item of $d.CriterionType"
            :key="item.id"
            :value="item.value"
            :label="item.label"
          />
        </el-select>
      </el-form-item>

      <el-form-item v-if="form.Id !== ''" label="是否配置完成">
        <el-switch v-model="form.IsCompleteConfig" />
      </el-form-item>
      <el-form-item label="描述">
        <el-input
          v-model="form.Description"
        />
      </el-form-item>

      <el-form-item label="显示序号: " prop="ShowOrder">
        <el-input-number v-model="form.ShowOrder" :min="0"  style="width:100%;" />
      </el-form-item>
      <el-form-item v-if="form.Id !== ''" label="是否启用">
        <el-switch v-model="form.IsEnable" />
      </el-form-item>
      <el-form-item v-if="form.Id !== ''" label="eCRF是否显示在影像阅片页面">
        <el-switch v-model="form.IseCRFShowInDicomReading" />
      </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 { addOrUpdateReadingQuestionCriterionSystem } from '@/api/dictionary'
export default {
  name: 'AddCriterionForm',
  props: {
    data: {
      type: Object,
      default() { return {} }
    }
  },

  data() {
    return {
      form: {
        Id: '',
        CriterionName: '',
        CriterionType: null,
        IsEnable: true,
        IsCompleteConfig: false,
        ShowOrder: null,
        Description: '',
        IseCRFShowInDicomReading: false
      },
      rules: {
        CriterionName: [{ required: true, message: this.$t('common:ruleMessage:specify'), trigger: ['blur'] }],
        CriterionType: [{ required: true, message: this.$t('common:ruleMessage:select'), trigger: ['blur'] }]
      },

      loading: false,
      criterions: []
    }
  },
  mounted() {
    this.initForm()
  },
  methods: {
    // 保存
    handleSave() {
      this.$refs.organForm.validate(valid => {
        if (!valid) return

        this.loading = true
        addOrUpdateReadingQuestionCriterionSystem(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() {
      this.loading = true
      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>
