@using Common.Dtos.Group
@using HushianWebApp.Service
@inject GroupService groupService;
<div class="row" style="height: fit-content; padding: 1rem;">


    <div class="col-md-12">

        <input dir="ltr" style="text-align:center;margin-bottom:10px" @bind-value="@model.Name" type="text" class="form-control" placeholder="نام گروه" />
    </div>
    <div class="col-md-12">

        <input dir="ltr" style="text-align:center;margin-bottom:10px" @bind-value="@model.Info" type="text" class="form-control" placeholder="توضیحات" />
    </div>
    <div class="col-md-12" style="display: flex;flex-wrap: nowrap;align-items: baseline;">
        <InputFile type="file" OnChange="OnFileChange" accept=".png" style="margin-bottom:10px" />


        @if (model.img != null && model.img.Length > 0)
        {
            <Image src="@GetImageSource()" Class="rounded mx-auto d-block" height="25" width="25" alt="Uploaded Image" />
        }

    </div>
    <Button Loading=loading LoadingText="در حال ذخیره اطلاعات..." Color="ButtonColor.Warning" @onclick="NewItem"> ویرایش </Button>


</div>

@code {
    [Parameter] public Update_GroupDto model { get; set; }
    [Parameter] public EventCallback OnMultipleOfThree { get; set; }
    public bool loading { get; set; } = false;
}
@functions {
    async Task NewItem()
    {
        if (!string.IsNullOrEmpty(model.Name))
        {
            loading = true;
            if (await groupService.UpdateGroup(model))
            {
                loading = false;
                await OnMultipleOfThree.InvokeAsync();
            }
            loading = false;
        }
    }
    private async Task OnFileChange(InputFileChangeEventArgs e)
    {
        var file = e.File;
        using (var memoryStream = new MemoryStream())
        {
            await file.OpenReadStream().CopyToAsync(memoryStream);
            model.img = memoryStream.ToArray();
        }
    }
    private string GetImageSource()
    {
        if (model.img != null)
        {
            return $"data:image/jpeg;base64,{Convert.ToBase64String(model.img)}";
        }
        return string.Empty;
    }
}