import { Component } from '@angular/core';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { UserRole } from '../../../manager/user/user.const';
import { CommonNzModule } from './../../../../common.nz.module';
import { BasicDataService } from './../../../../services/basic.service';
import { SettingService } from './../../../../services/setting.service';
import { AccidentFormComponent } from './accident-form/accident-form.component';
import { HistoryRecordComponent } from './history-record/history-record.component';

@Component({
  selector: 'digital-line-accident-modal',
  standalone: true,
  imports: [CommonNzModule, AccidentFormComponent, HistoryRecordComponent],
  templateUrl: './accident-modal.component.html',
  styleUrl: './accident-modal.component.less',
})
export class AccidentModalComponent {
  activeTab = 'new';
  action = 'new';
  editable = false;
  constructor(
    private modal: NzModalRef,
    private basic: BasicDataService,
    private setting: SettingService
  ) {
    this.setEditAbility();
    if (this.editable) {
      this.activeTab = 'new';
    } else {
      this.activeTab = 'history';
    }
  }
  setEditAbility() {
    let editable = false;
    if (!this.setting.user) {
      editable = false;
      return;
    }
    if (this.setting.user?.roles?.includes(UserRole.ADMIN)) {
      editable = true;
    } else if (this.setting.user?.roles?.includes(UserRole.MANAGER)) {
      editable = true;
    } else if (this.setting.user?.roles?.includes(UserRole.CHIEF) && this.isOperatingDepartment()) {
      editable = true;
    }
    this.editable = editable;
  }
  isOperatingDepartment() {
    const depId = this.setting.user?.department;
    if (!depId) {
      return false;
    }
    const dep = this.basic.getDepartmentName(depId) || '';
    if (dep.includes('运营')) {
      return true;
    }
    return false;
  }
  tabChange(tab: 'new' | 'history') {
    this.activeTab = tab;
  }

  onClose() {
    this.modal.destroy();
  }
  onToRecord() {
    this.activeTab = 'history';
  }
}
