@page "/dt_work_force_edit/{id:guid}"
@inject ReAct_PME.WebUI.ServicesUI.ApiService ApiService
@inject ReAct_PME.WebUI.ServicesUI.AuthService AuthService
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject NavigationManager Navigation
@inject IToastService ToastService
@inject Blazored.LocalStorage.ILocalStorageService localStorage
@inject HttpClient Http
@using Blazored.Toast.Configuration;
@using Blazored.Toast.Services;
@using Microsoft.AspNetCore.Components.Authorization;



<h3 class="text-center my-4">Edition d'un élément de DT_WORK_FORCE</h3>

@if (item == null)
{
    <p>Chargement...</p>
}
else
{
    <div class="container">
        <form>
	
			<div class="form-group row">
				<label for="ID_WORK_FORCE" class="col-sm-3 col-form-label">ID_WORK_FORCE* : </label>
				<div class="col-sm-9">
					<input id="ID_WORK_FORCE" @bind="item.ID_WORK_FORCE" class="form-control"  disabled/>
				</div>
			</div>
			<div class="form-group row">
				<label for="TRIGRAMME" class="col-sm-3 col-form-label">TRIGRAMME* : </label>
				<div class="col-sm-9">
					<input id="TRIGRAMME" @bind="item.TRIGRAMME" class="form-control" maxlength="3" />
				</div>
			</div>
			<div class="form-group row">
				<label for="LAST_NAME" class="col-sm-3 col-form-label">LAST_NAME* : </label>
				<div class="col-sm-9">
					<textarea id="LAST_NAME" @bind="item.LAST_NAME" class="form-control" rows="3" maxlength="255" />
				</div>
			</div>
			<div class="form-group row">
				<label for="FIRST_NAME" class="col-sm-3 col-form-label">FIRST_NAME* : </label>
				<div class="col-sm-9">
					<textarea id="FIRST_NAME" @bind="item.FIRST_NAME" class="form-control" rows="3" maxlength="255" />
				</div>
			</div>
			<div class="form-group row">
				<label for="EMAIL" class="col-sm-3 col-form-label">EMAIL* : </label>
				<div class="col-sm-9">
					<textarea id="EMAIL" @bind="item.EMAIL" class="form-control" rows="3" maxlength="255" />
				</div>
			</div>
			<div class="form-group row">
				<label for="IsManager" class="col-sm-3 col-form-label">IsManager* : </label>
				<div class="col-sm-9">
					<input id="IsManager" @bind="item.IsManager" type ="checkbox"  />
				</div>
			</div>
			<div class="form-group row">
				<label for="USERNAME" class="col-sm-3 col-form-label">USERNAME* : </label>
				<div class="col-sm-9">
					<textarea id="USERNAME" @bind="item.USERNAME" class="form-control" rows="3" maxlength="255" />
				</div>
			</div>
			<div class="form-group row">
				<label for="MAIL_Serveur_IMAP" class="col-sm-3 col-form-label">MAIL_Serveur_IMAP : </label>
				<div class="col-sm-9">
					<textarea id="MAIL_Serveur_IMAP" @bind="item.MAIL_Serveur_IMAP" class="form-control" rows="3" maxlength="255" />
				</div>
			</div>
			<div class="form-group row">
				<label for="MAIL_Serveur_IMAP_PORT" class="col-sm-3 col-form-label">MAIL_Serveur_IMAP_PORT : </label>
				<div class="col-sm-9">
					<input id="MAIL_Serveur_IMAP_PORT" @bind="item.MAIL_Serveur_IMAP_PORT" type ="number" class="form-control"  />
				</div>
			</div>
			<div class="form-group row">
				<label for="MAIL_Serveur_SMTP" class="col-sm-3 col-form-label">MAIL_Serveur_SMTP : </label>
				<div class="col-sm-9">
					<textarea id="MAIL_Serveur_SMTP" @bind="item.MAIL_Serveur_SMTP" class="form-control" rows="3" maxlength="255" />
				</div>
			</div>
			<div class="form-group row">
				<label for="MAIL_Serveur_SMTP_PORT" class="col-sm-3 col-form-label">MAIL_Serveur_SMTP_PORT : </label>
				<div class="col-sm-9">
					<input id="MAIL_Serveur_SMTP_PORT" @bind="item.MAIL_Serveur_SMTP_PORT" type ="number" class="form-control"  />
				</div>
			</div>
			<div class="form-group row">
				<label for="MAIL_IS_OK" class="col-sm-3 col-form-label">MAIL_IS_OK : </label>
				<div class="col-sm-9">
					<input id="MAIL_IS_OK" @bind="item.MAIL_IS_OK" type ="checkbox"  />
				</div>
			</div>
			<div class="form-group row">
				<label for="MAIL_ROLE_SIGNATURE" class="col-sm-3 col-form-label">MAIL_ROLE_SIGNATURE : </label>
				<div class="col-sm-9">
					<textarea id="MAIL_ROLE_SIGNATURE" @bind="item.MAIL_ROLE_SIGNATURE" class="form-control" rows="3" maxlength="255" />
				</div>
			</div>
			<div class="form-group row">
				<label for="MAIL_ENTREPRISE_SIGNATURE" class="col-sm-3 col-form-label">MAIL_ENTREPRISE_SIGNATURE : </label>
				<div class="col-sm-9">
					<textarea id="MAIL_ENTREPRISE_SIGNATURE" @bind="item.MAIL_ENTREPRISE_SIGNATURE" class="form-control" rows="3" maxlength="255" />
				</div>
			</div>
			<div class="form-group row">
				<label for="MAIL_DelayRefresh" class="col-sm-3 col-form-label">MAIL_DelayRefresh : </label>
				<div class="col-sm-9">
					<input id="MAIL_DelayRefresh" @bind="item.MAIL_DelayRefresh" type ="number" class="form-control"  />
				</div>
			</div>
			<div class="form-group row">
				<label for="MAIL_DelaySentRefresh" class="col-sm-3 col-form-label">MAIL_DelaySentRefresh : </label>
				<div class="col-sm-9">
					<input id="MAIL_DelaySentRefresh" @bind="item.MAIL_DelaySentRefresh" type ="number" class="form-control"  />
				</div>
			</div>
			<div class="form-group row">
				<label for="MAIL_DelaySentRecup" class="col-sm-3 col-form-label">MAIL_DelaySentRecup : </label>
				<div class="col-sm-9">
					<input id="MAIL_DelaySentRecup" @bind="item.MAIL_DelaySentRecup" type ="number" class="form-control"  />
				</div>
			</div>
			<div class="form-group row">
				<label for="MAIL_OverdueDaysSent" class="col-sm-3 col-form-label">MAIL_OverdueDaysSent : </label>
				<div class="col-sm-9">
					<input id="MAIL_OverdueDaysSent" @bind="item.MAIL_OverdueDaysSent" type ="number" class="form-control"  />
				</div>
			</div>
			<div class="form-group row">
				<label for="SystemDateCreated" class="col-sm-3 col-form-label">SystemDateCreated* : </label>
				<div class="col-sm-9">
					<InputDate id="SystemDateCreated" class="form-control" @bind-Value="item.SystemDateCreated" disabled/>
				</div>
			</div>
			<div class="form-group row">
				<label for="SystemDateUpdated" class="col-sm-3 col-form-label">SystemDateUpdated* : </label>
				<div class="col-sm-9">
					<InputDate id="SystemDateUpdated" class="form-control" @bind-Value="item.SystemDateUpdated" disabled/>
				</div>
			</div>
			<div class="form-group row">
				<label for="IsEnabled" class="col-sm-3 col-form-label">IsEnabled* : </label>
				<div class="col-sm-9">
					<input id="IsEnabled" @bind="item.IsEnabled" type ="checkbox"  />
				</div>
			</div>


            <br />
            <div class="form-group">
                <div class="button-container justify-content-end">
                    <button type="button" class="btn btn-action btn-primary btn-compact3" @onclick="UpdateItem"><i class="bi bi-save"></i>Enregistrer</button>
                    
                    @if (IsAuthorized)
                    {

                        <button type="button" class="btn btn-warning me-2" @onclick="UpdatePasswordItem">Modifier le mot de passe</button>
                    
                    }

                    <button type="button" class="btn btn-action btn-secondary btn-compact" @onclick="Cancel"><i class="bi bi-arrow-left"></i>Annuler</button>
                </div>
            </div>
        </form>
    </div>
}

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

	private Domain.DT_WORK_FORCE_DTO? item;


	private bool IsAuthorized = false;

    #region Rendre les zones extensibles
    [Inject] IJSRuntime? JS { get; set; }

    #endregion

    protected override async Task OnInitializedAsync()
    {
        #region Vérification si utilisateur est connecté et s''il a les droits

        var tok = await PagesInitializer.VerifConnexionAndRules(AuthenticationStateProvider, Navigation, Http, ToastService, "api/DT_WORK_FORCE_DTO");
        if (tok == null || tok.Length == 0)
            return;
        else
            AuthService.SetToken(tok);

        #endregion

		#region Chargement des listes
		// Si pas de sécurité ou profil Admin --> on montre tout
		// Sinon selon la customisation de Repository

		#endregion


		item = await ApiService.GetByIdAsync<Domain.DT_WORK_FORCE_DTO>($"api/DT_WORK_FORCE_DTO/{Id}");
		var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
		var user = authState.User;
		IsAuthorized = user.IsInRole("Admin");

    }

    private async Task UpdateItem()
    {
        if (item != null)
        {

            try
            {
                bool b = await ApiService.UpdateAsync($"api/DT_WORK_FORCE_DTO/{Id}", item);
                if(b)
                {
                    ToastService.ShowSuccess("Modification réussie !", settings => { settings.Timeout = 2000; settings.IconType = IconType.None; settings.Position = ToastPosition.BottomCenter; });
                    await Task.Delay(2000);
                    Navigation.NavigateTo("/dt_work_force_list", forceLoad: true);
                }
                else
                {
                    ToastService.ShowError($"Erreur : Modification impossible.", settings => { settings.Timeout = 2000; settings.IconType = IconType.None; settings.Position = ToastPosition.BottomCenter; });
                }
            }
            catch(Exception ex)
            {
                ToastService.ShowError($"Erreur : {ex.Message}", settings => { settings.Timeout = 2000; settings.IconType = IconType.None; settings.Position = ToastPosition.BottomCenter; });
            }
        }
    }



    private void UpdatePasswordItem()
    {
	    Navigation.NavigateTo($"/dt_work_force_edit_password/{Id}");
    }


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

