<?php
// admin/accounts/view-po.php
require_once __DIR__ . '/../../includes/config.php';
require_once __DIR__ . '/../../includes/functions.php';

if (!isAdmin()) {
    header('Location: ../login.php');
    exit;
}

$id = intval($_GET['id'] ?? 0);
if (!$id) {
    header('Location: purchase-orders.php');
    exit;
}

// Get PO details
$stmt = $pdo->prepare("SELECT po.*, v.name as vendor_name 
                       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) {
    header('Location: purchase-orders.php');
    exit;
}

// Get PO items
$stmt = $pdo->prepare("SELECT * FROM po_items WHERE po_id = ?");
$stmt->execute([$id]);
$items = $stmt->fetchAll();

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

<style>
.view-card {
    background: #fff;
    border-radius: 12px;
    padding: 25px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
    margin-bottom: 20px;
}

.view-card .card-title {
    font-weight: 600;
    margin-bottom: 20px;
    padding-bottom: 10px;
    border-bottom: 2px solid #e2e8f0;
}

.info-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 20px;
}

.info-item {
    background: #f8fafc;
    border-radius: 10px;
    padding: 15px 20px;
}

.info-item .label {
    color: #64748b;
    font-size: 13px;
    font-weight: 500;
    display: block;
    margin-bottom: 5px;
}

.info-item .value {
    font-size: 16px;
    font-weight: 600;
    color: #1e293b;
}

.info-item .value.text-success { color: #22c55e; }
.info-item .value.text-danger { color: #ef4444; }

.status-badge {
    padding: 4px 12px;
    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; }

.action-buttons {
    display: flex;
    gap: 10px;
    margin-top: 20px;
    flex-wrap: wrap;
}

.item-table td {
    vertical-align: middle;
}
</style>

<div class="admin-content">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2><i class="fa-solid fa-file-purchase me-2"></i> Purchase Order Details</h2>
        <div>
            <a href="purchase-orders.php" class="btn btn-secondary">
                <i class="fa-solid fa-arrow-left"></i> Back
            </a>
        </div>
    </div>

    <div class="view-card">
        <h5 class="card-title">
            <i class="fa-solid fa-receipt me-2"></i>
            PO #<?php echo $po['po_no']; ?>
            <span class="status-badge <?php echo $po['status']; ?>">
                <?php echo ucfirst($po['status']); ?>
            </span>
        </h5>

        <div class="info-grid">
            <div class="info-item">
                <span class="label">PO Number</span>
                <span class="value"><code><?php echo $po['po_no']; ?></code></span>
            </div>
            <div class="info-item">
                <span class="label">Vendor</span>
                <span class="value"><?php echo htmlspecialchars($po['vendor_name'] ?? 'N/A'); ?></span>
            </div>
            <div class="info-item">
                <span class="label">Order Date</span>
                <span class="value"><?php echo date('d M Y', strtotime($po['order_date'])); ?></span>
            </div>
            <div class="info-item">
                <span class="label">Delivery Date</span>
                <span class="value"><?php echo $po['delivery_date'] ? date('d M Y', strtotime($po['delivery_date'])) : 'Not set'; ?></span>
            </div>
            <div class="info-item">
                <span class="label">Subtotal</span>
                <span class="value"><?php echo number_format($po['subtotal']); ?> TK</span>
            </div>
            <div class="info-item">
                <span class="label">Tax</span>
                <span class="value"><?php echo number_format($po['tax']); ?> TK</span>
            </div>
            <div class="info-item">
                <span class="label">Discount</span>
                <span class="value"><?php echo number_format($po['discount']); ?> TK</span>
            </div>
            <div class="info-item">
                <span class="label">Total</span>
                <span class="value text-success"><?php echo number_format($po['total']); ?> TK</span>
            </div>
            <div class="info-item">
                <span class="label">Paid Amount</span>
                <span class="value text-success"><?php echo number_format($po['paid_amount']); ?> TK</span>
            </div>
            <div class="info-item">
                <span class="label">Due Amount</span>
                <span class="value text-danger"><?php echo number_format($po['due_amount']); ?> TK</span>
            </div>
            <?php if ($po['note']): ?>
            <div class="info-item" style="grid-column: 1 / -1;">
                <span class="label">Note</span>
                <span class="value"><?php echo nl2br(htmlspecialchars($po['note'])); ?></span>
            </div>
            <?php endif; ?>
            <div class="info-item">
                <span class="label">Created At</span>
                <span class="value"><?php echo date('d M Y, h:i A', strtotime($po['created_at'])); ?></span>
            </div>
            <div class="info-item">
                <span class="label">Created By</span>
                <span class="value"><?php echo $po['created_by'] ? 'Admin' : 'System'; ?></span>
            </div>
        </div>

        <!-- PO Items -->
        <div class="mt-4">
            <h6 class="mb-3"><i class="fa-solid fa-list me-2"></i> PO Items</h6>
            <div class="table-responsive">
                <table class="table table-bordered item-table">
                    <thead class="table-light">
                        <tr>
                            <th>#</th>
                            <th>Item</th>
                            <th>Description</th>
                            <th class="text-end">Qty</th>
                            <th class="text-end">Unit Price</th>
                            <th class="text-end">Total</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($items)): ?>
                        <tr>
                            <td colspan="6" class="text-center">No items found</td>
                        </tr>
                        <?php else: ?>
                        <?php foreach ($items as $index => $item): ?>
                        <tr>
                            <td><?php echo $index + 1; ?></td>
                            <td><?php echo htmlspecialchars($item['item_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['unit_price']); ?> TK</td>
                            <td class="text-end"><?php echo number_format($item['total']); ?> TK</td>
                        </tr>
                        <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                    <tfoot>
                        <tr>
                            <td colspan="5" class="text-end fw-bold">Total</td>
                            <td class="text-end fw-bold"><?php echo number_format($po['total']); ?> TK</td>
                        </tr>
                    </tfoot>
                </table>
            </div>
        </div>

        <div class="action-buttons">
            <a href="edit-po.php?id=<?php echo $po['id']; ?>" class="btn btn-warning">
                <i class="fa-solid fa-edit"></i> Edit
            </a>
            <a href="print-po.php?id=<?php echo $po['id']; ?>" class="btn btn-primary" target="_blank">
                <i class="fa-solid fa-print"></i> Print
            </a>
            <button onclick="deletePO(<?php echo $po['id']; ?>)" class="btn btn-danger">
                <i class="fa-solid fa-trash"></i> Delete
            </button>
        </div>
    </div>
</div>

<script>
function deletePO(id) {
    if (confirm('Are you sure you want to delete this purchase order?')) {
        window.location.href = 'delete-po.php?id=' + id;
    }
}
</script>

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