<template>
  <teleport to="body">
    <div v-if="modelValue" class="my-dialog-overlay" @click.self="close">
      <div class="my-dialog" :style="{ width: width + 'px' }">
        <div class="my-dialog-header">
          <span class="my-dialog-title">{{ title }}</span>
          <el-button size="small" text @click="close">×</el-button>
        </div>
        <div class="my-dialog-body">
          <slot />
        </div>
        <div v-if="$slots.footer" class="my-dialog-footer">
          <slot name="footer" />
        </div>
      </div>
    </div>
  </teleport>
</template>

<script setup lang="ts">
defineProps<{
  modelValue: boolean
  title?: string
  width?: number
}>()

const emit = defineEmits<{
  (e: 'update:modelValue', v: boolean): void
}>()

function close() {
  emit('update:modelValue', false)
}
</script>

<style scoped>
.my-dialog-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1001;
}
.my-dialog {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.15);
  max-width: 90vw;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
}
.my-dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid #e4e7ed;
}
.my-dialog-title {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
}
.my-dialog-close {
  background: none;
  border: none;
  font-size: 20px;
  color: #909399;
  cursor: pointer;
  padding: 0 4px;
  line-height: 1;
}
.my-dialog-close:hover {
  color: #409eff;
}
.my-dialog-body {
  padding: 20px;
  overflow-y: auto;
  flex: 1;
}
.my-dialog-footer {
  padding: 12px 20px;
  border-top: 1px solid #e4e7ed;
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}
</style>
