@using Indotalent.Features.Inventory.NegativeAdjustment.Cqrs
@using MudBlazor
@inject NegativeAdjustmentService NegativeAdjustmentService
@inject ISnackbar Snackbar

<MudDialog>
    <DialogContent>
        <MudForm @ref="_form" Model="_model">
            <MudGrid Spacing="2">
                <MudItem xs="12">
                    <MudText Typo="Typo.subtitle2" Class="mb-0">Product</MudText>
                    <MudSelect T="string" @bind-Value="_model.ProductId"
                               Variant="Variant.Outlined" Margin="Margin.Dense" Dense="true"
                               AnchorOrigin="Origin.BottomCenter" TransformOrigin="Origin.TopCenter">
                        @foreach (var item in _lookup.Products)
                        {
                            <MudSelectItem Value="@item.Id">@item.Name</MudSelectItem>
                        }
                    </MudSelect>
                </MudItem>
                <MudItem xs="12">
                    <MudText Typo="Typo.subtitle2" Class="mb-0">Warehouse</MudText>
                    <MudSelect T="string" @bind-Value="_model.WarehouseId"
                               Variant="Variant.Outlined" Margin="Margin.Dense" Dense="true"
                               AnchorOrigin="Origin.BottomCenter" TransformOrigin="Origin.TopCenter">
                        @foreach (var item in _lookup.Warehouses)
                        {
                            <MudSelectItem Value="@item.Id">@item.Name</MudSelectItem>
                        }
                    </MudSelect>
                </MudItem>
                <MudItem xs="12">
                    <MudText Typo="Typo.subtitle2" Class="mb-1">Quantity Minus</MudText>
                    <MudNumericField @bind-Value="_model.Movement" Variant="Variant.Outlined" Margin="Margin.Dense" />
                </MudItem>
            </MudGrid>
        </MudForm>
    </DialogContent>
    <DialogActions>
        <MudButton OnClick="Cancel" Variant="Variant.Text">Cancel</MudButton>
        <MudButton Color="Color.Primary" OnClick="Submit" Variant="Variant.Filled" Disabled="_processing" Style="text-transform: none; font-weight: 700; border-radius: 4px;">
            @if (_processing)
            {
                <MudProgressCircular Size="Size.Small" Indeterminate="true" />
                <MudText Class="ms-2" Typo="Typo.button" Style="text-transform: none !important;">Processing...</MudText>
            }
            else
            {
                <MudText>Add Item</MudText>
            }
        </MudButton>
    </DialogActions>
</MudDialog>

@code {
    [CascadingParameter] IMudDialogInstance MudDialog { get; set; } = default!;
    [Parameter] public string NegativeAdjustmentId { get; set; } = string.Empty;
    private MudForm _form = default!;
    private CreateNegativeAdjustmentItemRequest _model = new() { Movement = 1 };
    private NegativeAdjustmentLookupResponse _lookup = new();
    private bool _processing = false;

    protected override async Task OnInitializedAsync()
    {
        var res = await NegativeAdjustmentService.GetNegativeAdjustmentLookupAsync();
        if (res != null && res.IsSuccess) _lookup = res.Value ?? new();
        _model.NegativeAdjustmentId = NegativeAdjustmentId;
    }

    private void Cancel() => MudDialog.Cancel();

    private async Task Submit()
    {
        await _form.Validate();
        if (!_form.IsValid) return;
        _processing = true;
        try
        {
            var response = await NegativeAdjustmentService.CreateNegativeAdjustmentItemAsync(_model);
            await Task.Delay(500);
            if (response != null && response.IsSuccess)
            {
                Snackbar.Add("Item added successfully", Severity.Success);
                MudDialog.Close(DialogResult.Ok(true));
            }
        }
        finally { _processing = false; }
    }
}