<?php
/**
 * Simple PDF Generator
 * Creates downloadable PDF files using HTML content
 */

class SimplePDF {
    
    public static function generateQuotationPDF($quotation, $items, $customer) {
        $html = self::buildQuotationHTML($quotation, $items, $customer);
        return self::outputPDF($html, $quotation['quotation_number'] . '.pdf');
    }
    
    public static function generateInvoicePDF($invoice, $items, $customer) {
        $html = self::buildInvoiceHTML($invoice, $items, $customer);
        return self::outputPDF($html, $invoice['invoice_number'] . '.pdf');
    }
    
    private static function buildQuotationHTML($quotation, $items, $customer) {
        $html = '<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Quotation - ' . htmlspecialchars($quotation['quotation_number']) . '</title>
    <style>
        body { 
            font-family: Arial, sans-serif; 
            margin: 0; 
            padding: 20px; 
            font-size: 12px;
            line-height: 1.4;
            color: #333;
        }
        .quotation-header {
            margin-bottom: 30px;
            padding-bottom: 20px;
            border-bottom: 2px solid #007bff;
        }
        .header-row {
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
        }
        .header-left h1 {
            margin: 0;
            font-size: 28px;
            color: #007bff;
            font-weight: bold;
        }
        .header-left h3 {
            margin: 5px 0;
            font-size: 18px;
            color: #666;
            font-weight: normal;
        }
        .header-right {
            text-align: right;
        }
        .header-right p {
            margin: 5px 0;
            font-size: 12px;
        }
        .status-badge {
            padding: 3px 8px;
            border-radius: 12px;
            font-size: 10px;
            font-weight: bold;
            text-transform: uppercase;
        }
        .status-draft { background-color: #fff3cd; color: #856404; }
        .status-accepted { background-color: #d4edda; color: #155724; }
        .status-rejected { background-color: #f8d7da; color: #721c24; }
        
        .info-row {
            display: flex;
            margin-bottom: 30px;
            gap: 30px;
        }
        .company-info, .customer-info {
            flex: 1;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        .company-info h5, .customer-info h5 {
            margin: 0 0 15px 0;
            color: #007bff;
            font-size: 14px;
            font-weight: bold;
        }
        .customer-info h4 {
            margin: 0 0 10px 0;
            font-size: 16px;
            font-weight: bold;
        }
        .company-info p, .customer-info p {
            margin: 5px 0;
            font-size: 11px;
        }
        
        .items-table {
            width: 100%;
            border-collapse: collapse;
            margin: 20px 0;
        }
        .items-table th, .items-table td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        .items-table th {
            background-color: #f8f9fa;
            font-weight: bold;
            font-size: 11px;
        }
        .items-table .text-right {
            text-align: right;
        }
        .items-table .description {
            font-weight: bold;
        }
        .items-table .description small {
            font-weight: normal;
            color: #666;
            display: block;
            margin-top: 2px;
        }
        
        .totals-row {
            display: flex;
            gap: 30px;
            margin-top: 20px;
        }
        .notes-section {
            flex: 2;
        }
        .notes-section h6 {
            margin: 0 0 10px 0;
            color: #007bff;
            font-size: 12px;
            font-weight: bold;
        }
        .total-section {
            flex: 1;
            background-color: #f8f9fa;
            padding: 15px;
            border-radius: 5px;
        }
        .total-section .d-flex {
            display: flex;
            justify-content: space-between;
            margin-bottom: 8px;
        }
        .total-section hr {
            margin: 10px 0;
            border: none;
            border-top: 1px solid #ddd;
        }
        .total-section strong {
            font-size: 13px;
        }
        
        .terms-section {
            margin-top: 30px;
            padding-top: 20px;
            border-top: 1px solid #ddd;
        }
        .terms-section h6 {
            margin: 0 0 10px 0;
            color: #007bff;
            font-size: 12px;
            font-weight: bold;
        }
        .terms-section ul {
            margin: 0;
            padding-left: 20px;
            font-size: 10px;
            color: #666;
        }
        .terms-section li {
            margin-bottom: 3px;
        }
    </style>
</head>
<body>
    <div class="quotation-header">
        <div class="header-row">
            <div class="header-left">
                <h1>📄 QUOTATION</h1>
                <h3>' . htmlspecialchars($quotation['quotation_number']) . '</h3>
            </div>
            <div class="header-right">
                <p><strong>Date:</strong> ' . date('d M Y', strtotime($quotation['issue_date'])) . '</p>
                <p><strong>Status:</strong> <span class="status-badge status-' . strtolower($quotation['status']) . '">' . ucfirst($quotation['status']) . '</span></p>
            </div>
        </div>
    </div>
    
    <div class="info-row">
        <div class="company-info">
            <h5>🏢 From</h5>
               <img src="assets/img/logo.png" width="100" height="100" alt="The Sky Developers" class="company-logo">
            <h4>The Sky Developers</h4>
            <p><strong>Address:</strong> FF-29 Shree Sidhheswar Harbours,<br>
            Waghodiya Road, NH-8<br>
            Vadodara - 390025</p>
            <p><strong>Contact:</strong> +91 70163 25717</p>
            <p><strong>Email:</strong> theskydevelopers@yahoo.com</p>
            <p><strong>GST:</strong> NA</p>
        </div>
        
        <div class="customer-info">
            <h5>👤 To</h5>
            <h4>' . htmlspecialchars($customer['name']) . '</h4>';
            
        if($customer['contact_number']) {
            $html .= '<p>📞 ' . htmlspecialchars($customer['contact_number']) . '</p>';
        }
        if($customer['email']) {
            $html .= '<p>✉️ ' . htmlspecialchars($customer['email']) . '</p>';
        }
        if($customer['address']) {
            $html .= '<p>📍 ' . htmlspecialchars($customer['address']) . '</p>';
        }
        if($customer['gst_number']) {
            $html .= '<p>🆔 GST: ' . htmlspecialchars($customer['gst_number']) . '</p>';
        }
        
        $html .= '</div>
    </div>
    
    <table class="items-table">
        <thead>
            <tr>
                <th width="5%">#</th>
                <th width="35%">Description</th>
                <th width="15%" class="text-right">Quantity</th>
                <th width="15%" class="text-right">Rate (₹)</th>
                <th width="15%" class="text-right">Amount (₹)</th>
            </tr>
        </thead>
        <tbody>';
        
        foreach($items as $index => $item) {
            $html .= '<tr>
                <td>' . ($index + 1) . '</td>
                <td>
                    <div class="description"><strong>' . htmlspecialchars($item['material_name']) . '</strong>';
            
            if($item['description']) {
                $html .= '<small>' . htmlspecialchars($item['description']) . '</small>';
            }
            
            $html .= '</div>
                </td>
                <td class="text-right">' . $item['quantity'] . '</td>
                <td class="text-right">₹' . number_format($item['rate'], 2) . '</td>
                <td class="text-right">₹' . number_format($item['total'], 2) . '</td>
            </tr>';
        }
        
        $html .= '</tbody>
    </table>
    
    <div class="totals-row">
        <div class="notes-section">';
        
        if(!empty($quotation['notes'])) {
            $html .= '<h6>📝 Notes:</h6>
            <p style="color: #666; font-size: 11px;">' . nl2br(htmlspecialchars($quotation['notes'])) . '</p>';
        }
        
        $html .= '</div>
        <div class="total-section">
            <div class="d-flex">
                <span>Subtotal:</span>
                <span>₹' . number_format($quotation['subtotal'], 2) . '</span>
            </div>';
        
        if(isset($quotation['tax_amount']) && $quotation['tax_amount'] > 0) {
            $html .= '<div class="d-flex">
                <span>Tax:</span>
                <span>₹' . number_format($quotation['tax_amount'], 2) . '</span>
            </div>';
        }
        
        if(isset($quotation['discount_amount']) && $quotation['discount_amount'] > 0) {
            $html .= '<div class="d-flex">
                <span>Discount:</span>
                <span>-₹' . number_format($quotation['discount_amount'], 2) . '</span>
            </div>';
        }
        
        $html .= '<hr>
            <div class="d-flex">
                <strong>Total Amount:</strong>
                <strong>₹' . number_format($quotation['grand_total'], 2) . '</strong>
            </div>
        </div>
    </div>
    
    <div class="terms-section">
        <h6>📋 Terms & Conditions:</h6>
        <ul>
            <li>This quotation is valid for 30 days from the date of issue.</li>
            <li>Payment terms: 50% advance, 50% on delivery.</li>
            <li>Delivery time: 7-10 working days after order confirmation.</li>
            <li>All prices are exclusive of applicable taxes.</li>
        </ul>
    </div>
</body></html>';
        
        return $html;
    }
    
    private static function buildInvoiceHTML($invoice, $items, $customer) {
        $html = '<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Invoice - ' . htmlspecialchars($invoice['invoice_number']) . '</title>
    <style>
        body { 
            font-family: Arial, sans-serif; 
            margin: 0; 
            padding: 20px; 
            font-size: 12px;
            line-height: 1.4;
        }
        .header { 
            text-align: center; 
            margin-bottom: 30px; 
            border-bottom: 2px solid #333;
            padding-bottom: 15px;
        }
        .header h1 { 
            margin: 0; 
            font-size: 24px; 
            color: #333;
        }
        .header h2 { 
            margin: 5px 0; 
            font-size: 18px; 
            color: #666;
        }
        .company-info, .customer-info { 
            margin-bottom: 20px; 
            width: 48%; 
            display: inline-block;
            vertical-align: top;
        }
        .customer-info { 
            float: right; 
            text-align: right;
        }
        .items-table { 
            width: 100%; 
            border-collapse: collapse; 
            margin: 20px 0; 
            clear: both;
        }
        .items-table th, .items-table td { 
            border: 1px solid #ddd; 
            padding: 8px; 
            text-align: left; 
        }
        .items-table th { 
            background-color: #f5f5f5; 
            font-weight: bold;
        }
        .items-table .text-right { 
            text-align: right; 
        }
        .total-section { 
            margin-top: 20px; 
            text-align: right; 
            border-top: 1px solid #ddd;
            padding-top: 10px;
        }
        .total-section p { 
            margin: 5px 0; 
        }
        .footer { 
            margin-top: 30px; 
            text-align: center; 
            font-size: 10px;
            color: #666;
        }
        .clear { 
            clear: both; 
        }
        .status { 
            padding: 5px 10px; 
            border-radius: 3px; 
            font-weight: bold; 
            font-size: 11px;
        }
        .status-paid { 
            background-color: #d4edda; 
            color: #155724; 
        }
        .status-pending { 
            background-color: #fff3cd; 
            color: #856404; 
        }
        .status-partial { 
            background-color: #f8d7da; 
            color: #721c24; 
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>INVOICE</h1>
        <h2>' . htmlspecialchars($invoice['invoice_number']) . '</h2>
        <p>Issue Date: ' . date('d M Y', strtotime($invoice['issue_date'])) . ' | Due Date: ' . date('d M Y', strtotime($invoice['due_date'])) . '</p>
        <p>Status: <span class="status status-' . strtolower($invoice['payment_status']) . '">' . $invoice['payment_status'] . '</span></p>
    </div>
    
    <div class="company-info">
        <h3>From: THE SKY Developers</h3>
        <p>FF-29 Shree Sidhheswar Harbours,<br>
        Waghodiya Road, NH-8<br>
        Vadodara - 390025<br>
        Phone: +91 70163 25717<br>
        Email: theskydevelopers@yahoo.com<br>
        GST: NA</p>
    </div>
    
    <div class="customer-info">
        <h3>Bill To:</h3>
        <p><strong>' . htmlspecialchars($customer['name']) . '</strong><br>
        Phone: ' . htmlspecialchars($customer['contact_number']) . '<br>
        Email: ' . htmlspecialchars($customer['email']) . '<br>
        Address: ' . htmlspecialchars($customer['address']) . '<br>
        GST: ' . htmlspecialchars($customer['gst_number']) . '</p>
    </div>
    
    <div class="clear"></div>
    
    <table class="items-table">
        <thead>
            <tr>
                <th>Material</th>
                <th>Description</th>
                <th class="text-right">Quantity</th>
                <th class="text-right">Rate</th>
                <th class="text-right">Total</th>
            </tr>
        </thead>
        <tbody>';
        
        foreach($items as $item) {
            $html .= '<tr>
                <td>' . htmlspecialchars($item['material_name']) . '</td>
                <td>' . htmlspecialchars($item['description']) . '</td>
                <td class="text-right">' . $item['quantity'] . '</td>
                <td class="text-right">₹' . number_format($item['rate'], 2) . '</td>
                <td class="text-right">₹' . number_format($item['total'], 2) . '</td>
            </tr>';
        }
        
        $html .= '</tbody>
    </table>
    
    <div class="total-section">
        <p><strong>Subtotal: ₹' . number_format($invoice['subtotal'], 2) . '</strong></p>
        <p><strong>Grand Total: ₹' . number_format($invoice['grand_total'], 2) . '</strong></p>
    </div>';
    
        if(!empty($invoice['notes'])) {
            $html .= '<div class="footer">
                <p><strong>Notes:</strong> ' . htmlspecialchars($invoice['notes']) . '</p>
            </div>';
        }
        
        $html .= '</body></html>';
        
        return $html;
    }
    
    private static function outputPDF($html, $filename) {
        // Add print-specific CSS and JavaScript
        $html = str_replace('<head>', '<head>
        <style>
            @media print {
                body { margin: 0; padding: 20px; }
                .no-print { display: none !important; }
                .page-break { page-break-before: always; }
            }
        </style>', $html);
        
        $html = str_replace('</body>', '
        <div class="no-print" style="text-align: center; padding: 20px; background: #f0f0f0; margin: 20px;">
            <h3>Document Ready for Printing</h3>
            <p>Use your browser\'s print function (Ctrl+P) to save as PDF</p>
            <button onclick="window.print()" style="padding: 10px 20px; font-size: 16px; background: #007bff; color: white; border: none; border-radius: 5px; cursor: pointer;">Print / Save as PDF</button>
            <button onclick="window.close()" style="padding: 10px 20px; font-size: 16px; background: #6c757d; color: white; border: none; border-radius: 5px; cursor: pointer; margin-left: 10px;">Close</button>
        </div>
        </body>', $html);
        
        // Set headers to open in new window/tab
        header('Content-Type: text/html; charset=UTF-8');
        header('Cache-Control: private, max-age=0, must-revalidate');
        header('Pragma: public');
        
        // Output HTML that opens print dialog
        echo $html;
        exit;
    }
}
?>
