import { Component, EventEmitter, Input, Output, SimpleChanges } from '@angular/core';
import { CommonNzModule } from '../../../../common.nz.module';
import { BasicDataService } from '../../../../services/basic.service';
import { USER_ROLE_OPTIONS, UserRole } from '../user.const';

@Component({
  selector: 'user-table',
  standalone: true,
  imports: [CommonNzModule],
  templateUrl: './user-table.component.html',
  styleUrl: './user-table.component.less',
})
export class UserTableComponent {
  checkCurrentPageAll = false;
  indeterminate = false;
  @Input() checkedIds = new Set<string>();
  @Output() fetch = new EventEmitter();
  @Output() handleEdit = new EventEmitter<Auth.User>();
  @Output() handleDelete = new EventEmitter<Auth.User>();
  @Output() handleModifyRoles = new EventEmitter<Auth.User>();
  @Output() selectIdsChange = new EventEmitter();

  @Input() pageConfig = {
    total: 0,
    page: 0,
    size: 10,
  };
  @Input() list: Auth.User[] = [];
  @Input() roleOptions: Option<string>[] = [];
  @Input() statusOptions: Option<number>[] = [];

  constructor(private basic: BasicDataService) {}
  ngOnChanges(changes: SimpleChanges) {
    if (changes['list']) {
      this.refreshCheckedStatus();
    }
  }
  transformRolesToLabels(d: Auth.User) {
    return USER_ROLE_OPTIONS.filter(({ value }) => d.roles.includes(value)).map(op => op.label);
  }

  updateCheckedSet(id: string, checked: boolean): void {
    if (checked) {
      this.checkedIds.add(id);
    } else {
      this.checkedIds.delete(id);
    }
  }
  getJobName(jobId: number) {
    return this.basic.jobs.find(j => j.id === jobId)?.name;
  }
  getDepartmentName(departmentId: number) {
    return this.basic.departments.find(d => d.id === departmentId)?.name;
  }

  onItemChecked(id: string, checked: boolean): void {
    this.updateCheckedSet(id, checked);
    this.refreshCheckedStatus();
  }

  onAllChecked(checked: boolean): void {
    this.list.forEach(({ id }) => this.updateCheckedSet(id, checked));
    this.refreshCheckedStatus();
  }
  refreshCheckedStatus(): void {
    this.checkCurrentPageAll = this.list.every(({ id }) => this.checkedIds.has(id));
    this.indeterminate = this.list.some(({ id }) => this.checkedIds.has(id)) && !this.checkCurrentPageAll;

    this.selectIdsChange.emit(this.checkedIds);
  }
  getStatusText(status: number) {
    return this.statusOptions.find(item => item.value === status)?.label;
  }
  onCurrentPageChange(current: number) {
    this.pageConfig.page = current;
    this.fetch.emit();
  }
  onEdit(user: Auth.User) {
    this.handleEdit.emit(user);
  }
  onModifyRoles(user: Auth.User) {
    this.handleModifyRoles.emit(user);
  }
  onDelete(user: Auth.User) {
    this.handleDelete.emit(user);
  }
  editable(user: Auth.User) {
    return user.roles.every(r => r !== UserRole.ADMIN);
  }
  deleteAble(user: Auth.User) {
    return user.roles.every(r => r !== UserRole.ADMIN);
  }
  modifyRolesAble(user: Auth.User) {
    return user.roles.every(r => r !== UserRole.ADMIN);
  }
}
