<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-cascader
            v-model="labelAndValue"
            :options="labelsOptions"
            placeholder="请选择标签"
          />
        </el-form-item>
        <el-form-item label="关键字">
          <el-input
            v-model="message"
            size="mini"
            placeholder="请输入内容"
            clearable
          />
        </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="SQL日志" prop="filterSql">
          <el-switch
            v-model="filterSql"
            active-color="#409EFF"
            inactive-color="#F56C6C"
            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 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="margin: 5px;">{{log.system}}---{{log.message}}</span>
            </div>

          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>

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

let queryParam = {
  logLevel: null,
  startTime: null,
  endTime: null,
  message: null,
  filterSql: true,
  isRequest: true,
  page: null,
  size: 20
}
export default {
  name: 'ES',
  data() {
    return {
      labelAndValue: [], // 搜索的值
      labelsOptions: [], // 所有标签和对应所有值数据
      timeRange: [],
      message: '',
      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()
  },
  methods: {
    initLabelsValues() {
      logOperation.labelsValues().then(res => {
        this.labelsOptions = res
      })
    },
    queryData() {
      // 清空查询数据
      this.clearParam()
      queryParam.logLabel = this.labelAndValue[0]
      const time = new Date()
      if (this.timeZoneValue !== '') {
        queryParam.endTime = ((time.getTime() - this.timeZoneValue) * 1000000).toString()
      }
      queryParam.message = this.message.replace(/^\s*|\s*$/g, '')
      queryParam.filterSql = this.filterSql === '1'
      queryParam.isRequest = this.filterSql === '1'
      queryParam.size = this.size
      queryParam.page = this.page

      logOperation.getLogData(queryParam).then(res => {
        this.showEmpty = false
        // 清空
        this.logs = []
        for (const j in res.records) { // 用push的方式将所有日志数组添加进去
          this.logs.push(res.records[j])
        }
      })
    },
    clearParam() {
      queryParam = {
        logLabel: null,
        logLabelValue: null,
        startTime: null,
        endTime: null,
        message: 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>
