<?php
// view_payment_batch.php - View Payment Batch Details
include 'layouts/session.php';
include 'layouts/dbconn.php';

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

// Get batch details
$batch_sql = "SELECT dpb.*, u.full_name as doctor_name, d.specialization,
              approved_user.full_name as approved_by_name,
              processed_user.full_name as processed_by_name
              FROM doctor_payment_batches dpb
              JOIN doctors d ON dpb.doctor_id = d.doctor_id
              JOIN users u ON d.user_id = u.user_id
              LEFT JOIN users approved_user ON dpb.approved_by = approved_user.user_id
              LEFT JOIN users processed_user ON dpb.processed_by = processed_user.user_id
              WHERE dpb.batch_id = ?";
$stmt = $conn->prepare($batch_sql);
$stmt->bind_param('i', $batch_id);
$stmt->execute();
$batch = $stmt->get_result()->fetch_assoc();

if (!$batch) {
    $_SESSION['error_message'] = "Batch not found";
    header("Location: doctor_payment_batches.php");
    exit();
}

// Get allocations in batch
$items_sql = "SELECT pa.*, bai.amount_included, esp.provider_name
              FROM batch_allocation_items bai
              JOIN payment_allocations pa ON bai.allocation_id = pa.allocation_id
              JOIN external_service_providers esp ON pa.provider_id = esp.provider_id
              WHERE bai.batch_id = ?
              ORDER BY pa.service_date DESC";
$stmt = $conn->prepare($items_sql);
$stmt->bind_param('i', $batch_id);
$stmt->execute();
$items = $stmt->get_result();
?>

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

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'View Payment Batch')); ?>
    <?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">
                    
                    <div class="page-header" style="background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%); padding: 2rem; border-radius: 12px; color: white; margin-bottom: 2rem;">
                        <div class="row align-items-center">
                            <div class="col">
                                <h3 class="text-white mb-2">Batch: <?php echo htmlspecialchars($batch['batch_number']); ?></h3>
                                <p class="text-white opacity-75 mb-0">Payment batch details</p>
                            </div>
                            <div class="col-auto">
                                <a href="doctor_payment_batches.php" class="btn btn-light">
                                    <i class="ri-arrow-left-line me-1"></i>Back
                                </a>
                            </div>
                        </div>
                    </div>
                    
                    <div class="row">
                        <!-- Batch Details -->
                        <div class="col-lg-4">
                            <div class="card">
                                <div class="card-body">
                                    <h5 class="card-title mb-4">Batch Information</h5>
                                    
                                    <div class="mb-3">
                                        <label class="text-muted small">Status</label>
                                        <div>
                                            <span class="badge bg-<?php 
                                                echo $batch['status'] === 'PAID' ? 'success' : 
                                                    ($batch['status'] === 'APPROVED' ? 'info' : 'warning'); 
                                            ?>">
                                                <?php echo str_replace('_', ' ', $batch['status']); ?>
                                            </span>
                                        </div>
                                    </div>
                                    
                                    <div class="mb-3">
                                        <label class="text-muted small">Doctor</label>
                                        <div class="fw-bold"><?php echo htmlspecialchars($batch['doctor_name']); ?></div>
                                        <?php if ($batch['specialization']): ?>
                                            <small class="text-muted"><?php echo htmlspecialchars($batch['specialization']); ?></small>
                                        <?php endif; ?>
                                    </div>
                                    
                                    <div class="mb-3">
                                        <label class="text-muted small">Period</label>
                                        <div class="fw-bold">
                                            <?php echo date('d M', strtotime($batch['payment_period_start'])); ?> - 
                                            <?php echo date('d M Y', strtotime($batch['payment_period_end'])); ?>
                                        </div>
                                    </div>
                                    
                                    <div class="mb-3">
                                        <label class="text-muted small">Payment Method</label>
                                        <div class="fw-bold"><?php echo str_replace('_', ' ', $batch['payment_method']); ?></div>
                                    </div>
                                    
                                    <hr>
                                    
                                    <div class="mb-3">
                                        <label class="text-muted small">Gross Amount</label>
                                        <div class="fw-bold text-primary fs-5">KES <?php echo number_format($batch['gross_amount'], 2); ?></div>
                                    </div>
                                    
                                    <?php if ($batch['total_tax_withheld'] > 0): ?>
                                    <div class="mb-3">
                                        <label class="text-muted small">Less: Tax</label>
                                        <div class="fw-bold text-danger">KES <?php echo number_format($batch['total_tax_withheld'], 2); ?></div>
                                    </div>
                                    <?php endif; ?>
                                    
                                    <?php if ($batch['total_deductions'] > 0): ?>
                                    <div class="mb-3">
                                        <label class="text-muted small">Less: Deductions</label>
                                        <div class="fw-bold text-danger">KES <?php echo number_format($batch['total_deductions'], 2); ?></div>
                                    </div>
                                    <?php endif; ?>
                                    
                                    <div class="mb-3 p-3 bg-light rounded">
                                        <label class="text-muted small">Net Payable</label>
                                        <div class="fw-bold text-success fs-4">KES <?php echo number_format($batch['net_payable'], 2); ?></div>
                                    </div>
                                    
                                    <?php if ($batch['status'] === 'APPROVED'): ?>
                                    <a href="process_doctor_payment.php?id=<?php echo $batch_id; ?>" class="btn btn-success w-100">
                                        <i class="ri-check-double-line me-1"></i>Process Payment
                                    </a>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                        
                        <!-- Allocations in Batch -->
                        <div class="col-lg-8">
                            <div class="card">
                                <div class="card-body">
                                    <h5 class="card-title mb-4">Allocations (<?php echo $items->num_rows; ?>)</h5>
                                    
                                    <div class="table-responsive">
                                        <table class="table table-hover">
                                            <thead>
                                                <tr>
                                                    <th>Allocation #</th>
                                                    <th>Date</th>
                                                    <th>Provider</th>
                                                    <th>Service</th>
                                                    <th class="text-end">Amount</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php while ($item = $items->fetch_assoc()): ?>
                                                <tr>
                                                    <td>
                                                        <a href="view_payment_allocation.php?id=<?php echo $item['allocation_id']; ?>">
                                                            <?php echo htmlspecialchars($item['allocation_number']); ?>
                                                        </a>
                                                    </td>
                                                    <td><?php echo date('d M Y', strtotime($item['service_date'])); ?></td>
                                                    <td><?php echo htmlspecialchars($item['provider_name']); ?></td>
                                                    <td><?php echo ucfirst(strtolower($item['service_category'])); ?></td>
                                                    <td class="text-end">KES <?php echo number_format($item['amount_included'], 2); ?></td>
                                                </tr>
                                                <?php endwhile; ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                </div>
            </div>
            
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    
    <?php include 'layouts/vendor-scripts.php'; ?>
</body>
</html>