<template>
  <div class="app-container">
    <div class="head-container">
      <!--工具栏-->
      <el-form :inline="true" class="demo-form-inline" label-suffix=":" label-width="90px">
        <el-form-item label="标签">
          <el-select
            v-model="system"
            clearable
            style="width: 100px; height: 35px;top: -5px;"
            placeholder="所属标签"
          >
            <el-option
              v-for="item in systemOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="级别">
          <el-select
            v-model="logLevelValue"
            clearable
            style="width: 100px; height: 35px;top: -5px;"
            placeholder="日志级别"
          >
            <el-option
              v-for="item in labelsOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="关键字">
          <el-input
            v-model="message"
            size="mini"
            placeholder="请输入内容"
            clearable
          />
        </el-form-item>
        <el-form-item label="链路追踪">
          <el-input
            v-model="traceId"
            size="mini"
            placeholder="请输入链路id"
            clearable
          />
        </el-form-item>

        <el-form-item label="SQL日志" prop="filterSql">
          <el-switch
            v-model="filterSql"
            active-color="#F56C6C"
            inactive-color="#409EFF"
            active-value="1"
            inactive-valu="0"
          />
        </el-form-item>
        <el-form-item label="HTTP日志" prop="isRequest">
          <el-switch
            v-model="isRequest"
            active-color="#409EFF"
            inactive-color="#F56C6C"
            active-value="1"
            inactive-valu="0"
          />
        </el-form-item>

        <el-form-item v-show="!showOptions" label="时间范围">
          <el-date-picker
            v-model="timeRange"
            size="mini"
            clearable
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            align="right"
            @change="queryData"
            @blur="queryData"
          />
        </el-form-item>
        <el-form-item v-show="showOptions" label="时间段">
          <el-select v-model="timeZoneValue" filterable placeholder="请选择标签" size="mini" @change="queryData">
            <el-option
              v-for="item in timeZoneOptions"
              :key="item.index"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-tooltip class="item" effect="dark" content="切换查询条件" placement="top">
            <span class="el-icon-sort" @click="changeShow" />
          </el-tooltip>
        </el-form-item>
        <el-form-item label="显示条数">
          <el-input-number
            v-model="size"
            size="mini"
            controls-position="right"
            :min="20"
            :max="5000"
            :step="10"
          />
        </el-form-item>
        <el-form-item label="当前页">
          <el-input-number
            v-model="page"
            size="mini"
            controls-position="right"
            :min="1"
            :max="2000"
            :step="1"
          />
        </el-form-item>
        <el-form-item>
          <el-dropdown split-button type="primary" size="mini" @click="queryData">
            查询
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-for="(item, index) in runStatuOptions" :key="index" @click.native="startInterval(item)">{{ item.label }}</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-form-item>
      </el-form>
    </div>
    <!--  <div style="margin: 3px; min-height: 80vh;">
      <el-table :data="logs">
        <el-table-column label="ip" prop="requestIp" />
        <el-table-column label="所属系统" prop="system" />
        <el-table-column label="请求时间" prop="requestTime" />
        <el-table-column label="traceId" prop="traceId" />
        <el-table-column label="请求时间" prop="requestTime" />
        <el-table-column label="请求接口" prop="requestMethod" />
        <el-table-column label="logger" prop="logger" />
        <el-table-column label="thread" prop="thread" />
        <el-table-column label="message" prop="message" show-overflow-tooltip/>
      </el-table>
    </div>
-->
    <div style="margin: 3px; min-height: 80vh;">
      <!--数据判空-->
      <el-empty v-if="showEmpty" :description="emptyText" />
      <!--数据加载-->
      <el-card v-else shadow="hover" style="width: 100%;overflow-x: scroll" class="log-warpper">
        <div style="width: 100%">
          <div v-for="(log, index) in logs" :key="index">
            <div>
              <span style="color: #6c0a99;font-weight: 700">{{ log.system }}</span>
              <span style="color: #13ce66">{{ log.thread }}</span>
              <span :style="fontType(log.logLevel)">{{ log.logLevel }}</span>
              <span>{{ log.requestIp }}</span>
              <span style="color: #7c8db0">{{ log.requestTime }}</span>
              <span style="color: chocolate">{{ log.traceId }}</span>
              <span style="color: #7a6df0">{{ log.requestMethod }}</span>
              <span style="margin: 5px;font-size: 15px">-----{{ log.message }}</span>
            </div>
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>

