<?php
require_once 'config/database.php';
require_once 'config/session.php';
require_once 'includes/header.php';

requireLogin();

// Get quotation ID
$quotation_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if (!$quotation_id) {
    header('Location: quotations.php');
    exit;
}

try {
    // Get database connection
    $pdo = getConnection();
    
    // Get quotation details
    $stmt = $pdo->prepare("
        SELECT q.*, c.name as customer_name, c.contact_number, c.email, c.address, c.gst_number
        FROM quotations q
        JOIN customers c ON q.customer_id = c.id
        WHERE q.id = ?
    ");
    $stmt->execute([$quotation_id]);
    $quotation = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if (!$quotation) {
        header('Location: quotations.php');
        exit;
    }
    
    // Get quotation items
    $stmt = $pdo->prepare("
        SELECT qi.*, m.name as material_name, m.description
        FROM quotation_items qi
        JOIN materials m ON qi.material_id = m.id
        WHERE qi.quotation_id = ?
        ORDER BY qi.id
    ");
    $stmt->execute([$quotation_id]);
    $items = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
} catch (Exception $e) {
    header('Location: quotations.php');
    exit;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Download Quotation - <?php echo htmlspecialchars($quotation['quotation_number']); ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        .download-section {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 30px;
            border-radius: 10px;
            margin-bottom: 30px;
            text-align: center;
        }
        .download-section h4 {
            margin-bottom: 20px;
        }
        .download-section .btn {
            margin: 5px;
            min-width: 150px;
        }
        .quotation-preview {
            background: white;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
            overflow: hidden;
        }
        .quotation-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 30px;
            text-align: center;
        }
        .quotation-body {
            padding: 30px;
        }
        .company-info, .customer-info {
            margin-bottom: 30px;
        }
        .items-table {
            margin: 20px 0;
        }
        .total-section {
            background: #f8f9fa;
            padding: 20px;
            border-radius: 5px;
            margin-top: 20px;
        }
        .no-print {
            display: block;
        }
        @media print {
            .no-print {
                display: none !important;
            }
        }
    </style>
</head>
<body>
    <div class="container mt-4">
        <!-- Download Section -->
        <div class="download-section no-print">
            <h4><i class="fas fa-file-pdf"></i> Download Quotation as PDF</h4>
            <p>Click the button below to download this quotation as a PDF file.</p>
            <a href="pdf_quotation.php?id=<?php echo $quotation['id']; ?>" class="btn btn-light btn-lg" target="_blank">
                <i class="fas fa-download"></i> Download PDF
            </a>
            <a href="quotations.php" class="btn btn-outline-light">
                <i class="fas fa-arrow-left"></i> Back to Quotations
            </a>
        </div>
        
        <!-- Quotation Preview -->
        <div class="quotation-preview">
            <div class="quotation-header">
                <h1><i class="fas fa-file-invoice"></i> QUOTATION</h1>
                <h2><?php echo htmlspecialchars($quotation['quotation_number']); ?></h2>
                <p class="mb-0">Date: <?php echo date('d M Y', strtotime($quotation['issue_date'])); ?></p>
            </div>
            
            <div class="quotation-body">
                <div class="row">
                    <div class="col-md-6">
                        <div class="company-info">
                            <h4><i class="fas fa-building"></i> From: THE SKY Developers</h4>
                            <p class="mb-1">FF-29 Shree Sidhheswar Harbours,</p>
                            <p class="mb-1">Waghodiya Road, NH-8</p>
                            <p class="mb-1">Vadodara - 390025</p>
                            <p class="mb-1">Phone: +91 70163 25717</p>
                            <p class="mb-1">Email: theskydevelopers@yahoo.com</p>
                            <p class="mb-0">GST: NA</p>
                        </div>
                    </div>
                    
                    <div class="col-md-6">
                        <div class="customer-info">
                            <h4><i class="fas fa-user"></i> Bill To:</h4>
                            <p class="mb-1"><strong><?php echo htmlspecialchars($quotation['customer_name']); ?></strong></p>
                            <p class="mb-1">Phone: <?php echo htmlspecialchars($quotation['contact_number']); ?></p>
                            <p class="mb-1">Email: <?php echo htmlspecialchars($quotation['email']); ?></p>
                            <p class="mb-1">Address: <?php echo htmlspecialchars($quotation['address']); ?></p>
                            <p class="mb-0">GST: <?php echo htmlspecialchars($quotation['gst_number']); ?></p>
                        </div>
                    </div>
                </div>
                
                <div class="items-table">
                    <table class="table table-bordered">
                        <thead class="table-dark">
                            <tr>
                                <th>#</th>
                                <th>Material</th>
                                <th>Description</th>
                                <th class="text-end">Quantity</th>
                                <th class="text-end">Rate</th>
                                <th class="text-end">Total</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($items as $index => $item): ?>
                            <tr>
                                <td><?php echo $index + 1; ?></td>
                                <td><?php echo htmlspecialchars($item['material_name']); ?></td>
                                <td><?php echo htmlspecialchars($item['description']); ?></td>
                                <td class="text-end"><?php echo $item['quantity']; ?></td>
                                <td class="text-end">₹<?php echo number_format($item['rate'], 2); ?></td>
                                <td class="text-end">₹<?php echo number_format($item['total'], 2); ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                
                <div class="total-section">
                    <div class="row">
                        <div class="col-md-8"></div>
                        <div class="col-md-4">
                            <div class="d-flex justify-content-between">
                                <strong>Subtotal:</strong>
                                <strong>₹<?php echo number_format($quotation['subtotal'], 2); ?></strong>
                            </div>
                            <div class="d-flex justify-content-between">
                                <strong>Grand Total:</strong>
                                <strong>₹<?php echo number_format($quotation['grand_total'], 2); ?></strong>
                            </div>
                        </div>
                    </div>
                </div>
                
                <?php if (!empty($quotation['notes'])): ?>
                <div class="mt-4">
                    <h5>Notes:</h5>
                    <p><?php echo nl2br(htmlspecialchars($quotation['notes'])); ?></p>
                </div>
                <?php endif; ?>
                
                <div class="text-center mt-4">
                    <p class="text-muted">Thank you for your business!</p>
                    <p class="text-muted"><small>This quotation is valid for 30 days from the date of issue.</small></p>
                </div>
            </div>
        </div>
    </div>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>