import 'package:doctor_app_flutter/core/model/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/core/viewModel/SOAP_view_model.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/utils/dr_app_toast_msg.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:doctor_app_flutter/widgets/shared/buttons/app_buttons_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/loader/gif_loader_dialog_utils.dart';
import 'package:doctor_app_flutter/widgets/shared/text_fields/app-textfield-custom.dart';
import 'package:flutter/material.dart';


class UpdatePresentIllness extends StatefulWidget {
  PatiantInformtion? patientInfo;
  UpdatePresentIllness(this.patientInfo, {Key? key})  : super(key: key);

  @override
  State<UpdatePresentIllness> createState() => UpdatePresentIllnessState();
}

class UpdatePresentIllnessState extends State<UpdatePresentIllness> {
  bool isPatientSelected = true;
  bool isFamilySelected = false;
  bool isOtherSelected = false;

  final TextEditingController familyController = TextEditingController();
  final TextEditingController otherController = TextEditingController();
  final TextEditingController patientController = TextEditingController();
  @override
  void initState() {

    super.initState();
  }

  @override
  Widget build(BuildContext context) {

    return BaseView<SOAPViewModel>(
      onModelReady: (model){
        WidgetsBinding.instance.addPostFrameCallback((_) {
          getHopi(model);
        });
      },
        builder: (_, model, w) => Column(
          // key:UpdatePresentIllness.HopiKey,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        SizedBox(
          height: 8,
        ),
        AppText(
          TranslationBase.of(context).historyTakenFrom,
          fontSize: 14,
          fontWeight: FontWeight.w600,
          textAlign: TextAlign.start,
          color: Colors.black,
        ),

        ListTileTheme(
          horizontalTitleGap: 0,
          child: CheckboxListTile(
              value: this.isPatientSelected,
              activeColor: Color(0xFFD02127),
              checkColor: Colors.white,
              controlAffinity: ListTileControlAffinity.leading,
              side: MaterialStateBorderSide.resolveWith(
                (Set<MaterialState> states) {
                  if (states.contains(MaterialState.selected)) {
                    return const BorderSide(color: Color(0xFFD02127));
                  }
                  return const BorderSide(color: Color(0xFFE6E6E6));
                },
              ),
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(16)),
              onChanged: (bool? value) {
                setState(() {
                  isPatientSelected = value ?? false;
                });
              },
              title: AppText(
                TranslationBase.of(context).patient,
                color: Color(0XFF575757),
                fontSize: 14,
                fontWeight: FontWeight.w400,
              ),
              contentPadding: EdgeInsets.zero),
        ),

        ListTileTheme(
          horizontalTitleGap: 0,
          child: CheckboxListTile(
              value: isFamilySelected,
              activeColor: Color(0xFFD02127),
              checkColor: Colors.white,
              controlAffinity: ListTileControlAffinity.leading,
              side: MaterialStateBorderSide.resolveWith(
                (Set<MaterialState> states) {
                  if (states.contains(MaterialState.selected)) {
                    return const BorderSide(color: Color(0xFFD02127));
                  }
                  return const BorderSide(color: Color(0xFFE6E6E6));
                },
              ),
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(16)),
              onChanged: (bool? value) {
                setState(() {
                  isFamilySelected = value ?? false;
                });
              },
              title: AppText(
                TranslationBase.of(context).familySpecify,
                color: Color(0XFF575757),
                fontSize: 14,
                fontWeight: FontWeight.w400,
              ),
              contentPadding: EdgeInsets.zero),
        ),
        Visibility(
          visible: isFamilySelected,
            child: Column(children: [
          AppTextFieldCustom(
            hintText: TranslationBase.of(context).remarks,
            controller: familyController,
            inputType: TextInputType.multiline,
            maxLines: 25,
            minLines: 3,
            hasBorder: true,
            isTextFieldHasSuffix: true,
            onClick: () {},
            onChanged: (value) {},
            onFieldSubmitted: () {},

            suffixIcon: IconButton(icon:Icon(Icons.save_as_outlined, color: Colors.red, size: 25,), onPressed: (){
              saveHopi(model);
            },),
          ),
        ])),

