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

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

<div>
    <MudText Typo="Typo.h4" Class="mb-4">مدیریت انبار</MudText>

    @* Filters Section *@
    <MudPaper Class="pa-4 mb-4" Elevation="2">
        <MudGrid>
            <MudItem xs="12" sm="6" md="3">
                <MudSelect T="long?" @bind-Value="_filterWarehouseId"
                           Label="انبار"
                           Variant="Variant.Outlined"
                           Margin="Margin.Dense"
                           Clearable="true">
                    @foreach (var wh in _warehouses)
                    {
                        <MudSelectItem T="long?" Value="@((long?)wh.Id)">@wh.Name</MudSelectItem>
                    }
                </MudSelect>
            </MudItem>
            <MudItem xs="12" sm="6" md="3">
                <MudSelect T="ProductType" @bind-Value="_filterProductType"
                           Label="نوع محصول"
                           Variant="Variant.Outlined"
                           Margin="Margin.Dense">
                    <MudSelectItem T="ProductType" Value="ProductType.Unspecified">همه</MudSelectItem>
                    <MudSelectItem T="ProductType" Value="ProductType.RegularProduct">محصول عادی</MudSelectItem>
                    <MudSelectItem T="ProductType" Value="ProductType.DiscountProduct">محصول تخفیفی</MudSelectItem>
                </MudSelect>
            </MudItem>
            <MudItem xs="12" sm="6" md="3">
                <MudTextField T="string" @bind-Value="_searchTerm"
                              Label="جستجو"
                              Variant="Variant.Outlined"
                              Margin="Margin.Dense"
                              Adornment="Adornment.End"
                              AdornmentIcon="@Icons.Material.Filled.Search"
                              Clearable="true" />
            </MudItem>
            <MudItem xs="12" sm="6" md="3" Class="d-flex align-end">
                <MudButton Variant="Variant.Filled"
                           Color="Color.Primary"
                           OnClick="ApplyFilter"
                           StartIcon="@Icons.Material.Filled.FilterAlt"
                           Class="mr-2">
                    اعمال فیلتر
                </MudButton>
                <MudButton Variant="Variant.Outlined"
                           Color="Color.Secondary"
                           OnClick="ClearFilter"
                           StartIcon="@Icons.Material.Filled.Clear">
                    پاک کردن
                </MudButton>
            </MudItem>
        </MudGrid>
    </MudPaper>

    @* Stats Cards *@
    <MudGrid Class="mb-4">
        <MudItem xs="12" sm="6" md="3">
            <MudCard>
                <MudCardContent>
                    <MudStack Row="true" Justify="Justify.SpaceBetween" AlignItems="AlignItems.Center">
                        <MudStack Spacing="0">
                            <MudText Typo="Typo.caption">کل اقلام انبار</MudText>
                            <MudText Typo="Typo.h5">@_totalCount</MudText>
                        </MudStack>
                        <MudIcon Icon="@Icons.Material.Filled.Inventory" Color="Color.Primary" Size="Size.Large" />
                    </MudStack>
                </MudCardContent>
            </MudCard>
        </MudItem>
        <MudItem xs="12" sm="6" md="3">
            <MudCard>
                <MudCardContent>
                    <MudStack Row="true" Justify="Justify.SpaceBetween" AlignItems="AlignItems.Center">
                        <MudStack Spacing="0">
                            <MudText Typo="Typo.caption">محصولات کم‌موجود</MudText>
                            <MudText Typo="Typo.h5" Color="Color.Warning">@_lowStockCount</MudText>
                        </MudStack>
                        <MudIcon Icon="@Icons.Material.Filled.Warning" Color="Color.Warning" Size="Size.Large" />
                    </MudStack>
                </MudCardContent>
            </MudCard>
        </MudItem>
    </MudGrid>

    @* Data Grid *@
    <MudDataGrid T="InventoryItemDto"
                 ServerData="@(new Func<GridState<InventoryItemDto>, Task<GridData<InventoryItemDto>>>(ServerReload))"
                 Hover="true"
                 @ref="_gridData"
                 Height="calc(100vh - 240px)">
        <ToolBarContent>
            <MudText Typo="Typo.h6">لیست موجودی انبار</MudText>
            <MudSpacer />
            <MudStack Row="true" Spacing="2">
                <MudButton Variant="Variant.Outlined"
                           Color="Color.Info"
                           OnClick="ExportToExcel"
                           StartIcon="@Icons.Material.Filled.FileDownload">
                    خروجی Excel
                </MudButton>
                <MudButton Variant="Variant.Filled"
                           Color="Color.Primary"
                           OnClick="OpenAddStockDialog"
                           StartIcon="@Icons.Material.Filled.Add">
                    ورود کالا
                </MudButton>
            </MudStack>
        </ToolBarContent>
        <Columns>
            <PropertyColumn Property="x => x.Id" Title="شناسه" />
            
            <PropertyColumn Property="x => x.ProductTitle" Title="نام محصول">
                <CellTemplate>
                    <MudStack Spacing="0">
                        <MudText Typo="Typo.body2"><strong>@context.Item.ProductTitle</strong></MudText>
                        <MudText Typo="Typo.caption" Color="Color.Secondary">
                            @GetProductTypeLabel(context.Item.ProductType)
                        </MudText>
                    </MudStack>
                </CellTemplate>
            </PropertyColumn>
            
            <PropertyColumn Property="x => x.WarehouseName" Title="انبار">
                <CellTemplate>
                    <MudChip T="string" Color="Color.Info" Size="Size.Small">
                        @context.Item.WarehouseName
                    </MudChip>
                </CellTemplate>
            </PropertyColumn>

            <PropertyColumn Property="x => x.Quantity" Title="موجودی">
                <CellTemplate>
                    <MudText Color="@(context.Item.IsLowStock ? Color.Error : Color.Default)">
                        <strong>@context.Item.Quantity</strong>
                    </MudText>
                </CellTemplate>
            </PropertyColumn>

            <PropertyColumn Property="x => x.ReservedQuantity" Title="رزرو شده">
                <CellTemplate>
                    <MudText Color="Color.Warning">@context.Item.ReservedQuantity</MudText>
                </CellTemplate>
            </PropertyColumn>

            <PropertyColumn Property="x => x.AvailableQuantity" Title="قابل فروش">
                <CellTemplate>
                    <MudText Color="Color.Success"><strong>@context.Item.AvailableQuantity</strong></MudText>
                </CellTemplate>
            </PropertyColumn>

            <PropertyColumn Property="x => x.LowStockThreshold" Title="حد هشدار" />

            <PropertyColumn Property="x => x.IsLowStock" Title="وضعیت">
                <CellTemplate>
                    @if (context.Item.IsLowStock)
                    {
                        <MudChip T="string" Color="Color.Error" Size="Size.Small">
                            <MudIcon Icon="@Icons.Material.Filled.Warning" Size="Size.Small" Class="mr-1" />
                            کم‌موجود
                        </MudChip>
                    }
                    else
                    {
                        <MudChip T="string" Color="Color.Success" Size="Size.Small">
                            موجود
                        </MudChip>
                    }
                </CellTemplate>
            </PropertyColumn>

            <TemplateColumn Title="عملیات" Sortable="false">
                <CellTemplate>
                    <MudStack Row="true" Spacing="1">
                        <MudTooltip Text="ورود کالا">
                            <MudIconButton Icon="@Icons.Material.Filled.AddCircle"
                                           Size="Size.Small"
                                           Color="Color.Success"
                                           OnClick="@(() => OpenAddStockDialogForItem(context.Item))" />
                        </MudTooltip>
                        <MudTooltip Text="تنظیم موجودی">
                            <MudIconButton Icon="@Icons.Material.Filled.Edit"
                                           Size="Size.Small"
                                           Color="Color.Primary"
                                           OnClick="@(() => OpenAdjustStockDialog(context.Item))" />
                        </MudTooltip>
                        <MudTooltip Text="ثبت خسارت">
                            <MudIconButton Icon="@Icons.Material.Filled.RemoveCircle"
                                           Size="Size.Small"
                                           Color="Color.Error"
                                           OnClick="@(() => OpenRecordLossDialog(context.Item))" />
                        </MudTooltip>
                        <MudTooltip Text="تنظیمات">
                            <MudIconButton Icon="@Icons.Material.Filled.Settings"
                                           Size="Size.Small"
                                           Color="Color.Secondary"
                                           OnClick="@(() => OpenSettingsDialog(context.Item))" />
                        </MudTooltip>
                        <MudTooltip Text="تاریخچه تغییرات">
                            <MudIconButton Icon="@Icons.Material.Filled.History"
                                           Size="Size.Small"
                                           Color="Color.Info"
                                           OnClick="@(() => ViewMovements(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="InventoryItemDto"
                              PageSizeOptions="@(new int[] { 20, 50, 100 })"
                              InfoFormat="سطر {first_item} تا {last_item} از {all_items}"
                              RowsPerPageString="تعداد در صفحه" />
        </PagerContent>
    </MudDataGrid>
</div>