import logOperation from '@/views/loki/api/loki'

let queryParam = {
  logLevel: null,
  system: null,
  startTime: null,
  endTime: null,
  traceId: null,
  message: null,
  filterSql: true,
  isRequest: true,
  page: null,
  size: 20
}
export default {
  name: 'ES',
  data() {
    return {
      labelsOptions: [], // 所有标签和对应所有值数据
      systemOptions: [], // 所有标签和对应所有值数据
      logLevelValue: '',
      system: '',
      timeRange: [],
      message: '',
      traceId: '',
      size: 20,
      logData: [],
      filterSql: '1',
      isRequest: '1',
      logs: [], // 所有日志
      showEmpty: true,
      emptyText: '请选择标签',
      page: 1,
      runStatu: 'off',
      runStatuOptions: [{
        label: 'off',
        value: 0
      }, {
        label: '5s',
        value: 5000
      }, {
        label: '10s',
        value: 10000
      }, {
        label: '1m',
        value: 60000
      }, {
        label: '5m',
        value: 300000
      }, {
        label: '30m',
        value: 1800000
      }],
      timeZoneOptions: [{
        label: '最近5分钟',
        value: 300 * 1000
      }, {
        label: '最近15分钟',
        value: 900 * 1000
      }, {
        label: '最近30分钟',
        value: 1800 * 1000
      }, {
        label: '最近1小时',
        value: 3600 * 1000
      }, {
        label: '最近3小时',
        value: 3600 * 1000 * 3
      }, {
        label: '最近6小时',
        value: 3600 * 1000 * 6
      }, {
        label: '最近12小时',
        value: 3600 * 1000 * 12
      }, {
        label: '最近24小时',
        value: 3600 * 1000 * 24
      }, {
        label: '最近2天',
        value: 3600 * 1000 * 24 * 2
      }, {
        label: '最近7天',
        value: 3600 * 1000 * 24 * 7
      }, {
        label: '最近15天',
        value: 3600 * 1000 * 24 * 15
      }],
      timeZoneValue: '',
      showOptions: true
    }
  },
  created() {
    this.initLabelsValues('logLevel')
    this.initLabelsValues('system')
  },
  methods: {
    fontType(level) {
      if (level === 'INFO') {
        return { 'color': '#1b6cc4' }
      }
      if (level === 'ERROR') {
        return { 'color': '#e30a0a' }
      }
      if (level === 'DEBUG') {
        return { 'color': '#1e2022' }
      }
      return { 'color': '#a74dc6' }
    },
    initLabelsValues(type) {
      logOperation.labelsValues(type).then(res => {
        if (type === 'logLevel') {
          this.labelsOptions = res
        }
        if (type === 'system') {
          this.systemOptions = res
        }
      })
    },
    queryData() {
      // 清空查询数据
      this.clearParam()
      queryParam.logLevel = this.logLevelValue
      const time = new Date()
      if (this.timeZoneValue !== '') {
        queryParam.startTime = new Date(((time.getTime() - this.timeZoneValue)))
      }
      if (this.timeRange !== '' && this.timeRange.length > 0) {
        queryParam.startTime = this.timeRange[0]
        queryParam.endTime = this.timeRange[1]
      }
      queryParam.message = this.message.replace(/^\s*|\s*$/g, '')
      queryParam.filterSql = this.filterSql === '1'
      queryParam.isRequest = this.filterSql === '1'
      queryParam.traceId = this.traceId
      queryParam.size = this.size
      queryParam.page = this.page
      queryParam.system = this.system

      logOperation.getLogData(queryParam).then(res => {
        this.showEmpty = false
        // 清空
        this.logs = []
        for (const j in res.records) { // 用push的方式将所有日志数组添加进去
          this.logs.push(res.records[j])
        }
      })
    },
    changetype() {

    },
    clearParam() {
      queryParam = {
        logLevel: null,
        startTime: null,
        endTime: null,
        message: null,
        traceId: null,
        isRequest: true,
        filterSql: true,
        size: 20
      }
    },
    changeShow() {
      // 清空数据
      this.timeZoneValue = ''
      this.timeRange = []
      this.showOptions = !this.showOptions
    }
  }
}
</script>

<style scoped>
.log-warpper {
  white-space: nowrap;
  word-break: break-word;
}
</style>
