@using Indotalent.Features.Pipeline.Expense
@using Indotalent.Features.Pipeline.Expense.Cqrs
@using Indotalent.Shared.Utils
@using MudBlazor
@inject ExpenseService ExpenseService
@inject ISnackbar Snackbar

<MudPaper Elevation="0" Square="true" Class="pa-6 mb-3 d-flex align-center gap-4" Style="border: 1px solid #DCEBFA;">
    <MudIconButton Icon="@Icons.Material.Filled.ArrowBack" OnClick="() => OnCancel.InvokeAsync()" />
    <div>
        <MudText Typo="Typo.h5" Style="font-weight: 900; color: #1a1a1a;">Add New Expense</MudText>
        <MudText Typo="Typo.body2" Style="color: #64748b;">Record actual spending for your campaign.</MudText>
    </div>
</MudPaper>

<MudPaper Elevation="0" Outlined="true" Class="pa-8" Style="border-radius: 0px; border: 1px solid #DCEBFA;">
    <MudForm @ref="_form" Model="_model">
        <MudGrid Spacing="3">
            <MudItem xs="12">
                <MudText Typo="Typo.subtitle2" Class="mb-1">Title</MudText>
                <MudTextField @bind-Value="_model.Title" For="@(() => _model.Title)" Validation="@(_validator.ValidateValue())" Variant="Variant.Outlined" Margin="Margin.Dense" FullWidth="true" />
            </MudItem>
            
            <MudItem xs="12">
                <MudText Typo="Typo.subtitle2" Class="mb-1">Campaign</MudText>
                <MudSelect T="string" @bind-Value="_model.CampaignId" For="@(() => _model.CampaignId)" Validation="@(_validator.ValidateValue())" Variant="Variant.Outlined" Margin="Margin.Dense" Dense="true" FullWidth="true" AnchorOrigin="Origin.BottomCenter" TransformOrigin="Origin.TopCenter">
                    @foreach (var item in _lookup.Campaigns)
                    {
                        <MudSelectItem Value="@item.Id">@item.Name</MudSelectItem>
                    }
                </MudSelect>
            </MudItem>

            <MudItem xs="12">
                <MudText Typo="Typo.subtitle2" Class="mb-1">Status</MudText>
                <MudSelect T="Indotalent.Data.Enums.ExpenseStatus" @bind-Value="_model.Status" Variant="Variant.Outlined" Margin="Margin.Dense" Dense="true" FullWidth="true" AnchorOrigin="Origin.BottomCenter" TransformOrigin="Origin.TopCenter">
                    @foreach (var item in _lookup.Statuses)
                    {
                        <MudSelectItem Value="@((Indotalent.Data.Enums.ExpenseStatus)item.Value)">@item.Name</MudSelectItem>
                    }
                </MudSelect>
            </MudItem>

            <MudItem xs="12">
                <MudText Typo="Typo.subtitle2" Class="mb-1">Amount</MudText>
                <MudNumericField @bind-Value="_model.Amount" T="decimal?" Variant="Variant.Outlined" Margin="Margin.Dense" FullWidth="true" HideSpinButtons="false" />
            </MudItem>

            <MudItem xs="12">
                <MudText Typo="Typo.subtitle2" Class="mb-1">Expense Date</MudText>
                <MudDatePicker @bind-Date="_model.ExpenseDate" Variant="Variant.Outlined" Margin="Margin.Dense" />
            </MudItem>

            <MudItem xs="12">
                <MudText Typo="Typo.subtitle2" Class="mb-1">Description</MudText>
                <MudTextField @bind-Value="_model.Description" Variant="Variant.Outlined" Margin="Margin.Dense" Lines="3" FullWidth="true" />
            </MudItem>
        </MudGrid>
        <div class="d-flex justify-end gap-2 mt-8">
            <MudButton OnClick="() => OnCancel.InvokeAsync()" Variant="Variant.Outlined" Style="border-radius: 4px; border: 1px solid #e0e0e0; text-transform: none; font-weight: 700;">Cancel</MudButton>
            <MudButton Color="Color.Primary" Variant="Variant.Filled" OnClick="Submit" Disabled="_processing" Style="border-radius: 4px; text-transform: none; font-weight: 700;">
                @if (_processing)
                {
                    <MudProgressCircular Class="ms-n1" Size="Size.Small" Indeterminate="true" />
                    <MudText Class="ms-2" Typo="Typo.button" Style="text-transform: none !important;">Processing...</MudText>
                }
                else
                {

                    <MudText>Create Expense</MudText>
                }
            </MudButton>
        </div>
    </MudForm>
</MudPaper>

@code {
    [Parameter] public EventCallback OnCancel { get; set; }
    [Parameter] public EventCallback OnSuccess { get; set; }
    private MudForm _form = default!;
    private CreateExpenseValidator _validator = new();
    private CreateExpenseRequest _model = new();
    private ExpenseLookupResponse _lookup = new();
    private bool _processing = false;

    protected override async Task OnInitializedAsync()
    {
        var res = await ExpenseService.GetExpenseLookupAsync();
        if (res != null && res.IsSuccess) _lookup = res.Value ?? new();
    }

    private async Task Submit()
    {
        await _form.Validate();
        if (!_form.IsValid) return;
        _processing = true;
        var res = await ExpenseService.CreateExpenseAsync(_model);
        await Task.Delay(500);
        if (res != null && res.IsSuccess) { Snackbar.Add("Created successfully", Severity.Success); await OnSuccess.InvokeAsync(); }
        _processing = false;
    }
}