@{
/******************************************************************************
// 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-2020 Natalia Portillo
*******************************************************************************/
}

@page "/admin/companies/details/{Id:int}"
@page "/admin/companies/edit/{Id:int}"
@page "/admin/companies/create"
@using Marechai.Database
@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 Microsoft.AspNetCore.Identity.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 ReadOnly="!_editing" @bind-Text="@_model.Name">
                <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 TValue="bool" @bind-Checked="@_unknownLegalName">@L["Unknown (legal name)"]</Check>
            }
            @if (!_editing ||
                 !_unknownLegalName)
            {
                <Validation Validator="@ValidateLegalName">
                    <TextEdit Disabled="!_editing" @bind-Text="@_model.LegalName">
                        <Feedback>
                            <ValidationError>@L["Please enter a valid legal name."]</ValidationError>
                        </Feedback>
                    </TextEdit>
                </Validation>
            }
        </Field>
    }
    <Field>
        <FieldLabel>@L["Status"]</FieldLabel>
        <Select Disabled="!_editing" TValue="int" @bind-SelectedValue="@Status">
            @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 TValue="bool" @bind-Checked="@_unknownFounded">@L["Unknown (foundation date)"]</Check>
            }
            @if (!_editing || !_unknownFounded)
            {
                <Check TValue="bool" @bind-Checked="@_model.FoundedMonthIsUnknown" Disabled="!_editing">@L["Unknown foundation month"]</Check>
                <Check TValue="bool" @bind-Checked="@_model.FoundedDayIsUnknown" Disabled="_model.FoundedMonthIsUnknown || !_editing">@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 TValue="DateTime?" ReadOnly="!_editing" @bind-Date="@_model.Founded" >
                        <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 TValue="bool" @bind-Checked="@_unknownWebsite">@L["Unknown (website)"]</Check>
            }
            @if (!_editing ||
                 !_unknownWebsite)
            {
                <Validation Validator="@ValidateWebsite">
                    <TextEdit ReadOnly="!_editing" @bind-Text="@_model.Website">
                        <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 TValue="bool" @bind-Checked="@_unknownTwitter">@L["Unknown (twitter)"]</Check>
            }
            @if (!_editing ||
                 !_unknownTwitter)
            {
                <Validation Validator="@ValidateTwitter">
                    <TextEdit ReadOnly="!_editing" @bind-Text="@_model.Twitter">
                        <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 TValue="bool" @bind-Checked="@_unknownFacebook">@L["Unknown (facebook)"]</Check>
            }
            @if (!_editing ||
                 !_unknownFacebook)
            {
                <Validation Validator="@ValidateFacebook">
                    <TextEdit ReadOnly="!_editing" @bind-Text="@_model.Facebook">
                        <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 TValue="bool" @bind-Checked="@_unknownAddress">@L["Unknown (address)"]</Check>
            }
            @if (!_editing ||
                 !_unknownAddress)
            {
                <Validation Validator="@ValidateAddress">
                    <TextEdit ReadOnly="!_editing" @bind-Text="@_model.Address">
                        <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 TValue="bool" @bind-Checked="@_unknownCity">@L["Unknown (city)"]</Check>
            }
            @if (!_editing ||
                 !_unknownCity)
            {
                <Validation Validator="@ValidateCity">
                    <TextEdit ReadOnly="!_editing" @bind-Text="@_model.City">
                        <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 TValue="bool" @bind-Checked="@_unknownProvince">@L["Unknown (province)"]</Check>
            }
            @if (!_editing ||
                 !_unknownProvince)
            {
                <Validation Validator="@ValidateProvince">
                    <TextEdit ReadOnly="!_editing" @bind-Text="@_model.Province">
                        <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 TValue="bool" @bind-Checked="@_unknownPostalCode">@L["Unknown (postal code)"]</Check>
            }
            @if (!_editing ||
                 !_unknownPostalCode)
            {
                <Validation Validator="@ValidatePostalCode">
                    <TextEdit ReadOnly="!_editing" @bind-Text="@_model.PostalCode">
                        <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 TValue="bool" @bind-Checked="@_unknownCountry">@L["Unknown (country)"]</Check>
            }
            @if (!_editing ||
                 !_unknownCountry)
            {
                <Select Disabled="!_editing" TValue="short?" @bind-SelectedValue="@_model.CountryId">
                    @foreach (var 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 TValue="bool" @bind-Checked="@_unknownSold">@L["Unknown (sold/merged/bankrupt date)"]</Check>
                }
                @if (!_editing ||
                     !_unknownSold)
                {
                    <Check TValue="bool" @bind-Checked="@_model.SoldMonthIsUnknown" Disabled="!_editing">@L["Unknown sold/merge/bankruptcy month"]</Check>
                    <Check TValue="bool" @bind-Checked="@_model.SoldDayIsUnknown" Disabled="_model.SoldMonthIsUnknown || !_editing">@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 TValue="DateTime?" ReadOnly="!_editing" @bind-Date="@_model.Sold">
                            <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 TValue="bool" @bind-Checked="@_unknownSoldTo">@L["Unknown (sold to)"]</Check>
                }
                @if (!_editing ||
                     !_unknownSoldTo)
                {
                    <Select Disabled="!_editing" TValue="int?" @bind-SelectedValue="@_model.SoldToId">
                        @foreach (var company in _companies)
                        {
                            <SelectItem TValue="int?" Value="@company.Id">@company.Name</SelectItem>
                        }
                    </Select>
                }
            </Field>
        }
    }
