<?php
/**
 * Export buttons — only after results are shown, and only if permitted.
 */
if (empty($searched)) {
    return;
}

$this->load->helper('rbac');
$module_key = isset($permission_module) ? $permission_module : '';
if ($module_key === '') {
    // Infer from current report URI segment
    $segs = $this->uri->segment_array();
    $module_key = isset($segs[2]) ? $segs[2] : '';
}

if ($module_key !== '' && !rbac_can_export($module_key)) {
    return;
}

$row_count = isset($total_rows) ? (int) $total_rows : (isset($report) && is_array($report) ? count($report) : 0);
$pdf_chunk_size = 35;
$progress_url = base_url('index.php/report/export_progress');
?>
<div class="report-export-bar"
     data-export-rows="<?php echo (int) $row_count; ?>"
     data-pdf-chunk-size="<?php echo (int) $pdf_chunk_size; ?>"
     data-progress-url="<?php echo htmlspecialchars($progress_url, ENT_QUOTES); ?>">
  <span class="report-export-label">Export</span>
  <button type="submit" name="submitForm" value="pdf" class="btn btn-report-pdf btn-sm"
          title="Download all rows as one PDF file">
    <i class="fas fa-file-pdf mr-1"></i> PDF
  </button>
  <button type="submit" name="submitForm" value="excel" class="btn btn-report-excel btn-sm">
    <i class="fas fa-file-excel mr-1"></i> Excel
  </button>
  <button type="submit" name="submitForm" value="word" class="btn btn-report-word btn-sm">
    <i class="fas fa-file-word mr-1"></i> Word
  </button>
  <?php if ($row_count > 0) { ?>
  <span class="text-muted small ml-2">
    Size: <?php echo number_format($row_count); ?> row<?php echo $row_count === 1 ? '' : 's'; ?> — PDF exports all rows as one file
  </span>
  <?php } ?>
</div>
