@{
    /******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Author(s)      : Natalia Portillo <claunia@claunia.com>
//
// --[ License ] --------------------------------------------------------------
//
//     This program is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as
//     published by the Free Software Foundation, either version 3 of the
//     License, or (at your option) any later version.
//
//     This program is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
//
//     You should have received a copy of the GNU General Public License
//     along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2003-2026 Natalia Portillo
*******************************************************************************/
}

@page "/admin/companies/details/{Id:int}"
@page "/admin/companies/edit/{Id:int}"
@page "/admin/companies/create"
@using Marechai.Data
@using Marechai.Data.Dtos
@using Marechai.Database.Models
@inherits OwningComponentBase<CompaniesService>
@inject IStringLocalizer<CompaniesService> L
@inject Iso31661NumericService             CountriesService
@inject NavigationManager                  NavigationManager
@inject CompanyLogosService                CompanyLogosService
@inject IWebHostEnvironment                Host
@inject IFileReaderService                 FileReaderService;
@inject IJSRuntime                         JSRuntime
@inject UserManager<ApplicationUser>       UserManager
@inject AuthenticationStateProvider        AuthenticationStateProvider
@attribute [Authorize(Roles = "UberAdmin, Admin")]
<h3>@L["Company details"]</h3>
<hr/>
@if(!_loaded)
{
    <p align="center">@L["Loading..."]</p>

    return;
}
<div>
    <Field>
        <FieldLabel>@L["Common name (as usually displayed publicly)"]</FieldLabel>
        <Validation Validator="@ValidateName">
            <TextEdit @bind-Text="@_model.Name" ReadOnly="!_editing">
                <Feedback>
                    <ValidationError>@L["Please enter a valid name."]</ValidationError>
                </Feedback>
            </TextEdit>
        </Validation>
    </Field>
    @if(_editing || _model.LegalName != null)
    {
        <Field>
            <FieldLabel>@L["Legal name (as shown in governmental registries including \"Inc.\", \"Corp.\", \"gmbH\", etc...)"]</FieldLabel>
            @if(_editing)
            {
                <Check @bind-Checked="@_unknownLegalName" @TValue="bool">@L["Unknown (legal name)"]</Check>
            }
            @if(!_editing || !_unknownLegalName)
            {
                <Validation Validator="@ValidateLegalName">
                    <TextEdit @bind-Text="@_model.LegalName" Disabled="!_editing">
                        <Feedback>
                            <ValidationError>@L["Please enter a valid legal name."]</ValidationError>
                        </Feedback>
                    </TextEdit>
                </Validation>
            }
        </Field>
    }
    <Field>
        <FieldLabel>@L["Status"]</FieldLabel>
        <Select @bind-SelectedValue="@Status" Disabled="!_editing" @TValue="int">
            @foreach(int status in Enum.GetValues(typeof(CompanyStatus)))
            {
                <SelectItem @TValue="int" Value="@status">@(((CompanyStatus)status).ToString())</SelectItem>
            }
        </Select>
    </Field>
    @if(_editing || _model.Founded != null)
    {
        <Field>
            <FieldLabel>@L["Founded"]</FieldLabel>
            @if(_editing)
            {
                <Check @bind-Checked="@_unknownFounded" @TValue="bool">@L["Unknown (foundation date)"]</Check>
            }
            @if(!_editing || !_unknownFounded)
            {
                <Check @bind-Checked="@_model.FoundedMonthIsUnknown" Disabled="!_editing"
                       @TValue="bool">@L["Unknown foundation month"]</Check>

                <Check @bind-Checked="@_model.FoundedDayIsUnknown" Disabled="_model.FoundedMonthIsUnknown || !_editing"
                       @TValue="bool">@L["Unknown foundation day"]</Check>

                @L["If the foundation day or month are selected as unknown, pick anyone in the field below, it will be ignored."]

                <Validation Validator="@ValidateFounded">
                    <DateEdit @bind-Date="@_model.Founded" ReadOnly="!_editing" @TValue="DateTime?">
                        <Feedback>
                            <ValidationError>@L["Please enter a valid foundation date."]</ValidationError>
                        </Feedback>
                    </DateEdit>
                </Validation>
            }
        </Field>
    }
    @if(_editing || _model.Website != null)
    {
        <Field>
            <FieldLabel>@L["Website"]</FieldLabel>
            @if(_editing)
            {
                <Check @bind-Checked="@_unknownWebsite" @TValue="bool">@L["Unknown (website)"]</Check>
            }
            @if(!_editing || !_unknownWebsite)
            {
                <Validation Validator="@ValidateWebsite">
                    <TextEdit @bind-Text="@_model.Website" ReadOnly="!_editing">
                        <Feedback>
                            <ValidationError>@L["Please enter a valid website."]</ValidationError>
                        </Feedback>
                    </TextEdit>
                </Validation>
            }
        </Field>
    }
    @if(_editing || _model.Twitter != null)
    {
        <Field>
            <FieldLabel>@L["Twitter"]</FieldLabel>
            @if(_editing)
            {
                <Check @bind-Checked="@_unknownTwitter" @TValue="bool">@L["Unknown (twitter)"]</Check>
            }
            @if(!_editing || !_unknownTwitter)
            {
                <Validation Validator="@ValidateTwitter">
                    <TextEdit @bind-Text="@_model.Twitter" ReadOnly="!_editing">
                        <Feedback>
                            <ValidationError>@L["Please enter a valid Twitter handle."]</ValidationError>
                        </Feedback>
                    </TextEdit>
                </Validation>
            }
        </Field>
    }
    @if(_editing || _model.Facebook != null)
    {
        <Field>
            <FieldLabel>@L["Facebook"]</FieldLabel>
            @if(_editing)
            {
                <Check @bind-Checked="@_unknownFacebook" @TValue="bool">@L["Unknown (facebook)"]</Check>
            }
            @if(!_editing || !_unknownFacebook)
            {
                <Validation Validator="@ValidateFacebook">
                    <TextEdit @bind-Text="@_model.Facebook" ReadOnly="!_editing">
                        <Feedback>
                            <ValidationError>@L["Please enter a valid Facebook user name."]</ValidationError>
                        </Feedback>
                    </TextEdit>
                </Validation>
            }
        </Field>
    }
    @if(_editing || _model.Address != null)
    {
        <Field>
            <FieldLabel>@L["Address"]</FieldLabel>
            @if(_editing)
            {
                <Check @bind-Checked="@_unknownAddress" @TValue="bool">@L["Unknown (address)"]</Check>
            }
            @if(!_editing || !_unknownAddress)
            {
                <Validation Validator="@ValidateAddress">
                    <TextEdit @bind-Text="@_model.Address" ReadOnly="!_editing">
                        <Feedback>
                            <ValidationError>@L["Please enter a valid address."]</ValidationError>
                        </Feedback>
                    </TextEdit>
                </Validation>
            }
        </Field>
    }
    @if(_editing || _model.City != null)
    {
        <Field>
            <FieldLabel>@L["City"]</FieldLabel>
            @if(_editing)
            {
                <Check @bind-Checked="@_unknownCity" @TValue="bool">@L["Unknown (city)"]</Check>
            }
            @if(!_editing || !_unknownCity)
            {
                <Validation Validator="@ValidateCity">
                    <TextEdit @bind-Text="@_model.City" ReadOnly="!_editing">
                        <Feedback>
                            <ValidationError>@L["Please enter a valid city."]</ValidationError>
                        </Feedback>
                    </TextEdit>
                </Validation>
            }
        </Field>
    }
    @if(_editing || _model.Province != null)
    {
        <Field>
            <FieldLabel>@L["Province"]</FieldLabel>
            @if(_editing)
            {
                <Check @bind-Checked="@_unknownProvince" @TValue="bool">@L["Unknown (province)"]</Check>
            }
            @if(!_editing || !_unknownProvince)
            {
                <Validation Validator="@ValidateProvince">
                    <TextEdit @bind-Text="@_model.Province" ReadOnly="!_editing">
                        <Feedback>
                            <ValidationError>@L["Please enter a valid province."]</ValidationError>
                        </Feedback>
                    </TextEdit>
                </Validation>
            }
        </Field>
    }
    @if(_editing || _model.PostalCode != null)
    {
        <Field>
            <FieldLabel>@L["Postal code"]</FieldLabel>
            @if(_editing)
            {
                <Check @bind-Checked="@_unknownPostalCode" @TValue="bool">@L["Unknown (postal code)"]</Check>
            }
            @if(!_editing || !_unknownPostalCode)
            {
                <Validation Validator="@ValidatePostalCode">
                    <TextEdit @bind-Text="@_model.PostalCode" ReadOnly="!_editing">
                        <Feedback>
                            <ValidationError>@L["Please enter a valid postal code."]</ValidationError>
                        </Feedback>
                    </TextEdit>
                </Validation>
            }
        </Field>
    }
    @if(_editing || _model.CountryId != null)
    {
        <Field>
            <FieldLabel>@L["Country"]</FieldLabel>
            @if(_editing)
            {
                <Check @bind-Checked="@_unknownCountry" @TValue="bool">@L["Unknown (country)"]</Check>
            }
            @if(!_editing || !_unknownCountry)
            {
                <Select @bind-SelectedValue="@_model.CountryId" Disabled="!_editing" @TValue="short?">
                    @foreach(Iso31661Numeric country in _countries)
                    {
                        <SelectItem @TValue="short?" Value="@country.Id">@country.Name</SelectItem>
                    }
                </Select>
            }
        </Field>
    }
    @if((int)_model.Status > 1)
    {
        @if(_editing || _model.Sold != null)
        {
            <Field>
                <FieldLabel>@L["Sold"]</FieldLabel>
                @if(_editing)
                {
                    <Check @bind-Checked="@_unknownSold"
                           @TValue="bool">@L["Unknown (sold/merged/bankrupt date)"]</Check>
                }
                @if(!_editing || !_unknownSold)
                {
                    <Check @bind-Checked="@_model.SoldMonthIsUnknown" Disabled="!_editing"
                           @TValue="bool">@L["Unknown sold/merge/bankruptcy month"]</Check>

                    <Check @bind-Checked="@_model.SoldDayIsUnknown" Disabled="_model.SoldMonthIsUnknown || !_editing"
                           @TValue="bool">@L["Unknown sold/merge/bankruptcy day"]</Check>

                    @L["If the sold, merge or bankruptcy day or month are selected as unknown, pick anyone in the field below, it will be ignored."]

                    <Validation Validator="@ValidateSold">
                        <DateEdit @bind-Date="@_model.Sold" ReadOnly="!_editing" @TValue="DateTime?">
                            <Feedback>
                                <ValidationError>@L["Please enter a valid sold/merge/bankruptcy date."]</ValidationError>
                            </Feedback>
                        </DateEdit>
                    </Validation>
                }
            </Field>
        }

        @if(_editing || _model.SoldToId != null)
        {
            <Field>
                <FieldLabel>@L["Sold to"]</FieldLabel>
                @if(_editing)
                {
                    <Check @bind-Checked="@_unknownSoldTo" @TValue="bool">@L["Unknown (sold to)"]</Check>
                }
                @if(!_editing || !_unknownSoldTo)
                {
                    <Select @bind-SelectedValue="@_model.SoldToId" Disabled="!_editing" @TValue="int?">
                        @foreach(CompanyDto company in _companies)
                        {
                            <SelectItem @TValue="int?" Value="@company.Id">@company.Name</SelectItem>
                        }
                    </Select>
                }
            </Field>
        }
    }