</div>
<div>
    @if (!_editing)
    {
        <Button Color="Color.Primary" Clicked="@OnEditClicked">@L["Edit"]</Button>
    }
    else
    {
        <Button Color="Color.Success" Clicked="@OnSaveClicked">@L["Save"]</Button>
        <Button Color="Color.Danger" Clicked="@OnCancelClicked">@L["Cancel"]</Button>
    }
    <a href="/admin/companies" class="btn btn-secondary">@L["Back to list"]</a>
@if (!_editing)
{
    <Button Color="Color.Success" Clicked="@ShowUploadModal">@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 (var logo in _logos)
        {
            bool logoFound = File.Exists(Path.Combine(Host.WebRootPath, "assets/logos", logo.Guid + ".svg"));
            <tr>
                <td>
                    @if (logoFound)
                    {
                        <picture>
                            <source type="image/svg+xml" srcset="/assets/logos/@(logo.Guid).svg">
                            <source type="image/webp" srcset="/assets/logos/webp/1x/@(logo.Guid).webp, /assets/logos/webp/2x/@(logo.Guid).webp 2x, /assets/logos/webp/3x/@(logo.Guid).webp 3x">
                            <img srcset="/assets/logos/png/1x/@(logo.Guid).png, /assets/logos/png/2x/@(logo.Guid).png 2x, /assets/logos/png/3x/@(logo.Guid).png 3x"
                                 src="/assets/logos/png/1x/@(logo.Guid).png" alt="" height="auto" width="auto" style="max-height: 256px; max-width: 256px" />
                        </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 Color="Color.Success" Clicked="() => {ShowLogoYearModal(logo.Id);}">@L["Change year"]</Button>
                    }
                    <Button Color="Color.Danger" Clicked="() => {ShowDeleteModal(logo.Id);}">@L["Delete"]</Button>
                </td>
            </tr>
        }
        </tbody>
    </table>
}

<Modal @ref="_frmDelete" IsCentered="true" Closing="@DeleteModalClosing"> 
    <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 Color="Color.Primary" Clicked="@HideDeleteModal" Disabled="@_deleteInProgress">@L["Cancel"]</Button>
            <Button Color="Color.Danger" Clicked="@ConfirmDelete" Disabled="@_deleteInProgress">@L["Delete"]</Button>
        </ModalFooter>
    </ModalContent>
</Modal>

