part of '../../app/app.dart';

class TemplatePicker extends StatelessWidget {
  const TemplatePicker({
    super.key,
    required this.templates,
    required this.saving,
    required this.onUse,
  });

  final List<dynamic> templates;
  final bool saving;
  final ValueChanged<String> onUse;

  @override
  Widget build(BuildContext context) {
    final knownTemplates = templates
        .map(asMap)
        .where((template) => _knownDlp.contains(template['judul']?.toString()))
        .toList();

    if (knownTemplates.isEmpty) {
      return const EmptyPanel(text: 'Template expertise belum tersedia.');
    }

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const SectionTitle(title: 'Pilih Template'),
        ...knownTemplates.map(
          (template) => Card(
            margin: const EdgeInsets.only(bottom: 8),
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(8),
            ),
            child: ListTile(
              leading: const Icon(
                Icons.article_outlined,
                color: Color(0xFF0F766E),
              ),
              title: Text(template['judul']?.toString() ?? '-'),
              subtitle: Text('Kategori ${template['kategori'] ?? '-'}'),
              trailing: FilledButton(
                onPressed: saving
                    ? null
                    : () => onUse(template['judul'].toString()),
                child: const Text('Pakai'),
              ),
            ),
          ),
        ),
      ],
    );
  }
}
