@using CMSMicroservice.Protobuf.Protos.ClubMembership

<MudDialog>
    <DialogContent>
        @if (Member != null)
        {
            <MudStack Spacing="3">
                <MudPaper Class="pa-3" Elevation="0">
                    <MudText Typo="Typo.h6" Color="Color.Primary">اطلاعات کاربر</MudText>
                    <MudDivider Class="my-2" />
                    <MudSimpleTable Dense="true">
                        <tbody>
                            <tr>
                                <td><strong>شناسه:</strong></td>
                                <td>@Member.UserId</td>
                            </tr>
                            <tr>
                                <td><strong>نام کاربر:</strong></td>
                                <td>@Member.UserName</td>
                            </tr>
                        </tbody>
                    </MudSimpleTable>
                </MudPaper>

                <MudPaper Class="pa-3" Elevation="0">
                    <MudText Typo="Typo.h6" Color="Color.Primary">اطلاعات عضویت</MudText>
                    <MudDivider Class="my-2" />
                    <MudSimpleTable Dense="true">
                        <tbody>
                            <tr>
                                <td><strong>شناسه عضویت:</strong></td>
                                <td>@Member.Id</td>
                            </tr>
                            <tr>
                                <td><strong>پکیج:</strong></td>
                                <td>@Member.PackageName</td>
                            </tr>
                            <tr>
                                <td><strong>کد فعال‌سازی:</strong></td>
                                <td dir="ltr">@Member.ActivationCode</td>
                            </tr>
                            <tr>
                                <td><strong>تاریخ فعال‌سازی:</strong></td>
                                <td>@Member.ActivatedAt.ToDateTime().ToLocalTime().MiladiToJalaliWithTime()</td>
                            </tr>
                            <tr>
                                <td><strong>تاریخ انقضا:</strong></td>
                                <td>
                                    <MudText Color="@(Member.IsExpired ? Color.Error : Color.Success)">
                                        @Member.ExpiresAt.ToDateTime().ToLocalTime().MiladiToJalaliWithTime()
                                    </MudText>
                                </td>
                            </tr>
                            <tr>
                                <td><strong>وضعیت:</strong></td>
                                <td>
                                    <MudChip T="string" 
                                            Color="@(Member.IsActive ? Color.Success : Color.Error)" 
                                            Size="Size.Small">
                                        @(Member.IsActive ? "فعال" : "غیرفعال")
                                    </MudChip>
                                </td>
                            </tr>
                            <tr>
                                <td><strong>منقضی شده:</strong></td>
                                <td>
                                    <MudChip T="string" 
                                            Color="@(Member.IsExpired ? Color.Warning : Color.Default)" 
                                            Size="Size.Small">
                                        @(Member.IsExpired ? "بله" : "خیر")
                                    </MudChip>
                                </td>
                            </tr>
                        </tbody>
                    </MudSimpleTable>
                </MudPaper>

                <MudPaper Class="pa-3" Elevation="0">
                    <MudText Typo="Typo.h6" Color="Color.Primary">تاریخچه</MudText>
                    <MudDivider Class="my-2" />
                    <MudSimpleTable Dense="true">
                        <tbody>
                            <tr>
                                <td><strong>تاریخ ایجاد:</strong></td>
                                <td>@Member.Created.ToDateTime().ToLocalTime().MiladiToJalaliWithTime()</td>
                            </tr>
                        </tbody>
                    </MudSimpleTable>

                    @if (_historyLoading)
                    {
                        <MudProgressLinear Indeterminate="true" Color="Color.Primary" Class="my-2" />
                    }
                    else if (_history.Any())
                    {
                        <MudTable T="ClubMembershipHistoryModel" Items="_history" Dense="true" Hover="true" Class="mt-2">
                            <HeaderContent>
                                <MudTh>عملیات</MudTh>
                                <MudTh>تاریخ</MudTh>
                                <MudTh>دلیل</MudTh>
                                <MudTh>توسط</MudTh>
                            </HeaderContent>
                            <RowTemplate>
                                <MudTd DataLabel="عملیات">
                                    <MudChip T="string" Size="Size.Small" Color="Color.Info">@context.Action</MudChip>
                                </MudTd>
                                <MudTd DataLabel="تاریخ">@context.Created.ToDateTime().ToLocalTime().MiladiToJalaliWithTime()</MudTd>
                                <MudTd DataLabel="دلیل">@(string.IsNullOrEmpty(context.Reason) ? "-" : context.Reason)</MudTd>
                                <MudTd DataLabel="توسط">@(string.IsNullOrEmpty(context.PerformedBy) ? "-" : context.PerformedBy)</MudTd>
                            </RowTemplate>
                        </MudTable>
                    }
                    else
                    {
                        <MudText Typo="Typo.caption" Color="Color.Secondary" Class="mt-2">تاریخچه‌ای یافت نشد.</MudText>
                    }
                </MudPaper>
            </MudStack>
        }
    </DialogContent>
    <DialogActions>
        <MudButton Color="Color.Primary" OnClick="Close">بستن</MudButton>
    </DialogActions>
</MudDialog>

@code {
    [CascadingParameter] IMudDialogInstance MudDialog { get; set; }
    [Parameter] public ClubMembershipModel Member { get; set; }
    [Inject] public ClubMembershipContract.ClubMembershipContractClient ClubClient { get; set; }

    private List<ClubMembershipHistoryModel> _history = new();
    private bool _historyLoading;

    protected override async Task OnInitializedAsync()
    {
        await LoadHistory();
    }

    private async Task LoadHistory()
    {
        _historyLoading = true;
        try
        {
            var request = new GetClubMembershipHistoryRequest
            {
                UserId = Member.UserId,
                PageIndex = 0,
                PageSize = 50
            };
            var response = await ClubClient.GetClubMembershipHistoryAsync(request);
            _history = response.Models?.ToList() ?? new();
        }
        catch
        {
            _history = new();
        }
        finally
        {
            _historyLoading = false;
        }
    }

    private void Close() => MudDialog.Close();
}
