<?php
$assetBase =
    basename(rtrim(FCPATH, "\\/")) === "public" ? "assets/" : "public/assets/";
$isEdit = $mode === "edit";
$value = static function ($field, $default = "") use ($invoice) {
    return old($field, $invoice[$field] ?? $default);
};
$selectedClientId = (int) old("client_id", $invoice["client_id"] ?? 0);
$oldNames = old("item_name");
if (is_array($oldNames)) {
    $rows = [];
    foreach ($oldNames as $index => $name) {
        $rows[] = [
            "item_name" => $name,
            "description" => ((array) old("description"))[$index] ?? "",
            "quantity" => ((array) old("quantity"))[$index] ?? "1",
            "price" => ((array) old("price"))[$index] ?? "0",
        ];
    }
} else {
    $rows = $items;
}
if ($standalone && !$rows) {
    $rows = [
        [
            "item_name" => "",
            "description" => "",
            "quantity" => "1",
            "price" => "0",
        ],
    ];
}
?>
<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title><?= $isEdit
    ? "Edit Invoice"
    : "Create Invoice" ?> · BrandSquare Admin</title><link rel="preconnect" href="https://fonts.googleapis.com"><link rel="preconnect" href="https://fonts.gstatic.com" crossorigin><link href="https://fonts.googleapis.com/css2?family=DM+Sans:wght@400;500;600;700&family=Manrope:wght@600;700;800&display=swap" rel="stylesheet"><link rel="stylesheet" href="<?= base_url(
     $assetBase . "css/admin.css",
 ) ?>"></head><body class="admin-page">
<aside class="admin-sidebar"><a href="<?= base_url(
    "admin/leads",
) ?>" class="admin-logo">Brand<span>Square.</span></a><nav><a href="<?= base_url(
    "admin/leads",
) ?>"><i>◎</i> Leads</a><a href="<?= base_url(
    "admin/clients",
) ?>"><i>◉</i> Clients</a><a href="<?= base_url(
    "admin/quotations",
) ?>"><i>₹</i> Quotations</a><a class="active" href="<?= base_url(
    "admin/invoices",
) ?>"><i>▤</i> Invoices</a><a href="<?= base_url(
    "admin/industries",
) ?>"><i>◆</i> Industries</a><a href="<?= base_url(
    "admin/email-templates",
) ?>"><i>✦</i> Email templates</a><a href="<?= base_url(
    "admin/articles",
) ?>"><i>▤</i> Articles</a><a href="<?= base_url(
    "admin/settings",
) ?>"><i>⚙</i> Settings</a><a href="<?= base_url() ?>" target="_blank"><i>↗</i> View website</a></nav><div class="admin-user"><span><?= esc(
    strtoupper(substr((string) session("admin_name"), 0, 2)),
) ?></span><div><b><?= esc(session("admin_name")) ?></b><small><?= esc(
    session("admin_email"),
) ?></small></div></div><form action="<?= base_url(
    "admin/logout",
) ?>" method="post"><?= csrf_field() ?><button class="logout" type="submit">Sign out →</button></form></aside>
<main class="admin-main quotation-form-main"><header class="admin-heading"><div><a class="back-to-templates" href="<?= $isEdit
    ? base_url("admin/invoices/" . $invoice["id"])
    : base_url("admin/invoices") ?>">← Invoice register</a><h1><?= $isEdit
    ? "Edit invoice"
    : "Create invoice" ?></h1><p>Create from an accepted quotation or bill a client directly.</p></div></header>
<?php if (session("error")): ?><div class="alert error"><?= esc(
    session("error"),
) ?></div><?php endif; ?>
<?php if (!$quotation && !$standalone): ?>
<section class="invoice-source-panel"><div><span>CHOOSE INVOICE SOURCE</span><h2>How would you like to create this invoice?</h2><p>Use an accepted quotation or start a standalone invoice for any client.</p></div><a class="invoice-standalone-option" href="<?= base_url(
    "admin/invoices/new?standalone=1",
) ?>"><span>DIRECT BILLING</span><div><b>Create without a quotation</b><small>Select a client and enter invoice items manually.</small></div><i>→</i></a><div class="invoice-source-divider"><span>OR SELECT AN ACCEPTED QUOTATION</span></div><div class="invoice-source-list"><?php
if (
    !$acceptedQuotations
): ?><p class="empty-invoice-source">No accepted quotations are ready. Mark a quotation as accepted first.</p><?php endif;
foreach ($acceptedQuotations as $quote): ?><a href="<?= base_url(
    "admin/invoices/new?quotation_id=" . $quote["id"],
) ?>"><div><b><?= esc($quote["quotation_number"]) ?></b><span><?= esc(
    $quote["first_name"] . " " . $quote["last_name"],
) ?></span></div><strong>₹<?= number_format(
    (float) $quote["total"],
    2,
) ?></strong><i>→</i></a><?php endforeach;
?></div></section>
<?php else: ?>
<form method="post" class="quotation-form" data-quotation-form><?= csrf_field() ?><input type="hidden" name="quotation_id" value="<?= $quotation
    ? esc($quotation["id"])
    : "" ?>">
