<!-- Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License.  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. -->
<template>
  <div>
    <LogTable v-loading="logStore.loadLogs" :tableData="displayLogs" :type="type" :noLink="false" :data="data">
      <div class="log-tips" v-if="!logStore.logs.length">{{ t("noData") }}</div>
    </LogTable>
    <Pagination
      v-model:currentPage="logStore.conditions.paging.pageNum"
      :pageSize="pageSize"
      :total="logStore.logs.length"
      @change="updatePage"
    />
  </div>
</template>
<script lang="ts" setup>
  import { ref, computed } from "vue";
  import { useI18n } from "vue-i18n";
  import type { PropType } from "vue";
  import type { LayoutConfig } from "@/types/dashboard";
  import LogTable from "./LogTable/Index.vue";
  import Pagination from "@/views/components/Pagination.vue";
  import { useLogStore, PageSizeDefault } from "@/store/modules/log";
  import { useDashboardStore } from "@/store/modules/dashboard";
  import { ElMessage } from "element-plus";

  /*global defineProps*/
  defineProps({
    needQuery: { type: Boolean, default: false },
    data: {
      type: Object as PropType<LayoutConfig>,
      default: () => ({}),
    },
  });

  const { t } = useI18n();
  const logStore = useLogStore();
  const dashboardStore = useDashboardStore();
  const type = ref<string>(dashboardStore.layerId === "BROWSER" ? "browser" : "service");
  const pageSize = PageSizeDefault;
  const displayLogs = computed(() =>
    logStore.logs.length === pageSize ? logStore.logs.slice(0, pageSize - 1) : logStore.logs,
  );
  function updatePage(p: number) {
    logStore.setLogCondition({
      paging: { pageNum: p, pageSize: pageSize },
    });
    queryLogs();
  }
  async function queryLogs() {
    const res = await logStore.getLogs();
    if (res && res.errors) {
      ElMessage.error(res.errors);
    }
  }
</script>
<style lang="scss" scoped>
  .log-tips {
    width: 100%;
    text-align: center;
    margin: 50px 0;
  }
</style>
