<?php
$assetBase =
    basename(rtrim(FCPATH, "\\/")) === "public" ? "assets/" : "public/assets/";
$clientName = $quotation["first_name"] . " " . $quotation["last_name"];
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?= esc(
        $quotation["quotation_number"],
    ) ?> · 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 class="active" href="<?= base_url(
    "admin/quotations",
) ?>"><i>₹</i> Quotations</a><a 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-view-main">
        <header class="admin-heading"><div><a class="back-to-templates" href="<?= base_url(
            "admin/quotations",
        ) ?>">← Quotation register</a><h1>Quotation details</h1><p>Review the client, itemised costs, and document status.</p></div><div class="quotation-actions">
            <a href="<?= base_url(
                "admin/quotations/" . $quotation["id"] . "/download",
            ) ?>">Download PDF <span>↓</span></a>
            <form method="post" action="<?= base_url(
                "admin/quotations/" . $quotation["id"] . "/email-pdf",
            ) ?>" onsubmit="return confirm('Email this quotation PDF to <?= esc(
    addslashes($quotation["email"]),
) ?>?')"><?= csrf_field() ?><button type="submit">Email PDF <span>↗</span></button></form>
            <form method="post" action="<?= base_url(
                "admin/quotations/" . $quotation["id"] . "/email",
            ) ?>" onsubmit="return confirm('Email this quotation to <?= esc(
    addslashes($quotation["email"]),
) ?> without an attachment?')"><?= csrf_field() ?><button type="submit">Email quotation <span>↗</span></button></form>
            <?php if (
                $linkedInvoice
            ): ?><a class="invoice-from-quote-action" href="<?= base_url(
    "admin/invoices/" . $linkedInvoice["id"],
) ?>">View invoice <span>→</span></a><?php elseif (
                $quotation["status"] === "accepted"
            ): ?><a class="invoice-from-quote-action" href="<?= base_url(
    "admin/invoices/new?quotation_id=" . $quotation["id"],
) ?>">Create invoice <span>＋</span></a><?php endif; ?>
            <a class="edit-quotation-action" href="<?= base_url(
                "admin/quotations/" . $quotation["id"] . "/edit",
            ) ?>">Edit <span>→</span></a>
        </div></header>

        <?php if (session("error")): ?><div class="alert error"><?= esc(
    session("error"),
) ?></div><?php endif; ?>
        <?php if (session("success")): ?><div class="alert success"><?= esc(
    session("success"),
) ?></div><?php endif; ?>

        <article class="quotation-document">
            <header>
                <a class="quotation-brand" href="<?= base_url() ?>">Brand<span>Square.</span></a>
                <div><span>QUOTATION</span><h2><?= esc(
                    $quotation["quotation_number"],
                ) ?></h2><b class="quote-status status-<?= esc(
    $quotation["status"],
) ?>"><?= esc(ucfirst($quotation["status"])) ?></b></div>
            </header>
            <section class="quotation-parties">
                <div><span>Prepared for</span><h3><?= esc(
                    $clientName,
                ) ?></h3><a href="mailto:<?= esc(
    $quotation["email"],
) ?>"><?= esc($quotation["email"]) ?></a><a href="tel:<?= esc(
    preg_replace("/[^0-9+]/", "", $quotation["mobile"]),
) ?>"><?= esc($quotation["mobile"]) ?></a><p><?= nl2br(
    esc($quotation["address"]),
) ?></p></div>
                <dl>
                    <div><dt>Quotation date</dt><dd><?= date(
                        "d M Y",
                        strtotime($quotation["quotation_date"]),
                    ) ?></dd></div>
                    <div><dt>Valid until</dt><dd><?= $quotation["valid_until"]
                        ? date("d M Y", strtotime($quotation["valid_until"]))
                        : "No expiry" ?></dd></div>
                </dl>
            </section>
            <div class="quotation-document-table-wrap">
                <table class="quotation-document-table">
                    <thead><tr><th>Item</th><th>Description</th><th>Quantity</th><th>Price</th><th>Amount</th></tr></thead>
                    <tbody><?php foreach (
                        $items
                        as $item
                    ): ?><tr><td><b><?= esc(
    $item["item_name"],
) ?></b></td><td><?= nl2br(
    esc($item["description"]),
) ?></td><td><?= number_format(
    (float) $item["quantity"],
    2,
) ?></td><td>₹<?= number_format(
    (float) $item["price"],
    2,
) ?></td><td>₹<?= number_format(
    (float) $item["line_total"],
    2,
) ?></td></tr><?php endforeach; ?></tbody>
                </table>
            </div>
            <section class="quotation-document-total"><span>Total cost</span><strong>₹<?= number_format(
                (float) $quotation["total"],
                2,
            ) ?></strong></section>
            <?php if (
                $quotation["notes"]
            ): ?><section class="quotation-document-notes"><span>Notes & terms</span><p><?= nl2br(
    esc($quotation["notes"]),
) ?></p></section><?php endif; ?>
            <footer><span>Created <?= date(
                "d M Y, h:i A",
                strtotime($quotation["created_at"]),
            ) ?></span><form method="post" action="<?= base_url(
    "admin/quotations/" . $quotation["id"] . "/delete",
) ?>" onsubmit="return confirm('Permanently delete this quotation? This cannot be undone.')"><?= csrf_field() ?><button type="submit">Delete quotation</button></form></footer>
        </article>
    </main>
</body>
</html>
