import { Component, Inject, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
import {
  MatDialog,
  MatDialogRef,
  MAT_DIALOG_DATA,
} from '@angular/material/dialog';
import { DataService } from 'src/app/Services/data.service';

import { TransactionData } from '../dashboard.component';

@Component({
  selector: 'app-user-dialog',
  templateUrl: './user-dialog.component.html',
  styleUrls: ['./user-dialog.component.scss'],
})
export class UserDialogComponent {
  constructor(
    private dataService: DataService,
    public dialog: MatDialog,
    public dialogRef: MatDialogRef<UserDialogComponent>,
    @Inject(MAT_DIALOG_DATA) public data: TransactionData
  ) {}

  countValid = true;
  priceValid = true;

  onSubmit() {
    if (!isNaN(this.data.count) && !isNaN(this.data.price)) {
      this.priceValid = true;
      this.countValid = true;
      console.log(this.data.price);
      console.log(
        this.dataService
          .createTransaction(
            this.data.comment,
            this.data.isin,
            this.data.time,
            +this.data.count,
            +this.data.price.toFixed(2) * -1
          )
          .subscribe((data) => {
            console.log(data);
          })
      );
      this.dialog.closeAll();
    } else if (isNaN(this.data.count) && !isNaN(this.data.price)) {
      this.priceValid = true;
      this.countValid = false;
    } else if (!isNaN(this.data.count) && isNaN(this.data.price)) {
      this.priceValid = false;
      this.countValid = true;
    } else {
      this.countValid = false;
      this.priceValid = false;
    }
  }
}
