import { Component, ElementRef, EventEmitter, Input, Output, SimpleChanges, ViewChild } from '@angular/core';
import { NzTabChangeEvent, ɵNzTabNavBarComponent } from 'ng-zorro-antd/tabs';
import { CommonNzModule } from './../../../../../../common.nz.module';
import { BasicDataService } from './../../../../../../services/basic.service';

export interface RiskItemSearchConfig {
  visible: boolean;
  type: 'job' | 'involve';
  involveType: string;
  title: string;
  value?: number;
}

@Component({
  selector: 'risk-item-search',
  standalone: true,
  imports: [CommonNzModule],
  templateUrl: './risk-item-search.component.html',
  styleUrl: './risk-item-search.component.less',
})
export class RiskItemSearchComponent {
  @ViewChild('tabset') tabset!: ElementRef<ɵNzTabNavBarComponent>;
  @Input() config!: RiskItemSearchConfig;
  @Output() onChange = new EventEmitter<{ type: string; value: number }>();
  constructor(private basic: BasicDataService) {}
  checkValue?: number = undefined;
  cacheValue?: number = undefined;
  searchText = '';
  tabIndex = 0;
  loading = false;
  ngOnChanges(changes: SimpleChanges) {
    if (changes['config']) {
      this.checkValue = this.config.value;
      this.searchText = '';
      const index = ['position', 'operation', 'equipment'].findIndex(t => t === this.config.involveType);

      this.tabIndex = index === -1 ? 0 : index;
      // @ts-ignore
      this.tabset?.setSelectedIndex(this.tabIndex);
    }
  }
  onTabChange(ev: NzTabChangeEvent) {
    this.loading = true;
    if (this.checkValue) {
      this.cacheValue = this.checkValue;
    }
    this.checkValue = undefined;
    if (this.config.involveType === 'position' && ev.index === 0) {
      this.checkValue = this.cacheValue;
    } else if (this.config.involveType === 'operation' && ev.index === 1) {
      this.checkValue = this.cacheValue;
    } else if (this.config.involveType === 'equipment' && ev.index === 2) {
      this.checkValue = this.cacheValue;
    }
    setTimeout(() => {
      this.loading = false;
    }, 400);
  }
  onSearch() {}
  handleCheck(type: string, value: number) {
    this.onChange.emit({ type, value });
  }
  get positions() {
    const list = this.basic.positions;
    if (this.searchText) {
      return list.filter(l => l.name.includes(this.searchText));
    }
    return list;
  }
  get operations() {
    const list = this.basic.operations;
    if (this.searchText) {
      return list.filter(l => l.name.includes(this.searchText));
    }
    return list;
  }

  get equipments() {
    const list = this.basic.equipments;
    if (this.searchText) {
      return list.filter(l => l.name.includes(this.searchText));
    }
    return list;
  }
  get jobs() {
    const list = this.basic.jobs.map(j => {
      return {
        ...j,
        name: this.basic.getJobNameWithDepartment(j.id),
      };
    });
    if (this.searchText) {
      return list.filter(l => l.name.includes(this.searchText));
    }
    return list;
  }
  get type() {
    return this.config.type;
  }
  get isJob() {
    return this.type === 'job';
  }
}
