import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { Router, RouterModule } from '@angular/router';

@Component({
  selector: 'app-login',
  standalone: true,
  imports: [CommonModule, FormsModule, RouterModule],
  templateUrl: './login.html',
  styleUrls: ['./login.scss']
})
export class LoginPage {
  username = '';
  password = '';
  loading = false;
  error = '';
  currentYear = new Date().getFullYear();

  constructor(private router: Router) {}

  async signIn() {
    this.error = '';
    this.loading = true;
    try {
      // 这里保留为演示登录流程的占位，不破坏现有业务逻辑
      await new Promise(r => setTimeout(r, 400));
      // 登录成功后可跳转到常用角色，实际项目中可根据权限返回跳转
      await this.router.navigateByUrl('/customer-service/dashboard');
    } catch (e) {
      this.error = '登录失败，请重试';
    } finally {
      this.loading = false;
    }
  }

  goToRole(role: 'customer-service' | 'designer' | 'team-leader' | 'finance' | 'hr' | 'admin') {
    const map: Record<string, string> = {
      'customer-service': '/customer-service/dashboard',
      'designer': '/designer/dashboard',
      'team-leader': '/team-leader/dashboard',
      'finance': '/finance/dashboard',
      'hr': '/hr/dashboard',
      'admin': '/admin/dashboard'
    };

    // iOS 风格过渡动画（与项目内其他页面行为一致）
    document.body.classList.add('ios-page-transition');
    setTimeout(() => {
      this.router.navigateByUrl(map[role]).finally(() => {
        setTimeout(() => document.body.classList.remove('ios-page-transition'), 300);
      });
    }, 100);
  }
}