@using Indotalent.Features.Pipeline.Campaign.Cqrs
@using Indotalent.Features.Root.Shared
@using MudBlazor
@inject CampaignService CampaignService
@inject IDialogService DialogService
@inject ISnackbar Snackbar

<MudPaper Elevation="0" Outlined="true" Style="border-radius: 12px; border: 1px solid #E5E7EB;">
    <div style="padding: 16px; border-bottom: 1px solid #E5E7EB; display: flex; justify-content: space-between; align-items: center; background-color: #F9FAFB;">
        <MudText Typo="Typo.button" Style="font-weight: 600;">Campaign Expenses</MudText>
        @if (!ReadOnly)
        {
            <MudButton Variant="Variant.Filled" Color="Color.Primary" StartIcon="@Icons.Material.Filled.Add" OnClick="OnAddClick" Size="Size.Small" Style="text-transform: none; font-weight: 500; border-radius: 6px;">Add Expense</MudButton>
        }
    </div>
    <MudTable Striped="true" Class="mud-table-styled" Items="Items" Hover="true" Elevation="0" Dense="true" T="CampaignExpenseResponse" Context="expenseContext">
        <HeaderContent>
            <MudTh Style="font-weight: 700; color: #111827;">No</MudTh>
            <MudTh Style="font-weight: 700; color: #111827;">Title</MudTh>
            <MudTh Style="text-align: right; font-weight: 700;">Amount</MudTh>
            @if (!ReadOnly)
            {
                <MudTh Style="width: 100px; text-align: right; font-weight: 700;">Actions</MudTh>
            }
        </HeaderContent>
        <RowTemplate>
            <MudTd DataLabel="No" Style="padding-top: 0.75rem; padding-bottom: 0.75rem; color: #6B7280; font-size: 0.875rem;">@expenseContext.AutoNumber</MudTd>
            <MudTd DataLabel="Title" Style="padding-top: 0.75rem; padding-bottom: 0.75rem; color: #6B7280; font-size: 0.875rem;">@expenseContext.Title</MudTd>
            <MudTd DataLabel="Amount" Style="text-align: right;">@expenseContext.Amount?.ToString("N0")</MudTd>
            @if (!ReadOnly)
            {
                <MudTd Style="text-align: right; padding-top: 0.75rem; padding-bottom: 0.75rem; color: #6B7280; font-size: 0.875rem;">
                    <MudIconButton Icon="@Icons.Material.Filled.Edit" Size="Size.Small" Color="Color.Primary" OnClick="@(() => OnEditClick(expenseContext))" />
                    <MudIconButton Icon="@Icons.Material.Filled.Delete" Size="Size.Small" Color="Color.Error" OnClick="@(() => OnDeleteClick(expenseContext))" />
                </MudTd>
            }
        </RowTemplate>
    </MudTable>
</MudPaper>


<style>
    .mud-table-styled .mud-table-row:hover { background-color: #F9FAFB !important; }
</style>

@code {
    [Parameter] public string CampaignId { get; set; } = string.Empty;
    [Parameter] public List<CampaignExpenseResponse> Items { get; set; } = new();
    [Parameter] public CampaignLookupResponse Lookup { get; set; } = new();
    [Parameter] public bool ReadOnly { get; set; } = false;
    [Parameter] public EventCallback OnChanged { get; set; }

    private async Task OnAddClick()
    {
        var p = new DialogParameters { ["CampaignId"] = CampaignId, ["Lookup"] = Lookup };
        var d = await DialogService.ShowAsync<_ExpenseCreateForm>("Add Expense", p, new DialogOptions { MaxWidth = MaxWidth.Small, FullWidth = true });
        var result = await d.Result;
        if (result != null && !result.Canceled) 
        { 
            await OnChanged.InvokeAsync(); 
        }
    }

    private async Task OnEditClick(CampaignExpenseResponse item)
    {
        var p = new DialogParameters { ["Data"] = item, ["Lookup"] = Lookup };
        var d = await DialogService.ShowAsync<_ExpenseUpdateForm>("Edit Expense", p, new DialogOptions { MaxWidth = MaxWidth.Small, FullWidth = true });
        var result = await d.Result;
        if (result != null && !result.Canceled) 
        { 
            await OnChanged.InvokeAsync(); 
        }
    }

    private async Task OnDeleteClick(CampaignExpenseResponse item)
    {
        var p = new DialogParameters { ["ContentText"] = $"Are you sure you want to delete expense {item.AutoNumber}?" };
        var d = await DialogService.ShowAsync<_DeleteConfirmation>("Delete Confirmation", p, new DialogOptions { MaxWidth = MaxWidth.ExtraSmall, FullWidth = true });
        var result = await d.Result;

        if (result != null && !result.Canceled)
        {
            if (await CampaignService.DeleteExpenseAsync(item.Id!)) 
            { 
                Snackbar.Add("Expense removed successfully", Severity.Success); 
                await OnChanged.InvokeAsync(); 
            }
        }
    }
}