
import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/core/enum/view_state.dart';
import 'package:doctor_app_flutter/core/model/SOAP/progress_note/Clinic.dart';
import 'package:doctor_app_flutter/core/model/SOAP/progress_note/PatientCondition.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/core/viewModel/project_view_model.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/screens/patients/patient_search/patient_search_header.dart';
import 'package:doctor_app_flutter/screens/patients/profile/soap_update_vida_plus/assessment/widget/empty_dropdown.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_scaffold_widget.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/text_fields/app-textfield-custom.dart';
import 'package:flutter/material.dart';
import 'package:hexcolor/hexcolor.dart';
import 'package:provider/provider.dart';

class AddProgressNote extends StatefulWidget{
  final PatiantInformtion information;

  const AddProgressNote({super.key, required this.information});
  @override
  State<AddProgressNote> createState() => _AddProgressNoteState();
}

class _AddProgressNoteState extends State<AddProgressNote> {
  SOAPClinic? selectedClinicItem;

  PatientCondition? selectedPatientConditionItem;

  int status = 1;
  final TextEditingController noteController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    ProjectViewModel projectViewModel = Provider.of(context);

   return BaseView<SOAPViewModel>(
     onModelReady: (model){
       WidgetsBinding.instance.addPostFrameCallback((_) async {
         model.getClinics();
         model.getPatientConditionList();
       });
     },
     builder: (_, model, w) => AppScaffold(
       isLoading: model.state == ViewState.BusyLocal,
       isShowAppBar: true,
       appBar: PatientSearchHeader(
           title: TranslationBase.of(context).progressNote
       ),
       body: Padding(
         padding: const EdgeInsets.all(20.0),
         child: SingleChildScrollView(
           child: Material(
             shape: RoundedRectangleBorder(
                 borderRadius: BorderRadius.circular(10),
                 side: BorderSide(
                   width: 1,
                   color: Color(0xFFEFEFEF),
                 )),
             color: Colors.white,
             child: Padding(
               padding:
               const EdgeInsets.symmetric(horizontal: 16.0, vertical: 12),
               child: Column(
                 crossAxisAlignment: CrossAxisAlignment.start,
                 children: [
                   AppText(
                     TranslationBase.of(context).addProgressNote,
                     fontSize: 16,
                     fontWeight: FontWeight.w600,
                     textAlign: TextAlign.start,
                     color: Colors.black,
                   ),
                   SizedBox(
                     height: 16,
                   ),
                   Material(
                     shape: RoundedRectangleBorder(
                         borderRadius: BorderRadius.circular(10),
                         side: BorderSide(
                           width: 1,
                           color: Color(0xFFEFEFEF),
                         )),
                     color: Colors.white,
                     child: Padding(
                       padding: const EdgeInsets.symmetric(
                           horizontal: 16.0, vertical: 12),
                       child: Column(
                         crossAxisAlignment: CrossAxisAlignment.start,
                         children: [
                           AppText(
                             TranslationBase.of(context).patientCondition,
                             textAlign: TextAlign.start,
                             fontWeight: FontWeight.w600,
                             fontSize: 11,
                             color: Color(0xFF2E303A),
                           ),
                           SizedBox(
                             height: 4,
                           ),
                           model.patientConditionList.isEmpty
                               ? EmptyDropDown()
                               : DropdownButtonHideUnderline(
                             child: DropdownButton<PatientCondition>(
                               dropdownColor: Colors.white,
                               iconEnabledColor: Colors.black,
                               icon: Icon(Icons.keyboard_arrow_down),
                               isExpanded: true,
                               value:
                               selectedPatientConditionItem == null
                                   ? model.patientConditionList.first
                                   : selectedPatientConditionItem,
                               iconSize: 25,
                               elevation: 16,
                               onChanged: (newValue) async {
                                 setState(() {
                                   selectedPatientConditionItem =
                                       newValue;
                                 });
                               },
                               items: model.patientConditionList
                                   .map((item) {
                                 return DropdownMenuItem(
                                   child: AppText(
                                     item.name ?? '',
                                     fontSize: 14,
                                     letterSpacing: -0.96,
                                     color: AppGlobal.appTextColor,
                                     fontWeight: FontWeight.normal,
                                     textAlign: TextAlign.left,
                                   ),
                                   value: item,
                                 );
                               }).toList(),
                             ),
                           ),
                         ],
                       ),
                     ),
                   ),
                   SizedBox(
                     height: 16,
                   ),
                   AppText(
                     TranslationBase.of(context).progressNoteType,
                     fontWeight: FontWeight.bold,
                     fontFamily: 'Poppins',
                     fontSize: 11,
                   ),
                   SizedBox(
                     height: 8,
                   ),
                   Row(
                     children: [
                       Flexible(
                           child: InkWell(
                             onTap: () {
                               setState(() {
                                 status = 1;
                               });
                             },
                             child: Row(
                               children: [
                                 Container(
                                   padding: EdgeInsets.all(2.0),
                                   margin: EdgeInsets.symmetric(horizontal: 6),
                                   width: 20,
                                   height: 20,
                                   decoration: BoxDecoration(
                                     color: Colors.white,
                                     shape: BoxShape.circle,
                                     border:
                                     Border.all(color: Colors.grey, width: 1),
                                   ),
                                   child: Container(
                                     decoration: BoxDecoration(
                                       color: status == 1
                                           ? HexColor("#D02127")
                                           : Colors.white,
                                       shape: BoxShape.circle,
                                     ),
                                   ),
                                 ),
                                 AppText(
                                   TranslationBase.of(context).doctorProgressNote,
                                   fontWeight: FontWeight.w600,
                                   fontFamily: 'Poppins',
                                   fontSize: 12,
                                 ),
                               ],
                             ),
                           )),
                     ],
                   ),
                   SizedBox(
                     height: 24,
                   ),
                   Material(
                     shape: RoundedRectangleBorder(
                         borderRadius: BorderRadius.circular(10),
                         side: BorderSide(
                           width: 1,
                           color: Color(0xFFEFEFEF),
                         )),
                     color: Colors.white,
                     child: Padding(
                       padding: const EdgeInsets.symmetric(
                           horizontal: 16.0, vertical: 12),
                       child: Column(
                         crossAxisAlignment: CrossAxisAlignment.start,
                         children: [
                           AppText(
                             TranslationBase.of(context).speciality,
                             textAlign: TextAlign.start,
                             fontWeight: FontWeight.w600,
                             fontSize: 11,
                             color: Color(0xFF2E303A),
                           ),
                           SizedBox(
                             height: 4,
                           ),
                           model.clinics.isEmpty
                               ? EmptyDropDown()
                               : DropdownButtonHideUnderline(
                             child: DropdownButton<SOAPClinic>(
                               dropdownColor: Colors.white,
                               iconEnabledColor: Colors.black,
                               icon: Icon(Icons.keyboard_arrow_down),
                               isExpanded: true,
                               value: selectedClinicItem == null
                                   ? model.clinics.first
                                   : selectedClinicItem,
                               iconSize: 25,
                               elevation: 16,
                               onChanged: (newValue) async {
                                 setState(() {
                                   selectedClinicItem = newValue;
                                 });
                               },
                               items: model.clinics.map((item) {
                                 return DropdownMenuItem(
                                   child: AppText(
                                     projectViewModel.isArabic
                                         ? item.clinicNameArabic ?? ''
                                         : item.clinicNameEnglish ?? '',
                                     fontSize: 14,
                                     letterSpacing: -0.96,
                                     color: AppGlobal.appTextColor,
                                     fontWeight: FontWeight.normal,
                                     textAlign: TextAlign.left,
                                   ),
                                   value: item,
                                 );
                               }).toList(),
                             ),
                           ),
                         ],
                       ),
                     ),
                   ),
                   SizedBox(
                     height: 16,
                   ),
                   AppTextFieldCustom(
                     hintText: TranslationBase.of(context).addYourNote,
                     controller: noteController,
                     inputType: TextInputType.multiline,
                     maxLines: 25,
                     minLines: 4,
                     hasBorder: true,
                     onClick: () {},
                     onChanged: (value) {},
                     onFieldSubmitted: () {},
                   ),
                   SizedBox(
                     height: 16,
                   ),
                   // Row(
                   //   children: [
                   //     SvgPicture.asset(
                   //         'assets/images/svgs/save_as_draft.svg'),
                   //     SizedBox(
                   //       width: 4,
                   //     ),
                   //     AppText(
                   //       TranslationBase.of(context).saveAsDraft,
                   //       textAlign: TextAlign.start,
                   //       fontWeight: FontWeight.w600,
                   //       fontSize: 10,
                   //       color: Color(0xFF449BF1),
                   //     ),
                   //   ],
                   // )
                 ],
               ),
             ),
           ),
         ),
       ),
       bottomNavigationBar: Material(
           color: Colors.white,
           child: Padding(
               padding: const EdgeInsets.all(16.0),
               child: Row(
                 children: [
                   Expanded(
                     child: AppButton(
                       title: TranslationBase.of(context).cancelSmall,
                       color: Color(0xffEAEAEA),
                       fontColor: Colors.black,
                       fontWeight: FontWeight.w600,
                       onPressed: () async {
                         Navigator.pop(context);
                       },
                     ),
                   ),
                   SizedBox(
                     width: 10,
                   ),
                   Expanded(
                     child: AppButton(
                       title: TranslationBase.of(context).save,
                       color: Color(0xff359846),
                       fontColor: Colors.white,
                       fontWeight: FontWeight.w600,
                       onPressed: () async {
                         selectedPatientConditionItem ??= model.patientConditionList.first;
                         selectedClinicItem ??= model.clinics.first;

                         if(noteController.text.isEmpty){
                           DrAppToastMsg.showErrorToast(TranslationBase.of(context).progressNoteCanNotBeEmpty);
                           return;
                         }

                         bool result = await model.createProgressNote(widget.information, selectedClinicItem?.clinicID?.toString(), selectedPatientConditionItem?.code ?? '',noteController.text );
                         Navigator.pop(context, result);

                       },
                     ),
                   ),
                 ],
               ))),
     ),
   );
  }
}