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

      <el-form :inline="true" :model="form" ref="form">
        <el-alert
          style="margin: 20px 0 40px"
          title="财务规则"
          :closable="false"
          type="info"
        >
        </el-alert>

        <el-row>
          <el-form-item
            prop="91"
            :rules="[
              {
                required: false,
                pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
                message: '请输入正确的人数'
              }
            ]"
          >
            基本课酬:
            <el-input v-model="form['91']" placeholder="请输入金额">
              <template slot="append"
                >元</template
              >
            </el-input>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item
            prop="73"
            :rules="[
              {
                required: false,
                pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
                message: '请输入正确的人数'
              }
            ]"
          >
            全职资源公摊金额:
            <el-input v-model="form['73']" placeholder="请输入金额">
              <template slot="append"
                >元</template
              >
            </el-input>
          </el-form-item>
        </el-row>

        <el-alert
          style="margin: 40px 0"
          title="特殊参数"
          :closable="false"
          type="info"
        >
        </el-alert>
        <!-- <el-row>
          <el-form-item prop="108">
            学练宝活动分部目标
            <el-input
              class="marginLR5"
              style="width: 180px"
              v-model="form['108']"

              placeholder="请输入分部目标"
            >
            </el-input>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item prop="109">
            学练宝活动预约开始时间
            <el-date-picker
              class="marginLR5"
              v-model="form['109']"
              type="datetime"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择开始日期"

            >
            </el-date-picker>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item prop="110">
            学练宝活动预约结束时间
            <el-date-picker
              class="marginLR5"
              v-model="form['110']"
              type="datetime"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择开始日期"

            >
            </el-date-picker>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item prop="111">
            学练宝活动开屏广告编号
            <el-input
              class="marginLR5"
              v-model="form['111']"

              placeholder="请输入编号"
              style="width: 180px"
            />
          </el-form-item>
        </el-row> -->
        <!-- <el-row>
          <el-form-item prop="115">
            支持考级活动报名的分部编号
            <el-input
              class="marginLR5"
              v-model="form['115']"
              placeholder="请输入编号"
              style="width: 180px"
            />
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item prop="199">
            考级活动开始时间:
            <el-date-picker
              class="marginLR5"
              v-model="form['199']"
              type="datetime"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择开始日期"
            >
            </el-date-picker>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item prop="200">
            考级活动结束时间:
            <el-date-picker
              class="marginLR5"
              v-model="form['200']"
              type="datetime"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择开始日期"
            >
            </el-date-picker>
          </el-form-item>
        </el-row> -->
        <!-- <el-row>
          <el-form-item
            prop="203"
            :rules="[
              {
                required: false,
                pattern: /^[1-9][0-9]*$/,
                message: '请输入正确的分钟数',
              },
            ]"
          >
            云教室可提前
            <el-input
              v-model="form['203']"
              placeholder="请输入分钟数"

            >
              <template slot="append">分钟</template> </el-input
            >进入教室
          </el-form-item>
        </el-row> -->
        <el-row>
          <el-form-item
            prop="61"
            :rules="[
              {
                required: false,
                pattern: /^[1-9][0-9]*$/,
                message: '请输入正确的分钟数'
              }
            ]"
          >
            云教室可提前:
            <el-input v-model="form['61']" placeholder="请输入分钟数">
              <template slot="append"
                >分钟</template
              >
            </el-input>
            进入教室。
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item
            prop="60"
            :rules="[
              {
                required: false,
                pattern: /^[1-9][0-9]*$/,
                message: '请输入正确的分钟数'
              }
            ]"
          >
            课程结束后:
            <el-input v-model="form['60']" placeholder="请输入分钟数">
              <template slot="append"
                >分钟</template
              >
            </el-input>
            网络教室自动关闭。
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item
            prop="232"
            :rules="[
              {
                required: true,
                message: '请输入系统消息用户编号'
              }
            ]"
          >
            系统消息用户编号:
            <el-input
              class="marginLR5"
              style="width: 180px"
              v-model="form['232']"
              placeholder="请输入系统消息用户编号"
            >
            </el-input
            >。
          </el-form-item>
        </el-row>
      </el-form>
      <el-button
        type="primary"
        @click="save"
        class="saveBtn"
        v-if="!isPlatform"
        v-permission="'sysTenantConfig/batchUpSet_otherRules'"
        >保存</el-button
      >
      <el-button
        type="primary"
        @click="save"
        class="saveBtn"
        v-else
        v-permission="'sysConfig/batchUpdate_otherRules'"
        >保存</el-button
      >
    </div>
  </div>
</template>
<script>
import {
  getSysTenantConfig,
  questionnaireTopicQueryPage,
  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();
    try {
      const res = await questionnaireTopicQueryPage({ page: 1, rows: 999 });
      this.questionList = res.data.rows.map(item => {
        return {
          label: item.title,
          value: String(item.id)
        };
      });
    } catch (e) {
      console.log(e);
    }
  },
  methods: {
    getRules() {
      if (this.isPlatform) {
        this.getPlatformRules();
      } else {
        this.getInstitutionRules();
      }
    },
    async getPlatformRules() {
      try {
        const res = await sysConfigList({ group: "DAYA_BASIC" });
        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_BASIC" });
        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>
