@using Indotalent.Features.Pipeline.Campaign.Cqrs
@using MudBlazor
@inject CampaignService CampaignService
@inject ISnackbar Snackbar

<MudDialog>
    <DialogContent>
        <MudForm @ref="_form" Model="_model">
            <MudGrid Spacing="2">
                <MudItem xs="12">
                    <MudText Typo="Typo.subtitle2" Class="mb-1">Budget Title</MudText>
                    <MudTextField @bind-Value="_model.Title" Required="true" Variant="Variant.Outlined" Margin="Margin.Dense" FullWidth="true" />
                </MudItem>
                <MudItem xs="12">
                    <MudText Typo="Typo.subtitle2" Class="mb-1">Amount</MudText>
                    <MudNumericField @bind-Value="_model.Amount" Variant="Variant.Outlined" Margin="Margin.Dense" T="decimal?" HideSpinButtons="true" FullWidth="true" />
                </MudItem>
                <MudItem xs="12">
                    <MudText Typo="Typo.subtitle2" Class="mb-1">Date</MudText>
                    <MudDatePicker @bind-Date="_model.BudgetDate" Variant="Variant.Outlined" Margin="Margin.Dense" />
                </MudItem>
                <MudItem xs="12">
                    <MudText Typo="Typo.subtitle2" Class="mb-1">Status</MudText>
                    <MudSelect T="Indotalent.Data.Enums.BudgetStatus" @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.BudgetStatuses)
                        {
                            <MudSelectItem Value="@((Indotalent.Data.Enums.BudgetStatus)item.Value)">@item.Name</MudSelectItem>
                        }
                    </MudSelect>
                </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="2" FullWidth="true" />
                </MudItem>
            </MudGrid>
        </MudForm>
    </DialogContent>
    <DialogActions>
        <MudButton OnClick="Cancel" Variant="Variant.Text" Style="text-transform: none; font-weight: 700;">Cancel</MudButton>
        <MudButton Color="Color.Primary" OnClick="Submit" Variant="Variant.Filled" Disabled="_processing" Style="border-radius: 4px; text-transform: none; font-weight: 700;">
            @if (_processing)
            {
                <MudProgressCircular Size="Size.Small" Indeterminate="true" />
                <MudText Class="ms-2" Typo="Typo.button" Style="text-transform: none !important;">Processing...</MudText>
            }
            else
            {
                <MudText>Save Changes</MudText>
            }
        </MudButton>
    </DialogActions>
</MudDialog>

@code {
    [CascadingParameter] IMudDialogInstance MudDialog { get; set; } = default!;
    [Parameter] public CampaignBudgetResponse Data { get; set; } = new();
    [Parameter] public CampaignLookupResponse Lookup { get; set; } = new();
    private MudForm _form = default!;
    private UpdateBudgetRequest _model = new();
    private bool _processing = false;

    protected override void OnInitialized()
    {
        _model.Id = Data.Id;
        _model.Title = Data.Title;
        _model.Amount = Data.Amount;
        _model.BudgetDate = Data.BudgetDate;
        _model.Status = Data.Status;
        _model.Description = Data.Description;
    }

    private void Cancel() => MudDialog.Cancel();

    private async Task Submit()
    {
        await _form.Validate();
        if (!_form.IsValid) return;

        _processing = true;
        try
        {
            var res = await CampaignService.UpdateBudgetAsync(_model);
            await Task.Delay(500);

            if (res != null && res.IsSuccess)
            {
                Snackbar.Add("Budget updated successfully", Severity.Success);
                MudDialog.Close(DialogResult.Ok(true));
            }
        }
        catch (Exception ex)
        {
            Snackbar.Add($"Error: {ex.Message}", Severity.Error);
        }
        finally
        {
            _processing = false;
        }
    }
}