<template>
  <el-form
    ref="sponsorForm"
    :model="form"
    :rules="rules"
    class="demo-ruleForm"
    size="small"
    label-width="150px"
  >
    <div class="base-dialog-body">
      <!-- Sponsor Name -->
      <el-form-item :label="$t('institutions:sponsors:label:sponsorName')" prop="SponsorName">
        <el-input v-model="form.SponsorName" />
      </el-form-item>
      <!-- Sponsor NameCN -->
      <el-form-item :label="$t('institutions:sponsors:label:sponsorNameCN')" prop="SponsorName">
        <el-input v-model="form.SponsorNameCN" />
      </el-form-item>
      <!-- Sponsor Code -->
      <el-form-item :label="$t('institutions:sponsors:label:sponsorCode')" prop="SponsorCode">
        <el-input v-model="form.SponsorCode" />
      </el-form-item>
      <!-- Level -->
      <el-form-item :label="$t('institutions:sponsors: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"
          size="small"
          type="primary"
          @click="handleCancel"
        >
          {{ $t('common:button:cancel') }}
        </el-button>
        <el-button
          size="small"
          type="primary"
          :loading="btnLoading"
          @click="handleSave"
        >
          {{ $t('common:button:save') }}
        </el-button>
      </el-form-item>
    </div>
  </el-form>
</template>
<script>
import { addOrUpdateSponsor } from "@/api/dictionary";
export default {
  name: "SponsorForm",
  props: {
    data: {
      type: Object,
      default() {
        return {};
      },
    },
    IsTrialLevel: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      btnLoading: false,
      form: {
        Id: "",
        SponsorName: "",
        SponsorNameCN: "",
        SponsorCode: "",
        IsTrialLevel: true,
      },
      rules: {
        SponsorName: [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: "blur" },
          { max: 500, message: this.$t('common:ruleMessage:maxLength') + ' 500' },
        ],
        SponsorNameCN: [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: "blur" },
          { max: 500, message: this.$t('common:ruleMessage:maxLength') + ' 500' },
        ],
        SponsorCode: [
          { 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.sponsorForm.validate((valid) => {
        if (!valid) return;
        this.btnLoading = true;
        addOrUpdateSponsor(this.form)
          .then((res) => {
            this.btnLoading = false;
            if (res.IsSuccess) {
              this.$message.success(this.$t('common:message:savedSuccessfully'));
              this.$refs["sponsorForm"].resetFields();
              this.$emit("getList");
              this.$emit("close");
            }
          })
          .catch(() => {
            this.btnLoading = false;
          });
      });
    },
    handleCancel() {
      this.$emit("close");
    },
  },
};
</script>
