import 'package:flutter/material.dart';
import 'package:vitalapp/architecture/types/index.dart';
import 'package:vitalapp/components/button.dart';
import 'package:vitalapp/pages/check/models/form.dart';
import 'package:vitalapp/pages/check/widgets/exam_configurable/exam_card.dart';

class ExamTable extends StatelessWidget {
  const ExamTable({
    super.key,
    required this.tableThList,
    required this.currentFormObject,
    this.currentValue,
    this.addTableData,
    this.editTableData,
  });

  final List<String> tableThList;
  final FormObject currentFormObject;
  final List<dynamic>? currentValue;
  final Function? addTableData;
  final ValueCallback? editTableData;
  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        ExamCard(
          title: currentFormObject.label ?? '',
          clickCard: null,
          content: Container(
            alignment: Alignment.center,
            padding: const EdgeInsets.only(
              // bottom: 20,
              right: 20,
              left: 20,
            ),
            child: SingleChildScrollView(
                scrollDirection: Axis.horizontal,
                child: DataTable(
                  columnSpacing: 80,
                  headingRowColor: MaterialStateProperty.all<Color?>(
                      Theme.of(context).primaryColor),
                  dataRowHeight: 60,
                  headingTextStyle: const TextStyle(
                    fontSize: 24,
                    color: Colors.white,
                  ),
                  dataTextStyle: TextStyle(
                    fontSize: 22,
                    color: Colors.black.withOpacity(0.9),
                  ),
                  columns: tableThList
                      .map(
                        (e) => DataColumn(
                          numeric: false,
                          label: Text(
                            e,
                          ),
                        ),
                      )
                      .toList(),
                  rows: currentValue!.map((c) {
                    List<DataCell> cells = (c as Map)
                        .entries
                        .map(
                          (a) => DataCell(
                            Container(
                              constraints: const BoxConstraints(
                                maxWidth: 150,
                              ),
                              child: Text(
                                a.value ?? '',
                                overflow: TextOverflow.ellipsis,
                              ),
                            ),
                          ),
                        )
                        .toList();
                    cells.add(
                      DataCell(
                        Text(
                          '编辑',
                          style: TextStyle(
                            fontSize: 22,
                            color: Theme.of(context).primaryColor,
                          ),
                        ),
                        onTap: () {
                          editTableData!.call(
                            EditTableValue(
                              id: int.parse(c['id']),
                              value: currentValue![int.parse(c['id']) - 1],
                            ),
                          );
                        },
                      ),
                    );
                    return DataRow(
                      cells: cells,
                    );
                  }).toList(),
                )),
          ),
        ),
        Positioned(
          right: 16,
          top: 8,
          child: SizedBox(
            width: 130,
            height: 54,
            child: VButton(
              // label: '新增',
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: const [
                  Icon(Icons.add, size: 24),
                  SizedBox(
                    width: 8,
                  ),
                  Text("新增", style: TextStyle(fontSize: 20)),
                ],
              ),
              onTap: () {
                addTableData!();
              },
            ),
          ),
        ),
      ],
    );
  }
}
