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

require_once 'db.php';

$items = $pdo->query("SELECT MasterCheckID, item_name, Category, CheckItem, DisplayOrder, IsActive FROM tbl_master_checklists ORDER BY Category, item_name, DisplayOrder ASC")->fetchAll(PDO::FETCH_ASSOC);

$filename = 'Liftek_Checklists_' . 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, ['MasterCheckID', 'item_name', 'Category', 'CheckItem', 'DisplayOrder', 'IsActive']);

foreach ($items as $item) {
    fputcsv($out, [
        $item['MasterCheckID'],
        $item['item_name'],
        $item['Category'],
        $item['CheckItem'],
        $item['DisplayOrder'],
        $item['IsActive'],
    ]);
}

fclose($out);
exit;
