import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:horizontal_data_table/horizontal_data_table.dart';


class QuestionTable extends StatefulWidget {
  final List headList;
  final List bodyList;
  const QuestionTable({
    Key? key, required this.headList, required this.bodyList,
  }) : super(key: key);


  @override
  State<QuestionTable> createState() => _QuestionTableState();
}

class _QuestionTableState extends State<QuestionTable> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return HorizontalDataTable(
      leftHandSideColumnWidth: 100,
      rightHandSideColumnWidth: (80.r * (widget.headList.length - 1)).toDouble(),
      isFixedHeader: true,
      headerWidgets: _getTitleWidget(),
      isFixedFooter: false,
      footerWidgets: _getTitleWidget(),
      leftSideItemBuilder: _generateFirstColumnRow,
      rightSideItemBuilder: _generateRightHandSideColumnRow,
      itemCount:widget.bodyList.length,
      rowSeparatorWidget: const Divider(
        color: Color(0xFFE0E5FF),
        height: 1.0,
        thickness: 0.0,
      ),
      // leftHandSideColBackgroundColor: const Color(0xFFFFFFFF),
      // rightHandSideColBackgroundColor: const Color(0xFFFFFFFF),
      // itemExtent: 31.r,
    );
  }

  List<Widget> _getTitleWidget() {
    return List.generate(widget.headList.length, (index){
      var item = widget.headList[index];
      return Container(
        height: 30.r,
        decoration: BoxDecoration(
            color: Color(0xFFF0F3FF),
            border:Border(right: BorderSide(width: 0.5.r,color: Color(0xFFE0E5FF)))
        ),
          width: index == 0?100.r:80.r,
          child: Center(
            child: Text(item.rowData[0][0],style: TextStyle(
                fontSize: 12.sp, color: Color(0xFF505767))),
          ),);
    });
  }

  Widget _getTitleItemWidget(String label, double width) {
    return Container(
      width: width,
      height: 56,
      padding: const EdgeInsets.fromLTRB(5, 0, 0, 0),
      alignment: Alignment.centerLeft,
      child: Text(label, style: const TextStyle(fontWeight: FontWeight.bold)),
    );
  }

  Widget _generateFirstColumnRow(BuildContext context, int index) {
    var item = widget.bodyList[index][0];
    return Container(
      width: 100.r,
      height: 30.r,
      decoration: BoxDecoration(
          border:Border(right: BorderSide(width: 0.5.r,color: Color(0xFFE0E5FF)))
      ),
      padding: const EdgeInsets.fromLTRB(5, 0, 0, 0),
      alignment: Alignment.centerLeft,
      child: Center(
        child: Text(item.toString(), style: TextStyle(
            fontSize: 12.sp,
            color: Color(0xFF262626))),
      ),
    );
  }

  Widget _generateRightHandSideColumnRow(BuildContext context, int index) {
    var bodyItem = widget.bodyList[index];
    return Row(
      children: List.generate(widget.headList.length -1, (bodyIndex){
        return  Container(
          width: 80.r,
          height: 30.r,
          decoration: BoxDecoration(
           border:Border(right: BorderSide(width: 0.5.r,color: Color(0xFFE0E5FF)))
          ),
          padding: const EdgeInsets.fromLTRB(5, 0, 0, 0),
          alignment: Alignment.centerLeft,
          child:  Center(
            child: Text(bodyItem[bodyIndex + 1].toString(), style: TextStyle(
                fontSize: 12.sp,
                color: Color(0xFF262626))),
          ),
        );
      }),
    );
  }
}