import 'package:doctor_app_flutter/core/model/SOAP/chief_complaint/episode_by_chief_complaint_vidaplus.dart';
import 'package:doctor_app_flutter/core/model/SOAP/chief_complaint/get_chief_complaint_vida_plus.dart';
import 'package:doctor_app_flutter/screens/patients/profile/soap_update_vida_plus/subjective/chief_complaint/widgets/listOfComplaints.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/widgets/shared/app_texts_widget.dart';
import 'package:flutter/material.dart';
import 'package:quiver/time.dart';

class PreviousChiefComplaints extends StatelessWidget {
  final List<EpisodeByChiefComplaintVidaPlus> complaints;
  final Function(PatientPomrs , String)? onSendClicked;

  PreviousChiefComplaints(
    this.complaints,
    this.onSendClicked, {
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    return Material(
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(10),
            side: BorderSide(
              width: 1,
              color: Color(0xFFEFEFEF),
            )),
        color: Colors.white,
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 10.0, vertical: 10),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(TranslationBase.of(context).previousChiefCompaints,
                  textAlign: TextAlign.start,
                  style: TextStyle(
                    fontWeight: FontWeight.w600,
                    fontSize: 16,
                    color: Color(0xFF2E303A),
                  )),
              SizedBox(
                height: 16,
              ),
              AppText(
                TranslationBase.of(context).listOfActiveEpisodes,
                fontWeight: FontWeight.w400,
                fontSize: 14,
                textAlign: TextAlign.start,
                color: Color(0xFF575757),
              ),
              SizedBox(
                height: 10,
              ),
              Flexible(
                  child: ListView.builder(
                      shrinkWrap: true,
                      itemCount: complaints.length,
                      physics: NeverScrollableScrollPhysics(),
                      itemBuilder: (context, index) {
                        return ListView.builder(
                          shrinkWrap: true,
                          physics: NeverScrollableScrollPhysics(),
                          itemCount:
                              complaints[index].patientPomrs?.length ?? 0,
                          itemBuilder: (context, pomrID) {
                            return ListView.separated(
                                shrinkWrap: true,
                                physics: NeverScrollableScrollPhysics(),
                                itemCount: complaints[index]
                                        .patientPomrs?[pomrID]
                                        .chiefComplains
                                        ?.length ??
                                    0,
                                separatorBuilder: (context, _) => Column(
                                  children: [
                                    SizedBox(height: 10,),
                                    Divider(),
                                    SizedBox(height: 10,),
                                  ],
                                ),
                                itemBuilder: (context, cheifComplainIndex) {
                                  return ListOfComplaintsItem(
                                      name: complaints[index]
                                              .patientPomrs![pomrID]
                                              .chiefComplains?[
                                                  cheifComplainIndex]
                                              .chiefComplain ??
                                          '',
                                      // createdBy:   getChildValue(complaints[index].patientPomrs!,2),
                                      createdBy: complaints[index]
                                              .patientPomrs![pomrID]
                                              .chiefComplains?[
                                                  cheifComplainIndex]
                                              .createdBy ??
                                          '',
                                      createdAt: complaints[index]
                                              .patientPomrs![pomrID]
                                              .chiefComplains?[
                                                  cheifComplainIndex]
                                              .createdOn ??
                                          '',
                                      onSendClick: () {
                                        onSendClicked!(
                                            complaints[index]
                                                .patientPomrs![pomrID]
                                            ,complaints[index]
                                            .patientPomrs?[pomrID].chiefComplainTemplateId?.toString() ??'');
                                      });
                                });
                            // return Column(
                            //   children: complaints[index].patientPomrs![pomrID].chiefComplains?.map((element)=>ListOfComplaintsItem(
                            //     name:  element.chiefComplain ?? '',
                            //     // createdBy:   getChildValue(complaints[index].patientPomrs!,2),
                            //     createdBy:  element.createdBy ?? '',
                            //     createdAt:  element.createdOn ?? '',
                            //     onSendClick: () {
                            //       onSendClicked!(complaints[index].patientPomrs!);
                            //     })).toList()??[],
                            // );
                          },
                        );
                        // return complaints[index].patientPomrs!.isNotEmpty ? ListOfComplaintsItem(
                        //     name: getChildValue(complaints[index].patientPomrs!, 1),
                        //     createdBy:   getChildValue(complaints[index].patientPomrs!,2),
                        //       createdAt:  getChildValue(complaints[index].patientPomrs!, 3),
                        //     onSendClick: () {
                        //       onSendClicked!(complaints[index].patientPomrs!);
                        //     }) :SizedBox();
                      }))
            ],
          ),
        ));
  }

  String getChildValue(List<PatientPomrs>? patientPomrs, int key) {
    List<String> value = [];
    patientPomrs!.forEach((element) {
      if (key == 1) {
        element.chiefComplains!.forEach((element2) {
          value.add(element2.chiefComplain!);
        });
      } else if (key == 2) {
        value.add(element.doctorName!);
      } else {
        value.add(element.createdOn!);
      }
    });

    return value.join(" ");
  }
}
