<template>
  <div class="layout">
    <manager-ui
      :query-model="form.warningRuleMap"
      :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 #enableTag="{scope}">
        {{ scope.row.enableTag | toCase(

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

          statics.dataSource,
          "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 { getWarningRuleList, delWarningRuleInfoByIds, stopWarningRuleInfoByIds, startWarningRuleInfoByIds } from './api/warning-rule'

export default {
  name: 'WarningRule',
  data() {
    return {
      collapseVisible: false,
      size: 10,
      total: null,
      currentPage: 1,
      changeTimer: null,
      disabled: false,
      oper: '',
      enableTag: '',
      dialogVisible: false,
      multipleSelection: [],
      orEnableTag: [
        {
          dataId: '1',
          dataName: '是'
        },
        {
          dataId: '0',
          dataName: '否'
        }
      ],
      form: {
        warningRuleMap: {
          ruleName: null,
          dataSourceList: [],
          enableTag: null
        },
        page: {
          current: 1,
          size: 10
        }
      },
      page: {
        current: 1,
        size: 10,
        total: 0
      },
      defaultV: 4,
      menus: [
        {
          text: '查看',
          command: 'detail',
          icon: 'el-icon-s-order'
        },
        {
          text: '修改',
          command: 'modify',
          icon: 'el-icon-edit'
        },
        {
          text: '启用',
          command: 'enable',
          visible: true,
          icon: 'el-icon-thumb',
          color: 'red'
        },
        {
          text: '停用',
          command: 'disable',
          visible: true,
          icon: 'el-icon-thumb'
        },
        {
          text: '删除',
          command: 'del',
          icon: 'el-icon-delete',
          color: 'red'
        }
      ],
      addnewDialog: false,
      loading: false,
      importLoading: false,
      statics: {
        orEnableTag: [
          {
            dataId: '1',
            dataName: '是'
          },
          {
            dataId: '0',
            dataName: '否'
          }
        ],
        dataSource: [
          {
            dataId: '19',
            dataName: '攻击监测日志'
          }, {
            dataId: '10',
            dataName: 'TCP|UDP审计日志'
          }, {
            dataId: '11',
            dataName: 'HTTP审计日志'
          }, {
            dataId: '12',
            dataName: '邮件审计日志'
          }, {
            dataId: '13',
            dataName: '文件审计日志'
          }, {
            dataId: '14',
            dataName: 'SSL审计日志'
          }, {
            dataId: '15',
            dataName: 'FTP审计日志'
          }, {
            dataId: '16',
            dataName: 'DNS审计日志'
          }, {
            dataId: '17',
            dataName: 'NFS审计日志'
          }, {
            dataId: '18',
            dataName: 'SMB审计日志'
          }
        ]
      },
      tableData: [],
      columnItems: [
        {
          type: 'selection',
          width: 50
        },
        {
          _key: 'ruleName',
          prop: 'ruleName',
          label: '规则名称',
          showOverflowTooltip: true
        },
        {
          _key: 'startTime',
          prop: 'startTime',
          label: '有效开始时间',
          showOverflowTooltip: true
        },
        {
          _key: 'endTime',
          prop: 'endTime',
          label: '有效结束时间',
          showOverflowTooltip: true
        },
        {
          _key: 'enableTag',
          prop: 'enableTag',
          label: '是否启用',
          render: true,
          showOverflowTooltip: true
        },
        {
          _key: 'dataSource',
          prop: 'dataSource',
          label: '数据源',
          render: true,
          showOverflowTooltip: true
        },
        {
          _key: 'ruleDescription',
          prop: 'ruleDescription',
          label: '规则描述',
          showOverflowTooltip: true
        },
        {
          _key: 'operate',
          prop: 'operate',
          label: '操作',
          render: true,
          width: 120
        }
      ],
      queryItems: [
        {
          type: 'input',
          span: 12,
          key: 'ruleName',
          label: '规则名称'
        },
        {
          type: 'select',
          span: 12,
          key: 'enableTag',
          placeholder: '请选择是否启用',
          label: '是否启用',
          option: 'orEnableTag',
          optionLabel: 'dataName',
          optionValue: 'dataId'
        },
        {
          type: 'select',
          span: 12,
          key: 'dataSourceList',
          placeholder: '请选择数据源',
          label: '数据源',
          multiple: true,
          option: 'dataSource',
          optionLabel: 'dataName',
          optionValue: 'dataId'
        }
      ],
      // 删除数据
      deleteData:
          {
            ids: []
          },
      defaultprops: {
        value: 'dataId',
        label: 'label',
        children: 'children'
      }
    }
  },
  computed: {
    showAddBtn() {
      // auditAnalysis_del 删除权限
      // return checkRuleCodeExit('auditAnalysis_add')
      return true
    },
    downloadTemplateUrl: function() {
      return this.$store.getters.basicUrl
    }
  },
  created() {
    this.search()
  },
  methods: {
    addNew() {
      this.$router.push({ name: 'warningRuleEdit', params: { oper: 'add', id: null }})
      // const path = '#/asp/warningRuleEdit/add/' + null
      // window.open(path, '_blank')
    },
    clickRow: function(command, scope, index, rows) {
      index = scope.row.ruleId
      if (command === 'del') {
        const list = []
        list.push(index)
        this.deleteData.ids = list
        this.confirmDel()
      } else if (command === 'modify') {
        this.$router.push({ name: 'warningRuleEdit', params: { oper: 'modify', id: index }})
        // const path = '#/asp/warningRuleEdit/modify/' + index
        // window.open(path, '_blank')
      } else if (command === 'detail') {
        this.$router.push({ name: 'warningRuleEdit', params: { oper: 'detail', id: index }})
        // const path = '#/asp/warningRuleEdit/detail/' + index
        // window.open(path, '_blank')
      } else if (command === 'enable') {
        const list = []
        list.push(index)
        this.deleteData.ids = list
        this.confirmEnableStatus(scope.row.ruleName, '启用')
      } else if (command === 'disable') {
        const list = []
        list.push(index)
        this.deleteData.ids = list
        this.confirmEnableStatus(scope.row.ruleName, '停用')
      }
    },
    // 启停
    confirmEnableStatus(title, enableName) {
      this.$confirm(`此操作将${enableName} “${title}”这条规则，是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (enableName === '启用') {
          this.startWarningRuleInfo()
        } else if (enableName === '停用') {
          this.stopWarningRuleInfo()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    },

    // 停用
    stopWarningRuleInfo: function() {
      stopWarningRuleInfoByIds(this.deleteData).then((resp) => {
        if (resp.data.returnCode == '0') {
          this.search()
          this.$message.success('停用成功!')
        }
      }, (error) => {
        this.$message.error(error.message)
      })
    },
    // 启用
    startWarningRuleInfo: function() {
      startWarningRuleInfoByIds(this.deleteData).then((resp) => {
        if (resp.data.returnCode == '0') {
          this.search()
          this.$message.success('启用成功!')
        }
      }, (error) => {
        this.$message.error(error.message)
      })
    },
    searchClick: function() {
      this.page.current = 1
      this.search()
    },
    reset() {
      Object.assign(this.$data, this.$options.data())
      this.form.warningRuleMap.ruleName = null
      this.form.warningRuleMap.enableTag = null
      this.form.warningRuleMap.dataSourceList = []
      this.form.page.current = 1
      this.search()
    },
    search: function() {
      this.form.page = this.page
      getWarningRuleList(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
    },
    // 删除前确认
    confirmDel: function() {
      this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delWarningRuleInfo()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 删除
    delWarningRuleInfo: function() {
      delWarningRuleInfoByIds(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].ruleId)
      }
      if (list.length >= 1) {
        this.deleteData.ids = list
        this.confirmDel()
      } else {
        this.$message.warning('请选择需删除的行')
      }
    }
  }
}
</script>

