@using TaxBaik.Web.Services
@using TaxBaik.Domain.Entities
@using TaxBaik.Web.Components.Admin.Shared
@inject ITaxFilingBrowserClient FilingClient
@inject ISnackbar Snackbar

@if (Filings == null || Filings.Count == 0)
{
    <MudText Class="pa-4" Color="Color.Secondary">항목이 없습니다.</MudText>
}
else
{
    <MudTable Items="Filings" Hover="true" Dense="true" Class="mt-2">
        <HeaderContent>
            <MudTh>고객</MudTh>
            <MudTh>신고 유형</MudTh>
            <MudTh>기한</MudTh>
            <MudTh>D-day</MudTh>
            <MudTh>메모</MudTh>
            <MudTh>처리</MudTh>
        </HeaderContent>
        <RowTemplate>
            <MudTd>@context.ClientName</MudTd>
            <MudTd>@context.FilingType</MudTd>
            <MudTd>@BusinessDayCalculator.GetEffectiveDueDate(DateOnly.FromDateTime(context.DueDate)).ToDateTime(TimeOnly.MinValue).ToString("yyyy-MM-dd")</MudTd>
            <MudTd>
                @{
                    var dday = BusinessDayCalculator.GetDday(DateOnly.FromDateTime(context.DueDate));
                }
                @if (dday < 0)
                {
                    <MudChip T="string" Size="Size.Small" Color="Color.Error">D+@(-dday)</MudChip>
                }
                else if (dday <= 7)
                {
                    <MudChip T="string" Size="Size.Small" Color="Color.Warning">D-@dday</MudChip>
                }
                else
                {
                    <MudText Typo="Typo.body2">D-@dday</MudText>
                }
            </MudTd>
            <MudTd>@(context.Memo ?? "")</MudTd>
            <MudTd>
                @if (context.Status == "pending")
                {
                    <MudButton Size="Size.Small" Variant="Variant.Filled" Color="Color.Success"
                               OnClick="@(() => MarkFiled(context))">완료</MudButton>
                }
                else if (context.Status == "filed")
                {
                    <MudChip T="string" Size="Size.Small" Color="Color.Success">완료</MudChip>
                }
                <MudIconButton Icon="@Icons.Material.Filled.Delete" Size="Size.Small" Color="Color.Error"
                               OnClick="@(() => DeleteFiling(context.Id))" />
            </MudTd>
        </RowTemplate>
    </MudTable>
}

@code {
    [Parameter]
    public List<TaxFiling>? Filings { get; set; }

    [Parameter]
    public EventCallback OnStatusChange { get; set; }

    private async Task MarkFiled(TaxFiling filing)
    {
        try
        {
            filing.Status = "filed";
            var result = await FilingClient.UpdateAsync(filing.Id, filing);
            if (result != null)
            {
                Snackbar.Add("신고 완료 처리되었습니다.", Severity.Success);
                await OnStatusChange.InvokeAsync();
            }
            else
            {
                Snackbar.Add("처리 실패", Severity.Error);
            }
        }
        catch (Exception ex)
        {
            Snackbar.Add($"오류: {ex.Message}", Severity.Error);
        }
    }

    private async Task DeleteFiling(int id)
    {
        try
        {
            var success = await FilingClient.DeleteAsync(id);
            if (success)
            {
                Snackbar.Add("삭제되었습니다.", Severity.Info);
                await OnStatusChange.InvokeAsync();
            }
            else
            {
                Snackbar.Add("삭제 실패", Severity.Error);
            }
        }
        catch (Exception ex)
        {
            Snackbar.Add($"오류: {ex.Message}", Severity.Error);
        }
    }
}
