import 'package:flutter/material.dart';

import 'alert_dialog.dart';

class VDialogTable extends StatelessWidget {
  const VDialogTable({
    super.key,
    this.title,
    required this.columnNames,
    required this.tableData,
    this.diagnosisTime,
  });
  final List<List<String>> tableData;
  final List<String> columnNames;
  final String? title;
  final String? diagnosisTime;
  Future<DateTime?> show<DateTime>() => VAlertDialog.showDialog<DateTime>(this);
  @override
  Widget build(BuildContext context) {
    return VAlertDialog(
      width: 800,
      contentPadding: const EdgeInsets.only(left: 16, right: 8),
      content: _buildTable(),
      showCancel: true,
    );
  }

  Widget _buildTable() {
    return SizedBox(
      height: 500,
      child: Column(
        children: [
          const SizedBox(
            height: 10,
          ),
          if (title != null)
            Text(
              title!,
              style: const TextStyle(
                fontSize: 22,
              ),
            ),
          const SizedBox(
            height: 10,
          ),
          Table(
            children: [
              TableRow(
                decoration: const BoxDecoration(
                  color: Color.fromRGBO(215, 234, 255, 1),
                ),
                children: columnNames
                    .map((columnName) => _buildHeaderCell(columnName))
                    .toList(),
              ),
            ],
          ),
          Expanded(
            child: Scrollbar(
              thumbVisibility: true,
              child: SingleChildScrollView(
                child: Table(
                  children: _buildRow(tableData),
                ),
              ),
            ),
          ),
          const SizedBox(
            height: 10,
          ),
          Container(
              alignment: Alignment.centerLeft,
              child: Text(
                "检测时间：${diagnosisTime ?? ''}",
                style: const TextStyle(fontSize: 18),
              )),
          const SizedBox(
            height: 10,
          ),
          Container(
              alignment: Alignment.centerLeft,
              child: const Text(
                '注：本结果仅对该检验样本负责！',
                style: TextStyle(fontSize: 18),
              ))
        ],
      ),
    );
  }

  List<TableRow> _buildRow(List<List<String>> tableData) {
    var tableRows = <TableRow>[];
    for (var i = 0; i < tableData.length; i++) {
      tableRows.add(TableRow(
          decoration: i % 2 == 0
              ? null
              : const BoxDecoration(
                  color: Color.fromRGBO(233, 244, 255, 1),
                ),
          children: tableData[i]
              .map(
                (cellData) => _buildDataCell(cellData),
              )
              .toList()));
    }
    return tableRows;
  }

  TableCell _buildHeaderCell(String title) {
    return TableCell(
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 14),
        alignment: Alignment.centerLeft,
        height: 37,
        child: Text(
          title,
          style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
        ),
      ),
    );
  }

  TableCell _buildDataCell(String title) {
    return TableCell(
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 14),
        alignment: Alignment.centerLeft,
        height: 37,
        child: Text(
          title,
          style: const TextStyle(fontSize: 14),
        ),
      ),
    );
  }
}