</div>
<div>
    @if(!_editing)
    {
        <Button Clicked="@OnEditClicked" Color="Color.Primary">@L["Edit"]</Button>
    }
    else
    {
        <Button Clicked="@OnSaveClicked" Color="Color.Success">@L["Save"]</Button>

        <Button Clicked="@OnCancelClicked" Color="Color.Danger">@L["Cancel"]</Button>
    }
    <a class="btn btn-secondary" href="/admin/companies">@L["Back to list"]</a>
    @if(!_editing)
    {
        <Button Clicked="@ShowUploadModal" Color="Color.Success">@L["Upload new logo"]</Button>
    }
</div>
@if(_logos.Count > 0)
{
    <h4>@L["Logos"]</h4>
    <table class="table table-striped">
        <thead>
        <tr>
            <th>
                @L["Logo"]
            </th>
            <th>
                @L["Year logo came in use"]
            </th>
            <th></th>
        </tr>
        </thead>
        <tbody>
        @foreach(CompanyLogo logo in _logos)
        {
            bool logoFound = File.Exists(Path.Combine(Host.WebRootPath, "assets/logos", logo.Guid + ".svg"));
            <tr>
                <td>
                    @if(logoFound)
                    {
                        <picture>
                            <source srcset="/assets/logos/@(logo.Guid).svg" type="image/svg+xml">
                            <source
                                srcset="/assets/logos/webp/1x/@(logo.Guid).webp, /assets/logos/webp/2x/@(logo.Guid).webp 2x, /assets/logos/webp/3x/@(logo.Guid).webp 3x"
                                type="image/webp">
                            <img alt="" height="auto" src="/assets/logos/png/1x/@(logo.Guid).png"
                                 srcset="/assets/logos/png/1x/@(logo.Guid).png, /assets/logos/png/2x/@(logo.Guid).png 2x, /assets/logos/png/3x/@(logo.Guid).png 3x"
                                 style="max-height: 256px; max-width: 256px" width="auto"/>
                        </picture>
                    }
                    else
                    {
                        <strong>@L["Cannot find logo file"]</strong>
                    }
                </td>
                <td>
                    @if(logo.Year.HasValue)
                    {
                        @logo.Year
                    }
                    else
                    {
                        @L["Unknown (logo year)"]
                    }
                </td>
                <td>
                    @if(logoFound)
                    {
                        <Button Clicked="() => {ShowLogoYearModal(logo.Id);}"
                                Color="Color.Success">@L["Change year"]</Button>
                    }
                    <Button Clicked="() => {ShowDeleteModal(logo.Id);}" Color="Color.Danger">@L["Delete"]</Button>
                </td>
            </tr>
        }
        </tbody>
    </table>
}
<Modal Closing="@DeleteModalClosing" IsCentered="true" ref="_frmDelete">
    <ModalBackdrop/>
    <ModalContent Centered="true">
        <ModalHeader>
            <ModalTitle>@L["Delete logo"]</ModalTitle>
            <CloseButton Clicked="@HideDeleteModal"/>
        </ModalHeader>
        <ModalBody>
            @if(_currentLogo?.Year != null)
            {
                <Text>@string.Format(L["Are you sure you want to delete the company logo introduced in {0}?"], _currentLogo?.Year)</Text>
            }
            else
            {
                <Text>@L["Are you sure you want to delete the company logo you selected?"]</Text>
            }
        </ModalBody>
        <ModalFooter>
            <Button Clicked="@HideDeleteModal" Color="Color.Primary" Disabled="@_deleteInProgress">@L["Cancel"]</Button>
            <Button Clicked="@ConfirmDelete" Color="Color.Danger" Disabled="@_deleteInProgress">@L["Delete"]</Button>
        </ModalFooter>
    </ModalContent>
