<template>
  <div class="app-container">
        <el-form :model="form" ref="form" :rules="rules" size="small"  label-width="250px">
          <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="密码规则设置">
            <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: 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="审计日志保留月数" prop="auditLog">
            <el-input v-model="form.auditLog" style="width: 240px"></el-input>
          </el-form-item>
          <el-form-item label="内存低于阈值告警(%)" prop="memoryWarn">
            <el-input v-model="form.memoryWarn" style="width: 240px"></el-input>
          </el-form-item>
          <el-form-item label="日志存储低于阈值告警(%)" prop="logSpaceWarn">
            <el-input v-model="form.logSpaceWarn" style="width: 240px"></el-input>
          </el-form-item>
          <el-form-item label="A级别异常通知方式" prop="excNoticeWayA">
            <el-select style="width: 240px" v-model="form.excNoticeWayA" placeholder="请选择通知方式">
              <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 style="width: 240px" v-model="form.excNoticeWayB" placeholder="请选择通知方式">
              <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="连续登录失败异常级别" prop="excLevelLogin">
            <el-select style="width: 240px" v-model="form.excLevelLogin" placeholder="请选择异常级别">
              <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 style="width: 240px" v-model="form.excLevelSameUser" placeholder="请选择异常级别">
              <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>
            <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: '邮件'},
        {value: '1', label: '告警'}
      ],
      excLevelOptions: [
        {value: '0', label: 'A'},
        {value: '1', label: 'B'}
      ],
      form: {
        loginFails: '',
        loginLock: '',
        scanAccount: '',
        inactiveLogout: '',
        auditLog: '',
        memoryWarn: '',
        logSpaceWarn:'',
        excNoticeWayA: '0',
        excNoticeWayB: '0',
        excLevelLogin: "0",
        excLevelSameUser: '0',
        passwordRule: ''
      },
      // 表单校验
      rules: {
        loginFails: [
          { required: true, message: "登录失败次数限制不能为空", trigger: "blur" },
          { pattern: /^([1-9]|10)$/, message: "请输入1-10整数", trigger: "blur"}
        ],
        scanAccount: [
          { required: true, message: "扫描未使用的账号不能为空", trigger: "blur" },
          { pattern: /^([0-9]|(1[0-2]))$/, message: "请输入0-12整数", 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: /^(?:[2-9]\d|100)$/, message: "请输入20-100整数", trigger: "blur"}
        ],
        auditLog: [
          { required: true, message: "审计日志保留月数不能为空", trigger: "blur" },
          { pattern: /^([3-9]|(1[0-2]))$/, message: "请输入3-12整数", 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"}
        ]
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询用户列表 */
    async getList(){
      let sysTime
      let lk
      await this.$axios.get('/sysPolicyController/getLicenseKey').then((res) => {
        sysTime = res.data.sysTime
        lk = res.data.lk
      }).catch((error) => {
      })
      var searchParams = {
        sysTime: sysTime,
        lk: lk
      }
      await this.$axios.get('/sysPolicyController/getAll',{params: searchParams}).then((res) => {
        this.form = res.data
        this.form.passwordRule = res.data.passwordRule.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(",")
          }

          let sysTime
          let lk
          await this.$axios.get('/sysPolicyController/getLicenseKey').then((res) => {
            sysTime = res.data.sysTime
            lk = res.data.lk
          }).catch((error) => {
          })

          this.form.sysTime = sysTime
          this.form.lk = lk
          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>
