<?php
session_start();
require_once dirname(__DIR__) . '/includes/functions.php';
require_once dirname(__DIR__) . '/includes/sales_modules.php';

billing_require_auth();
billing_ensure_sales_module_support();

$user = billing_current_user();
$organization = billing_current_organization();
if (!$user) {
    billing_redirect('signin.php');
}
if (!$organization || empty($organization['onboarding_completed_at'])) {
    billing_redirect('onboarding.php?step=1');
}

$moduleKey = isset($salesModule) ? (string) $salesModule : 'quotes';
$module = billing_sales_module($moduleKey);
if (!$module) {
    billing_redirect('dashboard.php?tab=dashboard');
}
$isQuoteModule = $moduleKey === 'quotes';

$stats = billing_dashboard_stats((int) $user['id']);
$organizationName = (string) ($organization['organization_name'] ?? 'bala');
$userLabel = (string) ($user['email'] ?? 'billing-user');
$billingTopbarOrganizationName = $organizationName;
$billingTopbarUserLabel = $userLabel;
$billingActiveNav = (string) $module['nav'];

$getFn = (string) $module['get_fn'];
$saveFn = (string) $module['save_fn'];
$recordId = (int) ($_GET['id'] ?? 0);
$record = $recordId > 0 ? $getFn((int) $user['id'], (int) $organization['id'], $recordId) : null;
if ($recordId > 0 && !$record) {
    billing_flash('success', 'Record not found.');
    billing_redirect((string) $module['list_path']);
}

function billing_sales_form_address_lines(?array $snapshot, string $key): array
{
    $segment = is_array($snapshot[$key] ?? null) ? $snapshot[$key] : [];
    $lines = [];
    foreach (['attention', 'address_1', 'address_2', 'city', 'state', 'pin', 'country', 'phone'] as $field) {
        $value = trim((string) ($segment[$field] ?? ''));
        if ($value !== '') {
            $lines[] = $field === 'phone' ? 'Phone: ' . $value : $value;
        }
    }
    return $lines;
}

$customers = billing_sales_customer_options((int) $user['id'], (int) $organization['id']);
$items = billing_active_products((int) $user['id'], (int) $organization['id']);
$itemByName = [];
foreach ($items as $itemOption) {
    $itemByName[strtolower((string) $itemOption['item_name'])] = $itemOption;
}

$today = date('Y-m-d');
$defaultDate = $today;
$defaultSecondaryDate = $module['secondary_date_field'] !== '' ? date('Y-m-d', strtotime('+7 days')) : '';
$form = [
    'customer_id' => 0,
    'customer_name' => '',
    $module['number_field'] => billing_next_document_number((int) $user['id'], (int) $organization['id'], (string) $module['table'], (string) $module['number_field'], (string) $module['number_prefix']),
    $module['reference_field'] => '',
    $module['date_field'] => $defaultDate,
    'salesperson_name' => '',
    'subject' => '',
    'payment_terms' => 'Due on Receipt',
    'currency_code' => 'INR',
    'discount_amount' => 0,
    'adjustment_amount' => 0,
    'tax_total' => 0,
    $module['notes_field'] => $moduleKey === 'invoices' ? 'Thanks for your business.' : ($isQuoteModule ? 'Looking forward for your business.' : ''),
    $module['terms_field'] => '',
    $module['template_field'] => (string) $module['default_template'],
    'status_label' => $moduleKey === 'credit_notes' ? 'Open' : 'Draft',
    'items' => [
        ['item_id' => null, 'item_name' => '', 'item_description' => '', 'quantity' => 1, 'unit_name' => '', 'rate' => 0, 'account_name' => 'Sales'],
    ],
];

if ($module['secondary_date_field'] !== '') {
    $form[$module['secondary_date_field']] = $defaultSecondaryDate;
}
if ($module['extra_field'] !== '') {
    $form[$module['extra_field']] = '';
}

if ($record) {
    $form = array_merge($form, $record);
    $form['items'] = $record['items'] ?? $form['items'];
}

$errors = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $form['customer_id'] = (int) ($_POST['customer_id'] ?? 0);
    $form['customer_name'] = billing_clean((string) ($_POST['customer_name'] ?? ''));
    $form[$module['number_field']] = billing_clean((string) ($_POST[$module['number_field']] ?? ''));
    if ($module['reference_field'] !== '') {
        $form[$module['reference_field']] = billing_clean((string) ($_POST[$module['reference_field']] ?? ''));
    }
    $form[$module['date_field']] = billing_clean((string) ($_POST[$module['date_field']] ?? ''));
    if ($module['secondary_date_field'] !== '') {
        $form[$module['secondary_date_field']] = billing_clean((string) ($_POST[$module['secondary_date_field']] ?? ''));
    }
    if ($module['extra_field'] !== '') {
        $form[$module['extra_field']] = billing_clean((string) ($_POST[$module['extra_field']] ?? ''));
    }
    $form['salesperson_name'] = billing_clean((string) ($_POST['salesperson_name'] ?? ''));
    $form['subject'] = billing_clean((string) ($_POST['subject'] ?? ''));
    $form['payment_terms'] = billing_clean((string) ($_POST['payment_terms'] ?? ($form['payment_terms'] ?? 'Due on Receipt')));
    $form['discount_amount'] = (float) ($_POST['discount_amount'] ?? 0);
    $form['adjustment_amount'] = (float) ($_POST['adjustment_amount'] ?? 0);
    $form['tax_total'] = (float) ($_POST['tax_total'] ?? 0);
    $form[$module['notes_field']] = billing_clean((string) ($_POST[$module['notes_field']] ?? ''));
    $form[$module['terms_field']] = billing_clean((string) ($_POST[$module['terms_field']] ?? ''));
    $form[$module['template_field']] = billing_clean((string) ($_POST[$module['template_field']] ?? $module['default_template']));
    $form['status_label'] = billing_clean((string) ($_POST['status_label'] ?? $form['status_label']));
    if ($moduleKey === 'invoices') {
        $invoiceAction = billing_clean((string) ($_POST['invoice_action'] ?? 'save_draft'));
        $form['status_label'] = $invoiceAction === 'save_send' ? 'Sent' : 'Draft';
    } elseif ($moduleKey === 'quotes') {
        $quoteAction = billing_clean((string) ($_POST['quote_action'] ?? 'save_draft'));
        $form['status_label'] = $quoteAction === 'save_send' ? 'Sent' : 'Draft';
    }

    $itemNames = $_POST['item_name'] ?? [];
    $itemDescriptions = $_POST['item_description'] ?? [];
    $quantities = $_POST['quantity'] ?? [];
    $units = $_POST['unit_name'] ?? [];
    $rates = $_POST['rate'] ?? [];
    $itemRows = [];
    foreach ($itemNames as $index => $itemName) {
        $lookup = $itemByName[strtolower(billing_clean((string) $itemName))] ?? null;
        $itemRows[] = [
            'item_id' => $lookup ? (int) $lookup['id'] : null,
            'item_name' => billing_clean((string) $itemName),
            'item_description' => billing_clean((string) ($itemDescriptions[$index] ?? '')),
            'quantity' => (float) ($quantities[$index] ?? 0),
            'unit_name' => billing_clean((string) ($units[$index] ?? ($lookup['unit_name'] ?? ''))),
            'rate' => (float) ($rates[$index] ?? ($lookup['unit_price'] ?? 0)),
            'account_name' => 'Sales',
        ];
    }
    $form['items'] = $itemRows;

    if ($form['customer_name'] === '') {
        $errors[] = 'Select a customer.';
    }
    if ($form[$module['number_field']] === '') {
        $errors[] = 'Enter a document number.';
    }
    if (billing_sales_normalize_items($form['items']) === []) {
        $errors[] = 'Add at least one valid item row.';
    }

    if ($errors === []) {
        $savedId = $saveFn((int) $user['id'], (int) $organization['id'], $form, $recordId > 0 ? $recordId : null);
        if ($savedId) {
            billing_flash('success', $module['new_title'] . ' saved successfully.');
            billing_redirect((string) $module['list_path'] . '?id=' . $savedId);
        }
        $errors[] = 'Unable to save the record right now.';
    }
}

