@attribute [Route(RouteConstance.InventoryWarehouses)]
@attribute [Authorize]

@using CMSMicroservice.Protobuf.Protos.Inventory
@using BackOffice.Pages.Inventory.Components

<div>
    <MudStack Row="true" Justify="Justify.SpaceBetween" AlignItems="AlignItems.Center" Class="mb-4">
        <MudStack Spacing="0">
            <MudText Typo="Typo.h4">
                <MudIcon Icon="@Icons.Material.Filled.Warehouse" Color="Color.Primary" Class="mr-2" />
                مدیریت انبارها
            </MudText>
            <MudText Typo="Typo.subtitle1" Color="Color.Secondary">
                تعریف و مدیریت انبارهای سیستم
            </MudText>
        </MudStack>
        <MudStack Row="true" Spacing="2">
            <MudButton Variant="Variant.Filled"
                       Color="Color.Primary"
                       StartIcon="@Icons.Material.Filled.Add"
                       OnClick="OpenCreateWarehouseDialog">
                انبار جدید
            </MudButton>
            <MudButton Variant="Variant.Outlined"
                       Color="Color.Secondary"
                       StartIcon="@Icons.Material.Filled.ArrowBack"
                       OnClick="GoBack">
                بازگشت به انبار
            </MudButton>
        </MudStack>
    </MudStack>

    @* Filter Toggle *@
    <MudPaper Class="pa-4 mb-4" Elevation="2">
        <MudSwitch T="bool" @bind-Value="_showActiveOnly"
                   Color="Color.Primary"
                   Label="فقط انبارهای فعال"
                   LabelPosition="LabelPosition.Start" />
        <MudButton Variant="Variant.Text"
                   Color="Color.Primary"
                   OnClick="LoadWarehouses"
                   StartIcon="@Icons.Material.Filled.Refresh"
                   Class="ml-4">
            بارگذاری مجدد
        </MudButton>
    </MudPaper>

    @if (_isLoading)
    {
        <MudProgressLinear Indeterminate="true" Color="Color.Primary" />
    }
    else
    {
        <MudGrid>
            @foreach (var warehouse in _warehouses)
            {
                <MudItem xs="12" sm="6" md="4">
                    <MudCard Elevation="3" Class="@(warehouse.IsDefault ? "border-2 border-solid mud-border-primary" : "")">
                        <MudCardHeader>
                            <CardHeaderAvatar>
                                <MudAvatar Color="@(warehouse.IsActive ? Color.Success : Color.Default)" Size="Size.Large">
                                    <MudIcon Icon="@Icons.Material.Filled.Warehouse" />
                                </MudAvatar>
                            </CardHeaderAvatar>
                            <CardHeaderContent>
                                <MudText Typo="Typo.h6">
                                    @warehouse.Name
                                    @if (warehouse.IsDefault)
                                    {
                                        <MudChip T="string" Color="Color.Primary" Size="Size.Small" Class="ml-2">پیش‌فرض</MudChip>
                                    }
                                </MudText>
                                <MudText Typo="Typo.caption" Color="Color.Secondary">
                                    کد: @warehouse.Code
                                </MudText>
                            </CardHeaderContent>
                            <CardHeaderActions>
                                <MudChip T="string" Color="@(warehouse.IsActive ? Color.Success : Color.Error)" Size="Size.Small">
                                    @(warehouse.IsActive ? "فعال" : "غیرفعال")
                                </MudChip>
                            </CardHeaderActions>
                        </MudCardHeader>
                        <MudCardContent>
                            <MudStack Spacing="2">
                                <MudStack Row="true" Spacing="1" AlignItems="AlignItems.Center">
                                    <MudIcon Icon="@Icons.Material.Filled.LocationOn" Size="Size.Small" Color="Color.Secondary" />
                                    <MudText Typo="Typo.body2">
                                        @(string.IsNullOrEmpty(warehouse.Address) ? "آدرس ثبت نشده" : warehouse.Address)
                                    </MudText>
                                </MudStack>
                                <MudStack Row="true" Spacing="1" AlignItems="AlignItems.Center">
                                    <MudIcon Icon="@Icons.Material.Filled.CalendarToday" Size="Size.Small" Color="Color.Secondary" />
                                    <MudText Typo="Typo.caption" Color="Color.Secondary">
                                        ایجاد: @(warehouse.Created?.ToDateTime().ToLocalTime().MiladiToJalali() ?? "-")
                                    </MudText>
                                </MudStack>
                            </MudStack>
                        </MudCardContent>
                        <MudCardActions>
                            <MudButton Variant="Variant.Text"
                                       Color="Color.Primary"
                                       Size="Size.Small"
                                       StartIcon="@Icons.Material.Filled.Visibility"
                                       OnClick="@(() => ViewWarehouseInventory(warehouse))">
                                مشاهده موجودی
                            </MudButton>
                        </MudCardActions>
                    </MudCard>
                </MudItem>
            }

            @if (_warehouses.Count == 0)
            {
                <MudItem xs="12">
                    <MudAlert Severity="Severity.Info">
                        هیچ انباری یافت نشد. برای شروع، یک انبار جدید ایجاد کنید.
                    </MudAlert>
                </MudItem>
            }
        </MudGrid>
    }
</div>
