<?php
// admin/accounts/print-invoice.php
require_once __DIR__ . '/../../includes/config.php';
require_once __DIR__ . '/../../includes/functions.php';

if (!isAdmin()) {
    exit('Unauthorized');
}

$id = intval($_GET['id'] ?? 0);
if ($id <= 0) {
    exit('Invalid Invoice ID');
}

// Get invoice details
$stmt = $pdo->prepare("SELECT * FROM invoices WHERE id = ?");
$stmt->execute([$id]);
$invoice = $stmt->fetch();

if (!$invoice) {
    exit('Invoice not found');
}

// Get company settings
$company_name = getSetting($pdo, 'site_name') ?: 'SellBazar';
$company_logo = getSetting($pdo, 'site_logo') ?: 'logo.png';
$company_address = getSetting($pdo, 'company_address') ?: 'Dhaka, Bangladesh';
$company_phone = getSetting($pdo, 'company_phone') ?: '+880 1712-345678';
$company_email = getSetting($pdo, 'company_email') ?: 'info@sellbazar.com';
$company_website = getSetting($pdo, 'company_website') ?: 'www.sellbazar.com';

// Sample invoice items (you can store these in a separate table)
$items = [
    ['description' => 'Product 1', 'qty' => 2, 'price' => 500, 'total' => 1000],
    ['description' => 'Product 2', 'qty' => 1, 'price' => 750, 'total' => 750],
];
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Invoice <?php echo $invoice['invoice_no']; ?></title>
    <style>
        /* ===== A4 Invoice Print Styles ===== */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: #f1f5f9;
            padding: 20px;
        }
        
        .invoice-wrapper {
            max-width: 210mm; /* A4 width */
            margin: 0 auto;
            background: #fff;
            box-shadow: 0 5px 30px rgba(0,0,0,0.1);
            border-radius: 12px;
            overflow: hidden;
        }
        
        /* ===== LETTERHEAD ===== */
        .letterhead {
            background: #0f172a;
            color: #fff;
            padding: 30px 40px;
            border-bottom: 5px solid #047dc3;
        }
        
        .letterhead .row {
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
        }
        
        .letterhead .company-info h1 {
            font-size: 28px;
            font-weight: 700;
            letter-spacing: 1px;
        }
        
        .letterhead .company-info h1 span {
            color: #047dc3;
        }
        
        .letterhead .company-info .tagline {
            font-size: 14px;
            opacity: 0.7;
        }
        
        .letterhead .company-details {
            text-align: right;
            font-size: 13px;
            line-height: 1.6;
            opacity: 0.9;
        }
        
        .letterhead .company-details i {
            margin-right: 5px;
            width: 16px;
        }
        
        /* ===== INVOICE HEADER ===== */
        .invoice-header {
            padding: 30px 40px 20px;
            background: #f8fafc;
            border-bottom: 1px solid #e2e8f0;
        }
        
        .invoice-header .row {
            display: flex;
            justify-content: space-between;
            flex-wrap: wrap;
        }
        
        .invoice-header .title h2 {
            font-size: 32px;
            font-weight: 700;
            color: #0f172a;
        }
        
        .invoice-header .title .invoice-no {
            font-size: 16px;
            color: #64748b;
        }
        
        .invoice-header .info-box {
            text-align: right;
        }
        
        .invoice-header .info-box p {
            margin-bottom: 4px;
            font-size: 14px;
        }
        
        .invoice-header .info-box strong {
            color: #0f172a;
        }
        
        /* ===== BILLING INFO ===== */
        .billing-section {
            padding: 20px 40px;
            display: flex;
            justify-content: space-between;
            flex-wrap: wrap;
            border-bottom: 1px solid #e2e8f0;
        }
        
        .billing-section .box h4 {
            font-size: 14px;
            font-weight: 600;
            color: #64748b;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            margin-bottom: 8px;
        }
        
        .billing-section .box p {
            font-size: 14px;
            line-height: 1.6;
        }
        
        /* ===== ITEMS TABLE ===== */
        .items-section {
            padding: 20px 40px;
        }
        
        .items-table {
            width: 100%;
            border-collapse: collapse;
        }
        
        .items-table thead {
            background: #f1f5f9;
        }
        
        .items-table thead th {
            padding: 12px 15px;
            text-align: left;
            font-size: 13px;
            font-weight: 600;
            text-transform: uppercase;
            color: #475569;
            border-bottom: 2px solid #e2e8f0;
        }
        
        .items-table tbody td {
            padding: 12px 15px;
            border-bottom: 1px solid #e2e8f0;
            font-size: 14px;
        }
        
        .items-table tbody tr:last-child td {
            border-bottom: none;
        }
        
        .items-table .text-right {
            text-align: right;
        }
        
        .items-table .total-row {
            font-weight: 600;
        }
        
        /* ===== SUMMARY ===== */
        .summary-section {
            padding: 20px 40px 30px;
            border-top: 2px solid #e2e8f0;
        }
        
        .summary-section .row {
            display: flex;
            justify-content: flex-end;
        }
        
        .summary-section .summary-box {
            width: 300px;
        }
        
        .summary-section .summary-box .line {
            display: flex;
            justify-content: space-between;
            padding: 6px 0;
            font-size: 14px;
        }
        
        .summary-section .summary-box .line.total {
            font-size: 18px;
            font-weight: 700;
            border-top: 2px solid #0f172a;
            padding-top: 10px;
            margin-top: 5px;
        }
        
        .summary-section .summary-box .line.total .amount {
            color: #047dc3;
        }
        
        /* ===== FOOTER ===== */
        .invoice-footer {
            padding: 15px 40px;
            background: #f8fafc;
            border-top: 1px solid #e2e8f0;
            display: flex;
            justify-content: space-between;
            flex-wrap: wrap;
            font-size: 13px;
            color: #64748b;
        }
        
        .invoice-footer .terms {
            max-width: 60%;
        }
        
        .invoice-footer .signature {
            text-align: right;
        }
        
        .invoice-footer .signature .sig-line {
            margin-top: 20px;
            border-top: 1px solid #94a3b8;
            width: 150px;
            margin-left: auto;
        }
        
        /* ===== PRINT BUTTON ===== */
        .print-actions {
            text-align: center;
            padding: 20px;
            background: #fff;
            margin-top: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
        }
        
        .btn-print {
            padding: 12px 40px;
            background: #0f172a;
            color: #fff;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        
        .btn-print:hover {
            background: #047dc3;
        }
        
        .btn-back {
            padding: 12px 30px;
            background: #e2e8f0;
            color: #0f172a;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-right: 10px;
        }
        
        .btn-back:hover {
            background: #cbd5e1;
        }
        
        /* ===== PRINT STYLES ===== */
        @media print {
            body {
                background: #fff;
                padding: 0;
            }
            
            .invoice-wrapper {
                box-shadow: none;
                border-radius: 0;
                max-width: 100%;
            }
            
            .print-actions {
                display: none !important;
            }
            
            .letterhead {
                padding: 20px 30px;
            }
            
            .invoice-header,
            .billing-section,
            .items-section,
            .summary-section,
            .invoice-footer {
                padding: 15px 30px;
            }
            
            .items-table thead th {
                background: #f1f5f9 !important;
                -webkit-print-color-adjust: exact;
                print-color-adjust: exact;
            }
            
            .letterhead {
                background: #0f172a !important;
                -webkit-print-color-adjust: exact;
                print-color-adjust: exact;
            }
        }
        
        /* ===== RESPONSIVE ===== */
        @media (max-width: 768px) {
            .letterhead .row {
                flex-direction: column;
                text-align: center;
            }
            
            .letterhead .company-details {
                text-align: center;
                margin-top: 15px;
            }
            
            .invoice-header .row {
                flex-direction: column;
                text-align: center;
            }
            
            .invoice-header .info-box {
                text-align: center;
                margin-top: 10px;
            }
            
            .billing-section {
                flex-direction: column;
                text-align: center;
            }
            
            .billing-section .box {
                margin-bottom: 15px;
            }
            
            .summary-section .row {
                justify-content: center;
            }
            
            .invoice-footer {
                flex-direction: column;
                text-align: center;
            }
            
            .invoice-footer .terms {
                max-width: 100%;
                margin-bottom: 15px;
            }
            
            .invoice-footer .signature {
                text-align: center;
            }
            
            .invoice-footer .signature .sig-line {
                margin-left: auto;
                margin-right: auto;
            }
        }
    </style>
</head>
<body>

<div class="invoice-wrapper">
    <!-- ===== LETTERHEAD ===== -->
    <div class="letterhead">
        <div class="row">
            <div class="company-info">
                <h1>Sell<span>Bazar</span></h1>
                <div class="tagline">Your Trusted Online Shopping Store</div>
            </div>
            <div class="company-details">
                <div><i class="fa-solid fa-location-dot"></i> <?php echo $company_address; ?></div>
                <div><i class="fa-solid fa-phone"></i> <?php echo $company_phone; ?></div>
                <div><i class="fa-solid fa-envelope"></i> <?php echo $company_email; ?></div>
                <div><i class="fa-solid fa-globe"></i> <?php echo $company_website; ?></div>
            </div>
        </div>
    </div>
    
    <!-- ===== INVOICE HEADER ===== -->
    <div class="invoice-header">
        <div class="row">
            <div class="title">
                <h2>INVOICE</h2>
                <div class="invoice-no"># <?php echo $invoice['invoice_no']; ?></div>
            </div>
            <div class="info-box">
                <p><strong>Invoice Date:</strong> <?php echo date('d M Y', strtotime($invoice['invoice_date'])); ?></p>
                <p><strong>Due Date:</strong> <?php echo date('d M Y', strtotime($invoice['due_date'])); ?></p>
                <p><strong>Status:</strong> <span class="status-badge <?php echo $invoice['status']; ?>"><?php echo ucfirst($invoice['status']); ?></span></p>
            </div>
        </div>
    </div>
    
    <!-- ===== BILLING INFO ===== -->
    <div class="billing-section">
        <div class="box">
            <h4>Bill To</h4>
            <p>
                <strong><?php echo $invoice['customer_name']; ?></strong><br>
                <?php echo $invoice['customer_address'] ?? 'N/A'; ?><br>
                <?php if ($invoice['customer_phone']): ?>
                Phone: <?php echo $invoice['customer_phone']; ?><br>
                <?php endif; ?>
                <?php if ($invoice['customer_email']): ?>
                Email: <?php echo $invoice['customer_email']; ?>
                <?php endif; ?>
            </p>
        </div>
        <div class="box">
            <h4>Invoice Details</h4>
            <p>
                <strong>Invoice #:</strong> <?php echo $invoice['invoice_no']; ?><br>
                <strong>Total:</strong> <?php echo number_format($invoice['total']); ?> TK<br>
                <strong>Paid:</strong> <?php echo number_format($invoice['paid_amount']); ?> TK<br>
                <strong>Due:</strong> <?php echo number_format($invoice['due_amount']); ?> TK
            </p>
        </div>
    </div>
    
    <!-- ===== ITEMS TABLE ===== -->
    <div class="items-section">
        <table class="items-table">
            <thead>
                <tr>
                    <th style="width:50%;">Description</th>
                    <th style="width:15%;" class="text-right">Quantity</th>
                    <th style="width:20%;" class="text-right">Unit Price</th>
                    <th style="width:15%;" class="text-right">Total</th>
                </tr>
            </thead>
            <tbody>
                <?php 
                // You can fetch items from a separate table
                // For now, using sample data
                $sample_items = [
                    ['description' => 'Premium Cotton Shirt', 'qty' => 2, 'price' => 1200, 'total' => 2400],
                    ['description' => 'Designer Saree', 'qty' => 1, 'price' => 2500, 'total' => 2500],
                    ['description' => 'Leather Handbag', 'qty' => 1, 'price' => 3500, 'total' => 3500],
                ];
                ?>
                <?php foreach ($sample_items as $item): ?>
                <tr>
                    <td><?php echo $item['description']; ?></td>
                    <td class="text-right"><?php echo $item['qty']; ?></td>
                    <td class="text-right"><?php echo number_format($item['price']); ?> TK</td>
                    <td class="text-right"><?php echo number_format($item['total']); ?> TK</td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    
    <!-- ===== SUMMARY ===== -->
    <div class="summary-section">
        <div class="row">
            <div class="summary-box">
                <div class="line">
                    <span>Subtotal</span>
                    <span><?php echo number_format($invoice['subtotal']); ?> TK</span>
                </div>
                <?php if ($invoice['tax'] > 0): ?>
                <div class="line">
                    <span>Tax</span>
                    <span><?php echo number_format($invoice['tax']); ?> TK</span>
                </div>
                <?php endif; ?>
                <?php if ($invoice['discount'] > 0): ?>
                <div class="line">
                    <span>Discount</span>
                    <span>-<?php echo number_format($invoice['discount']); ?> TK</span>
                </div>
                <?php endif; ?>
                <div class="line total">
                    <span>Total</span>
                    <span class="amount"><?php echo number_format($invoice['total']); ?> TK</span>
                </div>
                <div class="line">
                    <span>Paid Amount</span>
                    <span><?php echo number_format($invoice['paid_amount']); ?> TK</span>
                </div>
                <div class="line" style="font-weight:600; color:#ef4444;">
                    <span>Due Amount</span>
                    <span><?php echo number_format($invoice['due_amount']); ?> TK</span>
                </div>
            </div>
        </div>
    </div>
    
    <!-- ===== FOOTER ===== -->
    <div class="invoice-footer">
        <div class="terms">
            <strong>Terms & Conditions:</strong>
            <p>Please make payment within the due date. Late payments may incur additional charges.</p>
            <?php if ($invoice['note']): ?>
            <p><strong>Note:</strong> <?php echo $invoice['note']; ?></p>
            <?php endif; ?>
        </div>
        <div class="signature">
            <p><strong>Authorized Signature</strong></p>
            <div class="sig-line"></div>
            <p style="font-size:12px; margin-top:5px;"><?php echo $company_name; ?></p>
        </div>
    </div>
</div>

<!-- ===== PRINT ACTIONS ===== -->
<div class="print-actions">
    <button class="btn-back" onclick="window.location.href='invoices.php'">
        <i class="fa-solid fa-arrow-left"></i> Back
    </button>
    <button class="btn-print" onclick="window.print()">
        <i class="fa-solid fa-print"></i> Print / Download PDF
    </button>
</div>

<!-- Font Awesome for icons -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">

</body>
</html>