$lineItems = $form['items'] ?? [];
if ($lineItems === []) {
    $lineItems[] = ['item_id' => null, 'item_name' => '', 'item_description' => '', 'quantity' => 1, 'unit_name' => '', 'rate' => 0, 'account_name' => 'Sales'];
}
$totals = billing_sales_calculate_totals(billing_sales_normalize_items($lineItems), (float) ($form['discount_amount'] ?? 0), (float) ($form['adjustment_amount'] ?? 0), (float) ($form['tax_total'] ?? 0));
$formCustomerSnapshot = !empty($form['customer_id']) ? billing_sales_customer_snapshot((int) $user['id'], (int) $organization['id'], (int) $form['customer_id']) : null;
$itemQuantityTotal = 0;
foreach (billing_sales_normalize_items($lineItems) as $normalizedItem) {
    $itemQuantityTotal += (float) ($normalizedItem['quantity'] ?? 0);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<base href="<?php echo billing_escape(billing_base_href()); ?>">
<title><?php echo billing_escape((string) $module['new_title']); ?> | Zoho Billing</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Manrope:wght@400;500;600;700;800&display=swap">
<link rel="stylesheet" href="Payslip_Project/assets/fontawesome/css/all.min.css">
<link rel="stylesheet" href="assets/billing.css?v=20260504-sales-1">
<style>
.sales-document-page--quotes .customer-form-page {
    padding-bottom: 0;
}

.sales-form-card {
    display: grid;
    gap: 0;
    padding: 0;
    overflow: hidden;
}

.sales-form-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 18px;
    padding: 22px 28px 0;
}

.sales-form-title h1 {
    margin: 0;
    font-size: 24px;
}

.sales-form-header-actions {
    display: flex;
    align-items: center;
    gap: 18px;
}

.sales-form-header-actions a {
    color: #737b8b;
    font-size: 22px;
    text-decoration: none;
}

.sales-quote-tabs {
    display: flex;
    align-items: center;
    gap: 34px;
    padding: 0 28px;
    min-height: 72px;
    border-bottom: 1px solid #e6ebf2;
}

.sales-quote-tabs a {
    color: #46546a;
    font-size: 17px;
    text-decoration: none;
    padding: 22px 0 20px;
    border-bottom: 4px solid transparent;
}

.sales-quote-tabs a.is-active {
    color: #111827;
    font-weight: 800;
    border-bottom-color: #4a8af4;
}

.sales-form-body {
    display: grid;
    gap: 0;
}

.sales-section {
    padding: 30px 26px;
    border-top: 1px solid #edf2f7;
}

.sales-section--soft {
    background: #fbfcff;
}

.sales-form-grid {
    display: grid;
    grid-template-columns: 180px minmax(0, 1fr);
    gap: 16px 44px;
    align-items: start;
}

.sales-form-grid > label,
.sales-field-label {
    color: #101828;
    font-size: 15px;
    font-weight: 500;
    padding-top: 12px;
}

.sales-form-grid > label.is-required,
.sales-field-label.is-required {
    color: #ff2d20;
}

.sales-form-control,
.sales-form-select,
.sales-form-combo,
.sales-amount-box,
.sales-adjust-box {
    width: 100%;
    min-height: 44px;
    border: 1px solid #ccd6e7;
    border-radius: 8px;
    background: #fff;
    padding: 10px 12px;
    font: inherit;
    color: #0f172a;
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.sales-form-control:focus,
.sales-form-select:focus,
.sales-form-combo:focus,
.sales-amount-box:focus,
.sales-adjust-box:focus {
    outline: none;
    border-color: #60a5fa;
    box-shadow: 0 0 0 2px rgba(96, 165, 250, 0.18);
}

textarea.sales-form-control {
    min-height: 68px;
    resize: vertical;
}

.sales-customer-shell,
.sales-field-stack,
.sales-upload-card {
    display: grid;
    gap: 10px;
}

.sales-field-stack--spaced {
    gap: 16px;
}

.sales-customer-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 0;
    align-items: stretch;
}

.sales-customer-select-wrap {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 42px;
    align-items: stretch;
    max-width: 680px;
}

