<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="250px" size="small">
      <fieldset>
        <legend>登录策略配置</legend>
        <el-form-item label="登录失败次数限制" prop="loginFails">
          <el-input v-model="form.loginFails" style="width: 240px"></el-input>
        </el-form-item>
        <el-form-item label="登录失败锁定时长(至少20分钟)" prop="loginLock">
          <el-input v-model="form.loginLock" style="width: 240px"></el-input>
        </el-form-item>
        <el-form-item label="非活动状态登出系统(设定分钟数)" prop="inactiveLogout">
          <el-input v-model="form.inactiveLogout" style="width: 240px"></el-input>
        </el-form-item>
        <el-form-item label="并发会话数(1-10个)" prop="bfhhs">
          <el-input v-model="form.bfhhs" style="width: 240px"></el-input>
        </el-form-item>
      </fieldset>
      <fieldset>
        <legend>账号、密码策略配置</legend>
        <el-form-item label="密码规则设置">
          <el-checkbox-group v-model="form.passwordRule">
            <el-checkbox v-for="(item,index) in passwordRuleList" :key="index" :label="item.value">{{ item.label }}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="账号" prop="scanAccount">
          <el-input v-model="form.scanAccount" style="width: 60px"></el-input>
          <span style="font-weight: bold;color: #606266;">  月数内未使用锁定。</span>
        </el-form-item>
      </fieldset>
<!--      <fieldset>-->
<!--        <legend>审计策略配置</legend>-->
<!--        <el-form-item label="审计策略配置" prop="auditableEvent">-->
<!--          <el-checkbox-group v-model="form.auditableEvent">-->
<!--            <el-checkbox v-for="(item,index) in auditableEventList" :key="index" :label="item.value">{{ item.label }}-->
<!--            </el-checkbox>-->
<!--          </el-checkbox-group>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="审计日志存储额定容量上限(GB)" prop="auditLogBakCapAlarm" type="number">-->
<!--          <el-input v-model="form.auditLogBakCapAlarm" style="width: 240px"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="审计日志存储剩余容量低于额定容量" prop="logSpaceWarn">-->
<!--          <el-input v-model="form.logSpaceWarn" style="width: 60px"></el-input>-->
<!--          <span style="font-weight: bold;color: #606266;">  (%)时，邮箱告警。</span>-->
<!--        </el-form-item>-->
<!--      </fieldset>-->
<!--      <fieldset>-->
<!--        <legend>系统策略配置</legend>-->
<!--        <el-form-item label="剩余内存低于总内存" prop="memoryWarn">-->
<!--          <el-input v-model="form.memoryWarn" style="width: 60px"></el-input>-->
<!--          <span style="font-weight: bold;color: #606266">  (%)时，邮箱告警。</span>-->
<!--        </el-form-item>-->
<!--        <el-divider></el-divider>-->
<!--        <el-row>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="A级别异常通知方式" prop="excNoticeWayA">-->
<!--              <el-select v-model="form.excNoticeWayA" placeholder="请选择通知方式" style="width: 240px">-->
<!--                <el-option-->
<!--                  v-for="item in excNoticeOptions"-->
<!--                  :key="item.value"-->
<!--                  :label="item.label"-->
<!--                  :value="item.value"-->
<!--                ></el-option>-->
<!--              </el-select>-->
<!--            </el-form-item>-->
<!--            <el-form-item label="B级别异常通知方式" prop="excNoticeWayB">-->
<!--              <el-select v-model="form.excNoticeWayB" placeholder="请选择通知方式" style="width: 240px">-->
<!--                <el-option-->
<!--                  v-for="item in excNoticeOptions"-->
<!--                  :key="item.value"-->
<!--                  :label="item.label"-->
<!--                  :value="item.value"-->
<!--                ></el-option>-->
<!--              </el-select>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="连续登录失败异常级别" prop="excLevelLogin">-->
<!--              <el-select v-model="form.excLevelLogin" placeholder="请选择异常级别" style="width: 240px">-->
<!--                <el-option-->
<!--                  v-for="item in excLevelOptions"-->
<!--                  :key="item.value"-->
<!--                  :label="item.label"-->
<!--                  :value="item.value"-->
<!--                ></el-option>-->
<!--              </el-select>-->
<!--            </el-form-item>-->
<!--            <el-form-item label="同一用户多点登录异常级别" prop="excLevelSameUser">-->
<!--              <el-select v-model="form.excLevelSameUser" placeholder="请选择异常级别" style="width: 240px">-->
<!--                <el-option-->
<!--                  v-for="item in excLevelOptions"-->
<!--                  :key="item.value"-->
<!--                  :label="item.label"-->
<!--                  :value="item.value"-->
<!--                ></el-option>-->
<!--              </el-select>-->
<!--            </el-form-item>-->
<!--            <el-form-item label="越权访问异常级别" prop="excLevelLogin">-->
<!--              <el-select v-model="form.unauthorizedAccessExceptionLevel" placeholder="请选择异常级别" style="width: 240px">-->
<!--                <el-option-->
<!--                  v-for="item in excLevelOptions"-->
<!--                  :key="item.value"-->
<!--                  :label="item.label"-->
<!--                  :value="item.value"-->
<!--                ></el-option>-->
<!--              </el-select>-->
<!--            </el-form-item>-->
<!--            <el-form-item label="IP地址异常级别" prop="excLevelSameUser">-->
<!--              <el-select v-model="form.ipAddressExceptionLevel" placeholder="请选择异常级别" style="width: 240px">-->
<!--                <el-option-->
<!--                  v-for="item in excLevelOptions"-->
<!--                  :key="item.value"-->
<!--                  :label="item.label"-->
<!--                  :value="item.value"-->
<!--                ></el-option>-->
<!--              </el-select>-->
<!--            </el-form-item>-->
<!--          </el-col>-->