<section class="quotation-form-card"><div class="quotation-form-section-title"><span>01</span><div><h2>Invoice details</h2><p><?= $quotation
    ? "Based on " .
        esc($quotation["quotation_number"]) .
        " for " .
        esc($quotation["first_name"] . " " . $quotation["last_name"]) .
        "."
    : "Standalone invoice. Choose the client who will receive it." ?></p></div></div><div class="quotation-details-grid"><?php if (
    $quotation
): ?><label>Accepted quotation<input readonly value="<?= esc(
    $quotation["quotation_number"],
) ?>"></label><?php else: ?><label>Client<select name="client_id" required><option value="">Choose client</option><?php foreach (
    $clients
    as $client
): ?><option value="<?= esc($client["id"]) ?>" <?= $selectedClientId ===
(int) $client["id"]
    ? "selected"
    : "" ?>><?= esc(
    $client["first_name"] .
        " " .
        $client["last_name"] .
        " · " .
        $client["email"],
) ?></option><?php endforeach; ?></select></label><?php endif; ?><label>Invoice date<input type="date" name="invoice_date" required value="<?= esc(
    $value("invoice_date", date("Y-m-d")),
) ?>"></label><label>Due date<input type="date" name="due_date" value="<?= esc(
    $value("due_date", date("Y-m-d", strtotime("+7 days"))),
) ?>"></label><label>Status<select name="status"><?php foreach (
    $statuses
    as $status
): ?><option value="<?= esc($status) ?>" <?= $value("status", "draft") ===
$status
    ? "selected"
    : "" ?>><?= esc(
    ucfirst($status),
) ?></option><?php endforeach; ?></select></label></div></section>
<section class="quotation-form-card quotation-items-card"><div class="quotation-form-section-title"><span>02</span><div><h2>Invoice items</h2><p><?= $quotation
    ? "Copied from the accepted quotation. Adjust if required."
    : "Add the services or products being billed to the client." ?></p></div><button type="button" class="add-quotation-item" data-add-item>＋ Add item</button></div><div class="quotation-items-head"><span>Item</span><span>Description</span><span>Qty</span><span>Price</span><span>Amount</span><span></span></div><div class="quotation-items" data-items><?php foreach (
    $rows
    as $row
): ?><div class="quotation-item-row" data-item-row><label><span>Item</span><input name="item_name[]" required value="<?= esc(
    $row["item_name"],
) ?>"></label><label><span>Description</span><textarea name="description[]" rows="2"><?= esc(
    $row["description"],
) ?></textarea></label><label><span>Quantity</span><input name="quantity[]" data-quantity type="number" min=".01" step=".01" required value="<?= esc(
    $row["quantity"],
) ?>"></label><label><span>Price</span><input name="price[]" data-price type="number" min="0" step=".01" required value="<?= esc(
    $row["price"],
) ?>"></label><output data-line-total>₹0.00</output><button type="button" class="remove-quotation-item" data-remove-item>×</button></div><?php endforeach; ?></div><button type="button" class="add-item-mobile" data-add-item>＋ Add another item</button><div class="quotation-summary"><span>Total due</span><strong data-grand-total>₹0.00</strong><small>Calculated from all invoice items</small></div></section>
<section class="quotation-form-card"><div class="quotation-form-section-title"><span>03</span><div><h2>Payment notes</h2><p>Add bank details, payment terms, or client instructions.</p></div></div><label class="quotation-notes">Notes<textarea name="notes" rows="5" maxlength="3000"><?= esc(
    $value("notes"),
) ?></textarea></label></section><div class="quotation-form-actions"><a href="<?= $isEdit
    ? base_url("admin/invoices/" . $invoice["id"])
    : base_url(
        "admin/invoices",
    ) ?>">Cancel</a><button type="submit">Save invoice <span>→</span></button></div></form>
<template data-item-template><div class="quotation-item-row" data-item-row><label><span>Item</span><input name="item_name[]" required></label><label><span>Description</span><textarea name="description[]" rows="2"></textarea></label><label><span>Quantity</span><input name="quantity[]" data-quantity type="number" min=".01" step=".01" required value="1"></label><label><span>Price</span><input name="price[]" data-price type="number" min="0" step=".01" required value="0"></label><output data-line-total>₹0.00</output><button type="button" class="remove-quotation-item" data-remove-item>×</button></div></template><script src="<?= base_url(
    $assetBase . "js/admin-quotation.js",
) ?>"></script>
<?php endif; ?></main></body></html>
