<template>
  <div class="school-set-sync">
    <el-form
      v-if="fields.length"
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      label-width="140px"
    >
      <el-form-item
        v-for="field in fields"
        :key="field.code"
        :prop="field.prop"
        :label="field.name + '：'"
      >
        <template v-if="field.options">
          <el-radio-group v-model="field.value">
            <el-radio
              v-for="item in field.options"
              :key="item.value"
              :label="item.value"
              >{{ item.label }}</el-radio
            >
          </el-radio-group>
        </template>
        <template v-else>
          <el-radio-group v-model="field.enable" @change="resetVal(field)">
            <el-radio
              v-for="item in OPEN_STATUS"
              :key="item.value"
              :label="item.value"
              >{{ item.label }}</el-radio
            >
          </el-radio-group>
          <br />
          <el-input
            v-if="field.enable"
            v-model.trim="field.value"
            placeholder="请输入调用地址"
            clearable
          >
          </el-input>
        </template>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :loading="loading" @click="confirm"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { schoolSetSyncInfo, schoolSetSyncUpdate } from "../../api";
import { OPEN_STATUS } from "../../../../constants/enumerate";

export default {
  name: "school-set-sync",
  props: {
    school: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      loading: false,
      OPEN_STATUS,
      modalForm: {},
      fields: [],
      rules: {},
    };
  },
  mounted() {
    this.initData();
  },
  methods: {
    async initData() {
      const data = await schoolSetSyncInfo(this.school.id);
      this.fields = data.result || [];
      this.fields.forEach((field) => {
        field.prop = field.code.split(".").join("_");
        this.$set(this.modalForm, field.prop, field.value);
        if (field.options) return;

        this.rules[field.prop] = [
          {
            validator: (rule, value, callback) => {
              if (!field.value && field.enable) {
                return callback(new Error(`请输入${field.name}请求地址`));
              }

              callback();
            },
            trigger: "change",
          },
        ];
      });
    },
    resetVal(field) {
      if (!field.enable) field.value = "";
    },
    async confirm() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      if (this.loading) return;
      this.loading = true;
      const datas = { param: this.fields, schoolId: this.school.id };
      const res = await schoolSetSyncUpdate(datas).catch(() => {});
      this.loading = false;
      if (!res) return;

      this.$message.success("修改成功！");
      this.initData();
    },
  },
};
</script>
