<?php
// admin/accounts/view-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';

include '../header.php';
?>

<style>
.invoice-view-wrapper {
    max-width: 1000px;
    margin: 0 auto;
}

.invoice-view-card {
    background: #fff;
    border-radius: 12px;
    padding: 30px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.invoice-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    padding-bottom: 20px;
    border-bottom: 2px solid #e2e8f0;
    margin-bottom: 20px;
    flex-wrap: wrap;
}

.invoice-header .company-info h2 {
    font-size: 24px;
    font-weight: 700;
    color: #0f172a;
}

.invoice-header .company-info h2 span {
    color: #047dc3;
}

.invoice-header .company-info p {
    color: #64748b;
    font-size: 14px;
    margin: 0;
}

.invoice-header .invoice-title {
    text-align: right;
}

.invoice-header .invoice-title h3 {
    font-size: 28px;
    font-weight: 700;
    color: #047dc3;
}

.invoice-header .invoice-title .invoice-no {
    font-size: 16px;
    color: #64748b;
}

.billing-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 30px;
    margin-bottom: 25px;
}

.billing-box h4 {
    font-size: 14px;
    font-weight: 600;
    color: #64748b;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 8px;
}

.billing-box p {
    font-size: 14px;
    line-height: 1.7;
    margin: 0;
}

.billing-box .label {
    color: #64748b;
    font-weight: 500;
}

.status-badge {
    padding: 4px 14px;
    border-radius: 20px;
    font-size: 13px;
    font-weight: 500;
    display: inline-block;
}

