@page
@model JobsMedical.Web.Pages.Calendar.IndexModel
@{
    ViewData["Title"] = "تقویم هفتگی شیفت‌ها";
    var weekEnd = Model.WeekStart.AddDays(6);
}

<div class="page-head">
    <div class="container">
        <h1>تقویم هفتگی شیفت‌ها</h1>
        <form method="get" style="margin-top:12px; max-width:360px;">
            <input type="hidden" name="WeekOffset" value="@Model.WeekOffset" />
            <select name="FacilityId" onchange="this.form.submit()">
                <option value="">همه مراکز درمانی</option>
                @foreach (var f in Model.Facilities)
                {
                    <option value="@f.Id" selected="@(Model.FacilityId == f.Id)">@f.Name</option>
                }
            </select>
        </form>
    </div>
</div>

<div class="container section">
    <div class="cal-nav">
        <a class="btn btn-outline" asp-page="/Calendar/Index"
           asp-route-FacilityId="@Model.FacilityId" asp-route-WeekOffset="@(Model.WeekOffset - 1)">→ هفته قبل</a>
        <strong>
            @JalaliDate.DayOfMonth(Model.WeekStart) @JalaliDate.MonthName(Model.WeekStart)
            تا
            @JalaliDate.DayOfMonth(weekEnd) @JalaliDate.MonthName(weekEnd)
        </strong>
        <a class="btn btn-outline" asp-page="/Calendar/Index"
           asp-route-FacilityId="@Model.FacilityId" asp-route-WeekOffset="@(Model.WeekOffset + 1)">هفته بعد ←</a>
    </div>

    <table class="cal">
        <thead>
            <tr>
                @foreach (var (date, _) in Model.Days)
                {
                    <th>@JalaliDate.WeekDayName(date)</th>
                }
            </tr>
        </thead>
        <tbody>
            <tr>
                @foreach (var (date, dayShifts) in Model.Days)
                {
                    var isToday = date == Model.Today;
                    <td class="@(isToday ? "today" : "") @(dayShifts.Count == 0 ? "empty" : "")">
                        <div class="day-num">@JalaliDate.DayOfMonth(date)</div>
                        @foreach (var s in dayShifts)
                        {
                            var cls = s.ShiftType switch
                            {
                                ShiftType.Day => "day",
                                ShiftType.Evening => "evening",
                                ShiftType.Night => "night",
                                _ => "oncall",
                            };
                            <a class="cal-chip @cls" asp-page="/Shifts/Details" asp-route-id="@s.Id"
                               title="@s.Facility?.Name">
                                @JalaliDate.Time(s.StartTime) @s.Facility?.Name
                            </a>
                        }
                    </td>
                }
            </tr>
        </tbody>
    </table>
</div>
