@{
    /******************************************************************************
// 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/magazines/details/{Id:long}"
@page "/admin/magazines/edit/{Id:long}"
@page "/admin/magazines/create"
@using Marechai.Data.Dtos
@using Marechai.Database.Models
@inherits OwningComponentBase<MagazinesService>
@inject IStringLocalizer<MagazinesService> L
@inject Iso31661NumericService             CountriesService
@inject DocumentCompaniesService           CompaniesService
@inject DocumentRolesService               DocumentRolesService
@inject CompaniesByMagazineService         CompaniesByMagazineService
@inject NavigationManager                  NavigationManager
@inject IWebHostEnvironment                Host
@inject IJSRuntime                         JSRuntime
@inject UserManager<ApplicationUser>       UserManager
@inject AuthenticationStateProvider        AuthenticationStateProvider
@attribute [Authorize(Roles = "UberAdmin, Admin")]
<h3>@L["Magazine details"]</h3>
<hr/>
@if(!_loaded)
{
    <p align="center">@L["Loading..."]</p>

    return;
}
<div>
    <Field>
        <FieldLabel>@L["Title using latin script"]</FieldLabel>
        <Validation Validator="@ValidateTitle">
            <TextEdit @bind-Text="@_model.Title" ReadOnly="!_editing">
                <Feedback>
                    <ValidationError>@L["Please enter a valid title."]</ValidationError>
                </Feedback>
            </TextEdit>
        </Validation>
    </Field>
    @if(_editing || _model.NativeTitle != null)
    {
        <Field>
            <FieldLabel>@L["Native title, that is, title using native script (cyrillic, chinese, etc)"]</FieldLabel>
            @if(_editing)
            {
                <Check @bind-Checked="@_unknownNativeTitle" @TValue="bool">@L["Unknown (native title)"]</Check>
            }
            @if(!_editing || !_unknownNativeTitle)
            {
                <Validation Validator="@ValidateNativeTitle">
                    <TextEdit @bind-Text="@_model.NativeTitle" Disabled="!_editing">
                        <Feedback>
                            <ValidationError>@L["Please enter a valid native title."]</ValidationError>
                        </Feedback>
                    </TextEdit>
                </Validation>
            }
        </Field>
    }
    @if(_editing || _model.FirstPublication != null)
    {
        <Field>
            <FieldLabel>@L["First publication"]</FieldLabel>
            @if(_editing)
            {
                <Check @bind-Checked="@_unknownFirstPublication"
                       @TValue="bool">@L["Unknown (first publication date)"]</Check>
            }
            @if(!_editing || !_unknownFirstPublication)
            {
                <Validation Validator="@ValidateFirstPublication">
                    <DateEdit @bind-Date="@_model.FirstPublication" ReadOnly="!_editing" @TValue="DateTime?">
                        <Feedback>
                            <ValidationError>@L["Please enter a valid first publication date."]</ValidationError>
                        </Feedback>
                    </DateEdit>
                </Validation>
            }
        </Field>
    }
    @if(_editing || _model.CountryId != null)
    {
        <Field>
            <FieldLabel>@L["Country of publication"]</FieldLabel>
            @if(_editing)
            {
                <Check @bind-Checked="@_unknownCountry" @TValue="bool">@L["Unknown (country of publication)"]</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(_editing || _model.Issn != null)
    {
        <Field>
            <FieldLabel>@L["ISSN"]</FieldLabel>
            @if(_editing)
            {
                <Check @bind-Checked="@_unknownIssn" @TValue="bool">@L["Unknown (ISSN)"]</Check>
            }
            @if(!_editing || !_unknownIssn)
            {
                <Validation Validator="@ValidateIssn">
                    <TextEdit @bind-Text="@_model.Issn" ReadOnly="!_editing">
                        <Feedback>
                            <ValidationError>@L["Please enter a valid ISSN."]</ValidationError>
                        </Feedback>
                    </TextEdit>
                </Validation>
            }
        </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/magazines">@L["Back to list"]</a>
</div>
@if(!_editing)
{
    <hr/>
    <h3>@L["Companies involved in this magazine"]</h3>

    <Button Clicked="OnAddCompanyClick" Color="Color.Success" Disabled="_addingCompany">@L["Add new (company)"]</Button>

    @if(_addingCompany)
    {
        <div>
            <Field>
                <FieldLabel>@L["Company"]</FieldLabel>
                <Select @bind-SelectedValue="@_addingCompanyId" Disabled="_savingCompany" @TValue="int?">
                    @foreach(DocumentCompanyDto company in _companies)
                    {
                        <SelectItem @TValue="int?" Value="@company.Id">@company.Name</SelectItem>
                    }
                </Select>
            </Field>
            <Field>
                <FieldLabel>@L["Role"]</FieldLabel>
                <Select @bind-SelectedValue="@_addingCompanyRoleId" Disabled="!_editing" @TValue="string">
                    @foreach(DocumentRoleDto role in _roles)
                    {
                        <SelectItem @TValue="string" Value="@role.Id">@role.Name</SelectItem>
                    }
                </Select>
            </Field>
            <Button Clicked="@CancelAddCompany" Color="Color.Primary" Disabled="@_savingCompany">@L["Cancel"]</Button>
            <Button Clicked="@ConfirmAddCompany" Color="Color.Success" Disabled="@_savingCompany">@L["Add"]</Button>
        </div>
    }

    @if(_magazineCompanies?.Count > 0)
    {
        <div>
            <table class="table table-striped">
                <thead>
                <tr>
                    <th>
                        @L["Company"]
                    </th>
                    <th>
                        @L["Role"]
                    </th>
                    <th></th>
                </tr>
                </thead>
                <tbody>
                @foreach(CompanyByMagazineDto item in _magazineCompanies)
                {
                    <tr>
                        <td>
                            @item.Company
                        </td>
                        <td>
                            @item.Role
                        </td>
                        <td>
                            <Button Clicked="() => {ShowCompanyDeleteModal(item.Id);}" Color="Color.Danger"
                                    Disabled="@_addingCompany">@L["Delete"]</Button>
                        </td>
                    </tr>
                }
                </tbody>
            </table>
        </div>
    }

    <Modal Closing="@ModalClosing" IsCentered="true" ref="_frmDelete">
        <ModalBackdrop/>
        <ModalContent Centered="true">
            <ModalHeader>
                <ModalTitle>@_deleteTitle</ModalTitle>
                <CloseButton Clicked="@HideModal"/>
            </ModalHeader>
            <ModalBody>
                <Text>@_deleteText</Text>
            </ModalBody>
            <ModalFooter>
                <Button Clicked="@HideModal" Color="Color.Primary" Disabled="@_deleteInProgress">@L["Cancel"]</Button>
                <Button Clicked="@ConfirmDelete" Color="Color.Danger"
                        Disabled="@_deleteInProgress">@L["Delete"]</Button>
            </ModalFooter>
        </ModalContent>
    </Modal>
}