<template>
  <div class="m-container">
    <div class="m-core">
      <el-form :inline="true" :model="form" ref="form">

        <el-alert
          style="margin: 10px 0"
          title="学员请假规则设置"
          :closable="false"
          type="info"
        >
        </el-alert>
        <el-row>
          <el-form-item
            prop="7"
            :rules="[
              {
                required: false,
                pattern: /^[1-9][0-9]*$/,
                message: '请输入正确的时间',
              },
            ]"
          >
            课程开始前
            <el-input v-model="form['7']" placeholder="请输入时间">
              <template slot="append">
                <!-- class="chiose" <el-select class="append" v-model.trim="form.organId">
                <el-option label="天" value="yuan"> </el-option>
                <el-option label="小时" value="bi"> </el-option>
                <el-option label="分钟" value="bi"> </el-option>
              </el-select> -->
                小时
              </template>
            </el-input>
            前可提交请假申请。
          </el-form-item>

          <!-- <el-button  type="primary">保存</el-button> -->
        </el-row>
        <el-alert
          style="margin: 10px 0"
          title="课程调整规则"
          :closable="false"
          type="info"
        >
        </el-alert>
        <el-row>
          <el-form-item
            prop="41"
            :rules="[
              {
                required: false,
                pattern: /^[1-9][0-9]*$/,
                message: '请输入正确的时间',
              },
            ]"
          >
            老师可在开课
            <el-input v-model="form['41']" placeholder="请输入时间">
              <template slot="append">小时</template>
            </el-input>
            前调整VIP、网管课程。
          </el-form-item>
        </el-row>

        <el-alert
          style="margin: 20px 0"
          title="学员练习参数"
          :closable="false"
          type="info"
        >
        </el-alert>
        <el-row>
          <el-form-item
            prop="106"
            :rules="[
              {
                required: false,
                pattern: /^[1-9][0-9]*$/,
                message: '请输入正确的分钟数',
              },
            ]"
          >
            学员每周使用团练宝时长小于
            <el-input v-model="form['106']" placeholder="请输入分钟数">
              <template slot="append">分钟</template> </el-input
            >时需要回访。
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item
            prop="107"
            :rules="[
              {
                required: false,
                pattern: /^[1-9][0-9]*$/,
                message: '请输入正确的次数',
              },
            ]"
          >
            学员每周使用团练宝练习次数小于
            <el-input v-model="form['107']" placeholder="请输入次数">
              <template slot="append">次</template> </el-input
            >时需要回访。
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <el-button
      type="primary"
      @click="save"
      class="saveBtn"
      v-if="!isPlatform"
      v-permission="'sysTenantConfig/batchUpSet_studentAttRules'"
      >保存</el-button
    >
    <el-button
      type="primary"
      @click="save"
      class="saveBtn"
      v-else
      v-permission="'sysConfig/batchUpdate_studentAttRules'"
      >保存</el-button
    >
  </div>
</template>
<script>
import { getSysTenantConfig, setSysTenantConfig } from "../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: "COURSE" });
        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: "COURSE" });
        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 "../index.scss";
</style>
