import { Component, Input, Output, EventEmitter, ChangeDetectionStrategy } from '@angular/core';
import { CommonModule } from '@angular/common';

type PromptMode = 'fullscreen' | 'corner';
type CornerPosition = 'top-right' | 'bottom-right';

@Component({
  selector: 'app-global-prompt',
  standalone: true,
  imports: [CommonModule],
  templateUrl: './global-prompt.component.html',
  styleUrls: ['./global-prompt.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class GlobalPromptComponent {
  @Input() visible = false;
  @Input() title = '上传成功！';
  @Input() message = '';
  @Input() mode: PromptMode = 'fullscreen';
  @Input() position: CornerPosition = 'bottom-right';
  @Input() icon: 'success' | 'info' | 'warning' = 'success';
  @Input() autoDismissMs = 0; // 0 表示不自动关闭

  @Output() closed = new EventEmitter<void>();
  @Output() action = new EventEmitter<void>();

  ngOnChanges() {
    if (this.visible && this.autoDismissMs > 0) {
      window.clearTimeout((this as any)._dismissTimer);
      (this as any)._dismissTimer = window.setTimeout(() => {
        this.onClose();
      }, this.autoDismissMs);
    }
  }

  onClose() {
    this.closed.emit();
  }

  onAction() {
    this.action.emit();
  }
}