@* Merged into /club *@
@attribute [Authorize]

@using Google.Protobuf.WellKnownTypes
@using BackOffice.Pages.Club.Components
@using BackOffice.Pages.AutoComplete
@using CMSMicroservice.Protobuf.Protos.ClubMembership

<div>
    <MudText Typo="Typo.h4" Class="mb-4">مدیریت اعضای باشگاه</MudText>

    <MudDataGrid T="ClubMembershipModel" 
                 ServerData="@(new Func<GridState<ClubMembershipModel>, Task<GridData<ClubMembershipModel>>>(ServerReload))"
                 Hover="true" 
                 @ref="_gridData" 
                 Height="calc(100vh - 240px)">
        <ToolBarContent>
            <MudText Typo="Typo.h6">لیست اعضا</MudText>
            <MudSpacer />
            <MudStack Row="true" Spacing="2" AlignItems="AlignItems.Center">
                <UserAutoComplete Label="جستجوی کاربر" @bind-SelectedUserId="_filterUserId" />
                <MudSwitch T="bool" @bind-Value="_filterIsActive" 
                          Color="Color.Success" 
                          Label="فقط فعال‌ها" />
                <MudButton Variant="Variant.Filled" 
                          Color="Color.Primary" 
                          OnClick="ApplyFilter"
                          StartIcon="@Icons.Material.Filled.Refresh">
                    بارگذاری
                </MudButton>
                <MudButton Variant="Variant.Filled" 
                          Color="Color.Success" 
                          OnClick="OpenActivateDialog"
                          StartIcon="@Icons.Material.Filled.Add">
                    فعال‌سازی عضو جدید
                </MudButton>
                <MudButton Variant="Variant.Outlined" 
                          Color="Color.Info" 
                          OnClick="ExportToExcel"
                          StartIcon="@Icons.Material.Filled.FileDownload">
                    خروجی Excel
                </MudButton>
            </MudStack>
        </ToolBarContent>
        <Columns>
            <PropertyColumn Property="x => x.Id" Title="شناسه" />
            
            <PropertyColumn Property="x => x.UserId" Title="کاربر">
                <CellTemplate>
                    <MudStack Spacing="0">
                        <MudText Typo="Typo.body2"><strong>@context.Item.UserName</strong></MudText>
                        <MudText Typo="Typo.caption" Color="Color.Secondary">ID: @context.Item.UserId</MudText>
                    </MudStack>
                </CellTemplate>
            </PropertyColumn>

            <PropertyColumn Property="x => x.PackageName" Title="پکیج">
                <CellTemplate>
                    <MudChip T="string" Color="Color.Info" Size="Size.Small">
                        @context.Item.PackageName
                    </MudChip>
                </CellTemplate>
            </PropertyColumn>

            <PropertyColumn Property="x => x.ActivationCode" Title="کد فعال‌سازی" />

            <PropertyColumn Property="x => x.ActivatedAt" Title="تاریخ فعال‌سازی">
                <CellTemplate>
                    @(context.Item.ActivatedAt != null ? context.Item.ActivatedAt.ToDateTime().ToLocalTime().MiladiToJalali() : "—")
                </CellTemplate>
            </PropertyColumn>

            <PropertyColumn Property="x => x.ExpiresAt" Title="تاریخ انقضا">
                <CellTemplate>
                    <MudText Color="@(context.Item.IsExpired ? Color.Error : Color.Success)">
                        @(context.Item.ExpiresAt != null ? context.Item.ExpiresAt.ToDateTime().ToLocalTime().MiladiToJalali() : "—")
                    </MudText>
                </CellTemplate>
            </PropertyColumn>

            <PropertyColumn Property="x => x.IsActive" Title="وضعیت">
                <CellTemplate>
                    <MudChip T="string" 
                            Color="@(context.Item.IsActive ? Color.Success : Color.Error)" 
                            Size="Size.Small">
                        @(context.Item.IsActive ? "فعال" : "غیرفعال")
                    </MudChip>
                </CellTemplate>
            </PropertyColumn>

            <TemplateColumn Title="عملیات" Sortable="false">
                <CellTemplate>
                    <MudStack Row="true" Spacing="1">
                        <MudTooltip Text="مشاهده جزئیات">
                            <MudIconButton Icon="@Icons.Material.Filled.Visibility" 
                                          Size="Size.Small" 
                                          Color="Color.Info"
                                          OnClick="@(() => ViewDetails(context.Item))" />
                        </MudTooltip>
                        @if (context.Item.IsActive)
                        {
                            <MudTooltip Text="غیرفعال کردن">
                                <MudIconButton Icon="@Icons.Material.Filled.Block" 
                                              Size="Size.Small" 
                                              Color="Color.Error"
                                              OnClick="@(() => DeactivateMembership(context.Item))" />
                            </MudTooltip>
                        }
                    </MudStack>
                </CellTemplate>
            </TemplateColumn>
        </Columns>
            <NoRecordsContent>
                <MudAlert Severity="Severity.Info" Dense="true" Variant="Variant.Text" Class="my-4">موردی یافت نشد.</MudAlert>
            </NoRecordsContent>
        <PagerContent>
            <MudDataGridPager T="ClubMembershipModel" 
                             PageSizeOptions="@(new int[] { 20, 50, 100 })" 
                             InfoFormat="سطر {first_item} تا {last_item} از {all_items}" 
                             RowsPerPageString="تعداد در صفحه" />
        </PagerContent>
    </MudDataGrid>
</div>
