@page "/sales/professionalform/"
@page "/sales/professionalform/{Id:int?}"
@using phronCare.UIBlazor.Services.Sales
@inject ProfessionalService ProfessionalService
@inject DocumentTypeService documentTypeService
@inject ProfessionalSpecialtyService specialtyService


@inject NavigationManager Navigation
@inject IToastService toastService

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

    <div class="card mt-4" style="zoom:80%">
        <div class="card-header d-flex justify-content-center align-items-center">
            <h3 class="card-title m-0">@((model.Id == 0) ? "Nuevo Profesional" : "Editar Profesional")</h3>
        </div>

        <div class="card-body">
            <div class="row">
                <div class="col-md-12 mb-3">
                    <label for="Fullname" class="form-label">Nombre Completo</label>
                    <InputText id="Fullname" class="form-control" @bind-Value="model.Fullname" />
                    <ValidationMessage For="@(() => model.Fullname)" />
                </div>
            </div>

            <div class="row">
                <div class="col-md-4 mb-3">
                    <label for="DocumentType" class="form-label">Tipo de documento:</label>
                    <InputSelect id="DocumentType" @bind-Value="model.DocumenttypeName" class="form-control">
                        <option value="">Seleccione un tipo</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 for="DocumentNumber" class="form-label">Número</label>
                    <InputText id="DocumentNumber" class="form-control" @bind-Value="model.DocumentNumber" />
                    <ValidationMessage For="@(() => model.DocumentNumber)" />
                </div>

                <div class="col-md-4 mb-3">
                    <label for="License" class="form-label">Matrícula</label>
                    <InputText id="License" class="form-control" @bind-Value="model.License" />
                </div>
            </div>

            <div class="row">
                <div class="col-md-6 mb-3">
                    <label for="Phone1" class="form-label">Teléfono Principal</label>
                    <InputText id="Phone1" class="form-control" @bind-Value="model.Phone1" />
                    <ValidationMessage For="@(() => model.Phone1)" />
                </div>

                <div class="col-md-6 mb-3">
                    <label for="Phone2" class="form-label">Teléfono Secundario</label>
                    <InputText id="Phone2" class="form-control" @bind-Value="model.Phone2" />
                    <ValidationMessage For="@(() => model.Phone2)" />
                </div>
            </div>

            <div class="row">
                <div class="col-md-4 mb-3">
                    <label for="Email" class="form-label">Email</label>
                    <InputText id="Email" type="email" class="form-control" @bind-Value="model.Email" />
                    <ValidationMessage For="@(() => model.Email)" />
                </div>

                <div class="col-md-3 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-4 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 class="col-md-1 mb-3 d-flex align-items-center justify-content-start mt-4">
                    <div class="form-check form-switch">
                        <InputCheckbox id="Isactive" @bind-Value="model.Active" class="form-check-input" />
                        <label class="form-check-label ms-2" for="Isactive">Activo</label>
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col-md-6 mb-3">
                    <label for="Address" class="form-label">Dirección</label>
                    <InputText id="Address" class="form-control" @bind-Value="model.Address" />
                    <ValidationMessage For="@(() => model.Address)" />
                </div>

                <div class="col-md-3 mb-3">
                    <label for="City" class="form-label">Ciudad</label>
                    <InputText id="City" class="form-control" @bind-Value="model.City" />
                    <ValidationMessage For="@(() => model.City)" />
                </div>

                <div class="col-md-3 mb-3">
                    <label for="Province" class="form-label">Provincia:</label>
                    <InputSelect id="Province" @bind-Value="model.Province" class="form-control">
                        <option value="">--- Seleccionar ---</option>
                        @foreach (var province in provinces)
                        {
                            <option value="@province">@province</option>
                        }
                    </InputSelect>
                    <ValidationMessage For="@(() => model.Province)" />
                </div>
            </div>
        </div>

        <div class="card-footer">
            <div class="d-flex justify-content-end align-items-center py-3">

                <button type="submit" class="btn btn-primary me-2">Guardar</button>
                <button type="button" class="btn btn-secondary" @onclick="Cancel">Cancelar</button>
            </div>
        </div>
    </div>
</EditForm>

@code {
    [Parameter]
    public int? Id { get; set; }

    private EProfessional model = new();
    private List<EDocumentType> documentTypes = new();
    private List<EProfessionalSpecialty> specialties = new();

    private List<string> provinces = new()
    {
        "Buenos Aires", "CABA", "Catamarca", "Chaco", "Chubut", "Córdoba", "Corrientes",
        "Entre Ríos", "Formosa", "Jujuy", "La Pampa", "La Rioja", "Mendoza", "Misiones",
        "Neuquén", "Río Negro", "Salta", "San Juan", "San Luis", "Santa Cruz",
        "Santa Fe", "Santiago del Estero", "Tierra del Fuego", "Tucumán"
    };
    private List<(string Value, string Text)> professionalTypes = new()
    {
        ("","--- Seleccionar ---"),
        ("Medico", "Médico"),
        ("Instrumentador", "Instrumentador quirúrgico"),
        ("Enfermero", "Enfermero/a"),
        ("Tecnico", "Técnico quirúrgico")
    };
    protected override async Task OnInitializedAsync()
    {
        await LoadDocumentTypes();
        await LoadSpecialties();


        if (Id.HasValue && Id > 0)
        {
            var result = await ProfessionalService.GetById(Id.Value);
            if (result != null)
                model = result;
            else
                toastService.ShowError("No se pudo cargar el profesional.");
        }
    }
    private async Task LoadDocumentTypes()
    {
        documentTypes = await documentTypeService.GetAllAsync();
    }
    private async Task LoadSpecialties()
    {
        specialties = await specialtyService.GetAllAsync();
    }
    private async Task HandleValidSubmit()
    {
        var result = model.Id == 0
            ? await ProfessionalService.CreateAsync(model)
            : await ProfessionalService.UpdateAsync(model);

        if (result.IsSuccessStatusCode)
        {
            toastService.ShowSuccess("Profesional guardado correctamente.");
            Navigation.NavigateTo("/sales/professionals");
        }
        else
        {
            var error = await result.Content.ReadAsStringAsync();
            toastService.ShowError($"Error: {error}");
        }
    }

    private void Cancel()
    {
        Navigation.NavigateTo("/sales/professionals");
    }
}