import { IDropBdg, ITrnmst } from './../../interfaces/main.interface';
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { GeneralService } from '../../services/generalservice';
import { TransactionStateService } from '../../services/state/transaction-state.service';

@Component({
  selector: 'app-budget-aproval-content',
  standalone: false,
  templateUrl: './budget-aproval-content.html',
  styleUrl: './budget-aproval-content.css',
})
export class BudgetAprovalContent implements OnInit {

  myTrnmst: ITrnmst[]=[];
  myDropBdg: IDropBdg[]=[];
  prjSeq:string = '';
  prjNam:string = '';
;  constructor(
    private router: Router,
    private route: ActivatedRoute,
    private generalService: GeneralService,
    private transactionStateService: TransactionStateService
  ){
    const nav = this.router.getCurrentNavigation();
    if (nav?.extras.state) {
      this.prjSeq = nav.extras.state['prjseq'];
      this.prjNam = nav.extras.state['prjnam'];
    }
  }

  ngOnInit(): void {
    // this.prjSeq = this.route.snapshot.param.get('seq');
   this.OnSearchTrn(this.prjSeq);
   this.OnBudgetSearch();
  }

  OnSearchTrn(value: any): void {
    const uri = '/api/ttc/transactionsearch';
    let request = {
      trnprjseq: value
    }
    this.generalService.postRequest(uri, request).subscribe({
      next: (result: any) => {
        if (result.code === '200') {
          this.generalService.trowApi(result);
          this.myTrnmst = result.data;
          this.transactionStateService.setTransactionState(this.myTrnmst);
        }else{
          this.generalService.trowApi(result);
        }
      },
      error: (error: any) => {
        this.generalService.trowApi(error);
      },
      complete: () => {}
    });
  }

  OnBudgetExp(value: any): void {
    const uri = '/api/ttc/budgetexpense';
    let request = value;
    this.generalService.postRequest(uri, request).subscribe({
      next: (result: any) => {
        if (result.code === '200') {
          this.generalService.trowApi(result);
        }else{
          this.generalService.trowApi(result);
        }
      },
      error: (error: any) => {
        this.generalService.trowApi(error);
      },
      complete: () => {
        this.router.navigate(['/main/manager'])
      }
    });
  }


  OnBudgetSearch(): void {
    const uri = '/api/ttc/budgetsearch';
    let request = {}
    this.generalService.postRequest(uri, request).subscribe({
      next: (result: any) => {
        if (result.code === '200') {
          // this.generalService.trowApi(result);
          this.myDropBdg = result.data;
          this.transactionStateService.setBudgetDrop(this.myDropBdg);
        }else{
          // this.generalService.trowApi(result);
        }
      },
      error: (error: any) => {
        // this.generalService.trowApi(error);
      },
      complete: () => {}
    });
  }
}
