@page "/Settings"
@using Common.Dtos.Company
@using HushianWebApp.Components
@using HushianWebApp.Service
@using HushianWebApp.Services
@inject ILocalStorageService localStorageService;
@inject NavigationManager navigationManager;
@inject CompanyService companyService;
<style>
    .section-box {
    background-color: #f8f9fa;
    border-radius: 1rem;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    padding: 1.5rem;
    margin-bottom: 1rem;
    height: 100%;
    }

    .section-title {
    display: flex;
    align-items: center;
    font-size: 1.25rem;
    font-weight: 600;
    color: #343a40;
    margin-bottom: 1rem;
    border-right: 4px solid #ffc107;
    padding-right: 0.75rem;
    }

    .section-title i {
    margin-left: 0.5rem;
    color: #ffc107;
    }

    .row-fullheight {
    min-height: 100vh;
    display: flex;
    flex-wrap: wrap;
    }
</style>

<div class="container-fluid">
    @*   <div class="row row-fullheight"> *@
    <div class="row">
        <!-- تغییر کلمه عبور -->
        <div class="col-md-6 d-flex flex-column" style="height: fit-content;">
            <div class="section-box w-100">
                <div class="section-title">
                    <i class="bi bi-layout-text-window-reverse"></i> تغییر کلمه عبور
                </div>
                <ChangePassWordComponent/>
            </div>
        </div>

        @if (ALLOWcompanyinfo)
        {
            <!-- اطلاعات شرکت -->
            <div class="col-md-6 d-flex flex-column" style="height: fit-content;">
                <div class="section-box w-100">
                    <div class="section-title">
                        <i class="bi bi-gear-fill"></i> اطلاعات شرکت
                    </div>

                    <div class="form-group row mb-3" style="padding-left: 10em;">
                        <div class="col-md-6">
                            <Switch @bind-Value="dto.Available" Label="در دسترس" />
                        </div>
                        <div class="col-md-6">
                            <Switch @bind-Value="dto.allowBot" Label="پاسخگوی هوشمند" />
                        </div>
                    </div>

                    <div class="col-md-12" style="margin-top:15px">
                        <input dir="ltr" class="form-control text-center mb-2" @bind-value="@dto.FullName" type="text" placeholder="نام شرکت" />
                    </div>
                    <div class="col-md-12" style="margin-top:15px">
                        <input dir="ltr" class="form-control text-center mb-2" @bind-value="@dto.FullNameManager" type="text" placeholder="نام مدیر" />
                    </div>
                    <div class="col-md-12">
                        <input dir="ltr" class="form-control text-center mb-2" @bind-value="@dto.Email" type="text" placeholder="پست الکترونیک" />
                    </div>
                    <div class="col-md-12">
                        <input dir="ltr" class="form-control text-center mb-2" @bind-value="@dto.WebSite" type="text" placeholder="وب سایت" />
                    </div>

                    <div class="col-md-12 d-flex align-items-center mb-2">
                        <InputFile type="file" OnChange="OnFileChange" accept=".png" />
                        @if (dto.logo != null && dto.logo.Length > 0)
                        {
                            <Image src="@GetImageSource()" class="rounded mx-2" height="25" width="25" alt="Uploaded Image" />
                        }
                    </div>
                    <div class="d-grid gap-2">
                        <Button Loading=loading LoadingText="در حال ذخیره اطلاعات..." Color="ButtonColor.Warning"
                        @onclick="updateItem">
                            ویرایش
                        </Button>
                    </div>
                </div>
            </div>
        }


        @*      <!-- پایین چپ -->
        <div class="col-md-6 d-flex flex-column">
            <div class="section-box w-100">
                <div class="section-title">
                    <i class="bi bi-graph-up"></i> بخش پایین چپ
                </div>
                <!-- محتوای دلخواه -->
            </div>
        </div>
*@
        @if (ALLOWcompanyinfo)
        {
                   <!-- پایین راست -->
        <div class="col-md-6 d-flex flex-column">
            <div class="section-box w-100">
                <div class="section-title">
                    <i class="bi bi-chat-dots-fill"></i> درباره مجموعه:
                </div>
                <PromptManagerComponent />
            </div>
        </div> 
        }
 

    </div>
</div>

@code {
    [Inject] protected ToastService ToastService { get; set; } = default!;

    bool ALLOWcompanyinfo = false;
    public bool loading { get; set; } = false;
    public ReadANDUpdate_CompanyDto dto { get; set; }
        = new();
    int CompanyID = 0;
    protected override async Task OnInitializedAsync()
    {
        if (await localStorageService.GetItem<string>("C/Role") == "User")
            navigationManager.NavigateTo("/NotFound");

        ALLOWcompanyinfo =  await localStorageService.GetItem<string>("C/Role") == "Company";
        CompanyID=  await localStorageService.GetItem<int>("C/CompanyId");

        dto = await companyService.GetCompany(CompanyID);
        await base.OnInitializedAsync();
    }
    private async Task OnFileChange(InputFileChangeEventArgs e)
    {
        var file = e.File;
        using (var memoryStream = new MemoryStream())
        {
            await file.OpenReadStream().CopyToAsync(memoryStream);
            dto.logo = memoryStream.ToArray();
        }
    }
    private string GetImageSource()
    {
        if (dto.logo != null)
        {
            return $"data:image/jpeg;base64,{Convert.ToBase64String(dto.logo)}";
        }
        return string.Empty;
    }
    async Task updateItem()
    {
        if (await companyService.UpdateCompany(dto)) 
        ToastService.Notify(new ToastMessage(ToastType.Success, "تغییر اطلاعات شرکت با موفقیت انجام شد"));
    }
}