<!-- 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 @click="showSelectSpan" :class="['log-item', 'clearfix']" ref="logItem">
    <div
      v-for="(item, index) in columns"
      :key="index"
      :class="[
        'method',
        ['message', 'stack'].includes(item.label) ? 'autoHeight' : '',
      ]"
      :style="{
        lineHeight: 1.3,
        width: `${item.drag ? item.method : ''}px`,
      }"
    >
      <span v-if="item.label === 'time'">{{ dateFormat(data.time) }}</span>
      <span v-else-if="item.label === 'errorUrl'">{{ data.pagePath }}</span>
      <el-tooltip v-else :content="data[item.label] || '-'">
        <span>
          {{ data[item.label] || "-" }}
        </span>
      </el-tooltip>
    </div>
  </div>
</template>
<script lang="ts" setup>
import dayjs from "dayjs";
import { BrowserLogConstants } from "./data";

/*global defineProps, defineEmits, NodeListOf  */
const props = defineProps({
  data: { type: Array as any, default: () => [] },
});
const columns = BrowserLogConstants;
const emit = defineEmits(["select"]);

const dateFormat = (date: number, pattern = "YYYY-MM-DD HH:mm:ss") =>
  dayjs(date).format(pattern);

function showSelectSpan() {
  const items: NodeListOf<any> = document.querySelectorAll(".log-item");

  for (const item of items) {
    item.style.background = "#fff";
  }
  const logItem: any = this.$refs.logItem;

  logItem.style.background = "rgba(0, 0, 0, 0.1)";
  emit("select", props.data);
}
</script>
<style lang="scss" scoped>
.log-item {
  white-space: nowrap;
  position: relative;
  cursor: pointer;
}

.log-item.selected {
  background: rgba(0, 0, 0, 0.04);
}

.log-item:not(.level0):hover {
  background: rgba(0, 0, 0, 0.04);
}

.log-item:hover {
  background: rgba(0, 0, 0, 0.04) !important;
}

.log-item > div {
  width: 140px;
  padding: 0 5px;
  display: inline-block;
  border: 1px solid transparent;
  border-right: 1px dotted silver;
  overflow: hidden;
  line-height: 30px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.log-item .text {
  width: 100% !important;
  display: inline-block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.log-item > div.method {
  padding: 7px 5px;
  line-height: 30px;
}
</style>
