import { Component, computed, inject, OnInit, Signal } from '@angular/core';
import { RouterModule } from '@angular/router';
import { BaseComponent, untilDestroy } from 'angularlib/base.component';
import { LoginModule } from 'angularlib/login';
import { Store } from '@ngxs/store';
import { LoginService } from 'angularlib/login/login.service';
import { CommonModule } from '@angular/common';
import { ComponentService } from 'angularlib/component.service';
import { LabelModule } from 'angularlib/labels/label.module';
import { masterMenu } from '../menu/menu';
import { InputModule } from 'angularlib/input/input.module';
import { EventI } from 'fis-commons/event/event';
import { LeaveModule } from 'fis/leave';
import { MatModule } from 'angularlib/mat.module';
import { TenderModule } from 'fis/tender/tender.module';
import { FISAccess } from 'fis/fis.access';

@Component({
    selector: 'spts-sales-submission-dashitem',
    imports: [
        CommonModule,
        RouterModule,
        MatModule,
        LoginModule,
        LabelModule,
        InputModule,
        LeaveModule,
        TenderModule
    ],
    providers: [ComponentService],
    templateUrl: './dashboard.component.html',
    styleUrl: './dashboard.component.scss'
})
export class DashboardComponent extends BaseComponent implements OnInit{
  protected masterMenu = masterMenu;
  protected username: Signal<string> = computed(() => {
    const GIVEN_NAME = this.loginService?.userSignal()?.name?.givenName;
    const FIS_NAME = this.loginService?.userSignal()?.fisInfo?.name;
    return GIVEN_NAME || FIS_NAME;
  });
  protected access = inject(FISAccess);
  protected accessRight:any = {};

  protected goto = {
    field: {
      key: 'goto',
      type: 'select',
      label: {key:'will_redirect_to',default:'Go to'},
      selectOptions: [
        {value:'home',label:{key:'home_page',default:'Home'}},
        {value:'leave-new',label:{key:'new_leave',default:'Apply New Leave'}},
        {value:'leave-applied',label:{key:'applied_leave',default:'Applied Leave'}},
        {value:'leave-approval',label:{key:'leave_approval',default:'Leave Approval'}},
      ]
    },
    value: 'home',
    onChange: (event: EventI) => {
      switch (event?.payload?.value) {
        case 'leave-new' : {
          this.cs.navigate('/leave',{type:'new'});
          break;
        }
        case 'leave-applied' : {
          this.cs.navigate('/leave',{type:'view'});
          break;
        }
        case 'leave-approval' : {
          this.cs.navigate('/leave/approval');
          break;
        }
        case 'spts-sales' : {
          this.cs.navigate('/tender',{type:'sales'});
          break;
        }
        default: break;
      }
    }
  }

  constructor(
    protected store: Store,
    protected loginService: LoginService,
    protected cs: ComponentService
  ) {
    super(store);
  }

  ngOnInit(): void {
    super.ngOnInit();
  }
  
  ngAfterViewInit() {
    this.loginService.user$.pipe(untilDestroy(this)).subscribe({
      next: res => {
        this.accessRight = {
          ...this.accessRight,
          leave: {approval: this.access.canActivate({data:{serviceId:'09 - Leave Application',accessRight:['canread','canapprove'],noprompt:true}},undefined)}
        };
      }
    })
  }
}