<template>
  <el-form
    ref="CROForm"
    :model="form"
    :rules="rules"
    class="demo-ruleForm"
    size="small"
    label-width="140px"
  >
    <div class="base-dialog-body">
      <!-- CRO Name -->
      <el-form-item :label="$t('institutions:cros:label:croName')" prop="CROName">
        <el-input v-model="form.CROName" />
      </el-form-item>
      <!-- CRO NameCN -->
      <el-form-item :label="$t('institutions:cros:label:croNameCN')" prop="CRONameCN">
        <el-input v-model="form.CRONameCN" />
      </el-form-item>
      <!-- CRO Code -->
      <el-form-item :label="$t('institutions:cros:label:croCode')" prop="CROCode">
        <el-input v-model="form.CROCode" />
      </el-form-item>
      <!-- Level -->
      <el-form-item :label="$t('institutions:cros:label:level')">
        <el-switch
          :disabled="!IsTrialLevel"
          v-model="form.IsTrialLevel"
          :active-text="$fd('IsTrialLevel', 'true')"
          :inactive-text="$fd('IsTrialLevel', 'false')"
        >
        </el-switch>
      </el-form-item>
    </div>
    <div class="base-dialog-footer" style="text-align: right; margin-top: 10px">
      <el-form-item>
        <el-button :disabled="btnLoading" type="primary" @click="handleCancel"
          >{{ $t('common:button:cancel') }}</el-button
        >
        <el-button type="primary" :loading="btnLoading" @click="handleSave"
          >{{ $t('common:button:save') }}</el-button
        >
      </el-form-item>
    </div>
  </el-form>
</template>
<script>
import { addOrUpdateCro } from "@/api/dictionary";
export default {
  name: "CroForm",
  props: {
    data: {
      type: Object,
      default() {
        return {};
      },
    },
    IsTrialLevel: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      btnLoading: false,
      form: {
        Id: "",
        CROName: "",
        CRONameCN: "",
        CROCode: "",
        IsTrialLevel: true,
      },
      rules: {
        CROName: [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: "blur" },
          { max: 500, message: this.$t('common:ruleMessage:maxLength') + ' 500' },
        ],
        CRONameCN: [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: "blur" },
          { max: 500, message: this.$t('common:ruleMessage:maxLength') + ' 500' },
        ],
        CROCode: [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: "blur" },
          { max: 50, message: this.$t('common:ruleMessage:maxLength') + ' 50' },
        ],
      },
    };
  },
  mounted() {
    if (Object.keys(this.data).length && this.data.Id) {
      this.form = { ...this.data };
    }
  },
  methods: {
    handleSave() {
      this.$refs.CROForm.validate((valid) => {
        if (!valid) return;
        this.btnLoading = true;
        addOrUpdateCro(this.form)
          .then((res) => {
            this.btnLoading = false;
            if (res.IsSuccess) {
              this.$message.success(this.$t('common:message:savedSuccessfully'));
              this.$refs["CROForm"].resetFields();
              this.$emit("getList");
              this.$emit("close");
            }
          })
          .catch(() => {
            this.btnLoading = false;
          });
      });
    },
    handleCancel() {
      this.$emit("close");
    },
  },
};
</script>
