<template>
  <Teleport :to="computedTeleportTo">
    <div v-if="modelValue" class="node-property-modal-mask" @click.self="onClose">
      <div class="node-property-modal-content" :style="modalStyle">
        <div class="node-property-modal-header">
          <slot name="header">
            <h4>{{ computedTitle }}</h4>
          </slot>
          <button class="modal-close" @click="onClose">×</button>
        </div>
        <div class="node-property-modal-body">
          <slot />
        </div>
        <div class="node-property-modal-footer">
          <slot name="footer">
            <button class="btn btn-secondary" @click="onClose">取消</button>
            <button class="btn btn-primary" @click="$emit('save')">保存</button>
          </slot>
        </div>
      </div>
    </div>
  </Teleport>
</template>

<script setup>
import { computed } from 'vue';
const props = defineProps({
  modelValue: Boolean,
  title: { type: String, default: '' },
  nodeLabel: { type: String, default: '' },
  width: { type: [String, Number], default: 900 },
  maxHeight: { type: [String, Number], default: '90vh' },
  teleportTo: { type: [String, Object], default: null }
});
const emit = defineEmits(['update:modelValue', 'close', 'save']);

// 计算标题：如果传入了title则使用，否则自动生成"节点属性配置-{节点名称}"格式
const computedTitle = computed(() => {
  if (props.title) {
    return props.title;
  }
  return `节点属性-${props.nodeLabel || '节点'}`;
});

// 动态计算 teleport 目标
const computedTeleportTo = computed(() => {
  // 优先使用传入的 teleportTo prop
  if (props.teleportTo) {
    return props.teleportTo;
  }
  // 其次使用全局的 teleport 目标
  if (window.nodePropertyTeleportTarget) {
    return window.nodePropertyTeleportTarget.value;
  }
  // 最后使用默认值
  return '#ai-agent-flow-builder-container';
});

const onClose = () => {
  emit('update:modelValue', false);
  emit('close');
};

const modalStyle = computed(() => ({
  width: typeof props.width === 'number' ? props.width + 'px' : props.width,
  maxWidth: '98vw',
  maxHeight: typeof props.maxHeight === 'number' ? props.maxHeight + 'px' : props.maxHeight,
  background: 'white',
  borderRadius: '10px',
  boxShadow: '0 8px 32px rgba(0,0,0,0.18)',
  display: 'flex',
  flexDirection: 'column',
}));
</script>

<style scoped>
.node-property-modal-mask {
  position: fixed;
  z-index: 99999;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.18);
  display: flex;
  align-items: center;
  justify-content: center;
}
.node-property-modal-content {
  min-width: 420px;
  max-width: 98vw;
  max-height: 90vh;
  background: white;
  border-radius: 10px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.18);
  display: flex;
  flex-direction: column;
}
.node-property-modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 24px 8px 24px;
  border-bottom: 1px solid #e5e7eb;
}
.node-property-modal-header h4 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
}
.modal-close {
  background: none;
  border: none;
  font-size: 24px;
  color: #888;
  cursor: pointer;
}
.node-property-modal-body {
  flex: 1;
  overflow-y: auto;
  padding: 0 24px;
  max-height: 60vh;
  min-height: 120px;
}
.node-property-modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding: 16px 24px;
  border-top: 1px solid #e5e7eb;
}
.btn {
  padding: 6px 18px;
  border-radius: 4px;
  font-size: 14px;
  font-weight: 500;
  border: none;
  cursor: pointer;
}
.btn-primary {
  background: #222;
  color: #fff;
}
.btn-secondary {
  background: #f3f4f6;
  color: #222;
}

/* 合并全局表单样式，仅作用于弹窗内容 */
.node-property-modal-content .property-section {
  margin-bottom: 24px;
}
.node-property-modal-content .property-section h5 {
  margin: 0 0 12px 0;
  font-size: 16px;
  font-weight: 600;
  color: #374151;
  border-bottom: 1px solid #e5e7eb;
  padding-bottom: 8px;
}
.node-property-modal-content .form-group {
  margin-bottom: 0;
}
.node-property-modal-content .form-group label {
  display: block;
  margin-bottom: 6px;
  font-weight: 500;
  color: #374151;
  font-size: 14px;
}
.node-property-modal-content .form-control {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  font-size: 14px;
  transition: border-color 0.2s, box-shadow 0.2s;
  box-sizing: border-box;
}
.node-property-modal-content .form-control:focus {
  outline: none;
  border-color: #6366f1;
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
}
.node-property-modal-content .form-control::placeholder {
  color: #9ca3af;
}
.node-property-modal-content textarea.form-control {
  resize: vertical;
  min-height: 60px;
}
.node-property-modal-content select.form-control {
  width: 100%;
  min-width: 0;
  max-width: 100%;
  background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='%236b7280' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='m6 8 4 4 4-4'/%3e%3c/svg%3e");
  background-position: right 8px center;
  background-repeat: no-repeat;
  background-size: 16px 12px;
  padding-right: 32px;
  appearance: none;
}
.node-property-modal-content .form-control-sm {
  padding: 4px 8px;
  font-size: 12px;
}
.node-property-modal-content .table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 16px;
}
.node-property-modal-content .table th,
.node-property-modal-content .table td {
  padding: 8px;
  border: 1px solid #e5e7eb;
  text-align: left;
}
.node-property-modal-content .table th {
  background-color: #f9fafb;
  font-weight: 600;
  font-size: 12px;
}
.node-property-modal-content .table td {
  font-size: 12px;
}
.node-property-modal-content .btn-danger {
  background: #ef4444;
  color: #fff;
}
.node-property-modal-content .btn-danger:hover {
  background: #dc2626;
}
.node-property-modal-content .form-text {
  font-size: 12px;
  color: #6b7280;
  margin-top: 4px;
}
</style> 