import 'package:doctor_app_flutter/core/model/SOAP/home_medication_vp/GetHomeMedication.dart';
import 'package:doctor_app_flutter/core/model/SOAP/selected_items/my_selected_allergy.dart';
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/screens/patients/profile/soap_update/shared_soap_widgets/SOAP_open_items.dart';
import 'package:doctor_app_flutter/screens/patients/profile/soap_update_vida_plus/subjective/chief_complaint/widgets/add_soap_item.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/loader/gif_loader_dialog_utils.dart';
import 'package:doctor_app_flutter/widgets/shared/text_fields/app-textfield-custom.dart';
import 'package:doctor_app_flutter/widgets/transitions/fade_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';

import 'add_medication.dart';

class UpdateMedicationWidget extends StatefulWidget {
  final PatiantInformtion patientInfo;

  UpdateMedicationWidget({
    Key? key,
    required this.patientInfo,
  });

  @override
  _UpdateMedicationWidgetState createState() => _UpdateMedicationWidgetState();
}

class _UpdateMedicationWidgetState extends State<UpdateMedicationWidget> {
  TextEditingController medicationController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return BaseView<SOAPViewModel>(
        onModelReady: (model) async {
          WidgetsBinding.instance.addPostFrameCallback((_) {
            model.getHomeMedication(widget.patientInfo);
          });
        },
        builder: (_, model, w) => Column(children: [
              AddSoapItem(
                title: "${TranslationBase.of(context).addMedication}",
                onAddSoapItemClicked: () {
                  Navigator.push(
                      context,
                      FadePage(
                          page: AddMedication(
                              patientInfo: widget.patientInfo,
                              addMedicationFun: () {})));
                  // openMedicationList(context);
                },
              ),
              SizedBox(
                height: 20,
              ),
              ListView(
                  padding: EdgeInsets.all(10),
                  shrinkWrap: true,
                  physics: NeverScrollableScrollPhysics(),
                  children: model.getHomeMedicationList!.map((medication) {
                    return ListTile(
                      trailing: TextButton.icon(
                          onPressed: () {
                            removeMedication(medication, model);
                          },
                          icon: SvgPicture.asset(
                            "assets/images/svgs/delete.svg",
                            height: 18,
                            color: Color(0xffD02127),
                          ),
                          label: AppText(TranslationBase.of(context).remove,
                              fontSize: 12, color: Color(0xffD02127))),
                      title: AppText(
                        medication.prescribedItemName!,
                        fontSize: 12,
                        fontWeight: FontWeight.w800,
                        letterSpacing: -0.48,
                      ),
                      subtitle: AppText(
                        '${medication.doseQuantity!} - ${medication.frequencyString!}',
                        fontSize: 10,
                      ),
                    );
                  }).toList()),
            ]));
  }

  removeMedication(
      GetHomeMedicationList medication, SOAPViewModel model) async {
    GifLoaderDialogUtils.showMyDialog(context);
    await model.removeCurrentMedication(medication.id!);
    await model.getHomeMedication(widget.patientInfo);
    GifLoaderDialogUtils.hideDialog(context);
  }
}
