import { Component, OnInit, ChangeDetectionStrategy, EventEmitter, Input, OnChanges, Output } from '@angular/core';
import { forkJoin } from 'rxjs';
import { filter, tap, concatMap } from 'rxjs/operators';
import { BaseList } from 'src/app/core/base/base-list';
import { OnlineCountersService } from 'src/app/core/service/settings/online-counters.service';
import { RecipientSignatureService } from 'src/app/core/service/settings/recipient-signature.service';
import { SweetalertService } from 'src/app/core/service/sweetalert/sweetalert';

@Component({
  selector: 'app-list',
  templateUrl: './list.component.html',
  styleUrls: ['./list.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ListComponent extends BaseList implements OnInit, OnChanges {
  @Input() signatureList: any = [];
  @Output() onEdit = new EventEmitter()
  @Output() onDelete = new EventEmitter()
  constructor(
    private swSV: SweetalertService,
    private recipientSignatureSV: RecipientSignatureService
  ) {
    super()
  }

  ngOnInit(): void {
  }

  ngOnChanges(): void {
    this.signatureList = this.updateMatTable(this.signatureList ? this.signatureList : [])
  }

  setDefault(event,i,self){
    event.preventDefault();
    this.swSV.confirm('ต้องการเปลี่ยนแปลงค่านี้ใช่หรือไม่').pipe(
      filter(x => x.isConfirmed),
      tap(x => {
        this.signatureList.data.map((data,index) => {
          if(index == i){
              data.is_default == true? data.is_default = false : data.is_default = true
              self.checked = data.is_default
            }else{
              data.is_default = false
            }
          })
      }),
      concatMap(x => {
        let updateData$ = this.signatureList.data.map(a => {
          return this.recipientSignatureSV.update2(a)
        })
        return forkJoin(updateData$)
      }),
    ).subscribe()
  }
}
