|
|
@@ -1,5 +1,6 @@
|
|
|
import { CommonModule } from '@angular/common';
|
|
|
-import { FormBuilder, FormGroup, Validators, ReactiveFormsModule } from '@angular/forms';
|
|
|
+import { Component, Inject, OnInit } from '@angular/core';
|
|
|
+import { FormBuilder, FormGroup, FormControl, Validators, ReactiveFormsModule } from '@angular/forms';
|
|
|
import { MatDialogRef, MAT_DIALOG_DATA, MatDialogModule } from '@angular/material/dialog';
|
|
|
import { MatFormFieldModule } from '@angular/material/form-field';
|
|
|
import { MatInputModule } from '@angular/material/input';
|
|
|
@@ -8,10 +9,10 @@ import { MatDatepickerModule } from '@angular/material/datepicker';
|
|
|
import { MatNativeDateModule } from '@angular/material/core';
|
|
|
import { MatIconModule } from '@angular/material/icon';
|
|
|
import { HttpClient, HttpClientModule } from '@angular/common/http';
|
|
|
-import { webConfig } from '../../config';
|
|
|
-import { Component, Inject } from '@angular/core';
|
|
|
import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar';
|
|
|
import { MatAutocompleteModule } from '@angular/material/autocomplete';
|
|
|
+import { Observable, startWith, map } from 'rxjs';
|
|
|
+import { webConfig } from '../../config';
|
|
|
|
|
|
@Component({
|
|
|
selector: 'app-create-ffb-harvest-dialog',
|
|
|
@@ -33,14 +34,25 @@ import { MatAutocompleteModule } from '@angular/material/autocomplete';
|
|
|
templateUrl: './create-ffb-harvest-dialog.component.html',
|
|
|
styleUrls: ['./create-ffb-harvest-dialog.component.css'],
|
|
|
})
|
|
|
-export class CreateFfbHarvestDialogComponent {
|
|
|
+export class CreateFfbHarvestDialogComponent implements OnInit {
|
|
|
form: FormGroup;
|
|
|
saving = false;
|
|
|
|
|
|
- // Options for UOMs
|
|
|
- weightUomOptions = ['Kg', 'Ton',];
|
|
|
+ weightUomOptions = ['Kg', 'Ton'];
|
|
|
quantityUomOptions = ['Bunch', 'Bundle', 'Bag'];
|
|
|
|
|
|
+ allSites: string[] = [];
|
|
|
+ allPhases: string[] = [];
|
|
|
+ allBlocks: string[] = [];
|
|
|
+ allHarvesters: string[] = [];
|
|
|
+
|
|
|
+ filteredSites!: Observable<string[]>;
|
|
|
+ filteredPhases!: Observable<string[]>;
|
|
|
+ filteredBlocks!: Observable<string[]>;
|
|
|
+ filteredHarvesters!: Observable<string[]>;
|
|
|
+
|
|
|
+ private conversionMap: Record<string, number> = { Bunch: 10, Bundle: 25, Bag: 100 };
|
|
|
+
|
|
|
constructor(
|
|
|
private fb: FormBuilder,
|
|
|
private http: HttpClient,
|
|
|
@@ -48,6 +60,13 @@ export class CreateFfbHarvestDialogComponent {
|
|
|
private snackBar: MatSnackBar,
|
|
|
@Inject(MAT_DIALOG_DATA) public data: any
|
|
|
) {
|
|
|
+ // Assign lists from parent
|
|
|
+ this.allSites = data?.allSites || [];
|
|
|
+ this.allPhases = data?.allPhases || [];
|
|
|
+ this.allBlocks = data?.allBlocks || [];
|
|
|
+ this.allHarvesters = data?.allHarvesters || [];
|
|
|
+ console.log(data)
|
|
|
+
|
|
|
this.form = this.fb.group({
|
|
|
harvestDate: [new Date(), Validators.required],
|
|
|
site: ['', Validators.required],
|
|
|
@@ -61,47 +80,49 @@ export class CreateFfbHarvestDialogComponent {
|
|
|
daysOfWork: [0, Validators.required],
|
|
|
});
|
|
|
|
|
|
- // Watch for changes on quantity or quantityUom to auto-calculate weight
|
|
|
+ // Auto-update weight
|
|
|
this.form.get('quantity')!.valueChanges.subscribe(() => this.updateWeight());
|
|
|
this.form.get('quantityUom')!.valueChanges.subscribe(() => this.updateWeight());
|
|
|
}
|
|
|
|
|
|
ngOnInit() {
|
|
|
- if (this.data) {
|
|
|
- this.form.patchValue({
|
|
|
- harvestDate: this.data.harvestDate,
|
|
|
- site: this.data.site,
|
|
|
- phase: this.data.phase,
|
|
|
- block: this.data.block,
|
|
|
- quantity: this.data.quantity,
|
|
|
- quantityUom: this.data.quantityUom || 'Bunch',
|
|
|
- weight: this.data.weight,
|
|
|
- weightUom: this.data.weightUom || 'Kg',
|
|
|
- harvester: this.data.harvester,
|
|
|
- daysOfWork: this.data.daysOfWork,
|
|
|
- });
|
|
|
+ // Patch data if editing
|
|
|
+ if (this.data?.harvest) {
|
|
|
+ this.form.patchValue(this.data.harvest);
|
|
|
}
|
|
|
+
|
|
|
+ // Autocomplete observables
|
|
|
+ this.filteredSites = this.siteControl.valueChanges.pipe(
|
|
|
+ startWith(this.siteControl.value || ''),
|
|
|
+ map(val => this.filterOptions(val, this.allSites))
|
|
|
+ );
|
|
|
+
|
|
|
+ this.filteredPhases = this.phaseControl.valueChanges.pipe(
|
|
|
+ startWith(this.phaseControl.value || ''),
|
|
|
+ map(val => this.filterOptions(val, this.allPhases))
|
|
|
+ );
|
|
|
+
|
|
|
+ this.filteredBlocks = this.blockControl.valueChanges.pipe(
|
|
|
+ startWith(this.blockControl.value || ''),
|
|
|
+ map(val => this.filterOptions(val, this.allBlocks))
|
|
|
+ );
|
|
|
+
|
|
|
+ this.filteredHarvesters = this.harvesterControl.valueChanges.pipe(
|
|
|
+ startWith(this.harvesterControl.value || ''),
|
|
|
+ map(val => this.filterOptions(val, this.allHarvesters))
|
|
|
+ );
|
|
|
}
|
|
|
|
|
|
- // Conversion map (quantityUom -> weight in kg)
|
|
|
- private conversionMap: Record<string, number> = {
|
|
|
- Bunch: 10,
|
|
|
- Bundle: 25,
|
|
|
- Bag: 100,
|
|
|
- };
|
|
|
+ private filterOptions(value: string, options: string[]): string[] {
|
|
|
+ const filterValue = (value || '').toLowerCase();
|
|
|
+ return options.filter(opt => opt.toLowerCase().includes(filterValue));
|
|
|
+ }
|
|
|
|
|
|
- /** Automatically update weight based on quantity and quantityUom */
|
|
|
private updateWeight() {
|
|
|
const quantity = this.form.get('quantity')!.value || 0;
|
|
|
const quantityUom = this.form.get('quantityUom')!.value;
|
|
|
const baseWeight = this.conversionMap[quantityUom] || 0;
|
|
|
-
|
|
|
- const totalWeightKg = quantity * baseWeight;
|
|
|
-
|
|
|
- this.form.patchValue(
|
|
|
- { weight: totalWeightKg },
|
|
|
- { emitEvent: false } // avoid infinite loop
|
|
|
- );
|
|
|
+ this.form.patchValue({ weight: quantity * baseWeight }, { emitEvent: false });
|
|
|
}
|
|
|
|
|
|
onSubmit() {
|
|
|
@@ -115,7 +136,7 @@ export class CreateFfbHarvestDialogComponent {
|
|
|
this.saving = true;
|
|
|
this.http.post(`${webConfig.exposedUrl}/api/ffb-harvest`, payload).subscribe({
|
|
|
next: () => {
|
|
|
- this.snackBar.open('FFB Harvest created!', 'Close', { duration: 3000 });
|
|
|
+ this.snackBar.open('FFB Harvest saved!', 'Close', { duration: 3000 });
|
|
|
this.dialogRef.close('refresh');
|
|
|
},
|
|
|
error: (err) => {
|
|
|
@@ -129,5 +150,10 @@ export class CreateFfbHarvestDialogComponent {
|
|
|
cancel() {
|
|
|
this.dialogRef.close();
|
|
|
}
|
|
|
-}
|
|
|
|
|
|
+ // FormControl getters
|
|
|
+ get siteControl(): FormControl { return this.form.get('site') as FormControl; }
|
|
|
+ get phaseControl(): FormControl { return this.form.get('phase') as FormControl; }
|
|
|
+ get blockControl(): FormControl { return this.form.get('block') as FormControl; }
|
|
|
+ get harvesterControl(): FormControl { return this.form.get('harvester') as FormControl; }
|
|
|
+}
|