import 'package:data_table_2/data_table_2.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:marking_app/common/model/job/job_data_report.dart';

class StudentKgTable extends StatefulWidget {
  final List headList;
  final List bodyList;
  final int? fixedRows;
  final int? fixedCols;

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

  @override
  State<StudentKgTable> createState() => _StudentKgTableState();
}

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

  String sortString(String str) {
    return String.fromCharCodes(str.codeUnits.toList()..sort());
  }
  DataRow _getRow(int index, [Color? color]) {
    assert(index >= 0);
    KgDetails 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(0xFF6888FD))),
          ),
        )),
        DataCell(Center(
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: 5.r),
            child: Text(item.studentAnswer == null?'未作答':item.studentAnswer!,
                style: TextStyle(fontSize: 12.sp, color: item.studentAnswer == null?Color(0xFF525252):
                item.state == 2?Color(0xFF4CC793):
                item.state == 1?Color(0xFFFF7474):item.state == 0?Color(0xFFD3D3D3):Colors.white),
          ),
        ))),
        DataCell(Center(
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: 5.r),
            child: Text(item.questionAnswer == null ?'无':item.questionAnswer!,
                style: TextStyle(fontSize: 12.sp, color: Color(0xFF525252))),
          ),
        )),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return DataTable2(
        dividerThickness: 0,
        scrollController: _controller,
        columnSpacing: 0,
        horizontalMargin: 0,
        bottomMargin: 0,
        dataRowHeight: 40.r,
        headingRowHeight: 40.r,
        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(color: Color(0xFFE6E6E6)),
        fixedColumnsColor: Color(0xFFE6E6E6),
        fixedCornerColor: Colors.grey[400],
        minWidth: 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: (MediaQuery.of(context).size.width - 20.r - 28.r)/3,
          );
        }),
        rows: List<DataRow>.generate(widget.bodyList.length,
            (index) => _getRow(index, Color(0xFFF5F5F5))));
  }
}
