<template>
  <div class="school-set-check">
    <el-form ref="modalFormComp" label-width="240px">
      <el-form-item
        v-for="field in fields"
        :key="field.code"
        :prop="field.code"
        :label="field.name + '：'"
      >
        <el-radio-group v-if="field.isBoolean" v-model="field.value">
          <el-radio
            v-for="item in OPEN_STATUS"
            :key="item.value"
            :label="item.value"
            >{{ item.label }}</el-radio
          >
        </el-radio-group>
        <el-input
          v-else
          v-model="field.value"
          :placeholder="`请输入${field.name}`"
          clearable
        >
        </el-input>
      </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 { OPEN_STATUS } from "../../../../constants/enumerate";
import { schoolSetCheckInfo, schoolSetCheckUpdate } from "../../api";

export default {
  name: "school-set-check",
  props: {
    school: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  data() {
    return {
      loading: false,
      fields: [],
      OPEN_STATUS
    };
  },
  mounted() {
    this.initData();
  },
  methods: {
    async initData() {
      const data = await schoolSetCheckInfo(this.school.id);
      this.fields = data.result || [];
      this.fields.forEach(field => {
        field.isBoolean =
          typeof field.value === "boolean" ||
          ["true", "false"].includes(field.value);
        if (field.isBoolean && ["true", "false"].includes(field.value))
          field.value = field.value === "true";
      });
    },
    async confirm() {
      if (this.loading) return;
      this.loading = true;
      const datas = { param: this.fields, schoolId: this.school.id };
      const res = await schoolSetCheckUpdate(datas).catch(() => {});
      this.loading = false;
      if (!res) return;

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