</Modal>
<Modal Closing="@LogoYearModalClosing" IsCentered="true" ref="_frmLogoYear">
    <ModalBackdrop/>
    <ModalContent Centered="true">
        <ModalHeader>
            <ModalTitle>@L["Change logo year"]</ModalTitle>
            <CloseButton Clicked="@HideLogoYearModal"/>
        </ModalHeader>
        <ModalBody>
            @if(_currentLogo != null)
            {
                <div class="text-center">
                    <picture>
                        <source srcset="/assets/logos/@(_currentLogo.Guid).svg" type="image/svg+xml">
                        <source
                            srcset="/assets/logos/webp/1x/@(_currentLogo.Guid).webp, /assets/logos/webp/2x/@(_currentLogo.Guid).webp 2x, /assets/logos/webp/3x/@(_currentLogo.Guid).webp 3x"
                            type="image/webp">
                        <img alt="" height="auto" src="/assets/logos/png/1x/@(_currentLogo.Guid).png"
                             srcset="/assets/logos/png/1x/@(_currentLogo.Guid).png, /assets/logos/png/2x/@(_currentLogo.Guid).png 2x, /assets/logos/png/3x/@(_currentLogo.Guid).png 3x"
                             style="max-height: 256px; max-width: 256px" width="auto"/>
                    </picture>
                </div>

                <Field>
                    <FieldLabel>@L["Year logo came in use"]</FieldLabel>
                    <Check @bind-Checked="@_unknownLogoYear" @TValue="bool">@L["Unknown (logo year)"]</Check>
                    @if(!_unknownLogoYear)
                    {
                        <Validation Validator="@ValidateLogoYear">
                            <NumericEdit @bind-Value="@_currentLogoYear" Decimals="0" @TValue="int?">
                                <Feedback>
                                    <ValidationError>@L["Please enter a valid year."]</ValidationError>
                                </Feedback>
                            </NumericEdit>
                        </Validation>
                    }
                </Field>
            }
        </ModalBody>
        <ModalFooter>
            <Button Clicked="@HideLogoYearModal" Color="Color.Primary"
                    Disabled="@_yearChangeInProgress">@L["Cancel"]</Button>
            @if(_currentLogo != null)
            {
                <Button Clicked="@ConfirmLogoYear" Color="Color.Success"
                        Disabled="@_yearChangeInProgress">@L["Save"]</Button>
            }
        </ModalFooter>
    </ModalContent>
