<?php
// export_master_items.php
// Exports the full Master Bank as CSV in the exact column order
// master_items.php's importer expects, so export → edit in Excel →
// re-import round-trips cleanly.

require_once 'includes/auth.php';
require_role(['admin', 'inspector']);

require_once 'db.php';

$items = $pdo->query("SELECT id, item_name, category, certificate_period_months, reference_standards, inspection_procedure, description_template FROM tbl_master_items ORDER BY id ASC")->fetchAll(PDO::FETCH_ASSOC);

$filename = 'Liftek_MasterBank_' . date('Y-m-d') . '.csv';
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="' . $filename . '"');

$out = fopen('php://output', 'w');
fputcsv($out, ['id', 'item_name', 'category', 'certificate_period_months', 'reference_standards', 'inspection_procedure', 'description_template']);

foreach ($items as $item) {
    fputcsv($out, [
        $item['id'],
        $item['item_name'],
        $item['category'],
        $item['certificate_period_months'],
        $item['reference_standards'],
        $item['inspection_procedure'],
        $item['description_template'],
    ]);
}

fclose($out);
exit;
