<?php
/**
 * Quote Preview Template
 *
 * @package     EasyInvoice
 * @since       1.0.0
 */

// Get quote data
$quote_id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
$quote = $quote ?? null;

if ($quote_id <= 0 || !$quote) {
    wp_die(__('Quote not found.', 'easy-invoice'));
}

// Initialize formatter for currency formatting
$formatter = $quote ? new \EasyInvoice\Helpers\QuoteFormatter($quote) : null;

// Get the current template (default to 'standard')
$current_template = $quote && method_exists($quote, 'getTemplate') ? $quote->getTemplate() : 'standard';
if (empty($current_template)) {
    $current_template = 'standard';
}

// Get company information from settings
$company_name = get_option('easy_invoice_company_name', 'Your Company Name');
$company_address = get_option('easy_invoice_company_address', 'Your Company Address');
$company_email = get_option('easy_invoice_company_email', 'info@yourcompany.com');
$company_phone = get_option('easy_invoice_company_phone', '+1 (555) 123-4567');
$company_website = get_option('easy_invoice_company_website', 'www.yourcompany.com');

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Quote <?php echo $quote ? esc_html($quote->getNumber()) : 'Unknown'; ?></title>

    <!-- Load jsPDF for PDF generation -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>

    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            line-height: 1.6;
            color: #333;
            background: #f8f9fa;
        }

        .quote-container {
            max-width: 800px;
            margin: 20px auto;
            background: white;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
            overflow: hidden;

        }

        .quote-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 30px;
            text-align: center;
        }

        .quote-header h1 {
            font-size: 2.5rem;
            font-weight: 300;
            margin-bottom: 10px;
        }

        .quote-header .quote-number {
            font-size: 1.2rem;
            opacity: 0.9;
        }

        .quote-content {
            padding: 40px;
        }

        .company-info {
            display: flex;
            justify-content: space-between;
            margin-bottom: 40px;
            padding-bottom: 20px;
            border-bottom: 2px solid #e9ecef;
        }

        .company-details h2 {
            color: #495057;
            font-size: 1.5rem;
            margin-bottom: 10px;
        }

        .company-details p {
            color: #6c757d;
            margin-bottom: 5px;
        }

        .client-info h3 {
            color: #495057;
            font-size: 1.2rem;
            margin-bottom: 10px;
        }

        .client-info p {
            color: #6c757d;
            margin-bottom: 5px;
        }

        .quote-details {
            margin-bottom: 30px;
        }

        .quote-details table {
            width: 100%;
            border-collapse: collapse;
        }

        .quote-details th,
        .quote-details td {
            padding: 8px;
            text-align: left;
            border-bottom: 1px solid #e9ecef;
        }

        .quote-details th {
            background-color: #f8f9fa;
            font-weight: 600;
            color: #495057;
        }

        .items-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 30px;
        }

        .items-table th,
        .items-table td {
            padding: 12px 8px;
            text-align: left;
            border-bottom: 1px solid #e9ecef;
        }

        .items-table th {
            background-color: #f8f9fa;
            font-weight: 600;
            color: #495057;
        }

        .item-name {
            font-weight: 500;
            color: #495057;
        }

        .item-description {
            font-size: 0.9rem;
            color: #6c757d;
            margin-top: 4px;
        }

        .amount {
            font-weight: 600;
            color: #495057;
        }

        .totals-section {
            margin-bottom: 30px;
        }

        .totals-table {
            width: 100%;
            max-width: 300px;
            margin-left: auto;
        }

        .total-row {
            display: flex;
            justify-content: space-between;
            padding: 8px 0;
            border-bottom: 1px solid #e9ecef;
        }

        .total-row:last-child {
            border-bottom: 2px solid #495057;
            font-weight: 600;
            font-size: 1.1rem;
        }

        .notes-section {
            margin-bottom: 30px;
        }

        .notes-section h3 {
            color: #495057;
            font-size: 1.2rem;
            margin-bottom: 10px;
        }

        .notes-section p {
            color: #6c757d;
            line-height: 1.6;
        }

        .quote-footer {
            background-color: #f8f9fa;
            padding: 20px;
            text-align: center;
            color: #6c757d;
        }

        .quote-footer p {
            margin-bottom: 5px;
        }

        /* Print styles */
        @media print {
            body {
                background: white;
            }

            .quote-container {
                box-shadow: none;
                margin: 0;
            }
        }
    </style>
</head>
<body>
    <div class="quote-container">
        <?php
        // Dynamically load the selected template
		$template_file = EASY_INVOICE_PLUGIN_DIR . 'templates/quote-templates/' . $current_template . '.php';

		$template_file = file_exists($template_file) ? $template_file : EASY_INVOICE_PLUGIN_DIR . 'templates/quote-templates/default.php';
		if (file_exists($template_file)) {
            include_once $template_file;
        } else {
            // Fallback to standard template if selected template doesn't exist
            $template_file = EASY_INVOICE_PLUGIN_DIR . 'templates/quote-templates/standard.php';
            if (file_exists($template_file)) {
                include_once $template_file;
            } else {
                // Show error if no template is available
                echo '<div style="padding: 40px; text-align: center;">';
                echo '<h2>Template Error</h2>';
                echo '<p>The selected template "' . esc_html($current_template) . '" could not be loaded.</p>';
                echo '</div>';
            }
        }
        ?>
        </div>

    <!-- Quote data for PDF generation -->
    <script>
    // Quote data for PDF generation
    window.quoteData = <?php
        $quote_data = $quote ? \EasyInvoice\Includes\Helpers\PdfHelper::getInvoiceDataForPdf($quote) : [];
        if ($quote) {
            $quote_data['status'] = $quote->getStatus();
        }
        echo json_encode($quote_data);
    ?>;

    // Global function for downloading PDF
    function downloadQuotePdf(quoteId) {
        try {
            if (typeof QuotePdfGenerator !== 'undefined') {
                // Use the new PDF generator that captures HTML directly
                const pdfGenerator = new QuotePdfGenerator();
                pdfGenerator.generatePDF();

                // Show success message after a short delay
                setTimeout(function() {
                    alert('PDF generated successfully');
                }, 2000);
            } else {
                alert('Error: PDF generator not available');
            }
        } catch (error) {
            console.error('PDF generation error:', error);
            alert('Error generating PDF');
        }
    }
    </script>
</body>
</html>
