@page "/sales"
@using Indotalent.Features.Sales.CreditNote.Components
@using Indotalent.Features.Sales.DeliveryOrder.Components
@using Indotalent.Features.Sales.DeliveryReport.Components
@using Indotalent.Features.Sales.Invoice.Components
@using Indotalent.Features.Sales.InvoiceReport.Components
@using Indotalent.Features.Sales.PaymentReceive.Components
@using Indotalent.Features.Sales.PaymentReceiveReport.Components
@using Indotalent.Features.Sales.SalesOrder.Components
@using Indotalent.Features.Sales.SalesQuotation.Components
@using Indotalent.Features.Sales.SalesReport.Components
@using Indotalent.Features.Sales.SalesReturn.Components
@using Indotalent.Features.Sales.SalesReturnReport.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="Quotation" Icon="@Icons.Material.Outlined.Description">
                <SalesQuotationPage />
            </MudTabPanel>

            <MudTabPanel Text="Sales Order" Icon="@Icons.Material.Outlined.ListAlt">
                <SalesOrderPage />
            </MudTabPanel>

            <MudTabPanel Text="Delivery Order" Icon="@Icons.Material.Outlined.LocalShipping">
                <DeliveryOrderPage />
            </MudTabPanel>

            <MudTabPanel Text="Sales Return" Icon="@Icons.Material.Outlined.KeyboardReturn">
                <SalesReturnPage />
            </MudTabPanel>

            <MudTabPanel Text="Invoice" Icon="@Icons.Material.Outlined.ReceiptLong">
                <InvoicePage />
            </MudTabPanel>

            <MudTabPanel Text="Credit Note" Icon="@Icons.Material.Outlined.NoteAdd">
                <CreditNotePage />
            </MudTabPanel>

            <MudTabPanel Text="Payment" Icon="@Icons.Material.Outlined.PriceCheck">
                <PaymentReceivePage />
            </MudTabPanel>

            <MudTabPanel Text="Sales Report" Icon="@Icons.Material.Outlined.Insights">
                <SalesReportPage />
            </MudTabPanel>

            <MudTabPanel Text="Delivery Report" Icon="@Icons.Material.Outlined.HistoryEdu">
                <DeliveryReportPage />
            </MudTabPanel>

            <MudTabPanel Text="Return Report" Icon="@Icons.Material.Outlined.AssignmentReturn">
                <SalesReturnReportPage />
            </MudTabPanel>

            <MudTabPanel Text="Invoice Report" Icon="@Icons.Material.Outlined.Analytics">
                <InvoiceReportPage />
            </MudTabPanel>

            <MudTabPanel Text="Payment Report" Icon="@Icons.Material.Outlined.MonetizationOn">
                <PaymentReceiveReportPage />
            </MudTabPanel>

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

@code {
    private int _activeTabIndex = 0;

    private readonly Dictionary<int, string> _tabMapping = new()
    {
        { 0, "quotation" },
        { 1, "order" },
        { 2, "delivery" },
        { 3, "return" },
        { 4, "invoice" },
        { 5, "credit-note" },
        { 6, "payment" },
        { 7, "report-sales" },
        { 8, "report-delivery" },
        { 9, "report-return" },
        { 10, "report-invoice" },
        { 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($"/sales?tab={tabName ?? "quotation"}", replace: false);
    }
}