<?php
// view_bulk_payment.php - View Bulk Payment with All Allocations
include 'layouts/session.php';
include 'layouts/dbconn.php';

$bulk_payment_id = isset($_GET['id']) ? intval($_GET['id']) : 0;

// Get bulk payment details
$payment_sql = "SELECT bp.*, esp.provider_name, esp.provider_code, u.full_name as received_by_name
                FROM bulk_payments bp
                JOIN external_service_providers esp ON bp.provider_id = esp.provider_id
                JOIN users u ON bp.received_by = u.user_id
                WHERE bp.bulk_payment_id = ?";
$stmt = $conn->prepare($payment_sql);
$stmt->bind_param('i', $bulk_payment_id);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows === 0) {
    $_SESSION['error_message'] = "Bulk payment not found";
    header("Location: list_bulk_payments.php");
    exit();
}

$payment = $result->fetch_assoc();

// Get all allocations for this payment
$allocations_sql = "SELECT pa.*, d.doctor_id, u.full_name as doctor_name
                    FROM payment_allocations pa
                    LEFT JOIN doctors d ON pa.doctor_id = d.doctor_id
                    LEFT JOIN users u ON d.user_id = u.user_id
                    WHERE pa.bulk_payment_id = ?
                    ORDER BY pa.service_date DESC, pa.created_at DESC";
$stmt = $conn->prepare($allocations_sql);
$stmt->bind_param('i', $bulk_payment_id);
$stmt->execute();
$allocations = $stmt->get_result();

$allocation_percentage = $payment['total_amount'] > 0 
    ? ($payment['allocated_amount'] / $payment['total_amount']) * 100 
    : 0;
?>

