@page "/inventory"
@using Indotalent.Features.Inventory.MovementReport.Components
@using Indotalent.Features.Inventory.NegativeAdjustment.Components
@using Indotalent.Features.Inventory.PositiveAdjustment.Components
@using Indotalent.Features.Inventory.Product.Components
@using Indotalent.Features.Inventory.ProductGroup.Components
@using Indotalent.Features.Inventory.Scrapping.Components
@using Indotalent.Features.Inventory.StockCount.Components
@using Indotalent.Features.Inventory.StockReport.Components
@using Indotalent.Features.Inventory.TransactionReport.Components
@using Indotalent.Features.Inventory.TransferIn.Components
@using Indotalent.Features.Inventory.TransferOut.Components
@using Indotalent.Features.Inventory.UnitMeasure.Components
@using Indotalent.Features.Inventory.Warehouse.Components
@using Microsoft.AspNetCore.Authorization
@using Indotalent.Infrastructure.Authorization.Identity
@using MudBlazor
@inject NavigationManager NavigationManager


@attribute [Authorize(Roles = $"{ApplicationRoles.Admin},{ApplicationRoles.Member}")]

<style>
    .clean-white-tabs .mud-tabs-toolbar {
        background-color: white !important;
        border-bottom: 2px solid #DCEBFA;
        border-radius: 0px !important;
    }

    .clean-white-tabs .mud-tab {
        color: #94a3b8 !important;
        text-transform: none;
        font-weight: 500;
        min-width: 120px;
        border-radius: 0px !important;
    }

    .clean-white-tabs .mud-tab-active {
        color: var(--mud-palette-primary) !important;
        font-weight: 800;
    }

    .clean-white-tabs .mud-tabs-slider {
        background-color: var(--mud-palette-primary) !important;
        height: 3px !important;
        bottom: 0;
    }
</style>

<MudContainer MaxWidth="MaxWidth.ExtraLarge" Class="mt-2 mb-8">
    <div class="clean-white-tabs">
        <MudTabs Elevation="0"
                 ActivePanelIndex="@_activeTabIndex"
                 ActivePanelIndexChanged="OnTabChanged"
                 ApplyEffectsToContainer="true"
                 TabPanelsClass="pt-4"
                 ScrollButtons="ScrollButtons.Always">

            <MudTabPanel Text="Unit Measure" Icon="@Icons.Material.Outlined.Straighten">
                <UnitMeasurePage />
            </MudTabPanel>

            <MudTabPanel Text="Product Group" Icon="@Icons.Material.Outlined.Category">
                <ProductGroupPage />
            </MudTabPanel>

            <MudTabPanel Text="Product" Icon="@Icons.Material.Outlined.Inventory2">
                <ProductPage />
            </MudTabPanel>

            <MudTabPanel Text="Warehouse" Icon="@Icons.Material.Outlined.Warehouse">
                <WarehousePage />
            </MudTabPanel>

            <MudTabPanel Text="Transfer Out" Icon="@Icons.Material.Outlined.FileUpload">
                <TransferOutPage />
            </MudTabPanel>

            <MudTabPanel Text="Transfer In" Icon="@Icons.Material.Outlined.FileDownload">
                <TransferInPage />
            </MudTabPanel>

            <MudTabPanel Text="Pos Adjustment" Icon="@Icons.Material.Outlined.AddCircleOutline">
                <PositiveAdjustmentPage />
            </MudTabPanel>

            <MudTabPanel Text="Neg Adjustment" Icon="@Icons.Material.Outlined.RemoveCircleOutline">
                <NegativeAdjustmentPage />
            </MudTabPanel>

            <MudTabPanel Text="Scrapping" Icon="@Icons.Material.Outlined.DeleteSweep">
                <ScrappingPage />
            </MudTabPanel>

            <MudTabPanel Text="Stock Count" Icon="@Icons.Material.Outlined.FactCheck">
                <StockCountPage />
            </MudTabPanel>

            <MudTabPanel Text="Trans Report" Icon="@Icons.Material.Outlined.Assessment">
                <TransactionReportPage />
            </MudTabPanel>

            <MudTabPanel Text="Stock Report" Icon="@Icons.Material.Outlined.Summarize">
                <StockReportPage />
            </MudTabPanel>

            <MudTabPanel Text="Movement" Icon="@Icons.Material.Outlined.Route">
                <MovementReportPage />
            </MudTabPanel>

        </MudTabs>
    </div>
</MudContainer>

@code {
    private int _activeTabIndex = 0;

    private readonly Dictionary<int, string> _tabMapping = new()
    {
        { 0, "unit-measure" },
        { 1, "product-group" },
        { 2, "product" },
        { 3, "warehouse" },
        { 4, "transfer-out" },
        { 5, "transfer-in" },
        { 6, "pos-adj" },
        { 7, "neg-adj" },
        { 8, "scrapping" },
        { 9, "stock-count" },
        { 10, "report-trans" },
        { 11, "report-stock" },
        { 12, "report-movement" }
    };

    protected override void OnInitialized()
    {
        var uri = NavigationManager.ToAbsoluteUri(NavigationManager.Uri);
        if (Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(uri.Query).TryGetValue("tab", out var tabValue))
        {
            var tabStr = tabValue.ToString().ToLower();
            var match = _tabMapping.FirstOrDefault(x => x.Value == tabStr);
            
            _activeTabIndex = match.Value != null ? match.Key : 0;
        }
    }

    private void OnTabChanged(int index)
    {
        _activeTabIndex = index;
        _tabMapping.TryGetValue(index, out var tabName);
        
        NavigationManager.NavigateTo($"/inventory?tab={tabName ?? "unit-measure"}", replace: false);
    }
}