        ListTileTheme(
          horizontalTitleGap: 0,
          child: CheckboxListTile(
              value: isOtherSelected,
              activeColor: Color(0xFFD02127),
              checkColor: Colors.white,
              controlAffinity: ListTileControlAffinity.leading,
              side: MaterialStateBorderSide.resolveWith(
                (Set<MaterialState> states) {
                  if (states.contains(MaterialState.selected)) {
                    return const BorderSide(color: Color(0xFFD02127));
                  }
                  return const BorderSide(color: Color(0xFFE6E6E6));
                },
              ),
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(16)),
              onChanged: (bool? value) {
                setState(() {
                  isOtherSelected = value ?? false;
                });
              },
              title: AppText(
                TranslationBase.of(context).otherSpecify,
                color: Color(0XFF575757),
                fontSize: 14,
                fontWeight: FontWeight.w400,
              ),
              contentPadding: EdgeInsets.zero),
        ),
        Visibility(
          visible: isOtherSelected,
          child: Column(
            children: [
              AppTextFieldCustom(
                hintText: TranslationBase.of(context).remarks,
                controller: otherController,
                inputType: TextInputType.multiline,
                maxLines: 25,
                minLines: 3,
                hasBorder: true,
                isTextFieldHasSuffix: true,
                onClick: () {},
                onChanged: (value) {},
                onFieldSubmitted: () {}, suffixIcon: IconButton(icon:Icon(Icons.save_as_outlined, color: Colors.red, size: 25,), onPressed: (){
                saveHopi(model);
              },),
              ),
            ],
          ),
        ),

        Divider(),
        SizedBox(
          height: 8,
        ),
        AppText(
          TranslationBase.of(context).historyOfIllness,
          fontSize: 14,
          fontWeight: FontWeight.w600,
          textAlign: TextAlign.start,
          color: Colors.black,
        ),
        SizedBox(
          height: 8,
        ),
        AppTextFieldCustom(
          isTextFieldHasSuffix: true,
          hintText: TranslationBase.of(context).remarks,
          controller: patientController,
          inputType: TextInputType.multiline,
          maxLines: 25,
          minLines: 3,
          hasBorder: true,
          onClick: () {},
          onChanged: (value) {
            print("On Field Changed..");
          },
          onFieldSubmitted: () {
              print("---Submit.....");
          },
          suffixIcon: IconButton(icon:Icon(Icons.save_as_outlined, color: Colors.red, size: 25,), onPressed: (){
            saveHopi(model);
          },),
        ),
        SizedBox(
          height: 8,
        ),

        // AppText(
        //   'Last saved: 22-10-2024 08:07 am [3 hours ago]',
        //   fontSize: 10,
        //   fontWeight: FontWeight.normal,
        //   textAlign: TextAlign.start,
        //   color: Color(0XFF575757),
        // ),
      ],
    ),

    );
  }
  saveHopi(SOAPViewModel model) async{
      if(patientController.text.isEmpty) {
        DrAppToastMsg.showErrorToast("Please Enter Remarks");
        return;
      }
      GifLoaderDialogUtils.showMyDialog(context);
    Map<String,dynamic> request = {
      "hpi": patientController.text,
      "isHpiTakenPatient": isPatientSelected,
      "isHpiTakenFamily": isFamilySelected,
      "isHpiTakenOther": isOtherSelected,
      "hpiTakenOtherText": otherController.text,
      "hpiTakenFamilyText": familyController.text,
    };
     bool result = await model!.saveHopi(request, widget.patientInfo!);
     if(result){
       getHopi(model);
     }

    GifLoaderDialogUtils.hideDialog(context);
  }
  getHopi(SOAPViewModel model) async{
    await model!.getHopi(widget.patientInfo!);
    setData(model);
  }
  setData(SOAPViewModel model){
    if(model.getHopiDetails.isNotEmpty) {
      patientController.text = model.getHopiDetails[0].hpi!;
      familyController.text = model.getHopiDetails[0].hpiTakenFamilyText!;
      otherController.text = model.getHopiDetails[0].hpiTakenOtherText!;
      isOtherSelected = model.getHopiDetails[0].isHpiTakenOther!;
      isFamilySelected = model.getHopiDetails[0].isHpiTakenFamily!;
      isPatientSelected = model.getHopiDetails[0].isHpiTakenPatient!;
    }
  }
}
