

import { Component, Input, OnInit } from '@angular/core';
import { EN_EmployeeRang, EN_EmployeeStatus } from '@app_consts';
import { Employee } from '@app_models/employee';
import { EmployeeToDepartment } from '@app_models/employee-to-department';
import { EmployeeToWebApp } from '@app_models/employee-to-webapp';
import { EmployeeDataService } from '@app_modules/employee/employee-data.service';
import { AppBaseEntityWrapper } from '@app_services/app.baseentity.wrapper';
import { AppBaseEntityListWrapper } from '@app_services/app.baseentitylist.wrapper';
import { AppDataService } from '@app_services/app.data.service';
import { EmployeeToDepartmentPopupeditComponent } from './employee-department.popupedit/employee-department.popupedit.component';
import { MatDialogService } from '@app_core/services/mat-dialog.service';
import { ColumnConfigList } from '@app_core/components/grid-config/columnconfiglist';
import { LocaleService } from '@app_core/services/localization/locale.service';
import { Globals } from '@app_core/services/globals';
import { ColumnConfig, EN_SortOrder } from '@app_core/components/grid-config/columnconfig';

@Component({
  selector: 'app-employee-department',
  templateUrl: './employee-department.component.html',
  styleUrls: ['./employee-department.component.scss']
})

export class EmployeeDepartmentComponent implements OnInit {

  @Input() employeeDepartmentList: AppBaseEntityListWrapper<EmployeeToDepartment>;
  @Input() public employeeDetails: AppBaseEntityWrapper<Employee>;

  public columnConfigList: ColumnConfigList = new ColumnConfigList();

  constructor(
    private dialog: MatDialogService,
    public appDataService: AppDataService,
    public employeeDataService: EmployeeDataService,
    public localeService: LocaleService,
    public globals: Globals,
  ) {
  }

  get emplDepDetails(): AppBaseEntityWrapper<EmployeeToDepartment> {
    return this.employeeDepartmentList.focusedEntityShadowed;
  }

  ngOnInit() {
    this.initColumns();
  }

  initColumns() {
    let col: ColumnConfig;

    if (this.appDataService.showIds) col = this.columnConfigList.addNumber('Id', 'entityId', 7, 0);

    col = this.columnConfigList.add('Name', 'departmentId', 25);

    col = this.columnConfigList.add('Status', 'employeeStatusId', 20);
    col.sortIndex = 1;
    col.sortOrder = EN_SortOrder.asc;
    col = this.columnConfigList.add('Rang', 'rangId', 10);

    this.columnConfigList.recalcWidth();
  }

  addItem() {
    this.emplDepDetails.new();
    this.emplDepDetails.entity.employeeId = this.employeeDetails.entityId;
    const employeeStatusId = this.employeeDepartmentList.items.find(e2d => e2d.employeeStatusId === EN_EmployeeStatus.MainFunction) ? EN_EmployeeStatus.AdditionalFunction : EN_EmployeeStatus.MainFunction;
    this.emplDepDetails.entity.employeeStatusId = employeeStatusId;
    this.emplDepDetails.entity.rangId = EN_EmployeeRang.Mitarbeiter;
    this.openPopupDialog(this.emplDepDetails);
  }

  dblClick(event: any) {
    if (!this.employeeDetails.entityIsLoading) this.employeeDepartmentList.focusedEntityShadowed.delayedEdit(event.data['entityId'], this.editItem);
  }

  editItem = () => {
    this.emplDepDetails.edit();
    this.openPopupDialog(this.emplDepDetails);
  }

  public openPopupDialog(editEntity: AppBaseEntityWrapper<EmployeeToDepartment>) {
    this.employeeDetails.keyDownListenerStopped = true;
    this.dialog.openDialog(EmployeeToDepartmentPopupeditComponent, {
      baseEntityWrapper: editEntity,
      keyItemName: this.employeeDetails.entity.firstName + ' ' + this.employeeDetails.entity.lastName,
      readOnly: !this.employeeDetails.inNotViewMode,
      description: (editEntity.entity.entityId === 0 ? 'Neue Abteilung zuweisen' : 'Daten zur Abteilung "' + editEntity.entity.departmentName + '" bearbeiten '),
      list: this.employeeDepartmentList,
    }, true, () => this.employeeDetails.keyDownListenerStopped = false);
  }


  public deleteDepartment() {
    let usedDepartmentIdsStr: string = '';
    this.employeeDataService.employee2WebAppList.items.forEach((e2w: EmployeeToWebApp) => usedDepartmentIdsStr += ',' + e2w.departmentId + ',' + e2w.arExtendedDepartmentIdList);
    const usedDepartmentIdsList: string[] = usedDepartmentIdsStr.split(',');
    if (usedDepartmentIdsList.indexOf('' + this.emplDepDetails.entity.departmentId) > -1) {
      this.emplDepDetails.warningMessageBox(`Löschen von Abteilung <${this.emplDepDetails.entity.departmentName}>`,
        'Die Abteilung kann nicht gelöscht werden, weil sie zu einer Webapplikation zugewiesen ist');
    } else this.emplDepDetails.delete();
  }
}