.sales-customer-select-wrap .sales-form-control {
    border-right: 0;
    border-radius: 12px 0 0 12px;
}

.sales-search-btn {
    border: 0;
    border-radius: 0 8px 8px 0;
    background: #27bc72;
    color: #fff;
    font-size: 20px;
    cursor: pointer;
}

.sales-form-inline-two {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 28px 36px;
}

.sales-form-split {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 28px 36px;
}

.sales-inline-grid--triple {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 280px 220px;
    gap: 18px;
}

.sales-form-pair {
    display: grid;
    grid-template-columns: 180px minmax(0, 1fr);
    gap: 16px;
    align-items: start;
}

.sales-form-pair .sales-field-label {
    padding-top: 12px;
}

.sales-form-note {
    color: #6b7280;
    font-size: 13px;
    line-height: 1.55;
}

.sales-form-note.is-settings {
    color: #66758a;
    font-size: 15px;
}

.sales-currency-pill {
    min-height: 44px;
    padding: 0 16px;
    border: 1px solid #ccd6e7;
    border-radius: 8px;
    background: #fff;
    display: inline-flex;
    align-items: center;
    gap: 10px;
    font-weight: 700;
}

.sales-address-summary {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 28px;
    padding-top: 4px;
}

.sales-address-summary strong {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 8px;
}

.sales-address-summary p {
    margin: 0;
    color: #0f172a;
    line-height: 1.5;
}

.sales-address-summary a {
    color: #2563eb;
    text-decoration: none;
}

.sales-item-shell {
    display: grid;
    gap: 18px;
}

.sales-items-card {
    border: 1px solid #d9e2ef;
    border-radius: 10px;
    background: #fff;
    overflow: hidden;
}

.sales-items-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 18px 18px 14px;
    background: #fbfcff;
}

.sales-items-head h2 {
    margin: 0;
    font-size: 17px;
}

.sales-bulk-actions {
    color: #2563eb;
    font-size: 14px;
    text-decoration: none;
}

.sales-items-table {
    width: 100%;
    border-collapse: collapse;
}

.sales-items-table thead th {
    padding: 12px 14px;
    border-top: 1px solid #dfe7f3;
    border-bottom: 1px solid #dfe7f3;
    color: #5d6b84;
    font-size: 12px;
    font-weight: 800;
    letter-spacing: 0.04em;
}

.sales-items-table tbody td {
    padding: 0;
    border-right: 1px solid #e4ebf5;
    border-bottom: 1px solid #edf2f8;
    vertical-align: top;
}

.sales-items-table tbody td:last-child,
.sales-items-table thead th:last-child {
    border-right: 0;
}

.sales-item-cell {
    padding: 14px;
}

.sales-item-cell--details {
    display: grid;
    grid-template-columns: 54px minmax(0, 1fr);
    gap: 12px;
    min-height: 88px;
}

.sales-item-thumb {
    width: 40px;
    height: 30px;
    border: 3px solid #d7dbe3;
    border-radius: 4px;
    color: #c3cad6;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
}

.sales-item-plain {
    display: inline-flex;
    align-items: center;
    min-height: 88px;
    color: #0f172a;
}

.sales-item-table-footer {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 14px;
    color: #334155;
    font-weight: 500;
}

.sales-item-actions {
    display: flex;
    align-items: center;
    gap: 14px;
}

.sales-btn-light,
.sales-btn-green,
.sales-btn-outline {
    min-height: 40px;
    padding: 0 16px;
    border-radius: 8px;
    font: inherit;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    cursor: pointer;
}

.sales-btn-light {
    border: 1px solid #e3e8f2;
    background: #f7f8fc;
    color: #0f172a;
}

.sales-btn-green {
    border: 1px solid #2ab96d;
    background: #2ab96d;
    color: #fff;
}

.sales-btn-outline {
    border: 1px solid #d6dfeb;
    background: #fff;
    color: #0f172a;
}

.sales-bottom-grid {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 37%;
    gap: 38px;
    align-items: start;
}

.sales-bottom-notes {
    display: grid;
    gap: 18px;
}

.sales-summary-card {
    border-radius: 16px;
    background: #f7f8fb;
    padding: 22px 14px 18px;
    display: grid;
    gap: 18px;
}

.sales-summary-card h3 {
    margin: 0;
    font-size: 18px;
}

.sales-summary-row,
.sales-summary-line {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 140px;
    align-items: center;
    gap: 18px;
}

.sales-summary-line {
    color: #0f172a;
}

.sales-summary-value {
    text-align: right;
    font-size: 16px;
}

.sales-summary-dual {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 64px;
    gap: 0;
}

.sales-summary-dual .sales-form-control {
    border-radius: 8px 0 0 8px;
}

.sales-summary-dual .sales-unit-box {
    min-height: 44px;
    border: 1px solid #ccd6e7;
    border-left: 0;
    border-radius: 0 8px 8px 0;
    background: #fff;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.sales-tax-choices {
    display: flex;
    align-items: center;
    gap: 18px;
}

.sales-tax-choices label {
    display: inline-flex;
    align-items: center;
    gap: 8px;
}

.sales-summary-divider {
    border-top: 1px solid #e1e6ef;
    padding-top: 18px;
}

.sales-summary-total {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    font-size: 18px;
    font-weight: 800;
}

.sales-summary-total strong {
    font-size: 20px;
}

.sales-bottom-dual {
    display: grid;
    grid-template-columns: minmax(0, 1.35fr) minmax(280px, .95fr);
    gap: 0;
    border: 1px solid #edf2f7;
    background: #fbfcff;
}

.sales-bottom-dual-pane {
    padding: 26px;
}

.sales-bottom-dual-pane + .sales-bottom-dual-pane {
    border-left: 1px solid #edf2f7;
}

.sales-upload-btn {
    width: max-content;
    min-height: 40px;
    padding: 0 16px;
    border: 1px solid #d7ddeb;
    border-radius: 8px;
    background: #fff;
    color: #0f172a;
    font: inherit;
    display: inline-flex;
    align-items: center;
    gap: 12px;
}

.sales-additional-note {
    padding: 28px 26px 26px;
    color: #66758a;
    font-size: 15px;
}