.status-badge.draft { background: #e2e8f0; color: #64748b; }
.status-badge.sent { background: #93c5fd; color: #1e40af; }
.status-badge.paid { background: #86efac; color: #166534; }
.status-badge.partial { background: #fcd34d; color: #92400e; }
.status-badge.overdue { background: #fca5a5; color: #991b1b; }
.status-badge.cancelled { background: #e2e8f0; color: #475569; }

.invoice-table {
    width: 100%;
    border-collapse: collapse;
    margin: 20px 0;
}

.invoice-table thead {
    background: #f1f5f9;
}

.invoice-table thead th {
    padding: 12px 15px;
    text-align: left;
    font-weight: 600;
    font-size: 13px;
    color: #475569;
    text-transform: uppercase;
}

.invoice-table tbody td {
    padding: 12px 15px;
    border-bottom: 1px solid #e2e8f0;
    font-size: 14px;
}

.invoice-table tbody tr:last-child td {
    border-bottom: none;
}

.text-right {
    text-align: right;
}

.summary-box {
    max-width: 350px;
    margin-left: auto;
    padding-top: 15px;
    border-top: 2px solid #e2e8f0;
}

.summary-box .summary-row {
    display: flex;
    justify-content: space-between;
    padding: 6px 0;
    font-size: 14px;
}

.summary-box .summary-row.total {
    font-size: 18px;
    font-weight: 700;
    border-top: 2px solid #0f172a;
    padding-top: 10px;
    margin-top: 5px;
}

.summary-box .summary-row.total .amount {
    color: #047dc3;
}

.amount-paid {
    color: #22c55e;
}

.amount-due {
    color: #ef4444;
}

.invoice-actions {
    display: flex;
    gap: 10px;
    margin-top: 25px;
    flex-wrap: wrap;
}

.invoice-actions .btn {
    padding: 10px 25px;
    border-radius: 8px;
    font-weight: 500;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    transition: all 0.3s ease;
}

.btn-primary {
    background: #047dc3;
    color: #fff;
    border: none;
}

.btn-primary:hover {
    background: #0369a1;
    color: #fff;
}

.btn-success {
    background: #22c55e;
    color: #fff;
    border: none;
}

.btn-success:hover {
    background: #16a34a;
    color: #fff;
}

.btn-warning {
    background: #f59e0b;
    color: #fff;
    border: none;
}

.btn-warning:hover {
    background: #d97706;
    color: #fff;
}

.btn-danger {
    background: #ef4444;
    color: #fff;
    border: none;
}

.btn-danger:hover {
    background: #dc2626;
    color: #fff;
}

.btn-secondary {
    background: #e2e8f0;
    color: #0f172a;
    border: none;
}

.btn-secondary:hover {
    background: #cbd5e1;
}

.invoice-note {
    background: #f8fafc;
    padding: 15px;
    border-radius: 8px;
    margin-top: 15px;
    border-left: 4px solid #047dc3;
}

.invoice-note strong {
    display: block;
    margin-bottom: 5px;
}

@media (max-width: 768px) {
    .invoice-header {
        flex-direction: column;
        text-align: center;
    }
    
    .invoice-header .invoice-title {
        text-align: center;
        margin-top: 10px;
    }
    
    .billing-grid {
        grid-template-columns: 1fr;
        gap: 15px;
    }
    
    .summary-box {
        max-width: 100%;
    }
    
    .invoice-actions {
        justify-content: center;
    }
}
</style>

<div class="admin-content">
    <div class="invoice-view-wrapper">
        <div class="invoice-view-card">
            <!-- Header -->
            <div class="invoice-header">
                <div class="company-info">
                    <h2>Sell<span>Bazar</span></h2>
                    <p><?php echo $company_address; ?></p>
                    <p>Phone: <?php echo $company_phone; ?> | Email: <?php echo $company_email; ?></p>
                </div>
                <div class="invoice-title">
                    <h3>INVOICE</h3>
                    <div class="invoice-no"># <?php echo $invoice['invoice_no']; ?></div>
                    <span class="status-badge <?php echo $invoice['status']; ?>"><?php echo ucfirst($invoice['status']); ?></span>
                </div>
            </div>
            
            <!-- Billing Info -->
            <div class="billing-grid">
                <div class="billing-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="billing-box">
                    <h4>Invoice Details</h4>
                    <p>
                        <span class="label">Invoice Date:</span> <?php echo date('d M Y', strtotime($invoice['invoice_date'])); ?><br>
                        <span class="label">Due Date:</span> <?php echo date('d M Y', strtotime($invoice['due_date'])); ?><br>
                        <span class="label">Created:</span> <?php echo date('d M Y, h:i A', strtotime($invoice['created_at'])); ?>
                    </p>
                </div>
            </div>
            
            <!-- Items Table -->
            <table class="invoice-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 
                    // Sample items - you can fetch from a separate table
                    $items = [
                        ['description' => 'Premium Cotton Shirt', 'qty' => 2, 'price' => 1200, 'total' => 2400],
                        ['description' => 'Designer Saree', 'qty' => 1, 'price' => 2500, 'total' => 2500],
                    ];
                    ?>
                    <?php foreach ($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>
            
            <!-- Summary -->
            <div class="summary-box">
                <div class="summary-row">
                    <span>Subtotal</span>
                    <span><?php echo number_format($invoice['subtotal']); ?> TK</span>
                </div>
                <?php if ($invoice['tax'] > 0): ?>
                <div class="summary-row">
                    <span>Tax</span>
                    <span><?php echo number_format($invoice['tax']); ?> TK</span>
                </div>
                <?php endif; ?>
                <?php if ($invoice['discount'] > 0): ?>
                <div class="summary-row">
                    <span>Discount</span>
                    <span>-<?php echo number_format($invoice['discount']); ?> TK</span>
                </div>
                <?php endif; ?>
                <div class="summary-row total">
                    <span>Total</span>
                    <span class="amount"><?php echo number_format($invoice['total']); ?> TK</span>
                </div>
                <div class="summary-row">
                    <span>Paid Amount</span>
                    <span class="amount-paid"><?php echo number_format($invoice['paid_amount']); ?> TK</span>
                </div>
                <div class="summary-row" style="font-weight:600; border-top:1px solid #e2e8f0; padding-top:8px;">
                    <span>Due Amount</span>
                    <span class="amount-due"><?php echo number_format($invoice['due_amount']); ?> TK</span>
                </div>
            </div>
            
            <!-- Note -->
            <?php if ($invoice['note']): ?>
            <div class="invoice-note">
                <strong>Note:</strong>
                <p><?php echo $invoice['note']; ?></p>
            </div>
            <?php endif; ?>
            
            <!-- Actions -->
            <div class="invoice-actions">
                <a href="edit-invoice.php?id=<?php echo $invoice['id']; ?>" class="btn btn-warning">
                    <i class="fa-solid fa-edit"></i> Edit
                </a>
                <a href="print-invoice.php?id=<?php echo $invoice['id']; ?>" class="btn btn-success" target="_blank">
                    <i class="fa-solid fa-print"></i> Print
                </a>
                <a href="invoices.php" class="btn btn-secondary">
                    <i class="fa-solid fa-arrow-left"></i> Back to List
                </a>
            </div>
        </div>
    </div>
</div>

<?php include '../footer.php'; ?>