<template>
  <div class="detail_container">
    <bg-breadcrumb></bg-breadcrumb>
    <div class="flex_row">
      <div class="flex_left">
        <div class="box">
          <el-date-picker
            style="width: 100%"
            type="date"
            value-format="YYYY-MM-DD"
            v-model="logTime"
            placeholder="请选择日期" />

          <div class="type_station bg-scroll">
            <div
              class="type-box"
              :class="{ 'current-type': selectedNodeContainer == item.value }"
              @click="nodeClick(item)"
              v-for="(item, index) in containerList"
              :key="index"
              :title="item">
              {{ item }}
            </div>
          </div>
        </div>
      </div>
      <div class="flex_right">
        <div class="form-filter">
          <el-form :model="filterForm" inline>
            <el-form-item label="级别">
              <el-select placeholder="全部" style="width: 160px" v-model="filterForm.logLevel">
                <el-option label="INFO" value="INFO"> </el-option>
                <el-option label="WARNING" value="WARNING"> </el-option>
                <el-option label="ERROR" value="ERROR"> </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="关键字">
              <el-input
                placeholder="请输入"
                maxlength="100"
                style="width: 240px"
                v-model="filterForm.search"></el-input>
            </el-form-item>
          </el-form>
          <div class="operate_btns">
            <el-button type="primary" @click="search"> 查询 </el-button>
            <el-button type="default" @click="reset"> 重置 </el-button>
          </div>
        </div>
        <div class="search-result">
          <div class="item-container apaas_scroll">
            <p v-for="(item, index) in logList" :key="index">{{ item }}</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, watch, onBeforeMount, toRefs } from "vue";
import { ElMessage } from "element-plus";
import axios from "@/request/http.js";
import bgBreadcrumb from "@/components/bg-breadcrumb.vue";
const state = reactive({
  logTime: "",
  containerList: [], // 容器数据
  selectedNodeContainer: "",
  filterForm: {
    logLevel: "",
    search: "",
  },
  logList: [],
});
watch(
  () => state.logTime,
  (val) => {
    state.selectedNodeContainer = "";
    getContainerList();
  }
);
const nodeClick = (item) => {
  state.selectedNodeContainer = item;
  reset();
};
// 获取容器列表
const getContainerList = () => {
  let params = {
    logTime: state.logTime,
  };
  axios
    .get("/v1/api/log/system/list", { params })
    .then((res) => {
      if (res.data.code == 200) {
        state.containerList = res.data.data || [];
        state.selectedNodeContainer = state.containerList.length ? state.containerList[0] : "";
        state.selectedNodeContainer ? reset() : (state.logList = []);
      } else {
        ElMessage.error(res.data.data);
      }
    })
    .catch((err) => {
      console.error(err);
    });
};
//查询
const search = () => {
  let params = {
    name: state.selectedNodeContainer,
    ...state.filterForm,
  };
  axios
    .get("/v1/api/log/system/log", { params })
    .then((res) => {
      if (res.data.code == 200) {
        state.logList = (res.data.data || []).map((item) => JSON.stringify(item));
        state.logList = state.logList.concat(state.logList);
      } else {
        ElMessage.error(res.data.data);
      }
    })
    .catch((err) => {
      console.error(err);
    });
};
//重置
const reset = () => {
  state.filterForm = {
    logLevel: "",
    search: "",
  };
  search();
};
onBeforeMount(() => {
  getContainerList();
});

const { logTime, containerList, selectedNodeContainer, filterForm, logList } = toRefs(state);
</script>

<style lang="scss" scoped>
.detail_container {
  width: 100%;
  height: calc(100vh - 56px);
  padding: 0 24px;
  min-height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: stretch;
  .flex_row {
    // flex-grow: 1;
    width: 100%;
    display: flex;

    margin-bottom: 16px;
    position: relative;
    height: calc(100% - 62px);
    .flex_left {
      width: 240px;
      background-color: #fff;
      margin-right: 16px;
      border-radius: 6px;
      padding: 16px;
      height: 100%;
      box-shadow: 0px 1px 4px 0px rgba(0, 7, 101, 0.15);
      overflow: hidden;

      .box {
        width: 208px;
        height: 100%;
        overflow: hidden;
        .type_station {
          width: 100%;
          margin-top: 16px;
          height: calc(100% - 50px);
          .type-box {
            overflow: hidden;
            text-overflow: ellipsis;
            height: 32px;
            white-space: nowrap;
            line-height: 32px;
            text-align: center;
            font-size: 14px;
            color: #404a62;
            cursor: pointer;
            &:hover {
              background-color: #f2f3f7;
            }
          }
          .current-type {
            background-color: #f2f3f7;
            border-radius: 4px;
          }
        }
      }
    }
    .flex_right {
      flex: 1;
      height: 100%;

      .form-filter {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 16px;
        height: 68px;
        width: 100%;
        padding: 16px 16px 16px 24px;
        background-color: #ffffff;
        box-shadow: 0px 1px 4px 0px rgba(0, 7, 101, 0.15);
        border-radius: 6px;
        .el-form {
          width: calc(100% - 144px);
        }
        .el-form-item {
          margin-bottom: 0px;
        }
        .operate_btns {
          width: 144px;
        }
      }
      .search-result {
        width: 100%;
        height: calc(100% - 84px);
        padding: 15px 0;
        color: #fff;
        background-color: #1a1a1a;
        box-shadow: 0px 1px 4px 0px rgba(0, 7, 101, 0.1);
        border-radius: 6px;
        .item-container {
          overflow: auto;
          padding: 0 15px;
          height: 100%;
        }
        p {
          word-wrap: break-word;
          word-break: break-all;
        }
      }
    }
  }
}
</style>
