<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>洪水知识图谱</span>
        <div style="float: right;">
          <el-button-group>
            <el-button type="primary" size="mini" @click="openNeo4jBrowser" icon="el-icon-link">打开Neo4j Browser</el-button>
            <el-button size="mini" @click="openInNewTab" icon="el-icon-full-screen">新窗口打开</el-button>
          </el-button-group>
        </div>
      </div>
      
      <!-- Neo4j Browser展示区域 -->
      <div v-loading="loading" style="height: calc(100vh - 120px); position: relative;">
        <!-- 尝试使用 iframe 嵌入 -->
        <iframe
          v-if="browserUrl && !loadError"
          ref="neo4jBrowser"
          :src="browserUrl"
          class="neo4j-browser-iframe"
          frameborder="0"
          @load="onIframeLoad"
          @error="onIframeError"
        ></iframe>
        </div>
    </el-card>
  </div>
</template>

<script>
import { getNeo4jBrowserUrl } from "@/api/flood/graph";

export default {
  name: "KnowledgeGraph",
  data() {
    return {
      loading: false,
      browserUrl: '',
      loadError: false,
      errorMessage: '',
      configHelpVisible: false
    };
  },
  mounted() {
    this.loadBrowserUrl();
  },
  methods: {
    /** 加载Neo4j Browser URL */
    loadBrowserUrl() {
      this.loading = true;
      this.loadError = false;
      
      getNeo4jBrowserUrl().then(response => {
        if (response.code === 200 && response.browserUrl) {
          this.browserUrl = response.browserUrl;
        } else {
          // 如果接口返回失败，使用默认地址
          this.browserUrl = 'http://localhost:7474/browser';
        }
        this.loading = false;
      }).catch((error) => {
        console.error("获取Neo4j Browser URL失败", error);
        // 使用默认地址
        this.browserUrl = 'http://localhost:7474/browser';
        this.loading = false;
      });
    },
    /** iframe加载完成 */
    onIframeLoad() {
      this.loading = false;
      // 延迟检查，给 iframe 一些时间加载
    },
    /** iframe加载错误 */
    onIframeError() {
      this.loading = false;
      this.loadError = true;
      this.errorMessage = '无法连接到 Neo4j Browser，请确认 Neo4j 服务已启动且地址配置正确。如需在 iframe 中嵌入，请参考配置指南修改 CSP 设置。';
    },
    /** 打开Neo4j Browser（当前窗口跳转） */
    openNeo4jBrowser() {
      if (this.browserUrl) {
        window.location.href = this.browserUrl;
      } else {
        this.$modal.msgWarning("Neo4j Browser地址未加载，请稍候再试");
        this.loadBrowserUrl();
      }
    },
    /** 在新窗口打开 */
    openInNewTab() {
      if (this.browserUrl) {
        window.open(this.browserUrl, '_blank');
      } else {
        this.$modal.msgWarning("Neo4j Browser地址未加载，请稍候再试");
        this.loadBrowserUrl();
      }
    },
    /** 显示配置帮助 */
    showConfigHelp() {
      this.configHelpVisible = true;
    }
  }
};
</script>

<style scoped>
.box-card {
  height: calc(100vh - 100px);
}

.neo4j-browser-iframe {
  width: 100%;
  height: 100%;
  border: none;
  background-color: #fff;
}

.browser-tip {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.tip-content {
  text-align: center;
  padding: 40px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  max-width: 600px;
  width: 90%;
}
</style>
