<?php
// admin/view-delivery-report.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: delivery-reports.php');
    exit;
}

// Get delivery report
$stmt = $pdo->prepare("SELECT dr.*, o.invoice_no as order_invoice,
                       da.name as agent_name, da.phone as agent_phone
                       FROM delivery_reports dr
                       LEFT JOIN orders o ON dr.order_id = o.id
                       LEFT JOIN delivery_agents da ON dr.delivery_agent = da.name
                       WHERE dr.id = ?");
$stmt->execute([$id]);
$delivery = $stmt->fetch();

if (!$delivery) {
    header('Location: delivery-reports.php');
    exit;
}

include 'header.php';
?>

<style>
.status-badge {
    padding: 6px 16px;
    border-radius: 20px;
    font-size: 14px;
    font-weight: 500;
}

.status-badge.pending { background: #fcd34d; color: #92400e; }
.status-badge.assigned { background: #93c5fd; color: #1e40af; }
.status-badge.picked { background: #86efac; color: #166534; }
.status-badge.in_transit { background: #f59e0b; color: #92400e; }
.status-badge.delivered { background: #22c55e; color: #fff; }
.status-badge.returned { background: #fca5a5; color: #991b1b; }
.status-badge.cancelled { background: #e2e8f0; color: #475569; }

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

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

.info-card h6 {
    font-weight: 600;
    color: #64748b;
    font-size: 13px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 10px;
    padding-bottom: 8px;
    border-bottom: 1px solid #e2e8f0;
}

.info-card p {
    margin-bottom: 4px;
    font-size: 14px;
}

.info-card .label {
    color: #64748b;
    font-weight: 500;
}
</style>

<div class="admin-content">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2><i class="fa-solid fa-clipboard-list me-2"></i> Delivery Report Details</h2>
        <div>
            <a href="edit-delivery-report.php?id=<?php echo $id; ?>" class="btn btn-warning">
                <i class="fa-solid fa-edit"></i> Edit
            </a>
            <a href="delivery-reports.php" class="btn btn-secondary">
                <i class="fa-solid fa-arrow-left"></i> Back
            </a>
        </div>
    </div>
    
    <div class="mb-4">
        <span class="status-badge <?php echo $delivery['delivery_status']; ?>">
            <?php echo ucfirst(str_replace('_', ' ', $delivery['delivery_status'])); ?>
        </span>
        <span class="ms-3 text-muted">Report #<?php echo $delivery['report_no']; ?></span>
    </div>
    
    <div class="info-grid">
        <div class="info-card">
            <h6><i class="fa-solid fa-user me-2"></i> Customer Information</h6>
            <p><strong><?php echo htmlspecialchars($delivery['customer_name']); ?></strong></p>
            <p><span class="label">Phone:</span> <?php echo htmlspecialchars($delivery['customer_phone']); ?></p>
            <p><span class="label">Address:</span> <?php echo htmlspecialchars($delivery['customer_address']); ?></p>
            <?php if ($delivery['area']): ?>
            <p><span class="label">Area:</span> <?php echo htmlspecialchars($delivery['area']); ?></p>
            <?php endif; ?>
        </div>
        
        <div class="info-card">
            <h6><i class="fa-solid fa-truck me-2"></i> Delivery Information</h6>
            <?php if ($delivery['order_invoice']): ?>
            <p><span class="label">Order:</span> 
                <a href="order-details.php?id=<?php echo $delivery['order_id']; ?>">
                    <?php echo $delivery['order_invoice']; ?>
                </a>
            </p>
            <?php endif; ?>
            <p><span class="label">Delivery Agent:</span> <?php echo htmlspecialchars($delivery['agent_name'] ?? 'Not assigned'); ?></p>
            <?php if ($delivery['agent_phone']): ?>
            <p><span class="label">Agent Phone:</span> <?php echo htmlspecialchars($delivery['agent_phone']); ?></p>
            <?php endif; ?>
            <p><span class="label">Delivery Date:</span> <?php echo date('d M Y', strtotime($delivery['delivery_date'])); ?></p>
        </div>
        
        <div class="info-card">
            <h6><i class="fa-solid fa-dollar-sign me-2"></i> Financial Summary</h6>
            <p><span class="label">COD Amount:</span> <?php echo number_format($delivery['cod_amount']); ?> TK</p>
            <p><span class="label">Delivery Charge:</span> <?php echo number_format($delivery['delivery_charge']); ?> TK</p>
            <p><span class="label">Collected Amount:</span> <?php echo number_format($delivery['collected_amount']); ?> TK</p>
            <p><span class="label">Created:</span> <?php echo date('d M Y, h:i A', strtotime($delivery['created_at'])); ?></p>
        </div>
    </div>
    
    <?php if ($delivery['note']): ?>
    <div class="alert alert-info">
        <strong>Note:</strong> <?php echo htmlspecialchars($delivery['note']); ?>
    </div>
    <?php endif; ?>
    
    <div class="mt-3">
        <a href="edit-delivery-report.php?id=<?php echo $id; ?>" class="btn btn-warning">
            <i class="fa-solid fa-edit"></i> Edit
        </a>
        <button onclick="window.print()" class="btn btn-primary">
            <i class="fa-solid fa-print"></i> Print
        </button>
    </div>
</div>

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