</Modal>
<Modal Closing="@UploadModalClosing" IsCentered="true" ref="_frmUpload">
    <ModalBackdrop/>
    <ModalContent Centered="true" Size="ModalSize.ExtraLarge">
        <ModalHeader>
            <ModalTitle>@L["Upload new logo"]</ModalTitle>
            <CloseButton Clicked="@HideUploadModal"/>
        </ModalHeader>
        <ModalBody>
            <Field>
                @if(!_uploaded)
                {
                    @if(!_uploading)
                    {
                        <FieldLabel>@L["Choose SVG (version 1.1 or lower) logo file"]</FieldLabel>

                        <input Accept=".svg" @ref=_inputUpload type="file"/>
                        <br/>

                        <Button Clicked="@UploadFile" Color="Color.Success" Disabled="@_uploading">@L["Upload"]</Button>

                        <br/>
                    }
                    else
                    {
                        @L["Uploading..."]
                        <div class="progress">
                            <div aria-valuemax="100" aria-valuemin="0" aria-valuenow="@_progressValue"
                                 class="progress-bar" role="progressbar"
                                 style="width: @(_progressValue)%;">@($"{_progressValue:F2}")%
                            </div>
                        </div>
                    }

                    @if(_uploadError)
                    {
                        <span class="text-danger">@_uploadErrorMessage</span>
                    }
                }
                else
                {
                    <table class="table table-dark">
                        <thead>
                        <tr>
                            <th>@L["SVG"]</th>
                            <th>@L["PNG"]</th>
                            <th>@L["WebP"]</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr>
                            <td>
                                <span class="text-warning">@L["May not show properly"]</span>
                            </td>
                            <td></td>
                            <td>
                                <span class="text-warning">@L["May not show properly"]</span>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <img alt="" height="auto" src="@_uploadedSvgData"
                                     style="max-height: 256px; max-width: 256px" width="auto"/>
                            </td>
                            <td>
                                <img alt="" height="auto" src="@_uploadedPngData"
                                     style="max-height: 256px; max-width: 256px" width="auto"/>
                            </td>
                            <td>
                                <img alt="" height="auto" src="@_uploadedWebpData"
                                     style="max-height: 256px; max-width: 256px" width="auto"/>
                            </td>
                        </tr>
                        </tbody>
                    </table>

                    <Field>
                        <FieldLabel>@L["Year logo came in use"]</FieldLabel>
                        <Check @bind-Checked="@_unknownLogoYear" @TValue="bool">@L["Unknown (logo year)"]</Check>
                        @if(!_unknownLogoYear)
                        {
                            <Validation Validator="@ValidateLogoYear">
                                <NumericEdit @bind-Value="@_currentLogoYear" Decimals="0" @TValue="int?">
                                    <Feedback>
                                        <ValidationError>@L["Please enter a valid year."]</ValidationError>
                                    </Feedback>
                                </NumericEdit>
                            </Validation>
                        }
                    </Field>
                }
            </Field>
        </ModalBody>
        <ModalFooter>
            <Button Clicked="@HideUploadModal" Color="Color.Primary"
                    Disabled="_uploading || _savingLogo">@L["Cancel"]</Button>
            <Button Clicked="@ConfirmUpload" Color="Color.Success"
                    Disabled="!_uploaded || _savingLogo">@L["Save"]</Button>
        </ModalFooter>
    </ModalContent>
