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

@using CMSMicroservice.Protobuf.Protos.Inventory
@using Google.Protobuf.WellKnownTypes

<div>
    <MudStack Row="true" Justify="Justify.SpaceBetween" AlignItems="AlignItems.Center" Class="mb-4">
        <MudStack Spacing="0">
            <MudText Typo="Typo.h4">تاریخچه تغییرات موجودی</MudText>
            @if (!string.IsNullOrEmpty(_productName))
            {
                <MudText Typo="Typo.subtitle1" Color="Color.Secondary">محصول: @_productName</MudText>
            }
        </MudStack>
        <MudButton Variant="Variant.Outlined"
                   Color="Color.Secondary"
                   StartIcon="@Icons.Material.Filled.ArrowBack"
                   OnClick="GoBack">
            بازگشت به انبار
        </MudButton>
    </MudStack>

    @* Filters Section *@
    <MudPaper Class="pa-4 mb-4" Elevation="2">
        <MudGrid>
            <MudItem xs="12" sm="6" md="3">
                <MudSelect T="StockMovementType" @bind-Value="_filterMovementType"
                           Label="نوع تغییر"
                           Variant="Variant.Outlined"
                           Margin="Margin.Dense">
                    <MudSelectItem T="StockMovementType" Value="StockMovementType.MovementTypeUnspecified">همه</MudSelectItem>
                    <MudSelectItem T="StockMovementType" Value="StockMovementType.InitialStock">موجودی اولیه</MudSelectItem>
                    <MudSelectItem T="StockMovementType" Value="StockMovementType.Restock">ورود کالا</MudSelectItem>
                    <MudSelectItem T="StockMovementType" Value="StockMovementType.Return">برگشت از مشتری</MudSelectItem>
                    <MudSelectItem T="StockMovementType" Value="StockMovementType.Sale">فروش</MudSelectItem>
                    <MudSelectItem T="StockMovementType" Value="StockMovementType.AdjustmentIncrease">افزایش موجودی</MudSelectItem>
                    <MudSelectItem T="StockMovementType" Value="StockMovementType.AdjustmentDecrease">کاهش موجودی</MudSelectItem>
                    <MudSelectItem T="StockMovementType" Value="StockMovementType.Reserved">رزرو</MudSelectItem>
                    <MudSelectItem T="StockMovementType" Value="StockMovementType.Released">آزادسازی رزرو</MudSelectItem>
                    <MudSelectItem T="StockMovementType" Value="StockMovementType.Loss">خسارت</MudSelectItem>
                    <MudSelectItem T="StockMovementType" Value="StockMovementType.Damaged">آسیب‌دیده</MudSelectItem>
                    <MudSelectItem T="StockMovementType" Value="StockMovementType.Expired">منقضی</MudSelectItem>
                    <MudSelectItem T="StockMovementType" Value="StockMovementType.TransferOut">انتقال به بیرون</MudSelectItem>
                    <MudSelectItem T="StockMovementType" Value="StockMovementType.TransferIn">انتقال به داخل</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">
                <MudDatePicker @bind-Date="_filterFromDate"
                               Label="از تاریخ"
                               Variant="Variant.Outlined"
                               Margin="Margin.Dense"
                               Clearable="true" />
            </MudItem>
            <MudItem xs="12" sm="6" md="3">
                <MudDatePicker @bind-Date="_filterToDate"
                               Label="تا تاریخ"
                               Variant="Variant.Outlined"
                               Margin="Margin.Dense"
                               Clearable="true" />
            </MudItem>
            <MudItem xs="12" Class="d-flex justify-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>

    @* Data Grid *@
    <MudDataGrid T="StockMovementDto"
                 ServerData="@(new Func<GridState<StockMovementDto>, Task<GridData<StockMovementDto>>>(ServerReload))"
                 Hover="true"
                 @ref="_gridData"
                 Height="calc(100vh - 240px)">
        <ToolBarContent>
            <MudText Typo="Typo.h6">تغییرات موجودی</MudText>
            <MudSpacer />
            <MudText Typo="Typo.body2" Color="Color.Secondary" Class="ml-4">
                کل: @_totalCount ردیف
            </MudText>
            <MudButton Variant="Variant.Outlined" 
                      Color="Color.Info" 
                      OnClick="ExportToExcel"
                      StartIcon="@Icons.Material.Filled.FileDownload"
                      Size="Size.Small">
                خروجی Excel
            </MudButton>
        </ToolBarContent>
        <Columns>
            <PropertyColumn Property="x => x.Id" Title="شناسه" />

            <PropertyColumn Property="x => x.ProductTitle" Title="محصول" />

            <PropertyColumn Property="x => x.MovementType" Title="نوع تغییر">
                <CellTemplate>
                    <MudChip T="string" 
                             Color="@GetMovementColor(context.Item.MovementType)" 
                             Size="Size.Small">
                        @GetMovementTypeLabel(context.Item.MovementType)
                    </MudChip>
                </CellTemplate>
            </PropertyColumn>

            <PropertyColumn Property="x => x.Quantity" Title="تعداد">
                <CellTemplate>
                    <MudText Color="@(IsIncreaseMovement(context.Item.MovementType) ? Color.Success : Color.Error)">
                        @(IsIncreaseMovement(context.Item.MovementType) ? "+" : "-")@Math.Abs(context.Item.Quantity)
                    </MudText>
                </CellTemplate>
            </PropertyColumn>

            <PropertyColumn Property="x => x.QuantityBefore" Title="قبل" />
            <PropertyColumn Property="x => x.QuantityAfter" Title="بعد" />

            <PropertyColumn Property="x => x.ReferenceNumber" Title="شماره مرجع">
                <CellTemplate>
                    @if (!string.IsNullOrEmpty(context.Item.ReferenceNumber))
                    {
                        <MudText Typo="Typo.caption">@context.Item.ReferenceNumber</MudText>
                    }
                    else
                    {
                        <MudText Typo="Typo.caption" Color="Color.Secondary">-</MudText>
                    }
                </CellTemplate>
            </PropertyColumn>

            <PropertyColumn Property="x => x.PerformedByUserId" Title="کاربر">
                <CellTemplate>
                    @if (context.Item.PerformedByUserId != null && context.Item.PerformedByUserId > 0)
                    {
                        <MudText Typo="Typo.caption">کاربر #@context.Item.PerformedByUserId</MudText>
                    }
                    else
                    {
                        <MudText Typo="Typo.caption" Color="Color.Secondary">سیستم</MudText>
                    }
                </CellTemplate>
            </PropertyColumn>

            <PropertyColumn Property="x => x.Created" Title="تاریخ">
                <CellTemplate>
                    @if (context.Item.Created != null)
                    {
                        <MudText Typo="Typo.caption">
                            @context.Item.Created.ToDateTime().ToLocalTime().MiladiToJalaliWithTime()
                        </MudText>
                    }
                </CellTemplate>
            </PropertyColumn>

            <PropertyColumn Property="x => x.Note" Title="توضیحات">
                <CellTemplate>
                    @if (!string.IsNullOrEmpty(context.Item.Note))
                    {
                        <MudTooltip Text="@context.Item.Note">
                            <MudText Typo="Typo.caption" Style="max-width: 150px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">
                                @context.Item.Note
                            </MudText>
                        </MudTooltip>
                    }
                </CellTemplate>
            </PropertyColumn>
        </Columns>
            <NoRecordsContent>
                <MudAlert Severity="Severity.Info" Dense="true" Variant="Variant.Text" Class="my-4">موردی یافت نشد.</MudAlert>
            </NoRecordsContent>
        <PagerContent>
            <MudDataGridPager T="StockMovementDto"
                              PageSizeOptions="@(new int[] { 20, 50, 100 })"
                              InfoFormat="سطر {first_item} تا {last_item} از {all_items}"
                              RowsPerPageString="تعداد در صفحه" />
        </PagerContent>
    </MudDataGrid>
</div>
