<template>
  <el-form
    ref="organForm"
    v-loading="loading"
    :model="form"
    label-width="170px"
    size="small"
    :rules="rules"
  >
    <div class="base-dialog-body">
      <!-- 标准名称 -->
      <el-form-item :label="$t('trials:auditRecord:table:criterion')" prop="CriterionName">
        <el-input
          v-model="form.CriterionName"
        />
      </el-form-item>
      <!-- 标准类型 -->
      <el-form-item :label="$t('dictionary:template:criterionConfig:table:criterionType')" 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 :label="$t('dictionary:template:criterionConfig:table:criterionGroup')" prop="CriterionGroup">
        <el-select v-model="form.CriterionGroup">
          <el-option
            v-for="item of $d.CriterionGroup"
            :key="item.id"
            :value="item.value"
            :label="item.label"
          />
        </el-select>
      </el-form-item>
      <!-- 是否配置完成 -->
      <el-form-item v-if="form.Id !== ''" :label="$t('dictionary:template:criterionConfig:table:isCompleteConfig')">
        <el-switch v-model="form.IsCompleteConfig" />
      </el-form-item>
      <!-- 描述 -->
      <el-form-item :label="$t('dictionary:template:criterionConfig:table:description')">
        <el-input
          v-model="form.Description"
        />
      </el-form-item>
      <!-- 显示序号 -->
      <el-form-item :label="$t('dictionary:template:criterionConfig:table:showOrder')" 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="$t('dictionary:template:criterionConfig:table:isEnable')">
        <el-switch v-model="form.IsEnable" />
      </el-form-item>
      <!-- eICRF仅展示阅片Tab -->
      <el-form-item v-if="form.Id !== ''" :label="$t('dictionary:template:criterionConfig:table:isECRFShowInDicomReading')">
        <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,
        CriterionGroup: 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'] }],
        CriterionGroup: [{ 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>
