<template>
  <div class="execution-results">
    <div class="execution-results-body">
      <!-- 节点详情区域 -->
      <div class="execution-details">
        <div class="details-left">
          <div class="history-list">
            <!-- 执行状态卡片 -->
            <div v-if="executionResult && executionResult.success" class="success-overview-card">
              <div class="overview-card-header">
                <i class="fa fa-check-circle text-success"></i>
                <div class="overview-info-header">
                  <h5>执行成功</h5>
                  <p>执行了 {{ executionHistory.length }} 个节点</p>
                </div>
              </div>
            </div>
            <div v-else-if="executionResult && !executionResult.success" class="error-overview-card">
              <div class="overview-card-header">
                <i class="fa fa-exclamation-circle text-danger"></i>
                <div class="overview-info-header">
                  <h5>执行失败</h5>
                  <p>{{ executionResult.error }}</p>
                </div>
              </div>
            </div>
            <div v-else class="no-execution-overview-card">
              <div class="overview-card-header">
                <i class="fa fa-info-circle text-info"></i>
                <div class="overview-info-header">
                  <h5>无执行历史</h5>
                  <p>请先执行流程查看结果</p>
                </div>
              </div>
            </div>
            
            <!-- 执行历史列表 -->
            <div v-if="executionHistory && executionHistory.length > 0">
              <div 
                v-for="(item, index) in executionHistory" 
                :key="index"
                class="history-item"
                :class="[item.status, { active: selectedNodeDetail?.nodeId === item.nodeId }]"
                @click="selectNode(item)"
              >
                <div class="history-item-header">
                  <span class="history-node-name">
                    {{ item.nodeLabel || (getNodeMetadataByType(item.nodeType)?.label || item.nodeType) }}
                  </span>
                  <span class="history-status-badge" :class="item.status">
                    {{ __(item.status) }}
                  </span>
                </div>
                <div class="history-item-meta">
                  <span class="history-time">{{ new Date(item.timestamp).toLocaleTimeString() }}</span>
                </div>
              </div>
            </div>
            <div v-else class="no-history">
              <div class="no-history-content">
                <i class="fa fa-history"></i>
                <p>暂无执行历史</p>
              </div>
            </div>
          </div>
        </div>
        
        <div class="details-right" v-if="selectedNodeDetail">
          <div class="node-detail-header">
            <h6>{{ selectedNodeDetail.nodeLabel || (getNodeMetadataByType(selectedNodeDetail.nodeType)?.label || selectedNodeDetail.nodeType) }}</h6>
            <div class="detail-tabs">
              <button 
                class="tab-btn" 
                :class="{ active: nodeDetailTab === 'input' }"
                @click="nodeDetailTab = 'input'"
              >
                输入
              </button>
              <button 
                class="tab-btn" 
                :class="{ active: nodeDetailTab === 'output' }"
                @click="nodeDetailTab = 'output'"
              >
                输出
              </button>
            </div>
          </div>
          <div class="node-detail-content">
            <div v-if="nodeDetailTab === 'input'" class="detail-panel">
              <button 
                v-if="selectedNodeDetail.inputs && Object.keys(selectedNodeDetail.inputs).length > 0"
                class="copy-btn-floating" 
                @click="copyContent(selectedNodeDetail.inputs)"
                :title="__('复制')"
              >
                <i class="fa fa-copy"></i>
              </button>
              <pre class="json-viewer">{{ JSON.stringify(formatDisplayContent(selectedNodeDetail.inputs || {}), null, 2) }}</pre>
            </div>
            <div v-if="nodeDetailTab === 'output'" class="detail-panel">
              <button 
                v-if="selectedNodeDetail.result && Object.keys(selectedNodeDetail.result).length > 0"
                class="copy-btn-floating" 
                @click="copyContent(selectedNodeDetail.result)"
                :title="__('复制')"
              >
                <i class="fa fa-copy"></i>
              </button>
              <pre class="json-viewer">{{ JSON.stringify(formatDisplayContent(selectedNodeDetail.result || {}), null, 2) }}</pre>
            </div>
          </div>
        </div>
        <div v-else class="details-right empty">
          <div class="empty-state">
            <i class="fa fa-mouse-pointer"></i>
            <p>点击左侧节点查看详情</p>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 复制提示 -->
    <transition name="fade">
      <div v-if="showCopyTip" class="copy-tip">{{ __('已复制') }}</div>
    </transition>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { getNodeMetadataByType } from '../utils/nodeMetadata';

const props = defineProps({
  executionResult: Object,
  executionHistory: Array
});

const emit = defineEmits(['close']);

const selectedNodeDetail = ref(null);
const nodeDetailTab = ref('output'); // 'input' | 'output'
const showCopyTip = ref(false);

const selectNode = (item) => {
  selectedNodeDetail.value = item;
  nodeDetailTab.value = 'output';
};