<!--        </el-row>-->

<!--      </fieldset>-->
      <el-form-item>
        <el-button type="primary" @click="onSubmit">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import {debounce} from "lodash";

export default {
  data() {
    return {
      passwordRuleList: [
        {value: 'A', label: '大写字母'},
        {value: 'B', label: '小写字母'},
        {value: 'C', label: '数字'},
        {value: 'D', label: '特殊字符：!@$%^*()_-+=?/'},
      ],
      excNoticeOptions: [
        {value: '0', label: '邮件'}
      ],
      excLevelOptions: [
        {value: '0', label: 'A'},
        {value: '1', label: 'B'}
      ],
      auditableEventList: [
        {value: '0', label: '不审计NWP预测查询'},
        {value: '1', label: '不审计短期预测查询'}
      ],
      form: {
        loginFails: '',
        loginLock: '',
        scanAccount: '',
        inactiveLogout: '',
        auditLog: '',
        auditLogBakCapAlarm: 1,
        memoryWarn: '',
        bfhhs: '',
        logSpaceWarn: '',
        excNoticeWayA: '0',
        excNoticeWayB: '0',
        excLevelLogin: "0",
        excLevelSameUser: '0',
        unauthorizedAccessExceptionLevel: '0',
        ipAddressExceptionLevel: '0',
        passwordRule: [],
        auditableEvent: []
      },
      // 表单校验
      rules: {
        loginFails: [
          {required: true, message: "登录失败次数限制不能为空", trigger: "blur"},
          {pattern: /^([1-9]|10)$/, message: "请输入1-10整数", trigger: "blur"}
        ],
        scanAccount: [
          {required: true, message: "设定月数不能为空", trigger: "blur"},
          {pattern: /^([1-3])$/, message: "请输入1-3整数", trigger: "blur"}
        ],
        loginLock: [
          {required: true, message: "登录失败锁定时长不能为空", trigger: "blur"},
          {pattern: /^(?:[2-9]\d|100)$/, message: "请输入20-100整数", trigger: "blur"}
        ],
        inactiveLogout: [
          {required: true, message: "非活动状态登出系统不能为空", trigger: "blur"},
          {pattern: /^([1-9]|[1-2]\d|30)$/, message: "请输入1-30整数", trigger: "blur"}
        ],
        auditLog: [
          {required: true, message: "审计日志保留月数不能为空", trigger: "blur"},
          {pattern: /^([6-9]|(1[0-2]))$/, message: "请输入6-12整数", trigger: "blur"}
        ],
        auditLogBakCapAlarm: [
          {required: true, message: "审计日志保存容量上限不能为空", trigger: "blur"},
          {pattern: /^([1-9]|10)$/, message: "请输入1-10整数", trigger: "blur"}
        ],
        memoryWarn: [
          {required: true, message: "内存设定值不能为空", trigger: "blur"},
          {pattern: /^(?:[2-8]\d|90)$/, message: "请输入20-90整数", trigger: "blur"}
        ],
        logSpaceWarn: [
          {required: true, message: "日志存储设定值告警不能为空", trigger: "blur"},
          {pattern: /^(?:[2-8]\d|90)$/, message: "请输入20-90整数", trigger: "blur"}
        ],
        bfhhs: [
          {required: true, message: "并发会话数不能为空", trigger: "blur"},
          {pattern: /^([1-9]|10)$/, message: "请输入1-10整数", trigger: "blur"}
        ]
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询用户列表 */
    async getList() {
      await this.$axios.get('/sysPolicyController/getAll').then((res) => {
        this.form = res.data
        this.form.passwordRule = res.data.passwordRule.split(',')

        if (res.data.auditableEvent==null){
          this.form.auditableEvent = []
        }
        else{
          this.form.auditableEvent = res.data.auditableEvent.split(',')
        }
      }).catch((error) => {
        // this.$message.error(error)
      })
    },
    onSubmit: debounce(async function () {
      this.$refs["form"].validate(async valid => {
        if (valid) {
          if (this.form.passwordRule.length < 3) {
            this.$message.warning('密码规则至少需要3种以上的组合！')
            return
          } else {
            this.form.passwordRule = this.form.passwordRule.join(",")
          }
          this.form.auditableEvent = this.form.auditableEvent.join(",")

          this.loading = true
          await this.$axios.post('/sysPolicyController', this.form).then((res) => {
            if (res.code == 0) {
              this.$message.success('策略配置保存成功')
            }
            if (res.code == 1) {
              this.$message.error(res.data)
            }
            this.loading = false
            this.getList()
          }).catch((error) => {
            this.$message.error(error)
            this.loading = false
          })
        }
      });
    }, 1000)
  }
}
</script>

<style lang="scss" scoped>

</style>
