@using phronCare.UIBlazor.Services.Sales
@using Blazored.Modal
@using Blazored.Modal.Services
@using Domain.Entities
@inject DocumentTypeService documentTypeService
@inject PatientService patientService
@inject IToastService Toast
@inherits OwningComponentBase

<EditForm Model="_model" OnValidSubmit="HandleValidSubmit">
    <DataAnnotationsValidator />
    <ValidationSummary />

    <div class="card" style="zoom: 80%;">
        <div class="card-header">
            <h5 class="card-title mb-0">Nuevo Paciente</h5>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-6 mb-3">
                    <label class="form-label">Nombre *</label>
                    <InputText class="form-control" @bind-Value="_model.Firstname" />
                    <ValidationMessage For="@(() => _model.Firstname)" />
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label">Apellido *</label>
                    <InputText class="form-control" @bind-Value="_model.Lastname" />
                    <ValidationMessage For="@(() => _model.Lastname)" />
                </div>
            </div>

            <div class="row">
                <div class="col-md-6 mb-3">
                    <label class="form-label">Tipo Documento *</label>
                    <InputSelect class="form-select" @bind-Value="_model.DocumenttypesId">
                        <option value="">Seleccione...</option>
                        @foreach (var doc in documentTypes)
                        {
                            <option value="@doc.Id">@doc.Description</option>
                        }
                    </InputSelect>
                    <ValidationMessage For="@(() => _model.DocumenttypesId)" />
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label">Nro Documento *</label>
                    <InputText class="form-control" @bind-Value="_model.DocumentNumber" />
                    <ValidationMessage For="@(() => _model.DocumentNumber)" />
                </div>
            </div>

            <div class="row">
                <div class="col-md-6 mb-3">
                    <label class="form-label">Fecha Nacimiento</label>
                    <InputDate class="form-control" @bind-Value="_model.Birthdate" />
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label">Nro Afiliado</label>
                    <InputText class="form-control" @bind-Value="_model.AffiliateNumber" />
                </div>
            </div>

            <div class="row">
                <div class="col-md-12 mb-3">
                    <label class="form-label d-block">Género</label>
                    <InputRadioGroup @bind-Value="_model.Gender">
                        <div class="form-check form-check-inline">
                            <InputRadio class="form-check-input" Value="@( "Masculino")" id="radioM" />
                            <label class="form-check-label" for="radioM">Masculino</label>
                        </div>
                        <div class="form-check form-check-inline">
                            <InputRadio class="form-check-input" Value="@( "Femenino")" id="radioF" />
                            <label class="form-check-label" for="radioF">Femenino</label>
                        </div>
                        <div class="form-check form-check-inline">
                            <InputRadio class="form-check-input" Value="@( "Otro")" id="radioO" />
                            <label class="form-check-label" for="radioO">Otro</label>
                        </div>
                    </InputRadioGroup>
                </div>
            </div>
        </div>

        <div class="card-footer text-end">
            <button type="submit" class="btn btn-success">Guardar</button>
            <button type="button" class="btn btn-secondary ms-2" @onclick="Cancelar">Cancelar</button>
        </div>
    </div>
</EditForm>

@code {
    [CascadingParameter] public BlazoredModalInstance ModalInstance { get; set; } = default!;
    private EPatient _model = new();
    private List<EDocumentType> documentTypes = new();

    protected override async Task OnInitializedAsync()
    {
        documentTypes = await documentTypeService.GetAllAsync();
    }

    private async Task HandleValidSubmit()
    {
        var result = await patientService.CreateAsync(_model);

        if (result.IsSuccessStatusCode)
        {
            Toast.ShowSuccess("Paciente creado.");
            var newItem = new ELookUpItem { Id = _model.Id, Nombre = $"{_model.Firstname} {_model.Lastname}" };
            await ModalInstance.CloseAsync(ModalResult.Ok(newItem));
        }
        else
        {
            var error = await result.Content.ReadAsStringAsync();
            Toast.ShowError($"Error: {error}");
        }
    }

    private async Task Cancelar()
    {
        await ModalInstance.CancelAsync();
    }
}
