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

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

// Get all invoices
$stmt = $pdo->query("SELECT * FROM invoices ORDER BY created_at DESC");
$invoices = $stmt->fetchAll();

// Get company settings
$company_name = getSetting($pdo, 'site_name') ?: 'SellBazar';
$company_address = getSetting($pdo, 'company_address') ?: 'Dhaka, Bangladesh';
$company_phone = getSetting($pdo, 'company_phone') ?: '+880 1712-345678';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Invoice List</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background: #f1f5f9; padding: 20px; }
        .container { max-width: 210mm; margin: 0 auto; background: #fff; padding: 30px; border-radius: 12px; box-shadow: 0 5px 30px rgba(0,0,0,0.1); }
        .header { border-bottom: 3px solid #047dc3; padding-bottom: 20px; margin-bottom: 20px; text-align: center; }
        .header h1 { font-size: 28px; color: #0f172a; }
        .header h1 span { color: #047dc3; }
        .header p { color: #64748b; }
        table { width: 100%; border-collapse: collapse; }
        thead { background: #f1f5f9; }
        th, td { padding: 10px 12px; text-align: left; border-bottom: 1px solid #e2e8f0; }
        th { font-weight: 600; color: #475569; text-transform: uppercase; font-size: 12px; }
        .text-right { text-align: right; }
        .status-badge { padding: 3px 10px; border-radius: 20px; font-size: 12px; font-weight: 500; }
        .status-badge.draft { background: #e2e8f0; color: #64748b; }
        .status-badge.paid { background: #86efac; color: #166534; }
        .status-badge.partial { background: #fcd34d; color: #92400e; }
        .status-badge.overdue { background: #fca5a5; color: #991b1b; }
        .footer { margin-top: 20px; text-align: center; color: #94a3b8; font-size: 13px; border-top: 1px solid #e2e8f0; padding-top: 15px; }
        .print-actions { text-align: center; padding: 20px; }
        .btn-print { padding: 12px 40px; background: #0f172a; color: #fff; border: none; border-radius: 8px; cursor: pointer; }
        .btn-back { padding: 12px 30px; background: #e2e8f0; color: #0f172a; border: none; border-radius: 8px; cursor: pointer; margin-right: 10px; }
        @media print { body { background: #fff; padding: 0; } .container { box-shadow: none; border-radius: 0; padding: 20px; } .print-actions { display: none !important; } }
        @media (max-width: 768px) { .container { padding: 15px; } th, td { padding: 6px 8px; font-size: 13px; } }
    </style>
</head>
<body>
<div class="container">
    <div class="header">
        <h1>Sell<span>Bazar</span></h1>
        <p><?php echo $company_address; ?> | <?php echo $company_phone; ?></p>
        <h2 style="margin-top:10px;">Invoice List</h2>
        <p>Generated: <?php echo date('d M Y, h:i A'); ?></p>
    </div>
    
    <table>
        <thead>
            <tr>
                <th>Invoice No</th>
                <th>Customer</th>
                <th class="text-right">Total</th>
                <th class="text-right">Paid</th>
                <th class="text-right">Due</th>
                <th>Status</th>
                <th>Date</th>
            </tr>
        </thead>
        <tbody>
            <?php if (empty($invoices)): ?>
            <tr><td colspan="7" class="text-center">No invoices found</td></tr>
            <?php else: ?>
            <?php foreach ($invoices as $invoice): ?>
            <tr>
                <td><code><?php echo $invoice['invoice_no']; ?></code></td>
                <td><?php echo $invoice['customer_name']; ?></td>
                <td class="text-right"><?php echo number_format($invoice['total']); ?> TK</td>
                <td class="text-right"><?php echo number_format($invoice['paid_amount']); ?> TK</td>
                <td class="text-right" style="color:#ef4444; font-weight:600;"><?php echo number_format($invoice['due_amount']); ?> TK</td>
                <td><span class="status-badge <?php echo $invoice['status']; ?>"><?php echo ucfirst($invoice['status']); ?></span></td>
                <td><?php echo date('d M Y', strtotime($invoice['invoice_date'])); ?></td>
            </tr>
            <?php endforeach; ?>
            <?php endif; ?>
        </tbody>
    </table>
    
    <div class="footer">
        <p>&copy; <?php echo date('Y'); ?> <?php echo $company_name; ?> - All Rights Reserved</p>
    </div>
</div>

<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>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
</body>
</html>