<?php
// admin/accounts/print-po.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 PO ID');
}

// Get PO details
$stmt = $pdo->prepare("SELECT po.*, v.name as vendor_name, v.company, v.address, v.phone, v.email, v.tax_id
                       FROM purchase_orders po 
                       LEFT JOIN vendors v ON po.vendor_id = v.id 
                       WHERE po.id = ?");
$stmt->execute([$id]);
$po = $stmt->fetch();

if (!$po) {
    exit('Purchase Order not found');
}

// Get PO items
$stmt = $pdo->prepare("SELECT * FROM po_items WHERE po_id = ?");
$stmt->execute([$id]);
$items = $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';
$company_email = getSetting($pdo, 'company_email') ?: 'info@sellbazar.com';
$company_logo = getSetting($pdo, 'site_logo') ?: 'logo.png';

// Calculate totals
$grand_total = 0;
foreach ($items as $item) {
    $grand_total += $item['total'];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Purchase Order <?php echo $po['po_no']; ?></title>
    <style>
        /* ========================================
           PURCHASE ORDER PRINT STYLES - A4 FORMAT
           ======================================== */
        
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: #f1f5f9;
            padding: 20px;
            color: #1e293b;
        }
        
        .po-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: 25px 35px;
            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: 26px;
            font-weight: 700;
            letter-spacing: 1px;
        }
        
        .letterhead .company-info h1 span {
            color: #047dc3;
        }
        
        .letterhead .company-info .tagline {
            font-size: 13px;
            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;
        }
        
        /* ===== PO HEADER ===== */
        .po-header {
            padding: 25px 35px 20px;
            background: #f8fafc;
            border-bottom: 2px solid #e2e8f0;
        }
        
        .po-header .row {
            display: flex;
            justify-content: space-between;
            flex-wrap: wrap;
        }
        
        .po-header .title h2 {
            font-size: 28px;
            font-weight: 700;
            color: #0f172a;
        }
        
        .po-header .title .po-no {
            font-size: 16px;
            color: #64748b;
        }
        
        .po-header .info-box {
            text-align: right;
        }
        
        .po-header .info-box p {
            margin-bottom: 4px;
            font-size: 14px;
        }
        
        .po-header .info-box strong {
            color: #0f172a;
        }
        
        /* ===== PARTIES SECTION ===== */
        .parties-section {
            padding: 20px 35px;
            display: flex;
            justify-content: space-between;
            flex-wrap: wrap;
            border-bottom: 1px solid #e2e8f0;
        }
        
        .parties-section .box h4 {
            font-size: 14px;
            font-weight: 600;
            color: #64748b;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            margin-bottom: 8px;
        }
        
        .parties-section .box p {
            font-size: 14px;
            line-height: 1.6;
        }
        
        /* ===== ITEMS TABLE ===== */
        .items-section {
            padding: 20px 35px;
        }
        
        .items-table {
            width: 100%;
            border-collapse: collapse;
        }
        
        .items-table thead {
            background: #f1f5f9;
        }
        
        .items-table thead th {
            padding: 10px 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: 10px 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;
        }
        
        /* ===== SUMMARY ===== */
        .summary-section {
            padding: 15px 35px 25px;
            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: 5px 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;
        }
        
        .summary-section .summary-box .line .amount-paid {
            color: #22c55e;
        }
        
        .summary-section .summary-box .line .amount-due {
            color: #ef4444;
        }
        
        /* ===== FOOTER ===== */
        .po-footer {
            padding: 15px 35px;
            background: #f8fafc;
            border-top: 1px solid #e2e8f0;
            display: flex;
            justify-content: space-between;
            flex-wrap: wrap;
            font-size: 13px;
            color: #64748b;
        }
        
        .po-footer .terms {
            max-width: 60%;
        }
        
        .po-footer .signature {
            text-align: right;
        }
        
        .po-footer .signature .sig-line {
            margin-top: 20px;
            border-top: 1px solid #94a3b8;
            width: 150px;
            margin-left: auto;
        }
        
        /* ===== STATUS BADGE ===== */
        .status-badge {
            padding: 4px 14px;
            border-radius: 20px;
            font-size: 13px;
            font-weight: 500;
            display: inline-block;
        }
        
        .status-badge.draft { background: #e2e8f0; color: #475569; }
        .status-badge.pending { background: #fcd34d; color: #92400e; }
        .status-badge.approved { background: #93c5fd; color: #1e40af; }
        .status-badge.received { background: #86efac; color: #166534; }
        .status-badge.partial { background: #f59e0b; color: #92400e; }
        .status-badge.paid { background: #22c55e; color: #fff; }
        .status-badge.cancelled { background: #fca5a5; color: #991b1b; }
        
        /* ===== 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;
            }
            
            .po-wrapper {
                box-shadow: none;
                border-radius: 0;
                max-width: 100%;
            }
            
            .print-actions {
                display: none !important;
            }
            
            .letterhead {
                padding: 20px 30px;
            }
            
            .po-header,
            .parties-section,
            .items-section,
            .summary-section,
            .po-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;
            }
            
            .status-badge {
                -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;
            }
            
            .po-header .row {
                flex-direction: column;
                text-align: center;
            }
            
            .po-header .info-box {
                text-align: center;
                margin-top: 10px;
            }
            
            .parties-section {
                flex-direction: column;
                text-align: center;
            }
            
            .parties-section .box {
                margin-bottom: 15px;
            }
            
            .summary-section .row {
                justify-content: center;
            }
            
            .po-footer {
                flex-direction: column;
                text-align: center;
            }
            
            .po-footer .terms {
                max-width: 100%;
                margin-bottom: 15px;
            }
            
            .po-footer .signature {
                text-align: center;
            }
            
            .po-footer .signature .sig-line {
                margin-left: auto;
                margin-right: auto;
            }
        }
    </style>
</head>
<body>

<div class="po-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>
        </div>
    </div>
    
    <!-- ===== PO HEADER ===== -->
    <div class="po-header">
        <div class="row">
            <div class="title">
                <h2>PURCHASE ORDER</h2>
                <div class="po-no"># <?php echo $po['po_no']; ?></div>
            </div>
            <div class="info-box">
                <p><strong>Order Date:</strong> <?php echo date('d M Y', strtotime($po['order_date'])); ?></p>
                <?php if ($po['delivery_date']): ?>
                <p><strong>Delivery Date:</strong> <?php echo date('d M Y', strtotime($po['delivery_date'])); ?></p>
                <?php endif; ?>
                <p><strong>Status:</strong> <span class="status-badge <?php echo $po['status']; ?>"><?php echo ucfirst($po['status']); ?></span></p>
            </div>
        </div>
    </div>
    
    <!-- ===== PARTIES ===== -->
    <div class="parties-section">
        <div class="box">
            <h4><i class="fa-solid fa-building"></i> Vendor</h4>
            <p>
                <strong><?php echo $po['vendor_name'] ?? 'N/A'; ?></strong><br>
                <?php if ($po['company']): ?>
                <?php echo $po['company']; ?><br>
                <?php endif; ?>
                <?php if ($po['address']): ?>
                <?php echo $po['address']; ?><br>
                <?php endif; ?>
                <?php if ($po['phone']): ?>
                <i class="fa-solid fa-phone"></i> <?php echo $po['phone']; ?><br>
                <?php endif; ?>
                <?php if ($po['email']): ?>
                <i class="fa-solid fa-envelope"></i> <?php echo $po['email']; ?><br>
                <?php endif; ?>
                <?php if ($po['tax_id']): ?>
                <i class="fa-solid fa-id-card"></i> TIN: <?php echo $po['tax_id']; ?>
                <?php endif; ?>
            </p>
        </div>
        <div class="box">
            <h4><i class="fa-solid fa-file-invoice"></i> PO Details</h4>
            <p>
                <strong>PO #:</strong> <?php echo $po['po_no']; ?><br>
                <strong>Total:</strong> <?php echo number_format($po['total']); ?> TK<br>
                <strong>Paid:</strong> <?php echo number_format($po['paid_amount']); ?> TK<br>
                <strong>Due:</strong> <?php echo number_format($po['due_amount']); ?> TK
            </p>
        </div>
    </div>
    
    <!-- ===== ITEMS TABLE ===== -->
    <div class="items-section">
        <table class="items-table">
            <thead>
                <tr>
                    <th style="width:5%;">#</th>
                    <th style="width:45%;">Item 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 if (empty($items)): ?>
                <tr>
                    <td colspan="5" class="text-center" style="padding: 20px; color: #94a3b8;">
                        No items found
                    </td>
                </tr>
                <?php else: ?>
                <?php $counter = 1; ?>
                <?php foreach ($items as $item): ?>
                <tr>
                    <td><?php echo $counter++; ?></td>
                    <td><?php echo htmlspecialchars($item['item_name']); ?></td>
                    <td class="text-right"><?php echo $item['quantity']; ?></td>
                    <td class="text-right"><?php echo number_format($item['unit_price']); ?> TK</td>
                    <td class="text-right"><?php echo number_format($item['total']); ?> TK</td>
                </tr>
                <?php endforeach; ?>
                <?php endif; ?>
            </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($po['subtotal']); ?> TK</span>
                </div>
                <?php if ($po['tax'] > 0): ?>
                <div class="line">
                    <span>Tax</span>
                    <span><?php echo number_format($po['tax']); ?> TK</span>
                </div>
                <?php endif; ?>
                <?php if ($po['discount'] > 0): ?>
                <div class="line">
                    <span>Discount</span>
                    <span>-<?php echo number_format($po['discount']); ?> TK</span>
                </div>
                <?php endif; ?>
                <div class="line total">
                    <span>Total</span>
                    <span class="amount"><?php echo number_format($po['total']); ?> TK</span>
                </div>
                <div class="line">
                    <span>Paid Amount</span>
                    <span class="amount-paid"><?php echo number_format($po['paid_amount']); ?> TK</span>
                </div>
                <div class="line" style="font-weight:600; border-top:1px solid #e2e8f0; padding-top:8px;">
                    <span>Due Amount</span>
                    <span class="amount-due"><?php echo number_format($po['due_amount']); ?> TK</span>
                </div>
            </div>
        </div>
    </div>
    
    <!-- ===== FOOTER ===== -->
    <div class="po-footer">
        <div class="terms">
            <strong>Terms & Conditions:</strong>
            <p>Please deliver the items as per the specifications mentioned above.</p>
            <?php if ($po['note']): ?>
            <p><strong>Note:</strong> <?php echo $po['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='purchase-orders.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>