import { Component, OnInit, Output, EventEmitter } from '@angular/core';
import { Router } from '@angular/router';
import { Store } from '@ngxs/store';

import { ModalService } from '@libs/ui';
import { ModalConfirmationService } from '@sales/core-services';

import { ResetBreadcrumbsTo } from 'apps/sales/src/app/core/store/actions/breadcrumb.actions';
import { SetBranchProcessCurrentPath } from 'apps/sales/src/app/core/store/actions/branch-process.actions';
import { ModuleSwitcherService } from 'apps/sales/src/app/core/services/module-switcher.service';
import { ModuleSwitcher } from 'apps/sales/src/app/core/models/app-switcher.enum';

@Component({
  selector: 'app-remission-leave-dialog',
  templateUrl: './remission-leave-dialog.component.html',
  styleUrls: ['./remission-leave-dialog.component.scss'],
})
export class RemissionLeaveDialogComponent implements OnInit {
  id = 'remission-leave-modal';
  constructor(
    private modalService: ModalService,
    private modalConfirmationService: ModalConfirmationService,
    private router: Router,
    private store: Store,
    private moduleSwitcher: ModuleSwitcherService
  ) {}

  ngOnInit() {}

  public openDialog() {
    this.modalService.open(this.id);
  }

  cancel() {
    this.close();
    this.modalConfirmationService.close();
  }

  confirm() {
    this.close();
    this.modalConfirmationService.close(true);
    this.moduleSwitcher.switchToBranch();
  }

  navigateRemissionCreateList() {
    const path = '/remission/create';
    this.store.dispatch(
      new ResetBreadcrumbsTo(
        {
          path: path,
          name: 'Remission',
        },
        'remission'
      )
    );
    this.router.navigate([path]);
    this.store.dispatch(new SetBranchProcessCurrentPath(path, true));
  }

  private close() {
    this.modalService.close(this.id);
  }
}
