import { Location } from '@angular/common';
import { Component, OnInit, ChangeDetectionStrategy, OnChanges, ChangeDetectorRef, ViewChild, AfterViewInit } from '@angular/core';
import { FormBuilder } from '@angular/forms';
import { MatDialog } from '@angular/material/dialog';
import { MatPaginator } from '@angular/material/paginator';
import { MatTableDataSource } from '@angular/material/table';
import { ActivatedRoute, Router } from '@angular/router';
import { concatMap, tap } from 'rxjs/operators';
import { BaseForm } from 'src/app/core/base/base-form';
import { BaseList } from 'src/app/core/base/base-list';
import { SweetalertService } from 'src/app/core/service/sweetalert/sweetalert';
import { TestingFeeService } from 'src/app/core/service/testing-information/testing-fee.service';
import { TestingInformationService } from 'src/app/core/service/testing-information/testing-information.service';
import { AcademicYearsDialogComponent } from 'src/app/feature/settings/academic-years/presenter/dialog/academic-years-dialog/academic-years-dialog.component';
import { ExamFinanceDialogComponent } from '../../dialog/exam-finance-dialog/exam-finance-dialog.component';

@Component({
    selector: 'app-exam-finance-info',
    templateUrl: './exam-finance-info.component.html',
    styleUrls: ['./exam-finance-info.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class ExamFinanceInfoComponent extends BaseForm implements OnInit {
    @ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
    subjectList;
    info: any;
    id;
    amount;
    column=['1','2','3','4']
    constructor(
        private dialog: MatDialog,
        private cdr: ChangeDetectorRef,
        private testingFeeSV: TestingFeeService,
        private testingInformationSV: TestingInformationService,
        public route: ActivatedRoute,
        public router: Router,
        public fb: FormBuilder,
        public location: Location,
        public swSV: SweetalertService
    ) {
        super(router,route,fb,location)
     }

    ngOnInit(): void {
        this.testingFeeSV.get(this.id).subscribe((x:any) => {
            this.subjectList = new MatTableDataSource(x)
            this.subjectList.paginator = this.paginator
        })
        this.testingInformationSV.search('?testing_information_uid='+this.id).subscribe(x => {
            this.info = x[0]
            this.cdr.detectChanges()
        })
    }

    edit(x){
        const dialogRef = this.dialog.open(ExamFinanceDialogComponent, {
            width: '500px',
            height: '460px',
            data: {
                info : x,
                type : this.info.type_of_examinee
            },
        });
        dialogRef.afterClosed().subscribe(
            x => {
                this.testingFeeSV.get(this.id).subscribe((x:any) => {
                    this.subjectList = new MatTableDataSource(x)
                    this.subjectList.paginator = this.paginator
                })
            }
        )
    }

    onAdd(){
        this.subjectList.data.map(x => {
            x.amount = +this.amount
            this.testingFeeSV.update2(x).subscribe(x => {
                this.swSV.updateSuccess()
            })
        })
    }

}
