import {Component, OnInit, ChangeDetectionStrategy, ChangeDetectorRef} from '@angular/core';
import {SharedUsercouterService} from '../../../../../core/service/testing-register/shared-usercouter.service';
import {Observable} from 'rxjs';
import {UserCounterLoginComponent} from '../../../register-summary/presenter/user-counter-login/user-counter-login.component';
import {concatMap, filter, tap} from 'rxjs/operators';
import {TestingRegisterService} from '../../../../../core/service/testing-register/testing-register.service';
import {SweetalertService} from '../../../../../core/service/sweetalert/sweetalert';
import {MatDialog} from '@angular/material/dialog';
import {TestingRecieptService} from '../../../../../core/service/testing-register/testing-reciept.service';
import {Router} from '@angular/router';

@Component({
  selector: 'app-receive-payment',
  templateUrl: './receive-payment.container.html',
  styleUrls: ['./receive-payment.container.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ReceivePaymentContainer implements OnInit {
  userPaymentdata$ = new Observable();
  userData$ = null;
  constructor(
    private router: Router,
    public sharedUsercouterSV: SharedUsercouterService,
    private registerSV: TestingRegisterService,
    private recieptSV: TestingRecieptService,
    private swSV: SweetalertService,
    public sharedCouterSV: SharedUsercouterService,
    private dialog: MatDialog,
    private cdr: ChangeDetectorRef,
  ) { }

  ngOnInit(): void {
    console.log(this.sharedUsercouterSV.data);
    if (this.sharedUsercouterSV.data == null){
      this.dialoglogin();
    }else {
      this.userData$ = this.sharedUsercouterSV.data;
      this.userPaymentdata$ = this.registerSV.getAllbyUsercouter(this.userData$.user_name);
      this.cdr.detectChanges();
    }
  }

  dialoglogin(){
    const dialogRef = this.dialog.open(UserCounterLoginComponent, {
      disableClose: true,
      width: '500px',
      height: '320px',
    });
    dialogRef.afterClosed().pipe(
      tap(x => {
        if (x != null){
          console.log(x.user_name);
          this.userData$ = x;
          this.sharedCouterSV.data = x;
          this.userPaymentdata$ = this.registerSV.getAllbyUsercouter(this.userData$.user_name);
          this.cdr.detectChanges();
          this.swSV.success(`ยินดีต้อนรับ ${x.user_name}`);
        }else {
          return this.swSV.error('ข้อมูลไม่ถูกต้อง โปรดตรวจสอบข้อมูล');
        }
      }),
    ).subscribe(
      result => {
        this.userData$ = result;
        this.cdr.detectChanges();
      }
    );
  }

  search(query){
    console.log(query);
    if(query) return this.userPaymentdata$ = this.registerSV.search(`?${query}`)
  }

  view(id){
    this.router.navigate(['receive-payment/view', id]);
  }

  recieve(id){
    console.log(id);
    this.swSV.recieve().pipe(
      filter(x => x.isConfirmed),
      concatMap(x => {
        return this.recieptSV.Paid(id).pipe(
          tap(x => this.swSV.removeSuccess()),
          // tap(x => this.subjectList$ = this.TestinforSV.getAll()),
          tap(x => this.cdr.detectChanges())
        );
      })
    ).subscribe();
  }

}
