<template>
  <div>
    <add-page-layout>
      <div slot="Header-Content">
        <span>事件分类规则 {{ headTitle }}</span>
        <i class="el-icon-close close" @click="$router.go(-1)" />
      </div>
      <div slot="Form-Content">
        <div style="margin:15px">
          <el-row style="margin-left:64px">
            <span style="font-size: 12px;color:#F56C6C;">
                添加关键字的格式为:-SC:((关键字1)),-SC:((关键字2))等多个中间用逗号隔开;添加正则表达式的格式为:-RE:((您的正则式))
            </span>
          </el-row>
          <el-row style="margin-top:20px">
            <el-form :model="addnewForm.eventclassRuleInfo" :rules="rules" label-width="120px" ref="addnewForm">
                <el-row>
                    <el-col :span="10">
                        <el-form-item label="规则名"  prop="ruleName">
                            <el-input v-model="addnewForm.eventclassRuleInfo.ruleName" :disabled="disabled"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="CVE编码">
                            <el-input v-model="addnewForm.eventclassRuleInfo.cveName" :disabled="disabled"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="10">
                        <el-form-item label="分类等级"  prop="ruleLevel">
                            <el-select v-model="addnewForm.eventclassRuleInfo.ruleLevel"  placeholder="请选择" style="width:100%;" :disabled="disabled">
                                <el-option v-for="item in statics.ruleLevelList" :label="item.label" :key="item.value" :value="item.value"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="规则描述">
                            <el-input type="textarea" :rows="2" v-model="addnewForm.eventclassRuleAssoc.ruleDescription" :disabled="disabled"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="10">
                        <el-form-item label="建议措施">
                            <el-input type="textarea" :rows="2" v-model="addnewForm.eventclassRuleAssoc.suggestionTag" :disabled="disabled"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="误报信息">
                            <el-input type="textarea" :rows="2" v-model="addnewForm.eventclassRuleAssoc.falsePositive" :disabled="disabled"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="10">
                        <el-form-item label="关键字">
                            <el-input  v-model="addnewForm.eventclassRuleInfo.keyWord" :disabled="disabled"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="正则表达式">
                            <el-input  v-model="addnewForm.eventclassRuleInfo.regex" :disabled="disabled"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="10">
                        <el-form-item label="规则所属设备" prop="test" class="is-required">
                            <el-select v-model="addnewForm.eventclassRuleAssoc.typeId"  placeholder="请选择" style="width:100%;" :disabled="disabled" filterable clearable>
                                <el-option v-for="item in statics.scannerTypeList" :label="item.dataName" :key="item.dataId" :value="item.dataId"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
          </el-row>
          <el-row type="flex" justify="center">
              <el-button @click="$router.go(-1)">取消</el-button>
              <el-button v-if="oper != 'detail'" type="primary" @click="submit" :loading="submitLoading">提交</el-button>
          </el-row>
        </div>
      </div>
    </add-page-layout>
  </div>
</template>

<script>

import { getAllDeviceType } from '../logAuditManage/api/log-retrival-manage'
import { 
    updEventclassRuleInfo,
    addEventclassRuleInfo,
    getEventclassRuleInfoByRuleId
} from './api/eventStrategy'
export default {
  name: "classifyRulesListAdd",
  components: {},
  data() {
    let validType = (rule, value, callback) => {
        if (!value) {
          callback(new Error('请选择规则所属设备'));
        } else {
          callback();
        }
    }
    return {
        headTitle: "",
        oper: "",
        addnewForm: {
            eventclassRuleInfo: {
                ruleName: '',
                ruleLevel: '',
                cveName: '',
                readOnlyTag: '0',
                validTag: '1',
                keyWord: '',
                regex: '',
                test: ''
            },
            eventclassRuleAssoc: {
                ruleDescription: '',
                suggestionTag: '',
                falsePositive: ''
            }
        },
        rules: {
            ruleName: [
                { required: true, message: '请输入过滤规则名称', trigger: 'blur' }
            ],
            ruleLevel: [
                { required: true, message: '请选择分类等级', trigger: 'change' }
            ],
            test: [
                { validator: validType, trigger: 'change' }
            ]
        },
        statics: {
            ruleLevelList: [
                {
                    value: '1',
                    label: '轻微'
                }, 
                {
                    value: '2',
                    label: '低级'
                }, 
                {
                    value: '3',
                    label: '中级'
                }, 
                {
                    value: '4',
                    label: '高级'
                }, 
                {
                    value: '5',
                    label: '严重'
                }
            ],
            scannerTypeList: [],
        },
        submitLoading: false,
        disabled: false,
    };
  },
  computed: {},
  watch:{
    'addnewForm.eventclassRuleAssoc.typeId'(value, oldValue) {
        this.addnewForm.eventclassRuleInfo.test = value ? "123" : null
    },
  },
  created() {
    this.oper = this.$route.params.oper;
    if (this.oper == "detail") {
      this.disabled = true
      this.headTitle = "查看"
    } else if (this.oper == "add") {
      this.headTitle = "新增"
    } else if (this.oper == "modify") {
      this.headTitle = "修改"
    }

    if (this.oper != 'add') this.backFill()
  },
  mounted() {
        this.getAllDeviceType()
  },
  methods: {
    // 通过id获取数据
    backFill() {
        getEventclassRuleInfoByRuleId(this.$route.params.id).then((resp) => {
            this.addnewForm.eventclassRuleInfo = resp.data.data.list[0].eventclassRuleInfo
            this.addnewForm.eventclassRuleAssoc = resp.data.data.list[1].eventclassRuleAssoc
        })
    },
    // 获取设备来源
    getAllDeviceType() {
      getAllDeviceType().then(resp => {
        if (resp.data.returnCode == '0') {
          this.statics.scannerTypeList = resp.data.data.list
        }
      }, error => {
        this.$message.error('设备类型获取失败')
      })
    },
    submit() {
        this.$refs['addnewForm'].validate((valid) => {
            if (valid) {
                let regex = this.addnewForm.eventclassRuleInfo.regex
                let keyword = this.addnewForm.eventclassRuleInfo.keyWord
                if (keyword.length > 0) {
                    if (keyword.indexOf('-SC:((') == -1 || keyword.indexOf('))') == -1) {
                        this.$message.error('添加关键字的格式为： -SC:((关键字1)),-SC:((关键字2))等,多个中间用逗号隔开')
                        return
                    }
                }
                if (regex.length > 0) {
                    if (regex.indexOf('-RE:((')== -1 || regex.indexOf('))') == -1) {
                        this.$message.error('添加正则表达式的格式为：-RE:((您的正则式))')
                        return
                    }
                }
                let form = JSON.parse(JSON.stringify(this.addnewForm))
                let interfaces = this.oper == 'add' ? addEventclassRuleInfo : updEventclassRuleInfo
                this.submitLoading = true
                interfaces(form).then((resp) => {
                    if (resp.data.returnCode == "0") {
                        this.$message.success(`${this.oper == 'add' ? '新增' : '修改'}成功!`)
                        this.$router.go(-1)
                    }
                    this.submitLoading = false
                }, (error) => {
                    this.$message.error(error.message)
                    this.submitLoading = false
                })
            }
        })
    }
  }
};
</script>
