<template>
  <div class="layout">
    <manager-ui
      :loading="loading"
      :query-model="form.bigdataAssociationRulesMap"
      :query-items="queryItems"
      :static-data="statics"
      :column-items="columnItems"
      :table-data="tableData"
      :pagination="page"
      :spread-threshold="1"
      :show-add-btn="showAddBtn"
      show-table-tools
      @create="addNew"
      @searchClick="searchClick"
      @search="search"
      @resetForm="reset"
    >
      <template #type="{scope}">
        {{ scope.row.type| toCase(
          statics.associateType,
          "dataId",
          "dataName"
        ) }}
      </template>
      <template #alertType="{scope}">
        {{ scope.row.alertType | toCase(

          statics.bigdataAlarmType,
          "dataId",
          "dataName"
        ) }}
      </template>
      <template #alertTypeSub="{scope}">
        {{ scope.row.alertTypeSub | toCase(

          statics.bigdataAlarmType,
          "dataId",
          "dataName"
        ) }}
      </template>
      <template #ruleLevel="{scope}">
        {{ scope.row.ruleLevel | toCase(

          statics.alertLevel,
          "dataId",
          "dataName"
        ) }}
      </template>
      <template #attackChainStage="{scope}">
        {{ scope.row.attackChainStage | toCase(

          statics.attackType,
          "dataId",
          "dataName"
        ) }}
      </template>
      <template #enableTag="{scope}">
        {{ scope.row.enableTag | toCase(

          statics.orEnableTag,
          "dataId",
          "dataName"
        ) }}
      </template>
      <template #operate="{scope}">
        <el-context-menu :menus="menus" :visibles="[scope.row.enableTag=='0',scope.row.enableTag!='0']" :scope="scope" :default="defaultV" @oper="clickRow" />
      </template>
    </manager-ui>

    <transition name="router-slid" mode="out-in">
      <router-view />
    </transition>
  </div>
</template>

<script>
import { queryBigdataAssociationRuleList, delBigdataAssociationRule, updateEnableTag } from './api/alarm-result'

