<?php
function generateDocumentHTML($doc, $type) {
    $items = json_decode($doc['items'], true);
    $doc_date = date('F d, Y', strtotime($doc['doc_date']));
    $logo = COMPANY_LOGO;
    $company = COMPANY_NAME;
    $address = COMPANY_ADDRESS;
    $phone = COMPANY_PHONE;
    $email = COMPANY_EMAIL;

    $title = '';
    switch($type) {
        case 'quotation': $title = 'QUOTATION'; break;
        case 'proforma': $title = 'PROFORMA INVOICE'; break;
        case 'invoice': $title = 'INVOICE'; break;
        case 'receipt': $title = 'RECEIPT'; break;
    }

    $html = '<div style="font-family:DejaVu Sans, sans-serif; color:#333;">';
    $html .= '<div style="text-align:center; margin-bottom:20px;">';
    if(file_exists($logo)) {
        $html .= '<img src="'.$logo.'" style="max-height:70px;"><br>';
    }
    $html .= '<h2 style="color:#0d47a1; margin:5px 0;">'.$company.'</h2>';
    $html .= '<p style="margin:0; font-size:0.9rem;">'.$address.' | '.$phone.' | '.$email.'</p>';
    $html .= '</div>';

    $html .= '<div style="border-bottom:2px solid #fbc02d; margin-bottom:20px;"></div>';

    $html .= '<div style="display:flex; justify-content:space-between;">';
    $html .= '<div><strong>Bill-to:</strong> '.$doc['customer_name'].'<br>';
    $html .= nl2br($doc['customer_address']).'<br>';
    if($doc['customer_phone']) $html .= 'Phone: '.$doc['customer_phone'].'<br>';
    if($doc['customer_email']) $html .= 'Email: '.$doc['customer_email'];
    $html .= '</div>';
    $html .= '<div style="text-align:right;"><h3 style="color:#0d47a1;">'.$title.'</h3>';
    $html .= '<strong>Document No:</strong> '.$doc['doc_number'].'<br>';
    $html .= '<strong>Date:</strong> '.$doc_date.'<br>';
    if($type == 'invoice') $html .= '<strong>Due Date:</strong> '.date('F d, Y', strtotime('+30 days', strtotime($doc['doc_date']))).'<br>';
    $html .= '</div></div>';

    // Table: Description, Qty, Unit Price, Amount (no discount/VAT columns)
    $html .= '<table style="width:100%; border-collapse:collapse; margin:20px 0;">';
    $html .= '<thead><tr style="background-color:#0d47a1; color:#fff;">';
    $html .= '<th style="padding:8px; border:1px solid #ddd; text-align:left;">Description</th>';
    $html .= '<th style="padding:8px; border:1px solid #ddd; text-align:right;">Qty</th>';
    $html .= '<th style="padding:8px; border:1px solid #ddd; text-align:right;">Unit Price</th>';
    $html .= '<th style="padding:8px; border:1px solid #ddd; text-align:right;">Amount</th>';
    $html .= '</tr></thead><tbody>';

    $i=1;
    foreach($items as $item) {
        $amount = $item['quantity'] * $item['unit_price'];
        $html .= '<tr>';
        $html .= '<td style="padding:6px; border:1px solid #ddd;">'.$item['description'].'</td>';
        $html .= '<td style="padding:6px; border:1px solid #ddd; text-align:right;">'.$item['quantity'].'</td>';
        $html .= '<td style="padding:6px; border:1px solid #ddd; text-align:right;">'.number_format($item['unit_price'], 2).'</td>';
        $html .= '<td style="padding:6px; border:1px solid #ddd; text-align:right;">'.number_format($amount, 2).'</td>';
        $html .= '</tr>';
    }
    $html .= '</tbody></table>';

    // Totals
    $html .= '<div style="text-align:right; margin-top:10px;">';
    $html .= '<strong>Subtotal:</strong> '.number_format($doc['subtotal'], 2).'<br>';
    if($doc['vat_amount'] > 0) {
        $html .= '<strong>VAT (16%):</strong> '.number_format($doc['vat_amount'], 2).'<br>';
    }
    $html .= '<h4 style="color:#0d47a1;"><strong>Total (KES):</strong> '.number_format($doc['total'], 2).'</h4>';
    $html .= '</div>';

    // Bank details (for proforma/invoice)
    if($type == 'proforma' || $type == 'invoice') {
        $html .= '<div style="background:#fff3c4; padding:12px; border-radius:8px; margin:15px 0;">';
        $html .= '<strong>Bank Details</strong><br>';
        $html .= 'Account Name: CHLORIDE EXIDE (K) LTD<br>';
        $html .= 'Bank: NCBA BANK<br>';
        $html .= 'Account No: 1486010217<br>';
        $html .= 'Branch: NCBA HOUSE BRANCH<br>';
        $html .= '</div>';
        $html .= '<div><strong>MPESA Paybill:</strong> 555300 &nbsp;|&nbsp; <strong>Account No:</strong> 200188</div>';
    }

    if($type == 'receipt') {
        $html .= '<div style="background:#e8f5e9; padding:12px; border-radius:8px; margin:15px 0; text-align:center;">';
        $html .= '<h4 style="color:#2e7d32;">✔ PAID – RECEIVED WITH THANKS</h4>';
        $html .= '<p>Payment confirmed on '.date('F d, Y').'</p>';
        $html .= '</div>';
    }

    // Conditions
    $html .= '<div class="conditions" style="margin-top:20px; font-size:0.85rem; color:#555;">';
    $html .= '<strong>CONDITIONS:</strong><br>';
    $html .= '1. Prices are subject to change anytime without prior notice.<br>';
    $html .= '2. This is only for quotation purpose and is NOT an invoice.';
    $html .= '</div>';

    $html .= '</div>';
    return $html;
}
?>