@page "/purchase"
@using Indotalent.Features.Purchase.Bill.Components
@using Indotalent.Features.Purchase.BillReport.Components
@using Indotalent.Features.Purchase.DebitNote.Components
@using Indotalent.Features.Purchase.GoodsReceive.Components
@using Indotalent.Features.Purchase.PaymentDisburse.Components
@using Indotalent.Features.Purchase.PaymentDisburseReport.Components
@using Indotalent.Features.Purchase.PurchaseOrder.Components
@using Indotalent.Features.Purchase.PurchaseReport.Components
@using Indotalent.Features.Purchase.PurchaseRequisition.Components
@using Indotalent.Features.Purchase.PurchaseReturn.Components
@using Indotalent.Features.Purchase.PurchaseReturnReport.Components
@using Indotalent.Features.Purchase.ReceiveReport.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: 150px;
        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="Requisition" Icon="@Icons.Material.Outlined.Assignment">
                <PurchaseRequisitionPage />
            </MudTabPanel>

            <MudTabPanel Text="Purchase Order" Icon="@Icons.Material.Outlined.ShoppingCart">
                <PurchaseOrderPage />
            </MudTabPanel>

            <MudTabPanel Text="Goods Receive" Icon="@Icons.Material.Outlined.MoveToInbox">
                <GoodsReceivePage />
            </MudTabPanel>

            <MudTabPanel Text="Purchase Return" Icon="@Icons.Material.Outlined.AssignmentReturn">
                <PurchaseReturnPage />
            </MudTabPanel>

            <MudTabPanel Text="Bill" Icon="@Icons.Material.Outlined.Receipt">
                <BillPage />
            </MudTabPanel>

            <MudTabPanel Text="Debit Note" Icon="@Icons.Material.Outlined.PostAdd">
                <DebitNotePage />
            </MudTabPanel>

            <MudTabPanel Text="Payment" Icon="@Icons.Material.Outlined.Payments">
                <PaymentDisbursePage />
            </MudTabPanel>

            <MudTabPanel Text="Purchase Report" Icon="@Icons.Material.Outlined.BarChart">
                <PurchaseReportPage />
            </MudTabPanel>

            <MudTabPanel Text="Receive Report" Icon="@Icons.Material.Outlined.Description">
                <ReceiveReportPage />
            </MudTabPanel>

            <MudTabPanel Text="Return Report" Icon="@Icons.Material.Outlined.History">
                <PurchaseReturnReportPage />
            </MudTabPanel>

            <MudTabPanel Text="Bill Report" Icon="@Icons.Material.Outlined.Assessment">
                <BillReportPage />
            </MudTabPanel>

            <MudTabPanel Text="Payment Report" Icon="@Icons.Material.Outlined.AccountBalanceWallet">
                <PaymentDisburseReportPage />
            </MudTabPanel>

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

@code {
    private int _activeTabIndex = 0;

    private readonly Dictionary<int, string> _tabMapping = new()
    {
        { 0, "requisition" },
        { 1, "order" },
        { 2, "receive" },
        { 3, "return" },
        { 4, "bill" },
        { 5, "debit-note" },
        { 6, "payment" },
        { 7, "report-purchase" },
        { 8, "report-receive" },
        { 9, "report-return" },
        { 10, "report-bill" },
        { 11, "report-payment" }
    };

    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($"/purchase?tab={tabName ?? "requisition"}", replace: false);
    }
}