import { Component, OnInit, signal } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { AuthService } from '../../../services/auth.service';

@Component({
  selector: 'app-dashboard',
  imports: [CommonModule, RouterModule],
  templateUrl: './dashboard.html',
  styleUrl: './dashboard.scss'
})
export class Dashboard implements OnInit {
  // 添加today属性用于模板中的日期显示
  today = new Date();
  
  // 待办任务数据
  todoTasks = signal([
    { id: 1, title: '临时收款待核定', priority: 'high', source: 'reconciliation', dueTime: '10:30' },
    { id: 2, title: '报价待审核', priority: 'medium', source: 'project-records', dueTime: '14:00' },
    { id: 3, title: '素材成本待录入', priority: 'low', source: 'reconciliation', dueTime: '16:30' },
    { id: 4, title: '临时收款待核定', priority: 'high', source: 'reconciliation', dueTime: '11:00' },
    { id: 5, title: '销售监管提醒', priority: 'high', source: 'project-records', dueTime: '09:30' }
  ]);

  // 数据概览数据
  dashboardStats = signal({
    todayOrders: 12,
    pendingPayment: 156800,
    quotedProjects: 28,
    materialCostSaved: 8900
  });

  // 用户角色
  userRole = signal('teamLead'); // teamLead 或 juniorMember

  constructor(private authService: AuthService) {}

  ngOnInit(): void {
    // 初始化用户角色
    this.initializeUserRole();
  }
  
  // 初始化用户角色
  initializeUserRole(): void {
    // 从AuthService获取用户角色
    const roles = this.authService.getUserRoles();
    // 默认使用teamLead角色
    let userRole = 'teamLead';
    
    // 如果用户有admin角色，也视为teamLead
    if (roles.includes('admin')) {
      userRole = 'teamLead';
    } else if (roles.length > 0) {
      // 否则使用第一个角色
      userRole = roles[0];
    }
    
    this.userRole.set(userRole);
    
    // 根据用户角色过滤待办任务
    this.filterTasksByRole();
  }
  
  // 根据用户角色过滤待办任务
  filterTasksByRole(): void {
    if (this.userRole() !== 'teamLead') {
      // 初级组员只显示非财务审批类任务
      const filteredTasks = this.todoTasks().filter(task => 
        task.title !== '临时收款待核定' && task.title !== '报价待审核'
      );
      this.todoTasks.set(filteredTasks);
    }
  }
  
  // 检查用户角色
  checkUserRole(requiredRole: string): boolean {
    return this.authService.hasRole(requiredRole);
  }

  // 处理待办任务点击
  handleTaskClick(task: any) {
    // 检查财务相关任务的权限
    if ((task.title === '临时收款待核定' || task.title === '报价待审核') && !this.checkUserRole('teamLead')) {
      console.warn('权限不足：只有组长可以处理此任务');
      return;
    }
    
    // 根据任务来源跳转到对应页面
    switch(task.source) {
      case 'reconciliation':
        window.location.href = '/finance/reconciliation';
        break;
      case 'project-records':
        window.location.href = '/finance/project-records';
        break;
    }
  }

  // 处理快捷操作点击
  handleQuickAction(action: string) {
    // 检查权限
    if ((action === 'recordPayment' || action === 'generateReport') && !this.checkUserRole('teamLead')) {
      console.warn('权限不足：只有组长可以执行此操作');
      return;
    }
    
    switch(action) {
      case 'newQuote':
        window.location.href = '/finance/project-records';
        break;
      case 'recordPayment':
        window.location.href = '/finance/reconciliation';
        break;
      case 'generateReport':
        window.location.href = '/finance/reports';
        break;
    }
  }

  // 格式化金额显示
  formatAmount(amount: number): string {
    return new Intl.NumberFormat('zh-CN', { style: 'currency', currency: 'CNY' }).format(amount);
  }
}
