<?php
$report_type = $_GET['report'] ?? 'low_stock';

// Data Fetching based on report type
$data = [];
$columns = [];
$departments = $db->query("SELECT id, name_en FROM departments ORDER BY name_en ASC")->fetchAll(PDO::FETCH_ASSOC);

if ($report_type === 'low_stock') {
    $title = __('low_stock_report');
    $columns = ['item_name', 'sku', 'min_level', 'current_stock', 'status'];
    $data = $db->query("
        SELECT i.name_en as item_name, i.sku, i.min_level, 
        COALESCE((SELECT SUM(quantity) FROM inventory_batches b WHERE b.item_id = i.id), 0) as current_stock 
        FROM inventory_items i 
        HAVING current_stock <= min_level 
        ORDER BY current_stock ASC
    ")->fetchAll(PDO::FETCH_ASSOC);
} elseif ($report_type === 'expiry') {
    $title = __('expiry_report');
    $columns = ['item_name', 'batch_number', 'expiry_date', 'quantity', 'days_remaining'];
    $data = $db->query("
        SELECT i.name_en as item_name, b.batch_number, b.expiry_date, b.quantity, 
        DATEDIFF(b.expiry_date, CURDATE()) as days_remaining 
        FROM inventory_batches b 
        JOIN inventory_items i ON b.item_id = i.id 
        WHERE b.quantity > 0 AND b.expiry_date IS NOT NULL 
        AND b.expiry_date <= DATE_ADD(CURDATE(), INTERVAL 90 DAY) 
        ORDER BY b.expiry_date ASC
    ")->fetchAll(PDO::FETCH_ASSOC);
} elseif ($report_type === 'valuation') {
    $title = __('stock_valuation_report');
    $columns = ['item_name', 'total_quantity', 'avg_cost', 'total_value'];
    $data = $db->query("
        SELECT i.name_en as item_name, 
        SUM(b.quantity) as total_quantity, 
        AVG(b.cost_price) as avg_cost, 
        SUM(b.quantity * b.cost_price) as total_value 
        FROM inventory_batches b 
        JOIN inventory_items i ON b.item_id = i.id 
        WHERE b.quantity > 0 
        GROUP BY i.id 
        ORDER BY total_value DESC
    ")->fetchAll(PDO::FETCH_ASSOC);
} elseif ($report_type === 'consumption') {
    $title = __('consumption_report');
    $columns = ['department', 'item_name', 'total_quantity', 'total_cost'];
    
    // Filters
    $filter_department = $_GET['department_id'] ?? '';
    $filter_start = $_GET['start_date'] ?? '';
    $filter_end = $_GET['end_date'] ?? '';

    $where = "WHERE t.transaction_type = 'out'";
    $params = [];

    if ($filter_department) {
        $where .= " AND t.department_id = ?";
        $params[] = $filter_department;
    }
    if ($filter_start) {
        $where .= " AND DATE(t.transaction_date) >= ?";
        $params[] = $filter_start;
    }
    if ($filter_end) {
        $where .= " AND DATE(t.transaction_date) <= ?";
        $params[] = $filter_end;
    }

    $sql = "
        SELECT d.name_en as department, 
               i.name_en as item_name, 
               SUM(t.quantity) as total_quantity, 
               SUM(t.quantity * COALESCE(b.cost_price, 0)) as total_cost 
        FROM inventory_transactions t 
        JOIN departments d ON t.department_id = d.id 
        JOIN inventory_items i ON t.item_id = i.id 
        LEFT JOIN inventory_batches b ON t.batch_id = b.id 
        $where
        GROUP BY d.id, i.id 
        ORDER BY d.name_en ASC, total_cost DESC
    ";
    
    $stmt = $db->prepare($sql);
    $stmt->execute($params);
    $data = $stmt->fetchAll(PDO::FETCH_ASSOC);

    // Calculate totals
    $grand_total_qty = 0;
    $grand_total_cost = 0;
    foreach ($data as $row) {
        $grand_total_qty += $row['total_quantity'];
        $grand_total_cost += $row['total_cost'];
    }
}

// Fetch site settings if not already set (fallback)
if (!isset($site_name)) {
    $stmt = $db->query("SELECT setting_key, setting_value FROM settings WHERE setting_key IN ('company_name', 'company_logo')");
    $settings = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
    $site_name = $settings['company_name'] ?? 'Hospital Management';
    $site_logo = $settings['company_logo'] ?? '';
}

?>

<style>
    @media print {
        @page {
            size: A4;
            margin: 1cm;
        }
        body {
            background-color: #fff !important;
            color: #000 !important;
            font-family: 'Times New Roman', serif;
        }
        /* Hide everything by default */
        .sidebar, .navbar, .btn-group, form, .btn, .no-print, header, footer {
            display: none !important;
        }
        /* Layout resets */
        .main-content, .content-wrapper, .container, .container-fluid {
            margin: 0 !important;
            padding: 0 !important;
            width: 100% !important;
            max-width: 100% !important;
            box-shadow: none !important;
            border: none !important;
        }
        .card {
            border: none !important;
            box-shadow: none !important;
        }
        .card-header {
            display: none !important; /* We use custom print header */
        }
        .card-body {
            padding: 0 !important;
        }
        
        /* Visible elements */
        .print-only {
            display: block !important;
        }
        
        /* Table Styling for Print */
        .table {
            width: 100% !important;
            border-collapse: collapse !important;
            border: 1px solid #000 !important;
            font-size: 12pt;
        }
        .table th, .table td {
            border: 1px solid #000 !important;
            padding: 8px !important;
            text-align: left;
        }
        .table th {
            background-color: #f0f0f0 !important;
            font-weight: bold;
            -webkit-print-color-adjust: exact;
        }
        /* Badge handling - simplified for print */
        .badge {
            border: 1px solid #000;
            color: #000 !important;
            background: none !important;
            font-weight: normal;
            padding: 2px 5px;
        }
    }
    
    .print-only {
        display: none;
    }
</style>

<!-- Print Header -->
<div class="print-only mb-4">
    <div class="row align-items-center border-bottom pb-3 mb-3">
        <div class="col-8">
            <h1 class="h3 fw-bold mb-1"><?php echo htmlspecialchars($site_name); ?></h1>
            <h2 class="h5 text-uppercase text-secondary mb-0"><?php echo htmlspecialchars($title); ?></h2>
        </div>
        <div class="col-4 text-end">
            <?php if (!empty($site_logo)): ?>
                <img src="<?php echo htmlspecialchars($site_logo); ?>" alt="Logo" style="max-height: 60px;">
            <?php endif; ?>
        </div>
    </div>
    <div class="row mb-4">
        <div class="col-6">
            <p class="mb-1"><strong><?php echo __('report_date'); ?>:</strong> <?php echo date('Y-m-d H:i'); ?></p>
            <p class="mb-1"><strong><?php echo __('generated_by'); ?>:</strong> <?php echo htmlspecialchars($_SESSION['username'] ?? 'System'); ?></p>
        </div>
        <?php if ($report_type === 'consumption'): ?>
        <div class="col-6 text-end">
             <?php if ($filter_start || $filter_end): ?>
                <p class="mb-1"><strong><?php echo __('period'); ?>:</strong> 
                <?php echo ($filter_start ? $filter_start : 'Begining') . ' - ' . ($filter_end ? $filter_end : 'Now'); ?>
                </p>
             <?php endif; ?>
             <?php if ($filter_department): 
                 $dept_name = '';
                 foreach($departments as $d) { if($d['id'] == $filter_department) $dept_name = $d['name_en']; }
             ?>
                <p class="mb-1"><strong><?php echo __('department'); ?>:</strong> <?php echo htmlspecialchars($dept_name); ?></p>
             <?php endif; ?>
        </div>
        <?php endif; ?>
    </div>
</div>

<!-- Screen Header (Hidden on Print) -->
<div class="d-flex justify-content-between align-items-center mb-4 no-print">
    <h2 class="mb-0 fw-bold text-dark"><?php echo __('inventory_reports'); ?></h2>
    <div class="btn-group">
        <a href="?report=low_stock" class="btn btn-outline-primary <?php echo $report_type === 'low_stock' ? 'active' : ''; ?>"><?php echo __('low_stock'); ?></a>
        <a href="?report=expiry" class="btn btn-outline-primary <?php echo $report_type === 'expiry' ? 'active' : ''; ?>"><?php echo __('expiry_dates'); ?></a>
        <a href="?report=valuation" class="btn btn-outline-primary <?php echo $report_type === 'valuation' ? 'active' : ''; ?>"><?php echo __('valuation'); ?></a>
        <a href="?report=consumption" class="btn btn-outline-primary <?php echo $report_type === 'consumption' ? 'active' : ''; ?>"><?php echo __('department_consumption'); ?></a>
    </div>
</div>

<div class="card shadow-sm border-0">
    <div class="card-header bg-white no-print">
        <h5 class="mb-0 fw-bold"><?php echo htmlspecialchars($title); ?></h5>
    </div>
    <div class="card-body">
        
        <?php if ($report_type === 'consumption'): ?>
        <form method="GET" class="row g-3 mb-4 border-bottom pb-4 no-print">
            <input type="hidden" name="report" value="consumption">
            
            <div class="col-md-3">
                <label class="form-label"><?php echo __('department'); ?></label>
                <select name="department_id" class="form-select">
                    <option value=""><?php echo __('all_departments'); ?></option>
                    <?php foreach ($departments as $dept): ?>
                    <option value="<?php echo $dept['id']; ?>" <?php echo (isset($filter_department) && $filter_department == $dept['id']) ? 'selected' : ''; ?>>
                        <?php echo htmlspecialchars($dept['name_en']); ?>
                    </option>
                    <?php endforeach; ?>
                </select>
            </div>
            
            <div class="col-md-3">
                <label class="form-label"><?php echo __('start_date'); ?></label>
                <input type="date" name="start_date" class="form-control" value="<?php echo htmlspecialchars($filter_start ?? ''); ?>">
            </div>
            
            <div class="col-md-3">
                <label class="form-label"><?php echo __('end_date'); ?></label>
                <input type="date" name="end_date" class="form-control" value="<?php echo htmlspecialchars($filter_end ?? ''); ?>">
            </div>
            
            <div class="col-md-3 d-flex align-items-end">
                <button type="submit" class="btn btn-primary w-100">
                    <i class="bi bi-filter"></i> <?php echo __('filter'); ?>
                </button>
            </div>
        </form>
        <?php endif; ?>

        <div class="table-responsive">
            <table class="table table-hover align-middle">
                <thead class="table-light">
                    <tr>
                        <?php foreach ($columns as $col): ?>
                        <th><?php echo __($col); ?></th>
                        <?php endforeach; ?>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($data)): ?>
                    <tr>
                        <td colspan="<?php echo count($columns); ?>" class="text-center py-4 text-muted"><?php echo __('no_data_available'); ?></td>
                    </tr>
                    <?php else: ?>
                        <?php foreach ($data as $row): ?>
                        <tr>
                            <?php foreach ($columns as $col): ?>
                                <td>
                                    <?php 
                                    if ($col === 'current_stock' || $col === 'total_quantity') {
                                        echo number_format($row[$col]);
                                    } elseif ($col === 'total_value' || $col === 'avg_cost' || $col === 'total_cost') {
                                        echo format_currency($row[$col]);
                                    } elseif ($col === 'status') {
                                        if ($row['current_stock'] == 0) echo '<span class="badge bg-danger">Out of Stock</span>';
                                        else echo '<span class="badge bg-warning text-dark">Low Stock</span>';
                                    } elseif ($col === 'days_remaining') {
                                        $days = $row['days_remaining'];
                                        if ($days < 0) echo '<span class="badge bg-danger">Expired</span>';
                                        elseif ($days < 30) echo '<span class="badge bg-warning text-dark">' . $days . ' days</span>';
                                        else echo '<span class="badge bg-info">' . $days . ' days</span>';
                                    } else {
                                        echo htmlspecialchars($row[$col]);
                                    }
                                    ?>
                                </td>
                            <?php endforeach; ?>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
                <?php if ($report_type === 'consumption' && !empty($data)): ?>
                <tfoot class="table-light fw-bold">
                    <tr>
                        <td colspan="2" class="text-end"><?php echo __('total'); ?>:</td>
                        <td><?php echo number_format($grand_total_qty); ?></td>
                        <td><?php echo format_currency($grand_total_cost); ?></td>
                    </tr>
                </tfoot>
                <?php endif; ?>
            </table>
        </div>
        
        <div class="mt-3 text-end no-print">
            <button onclick="window.print()" class="btn btn-outline-secondary"><i class="bi bi-printer me-2"></i> <?php echo __('print_report'); ?></button>
        </div>
    </div>
</div>