import 'dart:io';
import 'package:flutter/material.dart';

Widget HiddenListTable({
  required List<dynamic> hiddenList,
  required bool forbidEdit,
  required String baseImgPath,
  required String personSignImg,
  required String personSignTime,
  required void Function(Map<String, dynamic> item, int index) showHidden,
  required void Function(Map<String, dynamic> item, int index) removeHidden,
  required BuildContext context,
}) {
  Widget _buildCell(String text, {bool isHeader = false, Alignment alignment = Alignment.center, double minWidth = 50}) {
    return Container(
      padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 10),
      alignment: alignment,
      child: ConstrainedBox(
        constraints: BoxConstraints(minWidth: minWidth),
        child: Text(
          text,
          style: TextStyle(
            fontWeight: isHeader ? FontWeight.bold : FontWeight.normal,
            fontSize: 14,
            color: isHeader ? Colors.black87 : Colors.black54,
          ),
        ),
      ),
    );
  }

  // 仍保留 Table 的 header 用法（用于有数据时）
  TableRow _buildHeader() {
    return TableRow(
      decoration: BoxDecoration(color: Colors.grey.shade200),
      children: [
        _buildCell('序号', isHeader: true, alignment: Alignment.center),
        _buildCell('隐患部位', isHeader: true),
        _buildCell('隐患描述', isHeader: true),
        _buildCell('操作', isHeader: true, alignment: Alignment.center),
      ],
    );
  }

  TableRow _buildRow(Map<String, dynamic> item, int index) {
    final partName = (item['HIDDENPART_NAME'] ?? '').toString();
    final part = (item['HIDDENPART'] ?? '').toString();
    final descr = (item['HIDDENDESCR'] ?? '').toString();

    return TableRow(
      decoration: BoxDecoration(
        border: Border(
          bottom: BorderSide(color: Colors.grey.shade300, width: 0.5),
        ),
      ),
      children: [
        _buildCell('${index + 1}', alignment: Alignment.center),
        _buildCell(partName.isNotEmpty ? partName : part),
        _buildCell(descr),
        Padding(
          padding: const EdgeInsets.symmetric(vertical: 6),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              IconButton(
                icon: const Icon(Icons.info_outline, size: 22, color: Colors.blue),
                onPressed: () => showHidden(item, index),
                tooltip: '查看',
                padding: EdgeInsets.zero,
                constraints: const BoxConstraints(),
              ),
              if (forbidEdit) // 注意：原逻辑保留，如需反向请改为 if (!forbidEdit)
                IconButton(
                  icon: const Icon(Icons.delete_outline, size: 22, color: Colors.red),
                  onPressed: () => removeHidden(item, index),
                  tooltip: '删除',
                  padding: EdgeInsets.zero,
                  constraints: const BoxConstraints(),
                ),
            ],
          ),
        ),
      ],
    );
  }

  // 当 hiddenList 为空时，单独渲染表头（使用 Row + Expanded 来模仿 Table 的列宽）
  Widget _buildHeaderRowWidget() {
    return Container(
      decoration: BoxDecoration(color: Colors.grey.shade200),
      child: Row(
        children: [
          Expanded(flex: 2, child: _buildCell('序号', isHeader: true, alignment: Alignment.center)),
          Expanded(flex: 3, child: _buildCell('隐患部位', isHeader: true)),
          Expanded(flex: 3, child: _buildCell('隐患描述', isHeader: true)),
          Expanded(flex: 3, child: _buildCell('操作', isHeader: true, alignment: Alignment.center)),
        ],
      ),
    );
  }

  final tableWidth = MediaQuery.of(context).size.width - 50;

  // 如果没有数据，返回“表头 + 跨列居中显示暂无数据”的布局
  if (hiddenList.isEmpty) {
    return SingleChildScrollView(
      scrollDirection: Axis.horizontal,
      child: SizedBox(
        width: tableWidth,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            _buildHeaderRowWidget(),
            Container(
              height: 56,
              decoration: BoxDecoration(
                border: Border(
                  bottom: BorderSide(color: Colors.grey.shade300, width: 0.5),
                ),
              ),
              alignment: Alignment.center,
              child: Text(
                '暂无数据',
                style: TextStyle(fontSize: 14, color: Colors.black54),
              ),
            ),
          ],
        ),
      ),
    );
  }

  // 否则，使用原来的 Table 渲染有数据的行
  return SingleChildScrollView(
    scrollDirection: Axis.horizontal,
    child: SizedBox(
      width: tableWidth,
      child: Table(
        defaultVerticalAlignment: TableCellVerticalAlignment.middle,
        columnWidths: const {
          0: FlexColumnWidth(2), // 序号
          1: FlexColumnWidth(3), // 隐患部位
          2: FlexColumnWidth(3), // 隐患描述
          3: FlexColumnWidth(3), // 操作按钮
        },
        border: TableBorder.symmetric(
          inside: BorderSide(color: Colors.grey.shade300, width: 0.5),
        ),
        children: [
          _buildHeader(),
          ...hiddenList.asMap().entries.map((e) => _buildRow(e.value, e.key)),
        ],
      ),
    ),
  );
}
