<template>
  <div v-if="errorLogs.length > 0">
    <el-badge :is-dot="true" style="line-height: 25px; margin-top: -5px;" @click.native="dialogTableVisible = true">
      <el-button style="padding: 8px 10px;" size="small" type="danger">
        <svg-icon icon-class="bug" />
      </el-button>
    </el-badge>

    <el-dialog :visible.sync="dialogTableVisible" width="80%" append-to-body>
      <div slot="title">
        <span style="padding-right: 10px;">{{ abbreviation }}异常捕获日志</span>
        <el-button size="mini" type="primary" icon="el-icon-delete" @click="clearAll">Clear All</el-button>
      </div>
      <el-table :data="errorLogs" border>
        <el-table-column label="Message">
          <template slot-scope="{ row }">
            <div>
              <span class="message-title">错误信息:</span>
              <el-tag type="danger">
                {{ decodeUnicode(row.err.message) }}
              </el-tag>
            </div>
            <br />
            <div>
              <span class="message-title" style="padding-right: 10px;">Info: </span>
              <el-tag type="warning"> {{ row.vm.$vnode.tag }} error in {{ row.info }} </el-tag>
            </div>
            <br />
            <div>
              <span class="message-title" style="padding-right: 16px;">报错路由: </span>
              <el-tag type="success">
                {{ row.url }}
              </el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="Stack">
          <template slot-scope="scope">
            {{ scope.row.err.stack }}
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { abbreviation, title } from "@/config/settings";

export default {
  name: "ErrorLog",
  data() {
    return {
      title: title,
      abbreviation: abbreviation,
      dialogTableVisible: false,
    };
  },
  computed: {
    ...mapGetters({
      errorLogs: "errorLog/errorLogs",
    }),
  },
  methods: {
    clearAll() {
      this.dialogTableVisible = false;
      this.$store.dispatch("errorLog/clearErrorLog");
    },
    decodeUnicode(str) {
      str = str.replace(/\\/g, "%");
      str = unescape(str);
      str = str.replace(/%/g, "\\");
      str = str.replace(/\\/g, "");
      return str;
    },
  },
};
</script>

<style scoped>
.message-title {
  font-size: 16px;
  color: #333;
  font-weight: bold;
  padding-right: 8px;
}
</style>
