@using phronCare.UIBlazor.Services.Sales

 @inject ProfessionalSpecialtyService specialtyService
 @inject DocumentTypeService documentTypeService

@inject ProfessionalService ProfessionalService
@inject IToastService Toast
@inject Blazored.Modal.Services.IModalService ModalService


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

    <div class="card" style="zoom: 80%;">
        <div class="card-header">
            <h5 class="card-title mb-0">Nuevo Profesional</h5>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-12 mb-3">
                    <label class="form-label">Nombre Completo *</label>
                    <InputText class="form-control" @bind-Value="_model.Fullname" />
                    <ValidationMessage For="@(() => _model.Fullname)" />
                </div>
            </div>
            <div class="row">
                <div class="col-md-4 mb-3">
@*                     <label class="form-label">Tipo Doc *</label>
                    <InputSelect class="form-select" @bind-Value="_model.DocumenttypeName">
                        <option value="">Seleccione...</option>
                        <option value="DNI">DNI</option>
                        <option value="CUIL">CUIL</option>
                        <option value="LE">LE</option>
                    </InputSelect>
                    <ValidationMessage For="@(() => _model.DocumenttypeName)" /> *@
                    <label for="DocumentType" class="form-label">Tipo Doc *</label>
                    <InputSelect id="DocumentType" @bind-Value="_model.DocumenttypeName" class="form-control">
                        <option value="">Seleccione...</option>
                        @foreach (var type in documentTypes)
                        {
                            <option value="@type.Code">@type.Description</option>
                        }
                    </InputSelect>
                    <ValidationMessage For="@(() => _model.DocumenttypeName)" />

                </div>
                <div class="col-md-4 mb-3">
                    <label class="form-label">Número *</label>
                    <InputText class="form-control" @bind-Value="_model.DocumentNumber" />
                    <ValidationMessage For="@(() => _model.DocumentNumber)" />
                </div>
                <div class="col-md-4 mb-3">
                    <label class="form-label">Matrícula</label>
                    <InputText class="form-control" @bind-Value="_model.License" />
                </div>
            </div>
            <div class="row">
                <div class="col-md-6 mb-3">
                    <label for="Type" class="form-label">Tipo de Profesional</label>
                    <InputSelect id="Type" class="form-select" @bind-Value="_model.Type">
                        @foreach (var option in professionalTypes)
                        {
                            <option value="@option.Value">@option.Text</option>
                        }
                    </InputSelect>
                    <ValidationMessage For="@(() => _model.Type)" />
                </div>
                <div class="col-md-6 mb-3">
                    <label for="Specialtyid" class="form-label">Especialidad</label>
                    <InputSelect id="Specialtyid" class="form-select" @bind-Value="_model.SpecialtyId">
                        <option value="">--- Seleccionar ---</option>
                        @foreach (var s in specialties)
                        {
                            <option value="@s.Id">@s.Name</option>
                        }
                    </InputSelect>
                    <ValidationMessage For="@(() => _model.SpecialtyId)" />
                </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 List<EDocumentType> documentTypes = new();
    private List<EProfessionalSpecialty> specialties = new();


    private EProfessional _model = new();

    protected override async Task OnInitializedAsync()
    {
        await LoadSpecialties();
        await LoadDocumentTypes();
    }
    private List<(string Value, string Text)> professionalTypes = new()
    {
        ("","--- Seleccionar ---"),
        ("Medico", "Médico"),
        ("Instrumentador", "Instrumentador quirúrgico"),
        ("Enfermero", "Enfermero/a"),
        ("Tecnico", "Técnico quirúrgico")
    };
    private async Task LoadDocumentTypes()
    {
        documentTypes = await documentTypeService.GetAllAsync();
    }

    private async Task LoadSpecialties()
    {
        specialties = await specialtyService.GetAllAsync();
    }

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

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

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