@attribute [Route(RouteConstants.Profile.Addresses)]

<PageTitle>آدرس‌ها</PageTitle>

<MudContainer MaxWidth="MaxWidth.Large" Class="py-6">
    <MudStack Spacing="3">
        <PageHeader Title="مدیریت آدرس‌ها" BackHref="@RouteConstants.Profile.Index" />

        <MudPaper Elevation="2" Class="pa-4 rounded-lg">
            <MudStack Spacing="4">
                <MudStack Row="true" Justify="Justify.SpaceBetween" AlignItems="AlignItems.Center">
                    <MudText Typo="Typo.subtitle1">آدرس‌های شما</MudText>
                    <MudButton Variant="Variant.Filled"
                               Color="Color.Primary"
                               StartIcon="@Icons.Material.Filled.Add"
                               OnClick="OpenAddAddressDialog">افزودن آدرس جدید</MudButton>
                </MudStack>

                @if (_isLoadingAddresses)
                {
                    <MudStack AlignItems="AlignItems.Center" Class="py-8">
                        <MudProgressCircular Color="Color.Primary" Indeterminate="true" Size="Size.Large" />
                        <MudText Typo="Typo.body1" Class="mud-text-secondary mt-2">در حال بارگذاری آدرس‌ها...</MudText>
                    </MudStack>
                }
                else if (_addresses.Any())
                {
                    <MudStack Spacing="3">
                        @foreach (var address in _addresses)
                        {
                            <MudPaper Elevation="1" Class="pa-4 rounded-xl" Outlined="true">
                                <MudStack Row="true" Justify="Justify.SpaceBetween" AlignItems="AlignItems.Start">
                                    <MudStack Spacing="1">
                                        <MudStack Row="true" Spacing="2" AlignItems="AlignItems.Center">
                                            <MudText Typo="Typo.h6">@address.Title</MudText>
                                            @if (address.IsDefault)
                                            {
                                                <MudChip T="string" Color="Color.Success" Variant="Variant.Filled" Size="Size.Small">پیش‌فرض</MudChip>
                                            }
                                        </MudStack>
                                        <MudText Typo="Typo.body2" Class="mud-text-secondary">@address.Address</MudText>
                                        <MudText Typo="Typo.caption" Class="mud-text-secondary">کد پستی: @address.PostalCode</MudText>
                                    </MudStack>

                                    <MudMenu Icon="@Icons.Material.Filled.MoreVert" Size="Size.Small" AnchorOrigin="Origin.TopRight">
                                        <MudMenuItem OnClick="@(() => OpenEditAddressDialog(address))">ویرایش</MudMenuItem>
                                        @if (!address.IsDefault)
                                        {
                                            <MudMenuItem OnClick="@(() => SetAsDefaultAddress(address.Id))">تنظیم به عنوان پیش‌فرض</MudMenuItem>
                                        }
                                        <MudDivider />
                                        <MudMenuItem OnClick="@(() => DeleteAddress(address.Id))" Class="mud-text-error">حذف</MudMenuItem>
                                    </MudMenu>
                                </MudStack>
                            </MudPaper>
                        }
                    </MudStack>
                }
                else
                {
                    <MudStack AlignItems="AlignItems.Center" Class="py-8">
                        <MudIcon Icon="@Icons.Material.Filled.LocationOff" Size="Size.Large" Color="Color.Default" />
                        <MudText Typo="Typo.body1" Class="mud-text-secondary mt-2">هنوز آدرسی ثبت نکرده‌اید.</MudText>
                        <MudButton Variant="Variant.Outlined" Color="Color.Primary" StartIcon="@Icons.Material.Filled.Add" OnClick="OpenAddAddressDialog" Class="mt-2">افزودن اولین آدرس</MudButton>
                    </MudStack>
                }
            </MudStack>
        </MudPaper>
    </MudStack>
</MudContainer>
