import { Component, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { ActivatedRoute, RouterModule } from '@angular/router';
import { FmodeParse, FmodeObject } from 'fmode-ng/parse';
import { ProfileService } from '../../../../../app/services/profile.service';
import { ProjectIssuesModalComponent } from '../../../components/project-issues-modal/project-issues-modal.component';

const Parse = FmodeParse.with('nova');

@Component({
  selector: 'app-stage-issues',
  standalone: true,
  imports: [CommonModule, FormsModule, RouterModule, ProjectIssuesModalComponent],
  template: `
    <div class="stage-issues-page">
      <h2 class="page-title">问题追踪</h2>
      <div class="page-body">
        <app-project-issues-modal
          [project]="project"
          [currentUser]="currentUser"
          [isVisible]="true"
          (close)="onClose()">
        </app-project-issues-modal>
      </div>
    </div>
  `,
  styles: [
    `.stage-issues-page { padding: 12px; }`,
    `.page-title { font-size: 16px; font-weight: 600; color: #111827; margin: 8px 0 12px; }`
  ]
})
export class StageIssuesComponent implements OnInit {
  project: FmodeObject | null = null;
  currentUser: FmodeObject | null = null;

  constructor(private route: ActivatedRoute, private profileService: ProfileService) {}

  async ngOnInit() {
    const projectId = this.route.parent?.snapshot.paramMap.get('projectId') || '';
    // 从根参数取 cid（可选，不强制）
    const cid = this.route.parent?.parent?.snapshot.paramMap.get('cid') || '';

    // 加载当前用户
    this.currentUser = await this.profileService.getCurrentProfile(cid);

    // 加载项目
    if (projectId) {
      const query = new Parse.Query('Project');
      query.include('customer', 'assignee','department','department.leader');
      this.project = await query.get(projectId);
    }
  }

  onClose() {
    // 关闭时返回到订单阶段（或上一页）
    history.back();
  }
}