@page "/edit/{InvoiceId}"
@using QuestPDF
@using Server.Data
@using Server.Model
@rendermode InteractiveServer


<PageTitle>Home</PageTitle>



<h1>Rechnung #@Invoice.InvoiceId</h1>

<button type="button" class="btn btn-primary" @onclick="SaveInvoiceAsync"><i class="fas fa-save"></i> Speichern</button>
<button type="button" class="btn btn-info" @onclick="GenerateInvoiceAsync"><i class="fas fa-file-pdf"></i> Generieren</button>

@if (!string.IsNullOrWhiteSpace(AlertMessage))
{
  <div class="alert alert-danger mt-3">
    @AlertMessage
  </div>
}

<hr />

<div class="mb-3">
  <div class="form-check form-switch">
    <InputCheckbox @bind-Value="@Invoice.DeletionAllowed" class="form-check-input" id="deletionallowedcheck"></InputCheckbox>
    <label class="form-check-label" for="deletionallowedcheck">Löschen erlaubt</label>
  </div>
</div>

<div class="mb-3">
  <label class="form-label">Datum</label>
  <InputDate class="form-control" @bind-Value="@Invoice.IssueDate"></InputDate>
</div>

<hr />

@if (Invoice.Customer != null)
{
  <div class="card mb-2" style="width: 18rem;">
    <div class="card-body">
      <h5 class="card-title">@Invoice.Customer.Name</h5>
      <p class="card-text">
        @Invoice.Customer.Name2<br>
        @Invoice.Customer.Street<br>
        @Invoice.Customer.Zip - @Invoice.Customer.City<br>
        @Invoice.Customer.Phone
      </p>
    </div>
  </div>
}

<button class="btn btn-secondary" @onclick="SelectCustomerAsync">@(Invoice.Customer != null ? "Kunden ändern" : "Kunden auswählen")</button>

<hr />

<div class="mb-3">
  <label class="form-label" >Rechnungsnummer</label>
  <InputText class="form-control" @bind-Value="@Invoice.InvoiceId" disabled></InputText>
</div>

<div class="mb-3">
  <label class="form-label">Kommentar</label>
  <InputTextArea class="form-control" @bind-Value="@Invoice.Comment"></InputTextArea>
</div>

<hr />

<button type="button" class="btn btn-sm btn-primary" @onclick="CreateItemAsync">Neuer Artikel</button>

@foreach (var item in Invoice.Items)
{
  <hr />
  
  <div class="card" style="width: 100%; max-width: 1000px;">
    <div class="card-body">
      <div class="row pt-1">
        <div class="col-xl">
          <label class="form-label mb-0">Name</label>
          <InputText class="form-control" @bind-Value="@item.Name"></InputText>
        </div>
        <div class="col-sm">
          <label class="form-label mb-0">Menge</label>
          <InputNumber TValue="double" class="form-control" @bind-Value="@item.Quantity"></InputNumber>
        </div>
        <div class="col-sm">
          <label class="form-label mb-0">Price @(SettingsData.Instance.Kleinunternehmer ? string.Empty : "(Netto)")</label>
          <InputNumber TValue="double" class="form-control" @bind-Value="@item.PriceNetto"></InputNumber>
        </div>
        @if (!SettingsData.Instance.Kleinunternehmer)
        {
          <div class="col-sm">
            <label class="form-label mb-0">Tax Type</label>
            <InputSelect @bind-Value="@item.TaxType" class="form-select">
              @foreach (var taxType in Enum.GetValues(typeof(TaxType)))
              {
                <option value="@taxType">@taxType</option>
              }
            </InputSelect>
          </div>
        }
      </div>

      <div class="mb-1 mt-2">
        <label class="form-label mb-0">Description</label>
        <InputText class="form-control" @bind-Value="@item.Description"></InputText>
      </div>
    </div>
  </div>
}

<ModalProvider />