const copyContent = async (content) => {
  try {
    const text = JSON.stringify(content, null, 2);
    await navigator.clipboard.writeText(text);
    showCopyTip.value = true;
    setTimeout(() => { showCopyTip.value = false; }, 2000);
  } catch (e) {
    jingrow.msgprint(__('复制失败，请手动复制'), 'Error');
  }
};

// 格式化显示内容，解析JSON字符串字段
const formatDisplayContent = (content) => {
  if (typeof content !== 'object' || content === null) return content;
  
  const result = {};
  for (const [key, value] of Object.entries(content)) {
    if (typeof value === 'string' && value.trim().startsWith('{')) {
      try {
        result[key] = JSON.parse(value);
      } catch {
        result[key] = value;
      }
    } else {
      result[key] = value;
    }
  }
  return result;
};
</script>

<style scoped>
.execution-results {
  display: flex;
  flex-direction: column;
  height: 100%;
  background: white;
}


.overview-card-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 6px;
  background: #f9fafb;
  margin-bottom: 8px;
}

.overview-card-header i {
  font-size: 16px;
}

.overview-info-header h5 {
  margin: 0 0 2px 0;
  font-size: 14px;
  font-weight: 600;
}

.overview-info-header p {
  margin: 0;
  color: #6b7280;
  font-size: 12px;
}


.execution-results-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}


/* 执行详情区域 */
.execution-details {
  flex: 1;
  display: flex;
  overflow: hidden;
}

.details-left {
  width: 300px;
  border-right: 1px solid #e5e7eb;
  display: flex;
  flex-direction: column;
}


.history-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.history-item {
  padding: 8px;
  margin-bottom: 6px;
  border-radius: 4px;
  background: white;
  border: 1px solid #e5e7eb;
  cursor: pointer;
  transition: all 0.2s;
}

.history-item:hover {
  border-color: #3b82f6;
  box-shadow: 0 2px 4px rgba(59, 130, 246, 0.1);
}

.history-item.active {
  border-color: #3b82f6;
  background: #eff6ff;
  box-shadow: 0 2px 8px rgba(59, 130, 246, 0.15);
}

.history-item-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 4px;
}

.history-node-name {
  font-weight: 600;
  color: #374151;
  font-size: 12px;
}

.history-status-badge {
  padding: 1px 6px;
  border-radius: 10px;
  font-size: 10px;
  font-weight: 600;
}

.history-item.success .history-status-badge {
  background: #dcfce7;
  color: #166534;
}

.history-item.failed .history-status-badge {
  background: #fee2e2;
  color: #991b1b;
}

.history-item-meta {
  font-size: 10px;
  color: #6b7280;
}

.no-history {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 200px;
}

.no-history-content {
  text-align: center;
  color: #6b7280;
}

.no-history-content i {
  font-size: 32px;
  margin-bottom: 12px;
  opacity: 0.5;
}

.no-history-content p {
  margin: 0;
  font-size: 14px;
}

/* 右侧详情区域 */
.details-right {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: white;
}

.details-right.empty {
  align-items: center;
  justify-content: center;
}

.empty-state {
  text-align: center;
  color: #6b7280;
}

.empty-state i {
  font-size: 48px;
  margin-bottom: 16px;
  opacity: 0.5;
}

.empty-state p {
  margin: 0;
  font-size: 16px;
}

.node-detail-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

.node-detail-header h6 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: #374151;
}

.detail-tabs {
  display: flex;
  gap: 0;
  margin-top: 8px;
}

.tab-btn {
  background: none;
  border: none;
  color: #6b7280;
  font-size: 14px;
  cursor: pointer;
  padding: 8px 16px;
  border-bottom: 2px solid transparent;
  transition: all 0.2s;
  position: relative;
}

.tab-btn:hover {
  color: #374151;
  background: #f9fafb;
}

.tab-btn.active {
  color: #3b82f6;
  border-bottom-color: #3b82f6;
  background: white;
}

.node-detail-content {
  flex: 1;
  overflow: hidden;
  border-top: 1px solid #e5e7eb;
}

.detail-panel {
  height: 100%;
  overflow: auto;
  position: relative;
}

.copy-btn-floating {
  position: absolute;
  top: 8px;
  right: 8px;
  background: none;
  border: none;
  color: #6B7280;
  font-size: 18px;
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 5px;
  transition: background 0.15s;
  z-index: 10;
}

.copy-btn-floating:hover {
  background: #f3f3f3;
  color: #1fc76f;
}

.json-viewer {
  flex: 1;
  margin: 0;
  padding: 12px;
  background: white;
  color: #374151;
  font-size: 12px;
  line-height: 1.5;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
}

.copy-tip {
  position: absolute;
  top: 18px;
  right: 60px;
  background: #fff;
  color: #222;
  padding: 4px 8px;
  border-radius: 4px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
  font-size: 12px;
  z-index: 1000;
}

.fade-enter-active, .fade-leave-active {
  transition: opacity 0.3s;
}

.fade-enter-from, .fade-leave-to {
  opacity: 0;
}

</style>
