<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="9" :sm="12">
            <a-form-item label="规则名称">
              <a-input v-model="queryParam.id" placeholder="" />
            </a-form-item>
          </a-col>
          <a-col :md="5" :sm="12">
            <a-form-item label="指标类型">
              <a-select v-model="queryParam.status" placeholder="请选择" default-value="0">
                <a-select-option value="0">全部</a-select-option>
                <a-select-option value="1">病毒数据</a-select-option>
                <a-select-option value="2">IDS数据</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="5" :sm="12">
            <a-form-item label="告警级别">
              <a-select v-model="queryParam.level" placeholder="请选择" default-value="0">
                <a-select-option value="0">全部</a-select-option>
                <a-select-option value="1">高</a-select-option>
                <a-select-option value="2">中</a-select-option>
                <a-select-option value="3">低</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="5" :sm="12">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" @click="handleQueryPage">查询</a-button>
              <a-button type="primary" @click="$refs.createModal.add(true)">新增1</a-button>
              <router-link :to="{ name: 'AlarmRuleCreate' }"><a-button type="primary">新增2</a-button></router-link>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table :rowKey="(record, index) => index" :columns="columns" :dataSource="dataList" :pagination="false">
      <span slot="status" slot-scope="text">
        <a-badge :status="text | statusTypeFilter" :text="text | statusFilter" />
      </span>
      <span slot="description" slot-scope="text">
        <ellipsis :length="4" tooltip>{{ text }}</ellipsis>
      </span>

      <span slot="action" slot-scope="text, record">
        <template>
          <a @click="handleSub(record)">订阅报警</a>
        </template>
      </span>
    </a-table>
    <a-pagination
      showSizeChanger
      v-model="page.pageNo"
      :total="page.totalCount"
      :pageSize.sync="page.pageSize"
      @change="handlePageChange"
      @showSizeChange="handlePageSizeChange"
    />
    <create-form ref="createModal" @ok="queryPage" />
  </a-card>
</template>

<script>
import { Ellipsis } from '@/components'
import CreateForm from './modals/CreateForm'
import { getServiceList } from '@/api/manage'

const statusMap = {
  0: {
    status: 'default',
    text: '关闭'
  },
  1: {
    status: 'processing',
    text: '运行中'
  },
  2: {
    status: 'success',
    text: '已上线'
  },
  3: {
    status: 'error',
    text: '异常'
  }
}

export default {
  name: 'AlarmRuleList',
  components: {
    Ellipsis,
    CreateForm
  },
  data() {
    return {
      // 查询参数
      queryParam: {},
      loading: true,
      page: {
        pageNo: 1,
        pageSize: 10,
        totalCount: 0
      },
      // 表头
      columns: [
        {
          title: '规则编号',
          dataIndex: 'no'
        },
        {
          title: '描述',
          dataIndex: 'description',
          scopedSlots: { customRender: 'description' }
        },
        {
          title: '服务调用次数',
          dataIndex: 'callNo',
          customRender: text => text + ' 次'
        },
        {
          title: '状态',
          dataIndex: 'status',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '更新时间',
          dataIndex: 'updatedAt'
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '150px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      dataList: []
    }
  },
  filters: {
    statusFilter(type) {
      return statusMap[type].text
    },
    statusTypeFilter(type) {
      return statusMap[type].status
    }
  },
  created() {
    this.queryPage()
  },
  methods: {
    // 分页查询
    handleQueryPage() {
      this.page.pageNo = 1
      this.queryPage()
    },
    queryPage() {
      this.loading = true
      getServiceList(Object.assign(this.page, this.queryParam)).then(res => {
        this.dataList = res.result.data
        this.page.pageNo = res.result.pageNo
        this.page.pageSize = res.result.pageSize
        this.page.totalCount = res.result.totalCount
        this.page.totalPage = res.result.totalPage
        this.loading = false
      })
    },
    handlePageChange(pageNo) {
      this.page.pageNo = pageNo
      this.queryPage()
    },
    handlePageSizeChange(current, pageSize) {
      this.page.pageSize = pageSize
      this.queryPage()
    },
    handleSub(record) {
      if (record.status !== 0) {
        this.$message.info(`${record.no} 订阅成功`)
      } else {
        this.$message.error(`${record.no} 订阅失败，规则已关闭`)
      }
    }
  }
}
</script>