<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'View Bulk Payment')); ?>
    <?php include 'layouts/head-css.php'; ?>
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>
        
        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    
                    <!-- Page Header with gradient -->
                    <div class="page-header" style="background: linear-gradient(135deg, #10b981 0%, #059669 100%);">
                        <div class="row align-items-center">
                            <div class="col">
                                <h3 class="mb-2 text-white">
                                    <i class="ri-bank-line me-2"></i>
                                    Payment: <?php echo htmlspecialchars($payment['payment_number']); ?>
                                </h3>
                                <p class="mb-0 text-white opacity-75">Bulk payment details and allocations</p>
                            </div>
                            <div class="col-auto">
                                <a href="list_bulk_payments.php" class="btn btn-light">
                                    <i class="ri-arrow-left-line me-1"></i>Back
                                </a>
                            </div>
                        </div>
                    </div>
                    
                    <div class="row">
                        <!-- Payment Details -->
                        <div class="col-lg-4">
                            <div class="card">
                                <div class="card-body">
                                    <h5 class="card-title mb-4">Payment Information</h5>
                                    
                                    <div class="mb-3">
                                        <label class="text-muted small">Provider</label>
                                        <div class="fw-bold"><?php echo htmlspecialchars($payment['provider_name']); ?></div>
                                    </div>
                                    
                                    <div class="mb-3">
                                        <label class="text-muted small">Payment Date</label>
                                        <div class="fw-bold"><?php echo date('d M Y', strtotime($payment['payment_date'])); ?></div>
                                    </div>
                                    
                                    <div class="mb-3">
                                        <label class="text-muted small">Service Period</label>
                                        <div class="fw-bold">
                                            <?php echo date('d M', strtotime($payment['payment_period_start'])); ?> - 
                                            <?php echo date('d M Y', strtotime($payment['payment_period_end'])); ?>
                                        </div>
                                    </div>
                                    
                                    <div class="mb-3">
                                        <label class="text-muted small">Total Amount</label>
                                        <div class="fw-bold text-success fs-4">
                                            KES <?php echo number_format($payment['total_amount'], 2); ?>
                                        </div>
                                    </div>
                                    
                                    <div class="mb-3">
                                        <label class="text-muted small">Allocated</label>
                                        <div class="fw-bold text-primary">
                                            KES <?php echo number_format($payment['allocated_amount'], 2); ?>
                                        </div>
                                    </div>
                                    
                                    <div class="mb-3">
                                        <label class="text-muted small">Unallocated</label>
                                        <div class="fw-bold text-warning">
                                            KES <?php echo number_format($payment['unallocated_amount'], 2); ?>
                                        </div>
                                    </div>
                                    
                                    <!-- Progress Bar -->
                                    <div class="progress mb-2" style="height: 10px;">
                                        <div class="progress-bar bg-success" style="width: <?php echo $allocation_percentage; ?>%"></div>
                                    </div>
                                    <small class="text-muted"><?php echo number_format($allocation_percentage, 1); ?>% allocated</small>
                                    
                                    <div class="mt-4">
                                        <a href="create_payment_allocation.php?bulk_id=<?php echo $bulk_payment_id; ?>" 
                                           class="btn btn-success w-100">
                                            <i class="ri-add-line me-1"></i>Create Allocation
                                        </a>
                                        
                                        <?php if ($payment['reconciliation_status'] === 'PENDING'): ?>
                                        <a href="reconcile_bulk_payment.php?id=<?php echo $bulk_payment_id; ?>" 
                                           class="btn btn-info w-100 mt-2">
                                            <i class="ri-check-double-line me-1"></i>Reconcile
                                        </a>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <!-- Allocations List -->
                        <div class="col-lg-8">
                            <div class="card">
                                <div class="card-body">
                                    <h5 class="card-title mb-4">Allocations (<?php echo $allocations->num_rows; ?>)</h5>
                                    
                                    <?php if ($allocations->num_rows > 0): ?>
                                        <?php while ($alloc = $allocations->fetch_assoc()): ?>
                                        <div class="border rounded p-3 mb-3">
                                            <div class="d-flex justify-content-between align-items-start">
                                                <div>
                                                    <h6 class="mb-1">
                                                        <a href="view_payment_allocation.php?id=<?php echo $alloc['allocation_id']; ?>">
                                                            <?php echo htmlspecialchars($alloc['allocation_number']); ?>
                                                        </a>
                                                    </h6>
                                                    <small class="text-muted">
                                                        <?php echo date('d M Y', strtotime($alloc['service_date'])); ?> | 
                                                        <?php echo ucfirst(strtolower($alloc['service_category'])); ?>
                                                    </small>
                                                </div>
                                                <span class="badge bg-<?php echo $alloc['allocation_status'] === 'APPROVED' ? 'success' : 'warning'; ?>">
                                                    <?php echo str_replace('_', ' ', $alloc['allocation_status']); ?>
                                                </span>
                                            </div>
                                            
                                            <div class="mt-2">
                                                <small><?php echo htmlspecialchars($alloc['service_description']); ?></small>
                                            </div>
                                            
                                            <?php if ($alloc['doctor_name']): ?>
                                            <div class="mt-2">
                                                <i class="ri-user-line me-1"></i>
                                                <small class="fw-bold"><?php echo htmlspecialchars($alloc['doctor_name']); ?></small>
                                            </div>
                                            <?php endif; ?>
                                            
                                            <div class="mt-2 d-flex justify-content-between">
                                                <span class="text-muted small">Service Cost:</span>
                                                <strong>KES <?php echo number_format($alloc['total_service_cost'], 2); ?></strong>
                                            </div>
                                        </div>
                                        <?php endwhile; ?>
                                    <?php else: ?>
                                        <div class="text-center py-5">
                                            <i class="ri-file-list-line display-4 text-muted"></i>
                                            <p class="text-muted mt-3">No allocations created yet</p>
                                            <a href="create_payment_allocation.php?bulk_id=<?php echo $bulk_payment_id; ?>" 
                                               class="btn btn-primary">
                                                <i class="ri-add-line me-1"></i>Create First Allocation
                                            </a>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                </div>
            </div>
            
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    
    <?php include 'layouts/vendor-scripts.php'; ?>
</body>
</html>