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 CardList extends StatefulWidget {
  final List headList;
  final List bodyList;
  final Function? showCardDetail;
  final bool? isScore;
  final int? fixedRows;
  final int? fixedCols;
  final bool? isHtml;

  const CardList({
    Key? key,
    required this.headList,
    required this.bodyList,
    this.showCardDetail,
    this.isScore = false,
    this.isHtml = false,
     this.fixedCols,
     this.fixedRows,
  }) : super(key: key);

  @override
  State<CardList> createState() => _CardListState();
}

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

  DataRow _getRow(int index, [Color? color]) {
    assert(index >= 0);
    var body = widget.bodyList[index];
    return DataRow2.byIndex(
      index: index,
      color: color != null ? MaterialStateProperty.all(color) : null,
      cells: List.generate(widget.headList.length, (itemIndex) {
        var item = body[itemIndex];
        return
          itemIndex == 0 && widget.isScore == false ?DataCell(Center(
            child: Text((index + 1).toString(),
                style:
                TextStyle(fontSize: 12.sp, color: Color(0xFF262626))),
          )):
          DataCell(itemIndex == 2 && widget.isScore == false
            ? InkWell(
                onTap: () {
                  if (widget.showCardDetail != null) {
                    widget.showCardDetail!(context, index);
                  }
                },
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Expanded(
                      child: Center(
                        child: Text(item.toString(),
                            style: TextStyle(
                                fontSize: 12.sp, color: Color(0xFF262626))),
                      ),
                    ),
                    Padding(
                      padding: EdgeInsets.only(right: 5.r),
                      child: Image.asset(
                        'assets/images/search_icon.png',
                        width: 14.r,
                        height: 14.r,
                      ),
                    ),
                  ],
                ),
              )
            : Center(
                child: Padding(
                  padding: EdgeInsets.symmetric(horizontal: 5.r),
                  child:
                  widget.isHtml == true?HtmlWidget(item.toString(),textStyle: TextStyle(fontSize: 12.sp,color:  Color(0xFF262626)),):
                  Text(item.toString(),
                      style:
                          TextStyle(fontSize: 12.sp, color: Color(0xFF262626))),
                ),
              ));
      }),
    );
  }

  @override
  Widget build(BuildContext context) {
    return DataTable2(
        dividerThickness: 0,
        scrollController: _controller,
        columnSpacing: 0,
        horizontalMargin: 0,
        dataRowHeight:50.r,
        bottomMargin: 0,
        border: TableBorder(
            horizontalInside: BorderSide(
                width: 1, color: Color(0xFFE0E5FF), style: BorderStyle.solid),
            bottom: BorderSide(
                width: 1, color: Color(0xFFE0E5FF), style: BorderStyle.solid),
            verticalInside: BorderSide(
                width: 1, color: Color(0xFFE0E5FF), style: BorderStyle.solid)),
        headingRowColor: MaterialStateProperty.resolveWith((states) =>
            widget.fixedCols! > 0 ? Color(0xFFF0F3FF) : Colors.transparent),
        /* headingRowDecoration: BoxDecoration(
          gradient: LinearGradient(
            colors: [
              Colors.grey[400]!,
              Colors.grey[200]!,
            ],
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
          ),
        ),*/
        headingRowDecoration: BoxDecoration(color: Color(0xFFF0F3FF)),
        fixedColumnsColor: Color(0xFFF0F3FF),
        fixedCornerColor: Colors.grey[400],
        minWidth: widget.headList.length > 5?70.r * widget.headList.length + 10.r: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(widget.isScore == true ? item.rowData[0][0] : item,
                  style: TextStyle(fontSize: 12.sp, color: Color(0xFF505767))),
            ),
            // size: ColumnSize.S,
            fixedWidth: index == 0 && widget.isScore == false?50.r:widget.headList.length > 5 ?70.r:(MediaQuery.of(context).size.width - 28.r)/widget.headList.length,
          );
        }),
        rows: List<DataRow>.generate(widget.bodyList.length,
            (index) => _getRow(index, Colors.transparent)));
  }
}
