@using Indotalent.Features.Thirdparty.Customer.Cqrs
@using MudBlazor
@inject CustomerService CustomerService
@inject ISnackbar Snackbar

<MudDialog>
    <DialogContent>
        <MudForm @ref="_form" Model="_model">
            <MudGrid>
                <MudItem xs="12">
                    <MudText Typo="Typo.subtitle2" Class="mb-1">Contact Name</MudText>
                    <MudTextField @bind-Value="_model.Name" Required="true" Variant="Variant.Outlined" Margin="Margin.Dense" />
                </MudItem>
                <MudItem xs="12" sm="12">
                    <MudText Typo="Typo.subtitle2" Class="mb-1">Job Title</MudText>
                    <MudTextField @bind-Value="_model.JobTitle" Variant="Variant.Outlined" Margin="Margin.Dense" />
                </MudItem>
                <MudItem xs="12" sm="6">
                    <MudText Typo="Typo.subtitle2" Class="mb-1">Phone Number</MudText>
                    <MudTextField @bind-Value="_model.PhoneNumber" Variant="Variant.Outlined" Margin="Margin.Dense" />
                </MudItem>
                <MudItem xs="12" sm="6">
                    <MudText Typo="Typo.subtitle2" Class="mb-1">Email Address</MudText>
                    <MudTextField @bind-Value="_model.EmailAddress" 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" />
                </MudItem>
            </MudGrid>
        </MudForm>
    </DialogContent>
    <DialogActions>
        <MudButton OnClick="Cancel" Variant="Variant.Text">Cancel</MudButton>
        <MudButton Color="Color.Primary" OnClick="Submit" Variant="Variant.Filled" Disabled="_processing" Style="text-transform: none; font-weight: 700; border-radius: 4px;">
            @if (_processing)
            {
                <MudProgressCircular Size="Size.Small" Indeterminate="true" />
                <MudText Class="ms-2" Typo="Typo.button" Style="text-transform: none !important;">Processing...</MudText>
            }
            else
            {

                <MudText>Add Contact</MudText>
            }
        </MudButton>
    </DialogActions>
</MudDialog>

@code {
    [CascadingParameter] IMudDialogInstance MudDialog { get; set; } = default!;
    [Parameter] public string CustomerId { get; set; } = string.Empty;
    private MudForm _form = default!;
    private CreateCustomerContactRequest _model = new();
    private bool _processing = false;

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

    private async Task Submit()
    {
        await _form.Validate();
        if (!_form.IsValid) return;
        _processing = true;
        _model.CustomerId = CustomerId;
        try
        {
            var response = await CustomerService.CreateCustomerContactAsync(_model);
            await Task.Delay(500);
            if (response != null && response.IsSuccess)
            {
                Snackbar.Add("Contact added successfully", Severity.Success);
                MudDialog.Close(DialogResult.Ok(true));
            }
        }
        finally { _processing = false; }
    }
}