.sales-additional-note strong {
    color: #475569;
}

.sales-form-footer-quote {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 18px;
    padding: 18px 20px;
    border-top: 1px solid #e7edf5;
    background: #fff;
}

.sales-form-footer-actions {
    display: flex;
    align-items: center;
    gap: 12px;
}

.sales-template-note {
    color: #111827;
    font-size: 15px;
}

.sales-template-note a {
    color: #2563eb;
    text-decoration: none;
}

.sales-sticky-summary {
    margin-left: auto;
    text-align: right;
    color: #334155;
}

.sales-sticky-summary strong {
    display: block;
    font-size: 16px;
    color: #0f172a;
}

.sales-form-card--quote {
    border-radius: 0;
    box-shadow: none;
    border: 1px solid #e8edf4;
    background: #fff;
}

.sales-form-card--quote .sales-form-header {
    padding: 28px 28px 0;
    min-height: 80px;
}

.sales-form-card--quote .sales-form-title h1 {
    font-size: 18px;
    font-weight: 800;
}

.sales-form-card--quote .sales-form-header-actions a {
    color: #777;
    font-size: 28px;
}

.sales-form-card--quote .sales-quote-tabs {
    min-height: 58px;
    gap: 36px;
    padding: 0 28px;
    border-bottom-color: #e7edf5;
}

.sales-form-card--quote .sales-quote-tabs a {
    padding: 17px 0 14px;
    font-size: 18px;
    color: #344767;
    border-bottom-width: 3px;
}

.sales-form-card--quote .sales-quote-tabs a.is-active {
    color: #111827;
}

.sales-form-card--quote .sales-section {
    padding: 30px 26px;
    border-top-color: #edf2f7;
}

.sales-form-card--quote .sales-section--soft:first-of-type {
    background: #f7f9fc;
}

.sales-form-card--quote .sales-section--soft:last-of-type {
    background: #fbfcff;
}

.sales-form-card--quote .sales-form-grid {
    grid-template-columns: 190px minmax(0, 1fr);
    gap: 18px 34px;
}

.sales-form-card--quote .sales-form-grid > label,
.sales-form-card--quote .sales-field-label {
    font-size: 14px;
    font-weight: 500;
}

.sales-form-card--quote .sales-form-grid > label:not(.is-required),
.sales-form-card--quote .sales-field-label:not(.is-required) {
    color: #111827;
}

.sales-form-card--quote .sales-form-grid > label.is-required {
    color: #ff3b30;
}

.sales-form-card--quote .sales-form-control,
.sales-form-card--quote .sales-form-select,
.sales-form-card--quote .sales-form-combo,
.sales-form-card--quote .sales-amount-box,
.sales-form-card--quote .sales-adjust-box {
    min-height: 42px;
    border-color: #cfd8e6;
    border-radius: 8px;
    padding: 9px 12px;
    box-shadow: none;
}

.sales-form-card--quote textarea.sales-form-control {
    min-height: 66px;
}

.sales-form-card--quote .sales-customer-select-wrap {
    max-width: 678px;
}

.sales-form-card--quote .sales-customer-select-wrap .sales-form-control {
    border-radius: 8px 0 0 8px;
}

.sales-form-card--quote .sales-search-btn {
    border-radius: 0 8px 8px 0;
    font-size: 18px;
}

.sales-form-card--quote .sales-form-inline-two,
.sales-form-card--quote .sales-form-split {
    gap: 28px 34px;
}

.sales-form-card--quote .sales-item-shell {
    gap: 20px;
}

.sales-form-card--quote .sales-items-card {
    border: 1px solid #dbe3ef;
    border-radius: 8px;
    box-shadow: none;
}

.sales-form-card--quote .sales-items-head {
    padding: 18px 18px 16px;
    background: #fff;
}

.sales-form-card--quote .sales-items-head h2 {
    font-size: 17px;
    font-weight: 800;
}

.sales-form-card--quote .sales-bulk-actions {
    font-size: 15px;
}

.sales-form-card--quote .sales-items-table thead th {
    padding: 12px 12px 14px;
    color: #66758a;
    background: #fff;
}

.sales-form-card--quote .sales-item-cell--details {
    min-height: 84px;
}

.sales-form-card--quote .sales-item-thumb {
    width: 38px;
    height: 30px;
    border-width: 2px;
}

.sales-form-card--quote .sales-item-plain {
    justify-content: flex-end;
    font-weight: 800;
}

.sales-form-card--quote .sales-item-table-footer {
    min-height: 36px;
    padding: 10px 14px;
    color: #111827;
    background: #fff;
}

.sales-form-card--quote .sales-btn-light,
.sales-form-card--quote .sales-btn-green,
.sales-form-card--quote .sales-btn-outline,
.sales-form-card--quote .sales-upload-btn {
    min-height: 40px;
    border-radius: 8px;
}

.sales-form-card--quote .sales-btn-light {
    background: #f5f7fb;
    border-color: #e1e8f2;
}

.sales-form-card--quote .sales-bottom-grid {
    grid-template-columns: minmax(0, 1fr) minmax(330px, 0.82fr);
    gap: 40px;
    align-items: start;
}

.sales-form-card--quote .sales-summary-card {
    border: 0;
    border-radius: 16px;
    background: #f7f8fb;
    padding: 22px 16px 18px;
    box-shadow: none;
}

.sales-form-card--quote .sales-summary-row,
.sales-form-card--quote .sales-summary-line {
    grid-template-columns: minmax(0, 1fr) 152px;
}

.sales-form-card--quote .sales-summary-value {
    font-weight: 800;
}

.sales-form-card--quote .sales-summary-total {
    font-size: 20px;
}

.sales-form-card--quote .sales-summary-total strong {
    font-size: 18px;
}

.sales-form-card--quote .sales-bottom-dual {
    border-color: #edf2f7;
    background: #fbfcff;
}

.sales-form-card--quote .sales-bottom-dual-pane {
    padding: 24px 20px;
}

.sales-form-card--quote .sales-additional-note {
    padding: 26px 26px 30px;
    color: #66758a;
}

