import { Component, inject } from '@angular/core';
import { NonNullableFormBuilder, Validators } from '@angular/forms';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NZ_MODAL_DATA } from 'ng-zorro-antd/modal';
import { NzUploadChangeParam } from 'ng-zorro-antd/upload';
import { CommonNzModule } from '../../../../common.nz.module';
import { ApiService } from '../../../../services/api.service';
import { AuthService } from '../../../../services/auth.service';

export interface UserImportModalData {
  onImportSuccess: () => void; // Add this callback
}

@Component({
  selector: 'user-import',
  standalone: true,
  imports: [CommonNzModule],
  templateUrl: './user-import.component.html',
  styleUrl: './user-import.component.less',
})
export class UserImportComponent {
  readonly nzModalData: UserImportModalData = inject(NZ_MODAL_DATA);
  exporting: boolean = false;
  get companyOptions() {
    return [];
  }
  importing: boolean = false;

  // repeatWords: string[] = [];
  constructor(
    private message: NzMessageService,
    private auth: AuthService,
    private fb: NonNullableFormBuilder,
    private api: ApiService
  ) {}
  validateForm = this.fb.group({
    company: this.fb.control<string | undefined>(undefined, {
      validators: [Validators.required],
    }),
  });
  selectFields: {
    label: string;
    key: string;
    minWidth?: string;
    options: Option[];
  }[] = [
    { label: '公司', key: 'company', options: [], minWidth: '150px' },
    // { label: '线路', key: 'line', options: [], minWidth: '90px' },
    // { label: '岗位', key: 'post', options: [], minWidth: '150px' },
  ];

  handleImportStatusChange(event: NzUploadChangeParam) {
    if (event.type === 'start') {
      this.importing = true;
    }
    if (event.type === 'success') {
      if (event.file.response?.code !== 0) {
        this.message.error('导入失败: ' + event.file.response?.message);
        this.importing = false;
        return;
      }
      this.message.success('导入成功');
      this.importing = false;

      // Call the callback function to refresh the list in the parent component
      if (this.nzModalData.onImportSuccess) {
        this.nzModalData.onImportSuccess();
      }
    } else if (event.type === 'error') {
      if (event.file.error.error?.message) {
        this.message.error('导入失败: ' + event.file.error.error.message);
      } else {
        this.message.error('导入失败');
      }
      this.importing = false;
    }
  }
  handleReset() {
    this.validateForm.reset();
  }
}
