Files
rota/src/routes/components/schedule/generate.svelte

134 lines
4.3 KiB
Svelte
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script lang="ts">
import { Button } from "$lib/components/ui/button/index.js";
import { invoke } from "@tauri-apps/api/core";
import { type MonthlyScheduleDTO, rota, steps, type ShiftPosition } from "../../state.svelte.js";
function getResidentName(day: number, pos: ShiftPosition) {
const residentId = rota.solution[`${day}-${pos}`];
const r = rota.findResident(residentId);
if (r) return r.name;
// check for manual
const assignedResidents = rota.residents.filter((resident) =>
resident.manualShifts.some(
(shift) =>
shift.day === day &&
shift.month === rota.selectedMonth &&
shift.year === rota.selectedYear
)
);
let slot;
if (pos == "First") {
slot = 1;
} else {
slot = 2;
}
const resident = assignedResidents[slot - 1];
return resident ? resident.name : "-";
}
// 2 slots in odd days, 1 slot in even days
function getRequiredSlots(day: number) {
return day % 2 === 0 ? 1 : 2;
}
async function generate() {
let config = rota.toDTO();
console.log(config);
try {
let schedule = await invoke<MonthlyScheduleDTO>("generate", { config });
console.log("replyFromGenerate:", schedule);
rota.solution = schedule;
} catch (error) {
console.error("Error:", error);
}
}
async function export_file() {
let schedule = rota.solution;
try {
await invoke("export", { schedule });
} catch (error) {
console.error("Error:", error);
}
}
</script>
<div class="mb-6 flex items-center justify-between">
<h2 class="text-2xl font-bold text-zinc-800">{steps[rota.currentStep - 1].title}</h2>
<div class="justify-end">
<Button
onclick={() => (rota.currentStep -= 1)}
variant="outline"
class="border-zinc-200 bg-white text-zinc-600 shadow-sm transition-all hover:bg-zinc-50 hover:text-zinc-900 active:scale-95"
>
Προηγούμενο
</Button>
</div>
</div>
<header class="mb-2">
<p class="text-sm text-zinc-500">{steps[rota.currentStep - 1].description}</p>
</header>
<div class="overflow-hidden rounded-xl border border-zinc-200 bg-white">
<div class="grid grid-cols-7 border-b border-zinc-200 bg-zinc-50/50">
{#each ["ΔΕΥΤΕΡΑ", "ΤΡΙΤΗ", "ΤΕΤΑΡΤΗ", "ΠΕΜΠΤΗ", "ΠΑΡΑΣΚΕΥΗ", "ΣΑΒΒΑΤΟ", "ΚΥΡΙΑΚΗ"] as dayName}
<div class="py-2 text-center text-[10px] font-bold tracking-widest text-zinc-500 uppercase">
{dayName}
</div>
{/each}
</div>
<div class="grid auto-rows-fr grid-cols-7 gap-px bg-zinc-200">
{#each rota.emptySlots as _}<div class="bg-zinc-50/30"></div>{/each}
{#each rota.daysArray as day (day)}
{@const slotCount = getRequiredSlots(day)}
<div class="group min-h-25 bg-white p-2 transition-all hover:bg-blue-50/30">
<div class="mb-2 flex items-center justify-between">
<span class="text-xs font-black text-zinc-500">{day}</span>
</div>
<div class="space-y-1">
{#if slotCount > 0}
<button
onclick={() => "handleCellClick(day, 1)"}
class="w-full overflow-hidden rounded border border-pink-200 bg-pink-50 px-1.5 py-1 text-left text-[10px] font-bold text-pink-600 transition-colors hover:bg-pink-100"
>
{getResidentName(day, "First")}
</button>
{/if}
{#if slotCount > 1}
<button
onclick={() => "handleCellClick(day, 2)"}
class="w-full overflow-hidden rounded border border-emerald-200 bg-emerald-50 px-1.5 py-1 text-left text-[10px] font-bold text-emerald-600 transition-colors hover:bg-emerald-100"
>
{getResidentName(day, "Second")}
</button>
{/if}
</div>
</div>
{/each}
</div>
</div>
<div class="mt-8 flex justify-center gap-4">
<Button
variant="outline"
class="border-zinc-200 bg-white text-zinc-600 shadow-sm transition-all hover:bg-zinc-50 hover:text-zinc-900 active:scale-95"
onclick={() => generate()}
>
Ανακατανομή
</Button>
<Button
variant="outline"
class="border-zinc-200 bg-white text-zinc-600 shadow-sm transition-all hover:bg-zinc-50 hover:text-zinc-900 active:scale-95"
onclick={() => export_file()}
>
Εξαγωγή</Button
>
</div>