</Modal>
<hr/>
<div class="container">
    <h4>@L["Company description"]</h4>
    <hr/>
    @if(_readonlyDescription && _description is null)
    {
        <Button Clicked="@AddNewDescription" Color="Color.Success">@L["Add new description"]</Button>
    }
    @if(!_readonlyDescription || _description != null)
    {
        if(_readonlyDescription)
        {
            <Button Clicked="@EditDescription" Color="Color.Success">@L["Edit"]</Button>
        }
        else
        {
            <Button Clicked="@CancelDescription" Color="Color.Primary">@L["Cancel"]</Button>

            <Button Clicked="@SaveDescription" Color="Color.Success">@L["Save"]</Button>
        }

        <Tabs SelectedTab="@_selectedDescriptionTab" SelectedTabChanged="@OnSelectedDescriptionTabChanged">
            <Items>
                <Tab Name="markdown">Markdown</Tab>
                <Tab Name="preview">Preview</Tab>
            </Items>
            <Content>
                <TabPanel Name="markdown">
                    <textarea class="form-control" id="txtCompanyDescriptionMarkdown"
                              onchange="OnCompanyMarkdownChanged()" readonly="@_readonlyDescription"
                              rows="200">@_description.Markdown</textarea>
                </TabPanel>
                <TabPanel Name="preview">
                    @((MarkupString)_description.Html)
                </TabPanel>
            </Content>
        </Tabs>
    }
</div>