export default {
  name: 'AlarmStrategy',
  data() {
    return {
      collapseVisible: false,
      size: 10,
      total: null,
      currentPage: 1,
      changeTimer: null,
      disabled: false,
      oper: '',
      enableTag: '',
      dialogVisible: false,
      selectBusinessList: [],
      selectDepartList: [],
      multipleSelection: [],
      orEnableTag: [
        {
          dataId: '1',
          dataName: '是'
        },
        {
          dataId: '0',
          dataName: '否'
        }
      ],
      associateType: [
        {
          dataId: '1',
          dataName: '单设备关联规则'
        }, {
          dataId: '2',
          dataName: '多设备关联规则'
        }
      ], // 关联类型
      // 查询参数
      // queryParams: {
      //   listMap: {
      //     confName:null,
      //     orderInfo:null,
      //     businessIds:[],
      //     departIds:[],
      //     orderLevel:[]
      //   },
      //   page: {
      //     current: "1",
      //     size: "10"
      //   }
      // },
      form: {
        bigdataAssociationRulesMap: {
          name: null,
          type: null,
          alertType: null,
          alertTypeSubs: [],
          matchCount: 1,
          enableTag: null,
          reliability: null,
          attackChainStage: null
        },
        page: {
          current: 1,
          size: 10
        }
      },
      page: {
        current: 1,
        size: 10,
        total: 0
      },
      defaultV: 3,
      menus: [
        {
          text: '查看',
          command: 'detail',
          icon: 'el-icon-s-order'
        },
        {
          text: '修改',
          command: 'modify',
          icon: 'el-icon-edit'
        },
        {
          text: '删除',
          command: 'del',
          icon: 'el-icon-delete',
          iconColor: 'red'
        },
        {
          text: '启用',
          command: 'enable',
          visible: true,
          icon: 'el-icon-thumb',
          color: 'red'
        },
        {
          text: '停用',
          command: 'disable',
          visible: true,
          icon: 'el-icon-thumb'
        }
      ],
      addnewDialog: false,
      loading: false,
      importLoading: false,
      statics: {
        orEnableTag: [
          {
            dataId: '1',
            dataName: '是'
          },
          {
            dataId: '0',
            dataName: '否'
          }
        ],
        associateType: [
          {
            dataId: '1',
            dataName: '单设备关联规则'
          }, {
            dataId: '2',
            dataName: '多设备关联规则'
          }
        ],
        attackType: [],

        alertLevel: [
          {
            dataId: 1,
            dataName: '轻微'
          },
          {
            dataId: 2,
            dataName: '低级'
          },
          {
            dataId: 3,
            dataName: '中级'
          },
          {
            dataId: 4,
            dataName: '高级'
          },
          {
            dataId: 5,
            dataName: '严重'
          }
        ],
        bigdataAlarmType: [],
        bigdataAlarmTypeTree: []
      },
      tableData: [],
      columnItems: [
        {
          type: 'selection',
          width: 50
        },
        {
          _key: 'name',
          prop: 'name',
          label: '规则名称',
          showOverflowTooltip: true
        },
        {
          _key: 'type',
          prop: 'type',
          label: '关联类型',
          render: true,
          showOverflowTooltip: true
        },
        {
          _key: 'alertType',
          prop: 'alertType',
          label: '告警类型',
          render: true,
          showOverflowTooltip: true
        },
        {
          _key: 'alertTypeSub',
          prop: 'alertTypeSub',
          label: '告警小类',
          render: true,
          showOverflowTooltip: true
        },
        {
          _key: 'matchCount',
          prop: 'matchCount',
          label: '匹配次数',
          showOverflowTooltip: true
        },
        {
          _key: 'reliability',
          prop: 'reliability',
          label: '可信度',
          showOverflowTooltip: true
        },
        {
          _key: 'ruleLevel',
          prop: 'ruleLevel',
          label: '告警等级',
          render: true,
          showOverflowTooltip: true
        },
        {
          _key: 'attackChainStage',
          prop: 'attackChainStage',
          label: '攻击链阶段',
          render: true,
          showOverflowTooltip: true
        },
        {
          _key: 'enableTag',
          prop: 'enableTag',
          label: '是否启用',
          render: true,
          showOverflowTooltip: true
        },
        {
          _key: 'operate',
          prop: 'operate',
          label: '操作',
          render: true,
          width: 120
        }
      ],
      queryItems: [
        {
          type: 'input',
          span: 12,
          key: 'name',
          label: '规则名称'
        },
        {
          type: 'select',
          span: 12,
          key: 'type',
          placeholder: '请选择关联类型',
          label: '关联类型',
          filterable: true,
          option: 'associateType',
          optionLabel: 'dataName',
          optionValue: 'dataId'
        },
        {
          type: 'inputNumber',
          span: 12,
          key: 'matchCount',
          label: '匹配次数'
        },
        {
          type: 'treeSelect',
          span: 12,
          key: 'alertTypeSubs',
          placeholder: '请选择告警类型',
          label: '告警类型',
          dataTree: 'bigdataAlarmTypeTree'
        },
        {
          type: 'select',
          span: 12,
          key: 'enableTag',
          placeholder: '请选择是否启用',
          label: '是否启用',
          option: 'orEnableTag',
          optionLabel: 'dataName',
          optionValue: 'dataId'
        },
        {
          type: 'select',
          span: 12,
          key: 'attackChainStage',
          placeholder: '请选择攻击链阶段',
          label: '攻击链阶段',
          filterable: true,
          option: 'attackType',
          optionLabel: 'dataName',
          optionValue: 'dataId'
        }
      ],
      // 删除数据
      deleteData:
          {
            ids: []
          },
      defaultprops: {
        value: 'dataId',
        label: 'label',
        children: 'children'
      }
    }
  },
  computed: {
    showAddBtn() {
      // auditAnalysis_del 删除权限
      // return checkRuleCodeExit('auditAnalysis_add')
      return true
    },
    // businessTree: function () {
    //   return this.$store.getters.businessTree
    // },
    downloadTemplateUrl: function() {
      return this.$store.getters.basicUrl
    },
    // departTree: function () {
    //   return this.$store.getters.departTree
    // },
    // 等级
    RiskLevelStaticParam: function() {
      return this.$store.getters.RiskLevelStaticParam
    },
    attackType() {
      return this.$store.state.alarm.attackType
    },
    alertType() {
      return this.$store.state.alarm.newAlarmType
    },
    bigdataAlarmTypeTree() {
      return this.$store.state.alarm.newAlarmTypeTree
    },
    ruleLevel() {
      return this.$store.state.alarm.ruleLevel
    }
  },
  created() {
    this.$store.dispatch('alarm/getAllAlarmStaticList')
    this.getAttackTypeData()
    this.getNewAlarmType()
    this.search()
  },
  methods: {
    getNewAlarmType() {
      this.statics.bigdataAlarmType = this.alertType
      this.statics.bigdataAlarmTypeTree = this.bigdataAlarmTypeTree
    },
    getAttackTypeData() {
      this.statics.attackType = this.attackType
    },
    addNew() {
      this.$router.push({ name: 'alarmStrategyEdit', params: { oper: 'add', id: null }})
    },
    clickRow: function(command, scope, index, rows) {
      index = scope.row.id
      if (command === 'del') {
        const list = []
        list.push(index)
        this.deleteData.ids = list
        this.confirmDel()
      } else if (command === 'modify') {
        this.$router.push({ name: 'alarmStrategyEdit', params: { oper: 'modify', id: index }})
      } else if (command === 'detail') {
        this.$router.push({ name: 'alarmStrategyEdit', params: { oper: 'detail', id: index }})
      } else if (command === 'enable') {
        const list = []
        list.push(index)
        this.confirmEnableStatus({ ids: list }, scope.row.name, '启用')
      } else if (command === 'disable') {
        const list = []
        list.push(index)
        this.confirmEnableStatus({ ids: list }, scope.row.name, '停用')
      }
    },
    // 启停
    confirmEnableStatus(temp, title, enableName) {
      this.$confirm(`此操作将${enableName} “${title}”这条规则，是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.enableHandle(temp)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    },
    enableHandle: function(temp) {
      updateEnableTag(temp).then((resp) => {
        if (resp.data.returnCode == '0') {
          this.$message.success('启停成功')
          this.search()
        }
      }, (error) => {
        this.$message.error(error.message)
      })
    },
    searchClick: function() {
      this.page.current = 1
      this.search()
    },
    reset() {
      Object.assign(this.$data, this.$options.data())
      this.form.page.current = 1
      this.getAttackTypeData()
      this.getNewAlarmType()
      this.search()
    },
    search: function() {
      this.loading = true
      this.form.page = this.page
      queryBigdataAssociationRuleList(this.form).then((resp) => {
        if (resp.data.returnCode == '0') {
          this.tableData = resp.data.data.list
          const { current, size, total } = resp.data.data.page
          this.page.current = current
          this.page.size = size
          this.page.total = total
        }
        this.loading = false
      }, (error) => {
        this.$message.error(error.message)
        this.loading = false
      })
    },
    // 分页
    handleCurrentChange: function(currentPage) {
      this.form.page.current = currentPage
      this.search()
    },
    // 多选
    handleSelectionChange: function(val) {
      this.multipleSelection = val
    },
    // 重置查询条件
    resetForm() {
      this.form.bigdataAssociationRulesMap.name = null
      this.form.bigdataAssociationRulesMap.type = null
      this.form.bigdataAssociationRulesMap.alertType = null
      this.form.bigdataAssociationRulesMap.matchCount = 1
      this.form.bigdataAssociationRulesMap.enableTag = null
      this.form.bigdataAssociationRulesMap.reliability = null
      this.form.bigdataAssociationRulesMap.attackChainStage = null
      this.form.page.current = 1
      this.$refs.form.resetFields()
      this.search()
    },
    // 删除前确认
    confirmDel: function() {
      this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteStrategy()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 删除
    deleteStrategy: function() {
      delBigdataAssociationRule(this.deleteData).then((resp) => {
        if (resp.data.returnCode == '0') {
          this.form.page.current = 1
          this.search()
          this.$message.success('删除成功!')
        }
      }, (error) => {
        this.$message.error(error.message)
      })
    },
    // 拼装批量删除数据
    batchdel: function() {
      const list = []
      for (const temp in this.multipleSelection) {
        list.push(this.multipleSelection[temp].orderId)
      }
      if (list.length >= 1) {
        this.deleteData.ids = list
        this.confirmDel()
      } else {
        this.$message.warning('请选择需删除的行')
      }
    }
  }
}
</script>