<Modal @ref="_frmLogoYear" IsCentered="true" Closing="@LogoYearModalClosing"> 
    <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 type="image/svg+xml" srcset="/assets/logos/@(_currentLogo.Guid).svg">
                        <source type="image/webp" srcset="/assets/logos/webp/1x/@(_currentLogo.Guid).webp, /assets/logos/webp/2x/@(_currentLogo.Guid).webp 2x, /assets/logos/webp/3x/@(_currentLogo.Guid).webp 3x">
                        <img srcset="/assets/logos/png/1x/@(_currentLogo.Guid).png, /assets/logos/png/2x/@(_currentLogo.Guid).png 2x, /assets/logos/png/3x/@(_currentLogo.Guid).png 3x" src="/assets/logos/png/1x/@(_currentLogo.Guid).png" alt="" height="auto" width="auto" style="max-height: 256px; max-width: 256px" />
                    </picture>
                </div>
                <Field>
                    <FieldLabel>@L["Year logo came in use"]</FieldLabel>
                    <Check TValue="bool" @bind-Checked="@_unknownLogoYear">@L["Unknown (logo year)"]</Check>
                    @if (!_unknownLogoYear)
                    {
                        <Validation Validator="@ValidateLogoYear">
                            <NumericEdit TValue="int?" Decimals="0" @bind-Value="@_currentLogoYear">
                                <Feedback>
                                    <ValidationError>@L["Please enter a valid year."]</ValidationError>
                                </Feedback>
                            </NumericEdit>
                        </Validation>
                    }
                </Field>
            }
        </ModalBody>
        <ModalFooter>
            <Button Color="Color.Primary" Clicked="@HideLogoYearModal" Disabled="@_yearChangeInProgress">@L["Cancel"]</Button>
            @if (_currentLogo != null)
            {
                <Button Color="Color.Success" Clicked="@ConfirmLogoYear" Disabled="@_yearChangeInProgress">@L["Save"]</Button>
            }
        </ModalFooter>
    </ModalContent>
</Modal>

<Modal @ref="_frmUpload" IsCentered="true" Closing="@UploadModalClosing"> 
    <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 type="file" @ref=_inputUpload Accept=".svg" /><br/>
                        <Button Color="Color.Success" Clicked="@UploadFile" Disabled="@_uploading">@L["Upload"]</Button><br/>
                    }
                    else
                    {
                        @L["Uploading..."]
                        <div class="progress">
                            <div class="progress-bar" role="progressbar" style="width: @(_progressValue)%;" aria-valuenow="@_progressValue" aria-valuemin="0" aria-valuemax="100">@($"{_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 src="@_uploadedSvgData" alt="" height="auto" width="auto" style="max-height: 256px; max-width: 256px" />
                            </td>                                
                            <td>
                                <img src="@_uploadedPngData" alt="" height="auto" width="auto" style="max-height: 256px; max-width: 256px" />
                            </td>                                
                            <td>
                                <img src="@_uploadedWebpData" alt="" height="auto" width="auto" style="max-height: 256px; max-width: 256px" />
                            </td>                                
                        </tr>
                        </tbody>
                    </table>
                    <Field>
                        <FieldLabel>@L["Year logo came in use"]</FieldLabel>
                        <Check TValue="bool" @bind-Checked="@_unknownLogoYear">@L["Unknown (logo year)"]</Check>
                        @if (!_unknownLogoYear)
                        {
                            <Validation Validator="@ValidateLogoYear">
                                <NumericEdit TValue="int?" Decimals="0" @bind-Value="@_currentLogoYear">
                                    <Feedback>
                                        <ValidationError>@L["Please enter a valid year."]</ValidationError>
                                    </Feedback>
                                </NumericEdit>
                            </Validation>
                        }
                    </Field>
                }
            </Field>
        </ModalBody>
        <ModalFooter>
            <Button Color="Color.Primary" Clicked="@HideUploadModal" Disabled="_uploading || _savingLogo">@L["Cancel"]</Button>
            <Button Color="Color.Success" Clicked="@ConfirmUpload" 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 Color="Color.Success" Clicked="@AddNewDescription">@L["Add new description"]</Button>
    }
    @if (!_readonlyDescription || _description != null)
    {
        if (_readonlyDescription)
        {
            <Button Color="Color.Success" Clicked="@EditDescription">@L["Edit"]</Button>
        }
        else
        {
            <Button Color="Color.Primary" Clicked="@CancelDescription">@L["Cancel"]</Button>
            <Button Color="Color.Success" Clicked="@SaveDescription">@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 onchange="OnCompanyMarkdownChanged()" class="form-control" rows="200" id="txtCompanyDescriptionMarkdown" readonly="@_readonlyDescription">@_description.Markdown</textarea>
                </TabPanel>
                <TabPanel Name="preview">
                    @((MarkupString)_description.Html)
                </TabPanel>
            </Content>
        </Tabs>
    }
</div>
