import 'package:data_table_2/data_table_2.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_widget_from_html_core/flutter_widget_from_html_core.dart';

class ReportTable extends StatefulWidget {
  final List headList;
  final List bodyList;
  final int? fixedRows;
  final int? fixedCols;
  final bool? isKG;

  const ReportTable({
    Key? key,
    required this.headList,
    required this.bodyList,
     this.fixedCols = 0,
     this.fixedRows = 0,
    this.isKG = false,
  }) : super(key: key);

  @override
  State<ReportTable> createState() => _ReportTableState();
}

class _ReportTableState extends State<ReportTable> {
  final ScrollController _controller = ScrollController();
  int? _sortColumnIndex;
  bool _sortAscending = true;

  DataRow _getRow(int index, [Color? color]) {
    assert(index >= 0);
    var item = widget.bodyList[index];
    return DataRow2.byIndex(
      index: index,
      color: color != null ? MaterialStateProperty.all(color) : null,
      cells: [
        DataCell( Center(
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: 5.r),
            child:
            Text(item.questionNo,
                style:
                TextStyle(fontSize: 12.sp, color: Color(0xFF525252))),
          ),
        )),
        DataCell( Center(
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: 5.r),
            child:
            Text('${item.validRate}%',
                style:
                TextStyle(fontSize: 12.sp, color: Color(0xFF525252))),
          ),
        )),
        DataCell( Center(
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: 5.r),
            child:
            Text(item.validCount,
                style:
                TextStyle(fontSize: 12.sp, color: Color(0xFF4CC793))),
          ),
        )),
        DataCell( Center(
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: 5.r),
            child:
            Text('${item.correctRate}%',
                style:
                TextStyle(fontSize: 12.sp, color: Color(0xFF525252))),
          ),
        )),
        DataCell( Center(
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: 5.r),
            child:
            Text(
                widget.isKG == true?'原题':item.questionAnswer,
                style:
                TextStyle(fontSize: 12.sp, color: widget.isKG == true?Color(0xFFFF8A00):Color(0xFF4CC793))),
          ),
        )),
        DataCell( Center(
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: 5.r),
            child:
            Text(item.priorityGeneral,
                style:
                TextStyle(fontSize: 12.sp, color: Color(0xFF6888FD))),
          ),
        )),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return DataTable2(
        dividerThickness: 0,
        scrollController: _controller,
        columnSpacing: 0,
        horizontalMargin: 0,
        dataRowHeight:40.r,
        bottomMargin: 0,
        border: TableBorder(
            horizontalInside: BorderSide(
                width: 1, color: Colors.white, style: BorderStyle.solid),
            bottom: BorderSide(
                width: 1, color: Colors.white, style: BorderStyle.solid),
            verticalInside: BorderSide(
                width: 1, color: Colors.white, style: BorderStyle.solid)),
        headingRowColor: MaterialStateProperty.resolveWith((states) =>
            widget.fixedCols! > 0 ? Colors.white : Colors.transparent),
        /* headingRowDecoration: BoxDecoration(
          gradient: LinearGradient(
            colors: [
              Colors.grey[400]!,
              Colors.grey[200]!,
            ],
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
          ),
        ),*/
        headingRowDecoration: BoxDecoration(color: Color(0xFFE6E6E6)),
        fixedColumnsColor: Color(0xFFE6E6E6),
        fixedCornerColor: Colors.grey[400],
        minWidth: widget.headList.length > 6?80.r * widget.headList.length:MediaQuery.of(context).size.width,
        fixedTopRows:  widget.fixedRows!,
        fixedLeftColumns: widget.fixedCols!,
        sortColumnIndex: _sortColumnIndex,
        sortAscending: _sortAscending,
        // onSelectAll: (val) => setState(() => selectAll(val)),
        columns: List.generate(widget.headList.length, (index) {
          var item = widget.headList[index];
          return DataColumn2(
            label: Center(
              child: Text(item,
                  style: TextStyle(fontSize: 12.sp, color: Color(0xFF505767))),
            ),
            // size: ColumnSize.S,
            fixedWidth: index == 0 ? 40.r:widget.headList.length > 6 ?80.r:(MediaQuery.of(context).size.width)/widget.headList.length,
          );
        }),
        rows: List<DataRow>.generate(widget.bodyList.length,
            (index) => _getRow(index, Color(0xFFF5F5F5))));
  }
}
