import 'package:flutter/material.dart';
import 'package:qhdkfq_regulatory_flutter/home/home_page.dart';

class AdaptiveListItem extends StatelessWidget {
  final String title;
  final List<Map<String, String>> items;
  final List<Widget>? actions;
  final HomeCategroyType type;
  final int selectTabIndex;

  const AdaptiveListItem({
    super.key,
    required this.title,
    required this.items,
    required this.actions,
    required this.type,
    required this.selectTabIndex,
  });

  @override
  Widget build(BuildContext context) {
    // 按 2 个一组切片
    final List<List<Map<String, String>>> rows = [];
    final bool isAllowSelect = (type == HomeCategroyType.enterpriseInfo && selectTabIndex == 2) ||
        (type == HomeCategroyType.keySafety && selectTabIndex == 1);
    for (var i = 0; i < items.length; i += 2) {
      rows.add(items.sublist(i, i + 2 > items.length ? items.length : i + 2));
    }

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        // 标题
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              title,
              style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
            ),
            if (isAllowSelect)
              Icon(Icons.arrow_forward_ios_rounded, size: 15, color: Colors.black26,)
          ],
        ),
        const SizedBox(height: 8),

        // 每一行最多两个
        ...rows.map((rowItems) {
          return Padding(
            padding: const EdgeInsets.only(bottom: 8),
            child: _buildItemRow(rowItems[0]),
          );
        }),
        if (actions!.isNotEmpty)
          Column(
            spacing: 15,
            children: [
              Divider(height: 1,),
              Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: actions!,
              )
            ],
          )

      ],
    );
  }

  Widget _buildItemRow(Map<String, String> item) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          "${item['title']}: ",
          style: const TextStyle(
            fontWeight: FontWeight.w500,
            color: Colors.grey,
          ),
        ),
        Expanded(
          child: Text(
            item['value'] ?? '',
            style: const TextStyle(fontWeight: FontWeight.w500),
            softWrap: true,
          ),
        ),
      ],
    );
  }
}
