import { Component, OnInit } from '@angular/core';
import { map, switchMap } from 'rxjs/operators';
import { AuthService } from 'src/app/auth/services/auth.service';
import { ActionsUser } from 'src/app/models/actions-user.model';
import { PagesUser } from 'src/app/models/pages-user.model';
import { ShoppingService } from 'src/app/services/shopping.service';

@Component({
  templateUrl: './partner-menu.component.html',
  styleUrls: ['./partner-menu.component.scss']
})
export class PartnerMenuComponent implements OnInit {
  status = false;
  /* Header */
  public userName: string;
  public storeName: string;

  pagesUser: PagesUser[];

  /* Pages */
  pagePartners = true;
  pageCategory = true;
  pageRange = true;
  pageReportComission = true;
  pagePaymentPartner = true;
  pageExtractPartner = true;

  constructor(
    private authService: AuthService,
    private shoppingService: ShoppingService,
  ) { }

  ngOnInit(): void {
    this.userName = this.authService.getUserName();
    this.storeName = this.authService.getStoreName();
    const sellerId = this.authService.getSeller();
    this.authService.getPagesUser(2)
      .pipe(
        map((modules) => {
          this.pagesUser = modules;
          this.configPagesUser();
        }),
        switchMap((modules) => this.authService.getActionsUser(2)),
        map((actions) => this.authService.actionsUser = actions))
      .subscribe();
  }

  clickEvent() {
    this.status = !this.status;
  }

  logout() {
    this.shoppingService.cancelShopping();
    this.authService.logout();
  }

  configPagesUser() {
    this.pagePartners = this.pageEnable(1, 1);
    this.pageCategory = this.pageEnable(1, 2);
    this.pageRange = this.pageEnable(1, 3);
    this.pageReportComission = this.pageEnable(2, 1);
    this.pageExtractPartner = this.pageEnable(2, 2);
    this.pagePaymentPartner = this.pageEnable(3, 1);
  }

  pageEnable(processId: number, pageId: number) {
    const page = this.pagesUser.find(p => p.moduleId == 2 && p.processId == processId && p.pageId == pageId);
    if (page === undefined) {
      return false;
    } else {
      return page.access == 'S';
    }
  }

}
