<template>
  <div class="m-container">
    <div class="m-core">
      <!-- <el-button size="mini" type="primary">添加规则</el-button> -->

      <el-form :inline="true" :model="form" ref="form">
        <el-alert
          style="margin: 20px 0"
          title="预报名规则"
          :closable="false"
          type="info"
        >
        </el-alert>
        <el-row>
          <el-form-item
            prop="132"
            :rules="[
              {
                required: false,
                pattern: /^[1-9][0-9]*$/,
                message: '请输入正确的人数',
              },
            ]"
          >
            乐团预报名人数达到
            <el-input
              v-model="form['132']"
              size="mini"
              placeholder="请输入人数"
            >
              <template slot="append">人</template>
            </el-input>
            时，可将乐团状态变更为创建缴费中
          </el-form-item>
        </el-row>
        <el-alert
          style="margin: 20px 0"
          title="报名缴费规则"
          :closable="false"
          type="info"
        >
        </el-alert>
        <el-row>
          <el-form-item
            prop="134"
            :rules="[
              {
                required: false,
                pattern: /^[1-9][0-9]*$/,
                message: '请输入正确的人数',
              },
            ]"
          >
            报名缴费阶段乐团若未合并学员，缴费人数达到
            <el-input
              v-model="form['134']"
              size="mini"
              placeholder="请输入人数"
            >
              <template slot="append">人</template>
            </el-input>
            时，可确认开团
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item
            prop="135"
            :rules="[
              {
                required: false,
                pattern: /^[1-9][0-9]*$/,
                message: '请输入正确的人数',
              },
            ]"
          >
            报名缴费阶段乐团若合并学员，缴费人数达到
            <el-input
              v-model="form['135']"
              size="mini"
              placeholder="请输入人数"
            >
              <template slot="append">人</template>
            </el-input>
            时，可确认开团
          </el-form-item>
        </el-row>
      </el-form>
      <el-button  v-if="!isPlatform" size="mini" type="primary" @click="save" class="saveBtn"
       v-permission="'sysTenantConfig/batchUpSet_dayaTeamRules'"
        >保存</el-button
      >
        <el-button v-else size="mini" type="primary" @click="save" class="saveBtn"
       v-permission="'sysConfig/batchUpdate_dayaTeamRules'"
        >保存</el-button
      >
    </div>
  </div>
</template>
<script>
import {
  getSysTenantConfig,
  setSysTenantConfig,
} from "@/views/courseRulersManager/api";
import { sysConfigList, sysConfigUpdate } from "@/api/generalSettings"; // 平台的修改和查
export default {
  props: ["isPlatform"],
  data() {
    return {
      form: {},
      questionList: [],
      rulesVisiable: false,
    };
  },
  async mounted() {
    this.getRules();
  },
  methods: {
    getRules() {
      if (this.isPlatform) {
        this.getPlatformRules();
      } else {
        this.getInstitutionRules();
      }
    },
    async getPlatformRules() {
      try {
        const res = await sysConfigList({ group: "DAYA_MUSIC" });
        res.data.forEach((item) => {
          // this.form[item.paramName] = item.paranValue
          this.$set(this.form, item.id, item.paranValue);
        });
        // this.$forceUpdate()
        console.log(this.form);
      } catch (e) {
        console.log(e);
      }
    },
    async getInstitutionRules() {
      try {
        const res = await getSysTenantConfig({ group: "DAYA_MUSIC" });
        res.data.forEach((item) => {
          // this.form[item.paramName] = item.paranValue
          this.$set(this.form, item.id, item.paranValue);
        });
        // this.$forceUpdate()
        console.log(this.form);
      } catch (e) {
        console.log(e);
      }
    },
    savePlatform() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          let param = [];
          for (let i in this.form) {
            param.push({
              id: i,
              paranValue: this.form[i],
            });
          }
          try {
            const res = await sysConfigUpdate(param);
            this.$message.success("保存成功");
            this.getRules();
          } catch (e) {
            console.log(e);
          }
        }
      });
    },
    saveInstitution() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          let param = [];
          for (let i in this.form) {
            param.push({
              id: i,
              paranValue: this.form[i],
            });
          }
          try {
            const res = await setSysTenantConfig(param);
            this.$message.success("保存成功");
            this.getRules();
          } catch (e) {
            console.log(e);
          }
        }
      });
    },
    save() {
      if (this.isPlatform) {
        this.savePlatform();
      } else {
        this.saveInstitution();
      }
    },
  },
};
</script>
<style lang="scss" scoped>
@import "~@scss/views/courseRulersManager/index.scss";
</style>
