@page "/setting"
@using Microsoft.AspNetCore.Authorization
@using Indotalent.Infrastructure.Authorization.Identity
@attribute [Authorize(Roles = ApplicationRoles.TenantAdmin)]
@using Indotalent.Features.Setting.AutoNumberSequence.Components
@using Indotalent.Features.Setting.Company.Components
@using Indotalent.Features.Setting.Currency.Components
@using Indotalent.Features.Setting.SystemUser.Components
@using Indotalent.Features.Setting.Tax.Components
@using MudBlazor
@inject NavigationManager NavigationManager

<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: 160px;
        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">

            <MudTabPanel Text="Company Info" Icon="@Icons.Material.Outlined.Business">
                <CompanyPage />
            </MudTabPanel>

            <MudTabPanel Text="Users" Icon="@Icons.Material.Outlined.Group">
                <SystemUserPage />
            </MudTabPanel>

            <MudTabPanel Text="Currencies" Icon="@Icons.Material.Outlined.Paid">
                <CurrencyPage />
            </MudTabPanel>

            <MudTabPanel Text="Tax Rules" Icon="@Icons.Material.Outlined.ReceiptLong">
                <TaxPage />
            </MudTabPanel>

            <MudTabPanel Text="Auto Number" Icon="@Icons.Material.Outlined.Numbers">
                <AutoNumberSequencePage />
            </MudTabPanel>


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

@code {
    private int _activeTabIndex = 0;

    protected override void OnInitialized()
    {
        var uri = NavigationManager.ToAbsoluteUri(NavigationManager.Uri);
        if (Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(uri.Query).TryGetValue("tab", out var tabValue))
        {
            _activeTabIndex = tabValue.ToString().ToLower() switch
            {
                "company" => 0,
                "user" => 1,
                "currency" => 2,
                "tax" => 3,
                "autonumber" => 4,
                _ => 0
            };
        }
    }

    private void OnTabChanged(int index)
    {
        _activeTabIndex = index;
        string tabName = index switch
        {
            0 => "company",
            1 => "user",
            2 => "currency",
            3 => "tax",
            4 => "autonumber",
            _ => "company"
        };

        NavigationManager.NavigateTo($"/setting?tab={tabName}", replace: false);
    }
}