diff --git a/src/routes/dashboard/+page.svelte b/src/routes/dashboard/+page.svelte index 02bbfbe..d53a504 100644 --- a/src/routes/dashboard/+page.svelte +++ b/src/routes/dashboard/+page.svelte @@ -48,6 +48,7 @@ let personnel = []; let goodsManagers = []; let filteredGoodsManagers = []; + let filteredVehicles = []; let formError = ""; let formSuccess = ""; let isSubmitting = false; @@ -65,6 +66,13 @@ notes: "" }; + function setFormField(field, value) { + if (formData[field] === value) { + return; + } + formData = { ...formData, [field]: value }; + } + $: displayPendingSlips = pendingSlips; $: displayHistorySlips = approvedRejectedSlips; @@ -167,6 +175,7 @@ if (vehiclesRes.ok) { const vehiclesData = await vehiclesRes.json(); vehicles = vehiclesData.vehicles || []; + await syncVehicleSelection(formData.unit_id); } if (unitsRes.ok) { @@ -208,6 +217,7 @@ notes: "" }; syncGoodsManagerSelection(""); + syncVehicleSelection(""); } function getGoodsManagersByUnit(unitId) { @@ -221,7 +231,7 @@ filteredGoodsManagers = candidates; if (!unitId || candidates.length === 0) { - formData.goods_manager_id = ""; + setFormField("goods_manager_id", ""); return; } @@ -231,13 +241,56 @@ ); if (!hasCurrent) { - formData.goods_manager_id = candidates[0].id.toString(); + setFormField("goods_manager_id", candidates[0].id.toString()); } } - function handleUnitChange(event) { - formData.unit_id = event.target.value; - syncGoodsManagerSelection(formData.unit_id); + function getVehiclesByUnit(unitId) { + if (!unitId) { + return []; + } + + const normalized = parseInt(unitId); + return vehicles.filter((vehicle) => vehicle.unit_id === normalized); + } + + async function syncVehicleSelection(unitId = formData.unit_id) { + const candidates = getVehiclesByUnit(unitId); + filteredVehicles = candidates; + + await tick(); + + if (!unitId || candidates.length === 0) { + setFormField("vehicle_id", ""); + return; + } + + const hasVehicle = candidates.some( + (vehicle) => + vehicle.id.toString() === (formData.vehicle_id ?? "").toString() + ); + + if (!hasVehicle) { + setFormField("vehicle_id", candidates[0].id.toString()); + } + } + + $: if (formData.unit_id && filteredVehicles.length > 0) { + const hasVehicle = filteredVehicles.some( + (vehicle) => + vehicle.id.toString() === (formData.vehicle_id ?? "").toString() + ); + + if (!hasVehicle) { + setFormField("vehicle_id", filteredVehicles[0].id.toString()); + } + } + + async function handleUnitChange(event) { + const value = event.target.value; + setFormField("unit_id", value); + syncGoodsManagerSelection(value); + await syncVehicleSelection(value); } async function handleCreateSlip() { @@ -1252,14 +1305,19 @@ id="vehicle_id" class="form-select" bind:value={formData.vehicle_id} + disabled={!formData.unit_id || filteredVehicles.length === 0} required > - - {#each vehicles as vehicle} - + + {#if !formData.unit_id} + + {:else if filteredVehicles.length === 0} + + {/if} + {#each filteredVehicles as vehicle} + {/each} @@ -1310,9 +1368,9 @@ > {#each personnel as person} - + {/each} diff --git a/src/routes/fuel-slips/+page.svelte b/src/routes/fuel-slips/+page.svelte index 739ae23..61df4e4 100644 --- a/src/routes/fuel-slips/+page.svelte +++ b/src/routes/fuel-slips/+page.svelte @@ -7,18 +7,19 @@