.sales-form-card--quote .sales-form-footer-quote {
    position: sticky;
    bottom: 0;
    z-index: 8;
    padding: 18px 20px;
    border-top: 1px solid #e5ebf4;
    box-shadow: 0 -10px 24px rgba(15, 23, 42, 0.04);
}

.sales-form-card--quote .sales-form-footer-actions {
    gap: 12px;
}

.sales-form-card--quote .sales-template-note {
    color: #596780;
}

.sales-form-card--quote .sales-template-note a {
    font-weight: 700;
}

@media (max-width: 1180px) {
    .sales-form-inline-two,
    .sales-form-split,
    .sales-bottom-grid,
    .sales-bottom-dual {
        grid-template-columns: 1fr;
    }

    .sales-bottom-dual-pane + .sales-bottom-dual-pane {
        border-left: 0;
        border-top: 1px solid #edf2f7;
    }
}

@media (max-width: 900px) {
    .sales-form-grid,
    .sales-form-pair,
    .sales-customer-row,
    .sales-address-summary,
    .sales-inline-grid--triple,
    .sales-summary-row,
    .sales-summary-line,
    .sales-summary-total {
        grid-template-columns: 1fr;
    }

    .sales-form-grid > label,
    .sales-field-label,
    .sales-form-pair .sales-field-label {
        padding-top: 0;
    }

    .sales-form-footer-quote,
    .sales-form-header {
        flex-direction: column;
        align-items: flex-start;
    }

    .sales-form-card--quote .sales-bottom-grid,
    .sales-form-card--quote .sales-form-grid,
    .sales-form-card--quote .sales-summary-row,
    .sales-form-card--quote .sales-summary-line,
    .sales-form-card--quote .sales-summary-total {
        grid-template-columns: 1fr;
    }
}
</style>
</head>
<body class="sales-document-page sales-document-page--<?php echo billing_escape($moduleKey); ?>">
<div class="dashboard-shell customer-shell">
    <?php include dirname(__DIR__) . '/includes/app_sidebar.php'; ?>
    <div class="dashboard-main customer-main">
        <?php include dirname(__DIR__) . '/includes/topbar.php'; ?>
        <main class="customer-form-page">
            <form method="post" class="customer-form-card sales-form-card<?php echo $isQuoteModule ? ' sales-form-card--quote' : ''; ?>">
                <div class="sales-form-header">
                    <div class="sales-form-title"><h1><?php echo billing_escape((string) $module['new_title']); ?></h1></div>
                    <div class="sales-form-header-actions">
                        <a href="<?php echo billing_escape((string) $module['list_path']); ?>" aria-label="Close"><i class="fa-solid fa-xmark"></i></a>
                    </div>
                </div>
                <?php if ($isQuoteModule): ?>
                    <nav class="sales-quote-tabs">
                        <a class="is-active" href="#" onclick="return false;">Quote</a>
                        <a href="#" onclick="return false;">Subscription Quote</a>
                    </nav>
                <?php endif; ?>
                <?php if ($errors !== []): ?>
                    <div class="customer-error-banner"><span><?php echo billing_escape(implode(' ', $errors)); ?></span><i class="fa-solid fa-xmark"></i></div>
                <?php endif; ?>

                <div class="sales-form-body">
                    <section class="sales-section sales-section--soft">
                        <div class="sales-form-grid">
                            <label class="is-required">Customer Name*</label>
                            <div class="sales-customer-shell">
                                <div class="sales-customer-row">
                                    <div class="sales-customer-select-wrap">
                                        <select name="customer_id" class="sales-form-control" data-sales-customer-select>
                                            <option value="0"><?php echo $isQuoteModule ? 'Select or add a customer' : 'Select a customer'; ?></option>
                                            <?php foreach ($customers as $customer): ?>
                                                <option value="<?php echo (int) $customer['id']; ?>" data-name="<?php echo billing_escape((string) $customer['customer_name']); ?>" <?php echo (int) $form['customer_id'] === (int) $customer['id'] ? 'selected' : ''; ?>><?php echo billing_escape((string) $customer['customer_name']); ?></option>
                                            <?php endforeach; ?>
                                        </select>
                                        <button class="sales-search-btn" type="button" aria-label="Search customer"><i class="fa-solid fa-magnifying-glass"></i></button>
                                    </div>
                                    <?php if ($moduleKey === 'invoices'): ?>
                                        <div class="sales-currency-pill"><i class="fa-solid fa-circle-check"></i> <?php echo billing_escape((string) ($form['currency_code'] ?? 'INR')); ?></div>
                                    <?php endif; ?>
                                </div>
                                <input type="hidden" name="customer_name" value="<?php echo billing_escape((string) $form['customer_name']); ?>" data-sales-customer-name>
                                <?php if ($moduleKey === 'invoices' && $formCustomerSnapshot): ?>
                                    <div class="sales-address-summary">
                                        <div>
                                            <strong>BILLING ADDRESS <a href="#"><i class="fa-solid fa-pen"></i></a></strong>
                                            <?php foreach (billing_sales_form_address_lines($formCustomerSnapshot, 'billing') as $line): ?>
                                                <p><?php echo billing_escape($line); ?></p>
                                            <?php endforeach; ?>
                                        </div>
                                        <div>
                                            <strong>SHIPPING ADDRESS <a href="#"><i class="fa-solid fa-pen"></i></a></strong>
                                            <?php foreach (billing_sales_form_address_lines($formCustomerSnapshot, 'shipping') as $line): ?>
                                                <p><?php echo billing_escape($line); ?></p>
                                            <?php endforeach; ?>
                                        </div>
                                    </div>
                                <?php endif; ?>
                            </div>

                            <label class="is-required"><?php echo billing_escape((string) $module['number_label']); ?>*</label>
                            <div class="sales-form-split">
                                <div class="sales-field-stack">
                                    <input class="sales-form-control" type="text" name="<?php echo billing_escape((string) $module['number_field']); ?>" value="<?php echo billing_escape((string) $form[$module['number_field']]); ?>">
                                </div>
                                <div></div>
                            </div>

                            <?php if ($module['reference_field'] !== ''): ?>
                                <label><?php echo billing_escape((string) $module['reference_label']); ?></label>
                                <div class="sales-form-split">
                                    <div class="sales-field-stack">
                                        <input class="sales-form-control" type="text" name="<?php echo billing_escape((string) $module['reference_field']); ?>" value="<?php echo billing_escape((string) ($form[$module['reference_field']] ?? '')); ?>">
                                    </div>
                                    <div></div>
                                </div>
                            <?php endif; ?>

                            <label class="is-required"><?php echo billing_escape((string) $module['date_label']); ?>*</label>
                            <?php if ($moduleKey === 'invoices'): ?>
                                <div class="sales-inline-grid--triple">
                                    <input class="sales-form-control" type="date" name="<?php echo billing_escape((string) $module['date_field']); ?>" value="<?php echo billing_escape((string) ($form[$module['date_field']] ?? '')); ?>">
                                    <select class="sales-form-control" name="payment_terms">
                                        <?php foreach (['Due on Receipt', 'Net 15', 'Net 30', 'Net 45'] as $term): ?>
                                            <option value="<?php echo billing_escape($term); ?>" <?php echo (string) ($form['payment_terms'] ?? 'Due on Receipt') === $term ? 'selected' : ''; ?>><?php echo billing_escape($term); ?></option>
                                        <?php endforeach; ?>
                                    </select>
                                    <?php if ($module['secondary_date_field'] !== ''): ?>
                                        <input class="sales-form-control" type="date" name="<?php echo billing_escape((string) $module['secondary_date_field']); ?>" value="<?php echo billing_escape((string) ($form[$module['secondary_date_field']] ?? '')); ?>">
                                    <?php endif; ?>
                                </div>
                            <?php else: ?>
                                <div class="sales-form-inline-two">
                                    <div class="sales-field-stack">
                                        <input class="sales-form-control" type="date" name="<?php echo billing_escape((string) $module['date_field']); ?>" value="<?php echo billing_escape((string) ($form[$module['date_field']] ?? '')); ?>">
                                    </div>
                                    <?php if ($module['secondary_date_field'] !== ''): ?>
                                        <div class="sales-form-pair">
                                            <span class="sales-field-label"><?php echo billing_escape((string) $module['secondary_date_label']); ?></span>
                                            <input class="sales-form-control" type="date" name="<?php echo billing_escape((string) $module['secondary_date_field']); ?>" value="<?php echo billing_escape((string) ($form[$module['secondary_date_field']] ?? '')); ?>">
                                        </div>
                                    <?php else: ?>
                                        <div></div>
                                    <?php endif; ?>
                                </div>
                            <?php endif; ?>
                        </div>
                    </section>

                    <section class="sales-section">
                        <div class="sales-form-grid">
                            <?php if ($moduleKey !== 'delivery_challans'): ?>
                                <label>Salesperson</label>
                                <div class="sales-field-stack">
                                    <input class="sales-form-control" type="text" name="salesperson_name" value="<?php echo billing_escape((string) ($form['salesperson_name'] ?? '')); ?>" placeholder="Select or Add Salesperson">
                                </div>
                            <?php endif; ?>

                            <?php if ($module['extra_field'] !== ''): ?>
                                <label><?php echo billing_escape((string) $module['extra_label']); ?></label>
                                <div class="sales-field-stack">
                                    <?php if (!empty($module['extra_options'])): ?>
                                        <select class="sales-form-control" name="<?php echo billing_escape((string) $module['extra_field']); ?>">
                                            <option value="">Choose</option>
                                            <?php foreach ($module['extra_options'] as $option): ?>
                                                <option value="<?php echo billing_escape($option); ?>" <?php echo (string) ($form[$module['extra_field']] ?? '') === $option ? 'selected' : ''; ?>><?php echo billing_escape($option); ?></option>
                                            <?php endforeach; ?>
                                        </select>
                                    <?php else: ?>
                                        <input class="sales-form-control" type="text" name="<?php echo billing_escape((string) $module['extra_field']); ?>" value="<?php echo billing_escape((string) ($form[$module['extra_field']] ?? '')); ?>" placeholder="<?php echo $isQuoteModule ? 'Select a project' : ''; ?>">
                                    <?php endif; ?>
                                    <?php if ($isQuoteModule && $module['extra_field'] === 'project_name'): ?>
                                        <div class="sales-form-note">Select a customer to associate a project.</div>
                                    <?php endif; ?>
                                </div>
                            <?php endif; ?>

                            <?php if ($module['subject_label'] !== ''): ?>
                                <label><?php echo billing_escape((string) $module['subject_label']); ?></label>
                                <div class="sales-field-stack">
                                    <textarea class="sales-form-control" name="subject" rows="2" placeholder="Let your customer know what this Quote is for"><?php echo billing_escape((string) ($form['subject'] ?? '')); ?></textarea>
                                </div>
                            <?php endif; ?>
                        </div>
                    </section>

                    <section class="sales-section">
                        <div class="sales-item-shell">
                            <div class="sales-items-card">
                                <div class="sales-items-head">
                                    <h2>Item Table</h2>
                                    <a class="sales-bulk-actions" href="#" onclick="return false;"><i class="fa-regular fa-circle-check"></i> Bulk Actions</a>
                                </div>
                                <table class="sales-items-table">
                                    <thead>
                                        <tr>
                                            <th style="width: 50%;">ITEM DETAILS</th>
                                            <th style="width: 18%;">QUANTITY</th>
                                            <th style="width: 18%;">RATE</th>
                                            <th style="width: 14%;">AMOUNT</th>
                                        </tr>
                                    </thead>
                                    <tbody id="sales-items-body">
                                        <?php foreach ($lineItems as $item): ?>
                                            <?php $rowAmount = ((float) ($item['quantity'] ?? 0) * (float) ($item['rate'] ?? 0)); ?>
                                            <tr>
                                                <td>
                                                    <div class="sales-item-cell sales-item-cell--details">
                                                        <span class="sales-item-thumb"><i class="fa-regular fa-image"></i></span>
                                                        <div class="sales-field-stack sales-field-stack--spaced">
                                                            <input class="sales-form-control" type="text" name="item_name[]" list="salesItemsList" value="<?php echo billing_escape((string) ($item['item_name'] ?? '')); ?>" placeholder="Type or click to select an item.">
                                                            <input class="sales-form-control" type="text" name="item_description[]" value="<?php echo billing_escape((string) ($item['item_description'] ?? '')); ?>" placeholder="Description">
                                                            <input type="hidden" name="unit_name[]" value="<?php echo billing_escape((string) ($item['unit_name'] ?? '')); ?>">
                                                        </div>
                                                    </div>
                                                </td>
                                                <td><div class="sales-item-cell"><input class="sales-form-control" type="number" step="0.01" min="0" name="quantity[]" value="<?php echo billing_escape((string) ($item['quantity'] ?? 1)); ?>"></div></td>
                                                <td><div class="sales-item-cell"><input class="sales-form-control" type="number" step="0.01" min="0" name="rate[]" value="<?php echo billing_escape((string) ($item['rate'] ?? 0)); ?>"></div></td>
                                                <td><div class="sales-item-cell sales-item-plain"><strong data-row-amount><?php echo billing_escape(number_format($rowAmount, 2)); ?></strong></div></td>
                                            </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                </table>
                                <div class="sales-item-table-footer">
                                    <i class="fa-solid fa-tag"></i> Reporting Tags <i class="fa-solid fa-caret-down"></i>
                                </div>
                            </div>

                            <div class="sales-item-actions">
                                <button class="sales-btn-light" type="button" id="sales-add-row"><i class="fa-solid fa-circle-plus"></i> Add New Row <i class="fa-solid fa-caret-down"></i></button>
                                <button class="sales-btn-light" type="button"><i class="fa-solid fa-circle-plus"></i> Add Items in Bulk</button>
                            </div>
                            <datalist id="salesItemsList">
                                <?php foreach ($items as $itemOption): ?>
                                    <option value="<?php echo billing_escape((string) $itemOption['item_name']); ?>"></option>
                                <?php endforeach; ?>
                            </datalist>
                        </div>
                    </section>

                    <section class="sales-section">
                        <div class="sales-bottom-grid">
                            <div class="sales-bottom-notes">
                                <div class="sales-field-stack">
                                    <label><?php echo billing_escape((string) $module['notes_label']); ?></label>
                                    <textarea class="sales-form-control" name="<?php echo billing_escape((string) $module['notes_field']); ?>" rows="4"><?php echo billing_escape((string) ($form[$module['notes_field']] ?? '')); ?></textarea>
                                </div>
                            </div>
                            <div class="sales-summary-card">
                                <div class="sales-summary-line"><span><strong>Sub Total</strong></span><strong class="sales-summary-value" data-sales-subtotal><?php echo billing_escape(number_format((float) $totals['sub_total'], 2)); ?></strong></div>
                                <div class="sales-summary-row">
                                    <span>Discount</span>
                                    <div class="sales-summary-dual">
                                        <input class="sales-form-control" type="number" step="0.01" min="0" name="discount_amount" value="<?php echo billing_escape((string) ($form['discount_amount'] ?? 0)); ?>">
                                        <span class="sales-unit-box">%</span>
                                    </div>
                                </div>
                                <div class="sales-summary-row">
                                    <div class="sales-tax-choices">
                                        <label><input type="radio" checked> TDS</label>
                                        <label><input type="radio"> TCS</label>
                                    </div>
                                    <?php if ($moduleKey !== 'delivery_challans'): ?>
                                        <input class="sales-form-control" type="number" step="0.01" min="0" name="tax_total" value="<?php echo billing_escape((string) ($form['tax_total'] ?? 0)); ?>" placeholder="Select a Tax">
                                    <?php else: ?>
                                        <input type="hidden" name="tax_total" value="0">
                                        <input class="sales-form-control" type="text" value="Select a Tax" disabled>
                                    <?php endif; ?>
                                </div>
                                <div class="sales-summary-row">
                                    <input class="sales-form-control" type="text" value="Adjustment" readonly>
                                    <input class="sales-form-control" type="number" step="0.01" name="adjustment_amount" value="<?php echo billing_escape((string) ($form['adjustment_amount'] ?? 0)); ?>">
                                </div>
                                <div class="sales-summary-divider">
                                    <div class="sales-summary-total">
                                        <span>Total ( &#8377; )</span>
                                        <strong data-sales-total><?php echo billing_escape(number_format((float) $totals['total_amount'], 2)); ?></strong>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </section>

                    <section class="sales-section sales-section--soft">
                        <div class="sales-bottom-dual">
                            <div class="sales-bottom-dual-pane">
                                <div class="sales-field-stack">
                                    <label><?php echo billing_escape((string) $module['terms_label']); ?></label>
                                    <textarea class="sales-form-control" name="<?php echo billing_escape((string) $module['terms_field']); ?>" rows="5" placeholder="Enter the terms and conditions of your business to be displayed in your transaction"><?php echo billing_escape((string) ($form[$module['terms_field']] ?? '')); ?></textarea>
                                </div>
                            </div>
                            <div class="sales-bottom-dual-pane">
                                <div class="sales-upload-card">
                                    <label>Attach File(s) to <?php echo $isQuoteModule ? 'Quote' : billing_escape((string) rtrim($module['new_title'], 's')); ?></label>
                                    <button class="sales-upload-btn" type="button"><i class="fa-solid fa-upload"></i> Upload File <i class="fa-solid fa-caret-down"></i></button>
                                    <div class="sales-form-note">You can upload a maximum of 5 files, 10MB each</div>
                                </div>
                            </div>
                        </div>
                    </section>

                    <div class="sales-additional-note">
                        <strong>Additional Fields:</strong> Start adding custom fields to your <?php echo $isQuoteModule ? 'quotes' : strtolower((string) $module['list_title']); ?> by going to <em>Settings</em> <i class="fa-solid fa-caret-right"></i> <em>Sales</em> <i class="fa-solid fa-caret-right"></i> <em><?php echo billing_escape((string) ucfirst(str_replace('_', ' ', $moduleKey))); ?></em>.
                    </div>

                    <input type="hidden" name="<?php echo billing_escape((string) $module['template_field']); ?>" value="<?php echo billing_escape((string) ($form[$module['template_field']] ?? $module['default_template'])); ?>">
                    <input type="hidden" name="status_label" value="<?php echo billing_escape((string) $form['status_label']); ?>">
                </div>

                <div class="sales-form-footer-quote">
                    <div class="sales-form-footer-actions">
                        <?php if ($moduleKey === 'invoices'): ?>
                            <button class="sales-btn-light" type="submit" name="invoice_action" value="save_draft">Save as Draft</button>
                            <button class="sales-btn-green" type="submit" name="invoice_action" value="save_send"><?php echo $record ? 'Update and Send' : 'Save and Send'; ?></button>
                        <?php elseif ($moduleKey === 'quotes'): ?>
                            <button class="sales-btn-light" type="submit" name="quote_action" value="save_draft">Save as Draft</button>
                            <button class="sales-btn-green" type="submit" name="quote_action" value="save_send"><?php echo $record ? 'Update and Send' : 'Save and Send'; ?></button>
                        <?php else: ?>
                            <button class="sales-btn-green" type="submit"><?php echo $record ? 'Update' : 'Save'; ?></button>
                        <?php endif; ?>
                        <a class="sales-btn-outline" href="<?php echo billing_escape((string) $module['list_path']); ?>">Cancel</a>
                    </div>
                    <?php if ($isQuoteModule): ?>
                        <div class="sales-template-note">PDF Template: '<?php echo billing_escape((string) ($form[$module['template_field']] ?? $module['default_template'])); ?>' <a href="#" onclick="return false;">Change</a></div>
                    <?php elseif ($moduleKey === 'invoices'): ?>
                        <div class="sales-sticky-summary">
                            <strong>Total Amount: <?php echo billing_escape('INR ' . number_format((float) $totals['total_amount'], 2)); ?></strong>
                            <span>Total Quantity: <?php echo billing_escape(number_format($itemQuantityTotal, 2)); ?></span>
                        </div>
                    <?php endif; ?>
                </div>
            </form>
        </main>
    </div>
</div>
<script src="assets/billing.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function () {
  const customerSelect = document.querySelector('[data-sales-customer-select]');
  const customerName = document.querySelector('[data-sales-customer-name]');
  const addRowButton = document.getElementById('sales-add-row');
  const tableBody = document.getElementById('sales-items-body');
  const subtotalOutput = document.querySelector('[data-sales-subtotal]');
  const totalOutput = document.querySelector('[data-sales-total]');
  const discountInput = document.querySelector('input[name="discount_amount"]');
  const taxInput = document.querySelector('input[name="tax_total"]');
  const adjustmentInput = document.querySelector('input[name="adjustment_amount"]');
  const formatAmount = function (value) {
    return Number(value || 0).toFixed(2);
  };
  const recalcTotals = function () {
    if (!tableBody) {
      return;
    }

    let subtotal = 0;
    tableBody.querySelectorAll('tr').forEach(function (row) {
      const qty = row.querySelector('input[name="quantity[]"]');
      const rate = row.querySelector('input[name="rate[]"]');
      subtotal += (parseFloat(qty && qty.value ? qty.value : '0') || 0) * (parseFloat(rate && rate.value ? rate.value : '0') || 0);
    });

    const discountPercent = parseFloat(discountInput && discountInput.value ? discountInput.value : '0') || 0;
    const taxValue = parseFloat(taxInput && taxInput.value ? taxInput.value : '0') || 0;
    const adjustmentValue = parseFloat(adjustmentInput && adjustmentInput.value ? adjustmentInput.value : '0') || 0;
    const discountValue = subtotal * (discountPercent / 100);
    const total = subtotal - discountValue + taxValue + adjustmentValue;

    if (subtotalOutput) {
      subtotalOutput.textContent = formatAmount(subtotal);
    }
    if (totalOutput) {
      totalOutput.textContent = formatAmount(total);
    }
  };
  const syncRowAmount = function (row) {
    const qty = row.querySelector('input[name="quantity[]"]');
    const rate = row.querySelector('input[name="rate[]"]');
    const amount = row.querySelector('[data-row-amount]');
    if (!qty || !rate || !amount) {
      return;
    }
    amount.textContent = formatAmount((parseFloat(qty.value || '0') || 0) * (parseFloat(rate.value || '0') || 0));
    recalcTotals();
  };
  const bindRow = function (row) {
    row.querySelectorAll('input[name="quantity[]"], input[name="rate[]"]').forEach(function (input) {
      input.addEventListener('input', function () {
        syncRowAmount(row);
      });
    });
    syncRowAmount(row);
  };

  if (customerSelect && customerName) {
    const syncCustomer = function () {
      const option = customerSelect.options[customerSelect.selectedIndex];
      customerName.value = option ? (option.getAttribute('data-name') || option.textContent.trim()) : '';
    };
    customerSelect.addEventListener('change', syncCustomer);
    syncCustomer();
  }

  if (addRowButton && tableBody) {
    tableBody.querySelectorAll('tr').forEach(bindRow);
    addRowButton.addEventListener('click', function () {
      const row = document.createElement('tr');
      row.innerHTML = '<td><div class="sales-item-cell sales-item-cell--details"><span class="sales-item-thumb"><i class="fa-regular fa-image"></i></span><div class="sales-field-stack sales-field-stack--spaced"><input class="sales-form-control" type="text" name="item_name[]" list="salesItemsList" placeholder="Type or click to select an item."><input class="sales-form-control" type="text" name="item_description[]" placeholder="Description"><input type="hidden" name="unit_name[]" value=""></div></div></td><td><div class="sales-item-cell"><input class="sales-form-control" type="number" step="0.01" min="0" name="quantity[]" value="1"></div></td><td><div class="sales-item-cell"><input class="sales-form-control" type="number" step="0.01" min="0" name="rate[]" value="0"></div></td><td><div class="sales-item-cell sales-item-plain"><strong data-row-amount>0.00</strong></div></td>';
      tableBody.appendChild(row);
      bindRow(row);
    });
  }

  [discountInput, taxInput, adjustmentInput].forEach(function (input) {
    if (!input) {
      return;
    }
    input.addEventListener('input', recalcTotals);
  });

  recalcTotals();
});
</script>
</body>
</html>



