import 'package:dropdown_button2/dropdown_button2.dart';
import 'package:flutter/material.dart';
import 'package:generp/Notifiers/PendingComplaintsProvider.dart';
import 'package:generp/Utils/app_colors.dart';
import 'package:generp/Utils/commonWidgets.dart';
import 'package:provider/provider.dart';

class Updatecomplaints extends StatefulWidget {
  final ComplaintID;
  const Updatecomplaints({super.key,required this.ComplaintID});

  @override
  State<Updatecomplaints> createState() => _UpdatecomplaintsState();
}

class _UpdatecomplaintsState extends State<Updatecomplaints> {
  TextEditingController fsrNumberController = TextEditingController();
  TextEditingController runningHoursController = TextEditingController();
  TextEditingController feedbackController = TextEditingController();
  final List<Map<String, dynamic>> CompletedStatus = [
    {"id": "1", "name": "Pending"},
    {"id": "2", "name": "Completed"}
  ];

  @override
  Widget build(BuildContext context) {
    return Consumer<Pendingcomplaintsprovider>(
      builder: (context, provider, child) {
        return WillPopScope(
          onWillPop: () => onBackPressed(context),
          child: Scaffold(
            resizeToAvoidBottomInset: true,
            appBar: appbar(context, "Update Complaint"),
            backgroundColor: AppColors.scaffold_bg_color,
            body: Container(
              padding: EdgeInsets.symmetric(horizontal: 10,vertical: 10),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(20)
              ),
              child: SingleChildScrollView(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    SizedBox(height: 15),

                    Padding(
                      padding: const EdgeInsets.only(bottom: 5.0),
                      child: Text(
                        "Complaint Status",
                        style: TextStyle(
                          fontFamily: "JakartaMedium",
                          color: AppColors.semi_black,
                        ),
                      ),
                    ),
                    DropdownButtonHideUnderline(
                      child: Row(
                        children: [
                          Expanded(
                            child: DropdownButton2<String>(
                              isExpanded: true,
                              hint: const Row(
                                children: [
                                  Expanded(
                                    child: Text(
                                      'Select Complaint Status',
                                      style: TextStyle(
                                        fontSize: 14,
                                      ),
                                      overflow: TextOverflow.ellipsis,
                                    ),
                                  ),
                                ],
                              ),
                              items: CompletedStatus.map(
                                      (complaintStatus) =>
                                      DropdownMenuItem<String>(
                                        value: complaintStatus['name'],
                                        child: Text(
                                          complaintStatus['name'] ?? '',
                                          style: const TextStyle(
                                            fontSize: 14,
                                          ),
                                          overflow: TextOverflow.ellipsis,
                                        ),
                                      )).toList(),
                              value: provider.statusID,
                              onChanged: (value) {
                                if (value != null) {
                                  provider.statusID = value;
                                  print("statusId:${provider.statusID}");
                                }
                              },
                              buttonStyleData: ButtonStyleData(
                                height: 50,
                                width: 160,
                                padding: const EdgeInsets.only(
                                    left: 14, right: 14),
                                decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(14),
                                  color: AppColors.text_field_color,
                                ),
                              ),
                              iconStyleData: const IconStyleData(
                                icon: Icon(
                                  Icons.keyboard_arrow_down_sharp,
                                ),
                                iconSize: 14,
                                iconEnabledColor: Color(0xFF2D2D2D),
                                iconDisabledColor: Colors.grey,
                              ),
                              dropdownStyleData: DropdownStyleData(
                                maxHeight: 200,
                                width: 350,
                                decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(14),
                                  color: AppColors.text_field_color,
                                ),
                                scrollbarTheme: ScrollbarThemeData(
                                  radius: const Radius.circular(15),
                                  thickness:
                                  MaterialStateProperty.all<double>(6),
                                  thumbVisibility:
                                  MaterialStateProperty.all<bool>(true),
                                ),
                              ),
                              menuItemStyleData: const MenuItemStyleData(
                                height: 40,
                                padding:
                                EdgeInsets.only(left: 14, right: 14),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                    SizedBox(height: 5,),
                    Padding(
                      padding: const EdgeInsets.only(bottom: 5.0),
                      child: Text(
                        "FSR Number",
                        style: TextStyle(
                          fontFamily: "JakartaMedium",
                          color: AppColors.semi_black,
                        ),
                      ),
                    ),
                    Container(
                      height: 50,

                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                        color: AppColors.text_field_color,
                        borderRadius:
                        BorderRadius.circular(14),
                      ),
                      child: Padding(
                        padding:
                        const EdgeInsets.fromLTRB(
                            10.0, 0.0, 10, 0),
                        child: TextFormField(
                          controller:
                          fsrNumberController,
                          keyboardType:
                          TextInputType.text,
                          decoration: InputDecoration(

                            hintText:
                            "Enter FSR Number",
                            hintStyle: TextStyle(
                                fontWeight: FontWeight.w400,
                                color: Color(0xFFB4BEC0),
                                fontSize: 14
                            ),

                            enabledBorder:
                            InputBorder.none,
                            focusedBorder:
                            InputBorder.none,
                          ),

                        ),
                      ),
                    ),
                    SizedBox(height: 5,),
                    Row(
                      children: [
                        Expanded(
                          child: Padding(
                            padding: const EdgeInsets.symmetric(
                                horizontal: 5.0,vertical: 3),
                            child: Text(
                              "Select Time",
                              style: TextStyle(
                                fontFamily: "JakartaMedium",
                                color: AppColors.semi_black,
                              ),
                            ),
                          ),
                        ),
                        Expanded(
                          child: Padding(
                            padding: const EdgeInsets.symmetric(
                                horizontal: 20.0),
                            child: Text(
                              "Running Hours",
                              style: TextStyle(
                                fontWeight: FontWeight.w400,
                                color: AppColors.semi_black,
                              ),
                            ),
                          ),
                        ),

                      ],
                    ),

                    Row(
                      children: [
                        Expanded(
                          child: Container(
                              decoration: BoxDecoration(
                                color: AppColors.text_field_color,
                                borderRadius:
                                BorderRadius.circular(14),
                              ),
                              child: InkWell(
                                onTap: () {
                                  provider.selectTime(context);
                                },
                                child: Container(
                                  alignment: Alignment.center,
                                  height: 45,
                                  margin:
                                  EdgeInsets.only(
                                      left: 15.0, right: 15.0),
                                  decoration: BoxDecoration(
                                    borderRadius: BorderRadius.circular(
                                        15.0),
                                  ),
                                  child: Row(children: [

                                    Text(
                                      provider.selectedTime.hour
                                          .toString() +
                                          ":" +
                                          provider.selectedTime.minute
                                              .toString() ??
                                          "Enter in Time",
                                      textAlign: TextAlign.center,
                                      style: TextStyle(
                                          fontWeight: FontWeight.w400),
                                    ),
                                  ]),
                                ),
                              )),
                        ),
                        SizedBox(width: 10,),
                        Expanded(
                          child: Container(
                            height: 50,

                            alignment: Alignment.center,
                            decoration: BoxDecoration(
                              color: AppColors.text_field_color,
                              borderRadius:
                              BorderRadius.circular(14),
                            ),
                            child: Padding(
                              padding:
                              const EdgeInsets.fromLTRB(
                                  10.0, 0.0, 10, 0),
                              child: TextFormField(
                                controller:
                                runningHoursController,
                                keyboardType:
                                TextInputType.text,
                                decoration: InputDecoration(

                                  hintText:
                                  "Enter FSR Number",
                                  hintStyle: TextStyle(
                                      fontWeight: FontWeight.w400,
                                      color: Color(0xFFB4BEC0),
                                      fontSize: 14
                                  ),
                                  enabledBorder:
                                  InputBorder.none,
                                  focusedBorder:
                                  InputBorder.none,
                                ),

                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                    InkResponse(
                      onTap: () {
                        _showAttachmentSheet(context);
                      },
                      child: Container(
                        margin: EdgeInsets.symmetric(vertical: 10),
                        height: 45,
                        width: MediaQuery
                            .of(context)
                            .size
                            .width,
                        decoration: BoxDecoration(
                            color: Color(0xFFE6F6FF),
                            borderRadius: BorderRadius.circular(12),
                            border: Border.all(
                                color: AppColors.app_blue, width: 0.5)
                        ),
                        child: Center(
                          child: Text("Scan Document", style: TextStyle(
                              color: AppColors.app_blue
                          ),),
                        ),
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.only(bottom: 5.0),
                      child: Text(
                        "Feedback",
                        style: TextStyle(
                          fontFamily: "JakartaMedium",
                          color: AppColors.semi_black,
                        ),
                      ),
                    ),
                    Container(
                      height: 150,

                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                        color: AppColors.text_field_color,
                        borderRadius:
                        BorderRadius.circular(14),
                      ),
                      child: Padding(
                        padding:
                        const EdgeInsets.fromLTRB(
                            10.0, 0.0, 10, 0),
                        child: TextFormField(
                          controller:
                          feedbackController,
                          maxLines: 60,
                          keyboardType:
                          TextInputType.text,
                          decoration: InputDecoration(

                            hintText:
                            "Enter Feedback",
                            hintStyle: TextStyle(
                                fontWeight: FontWeight.w400,
                                color: Color(0xFFB4BEC0),
                                fontSize: 14
                            ),
                            enabledBorder:
                            InputBorder.none,
                            focusedBorder:
                            InputBorder.none,
                          ),

                        ),
                      ),
                    ),


                  ],
                ),
              ),
            ),
            floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
            floatingActionButton: InkWell(
              onTap: () {
                provider.UpdateComplaintAPIFunction(
                    context,
                    widget.ComplaintID,
                    feedbackController.text,
                    fsrNumberController.text,
                    runningHoursController.text,
                    provider.statusID);
              },
              child: Container(
                alignment: Alignment.center,
                height: 45,
                margin: EdgeInsets.only(
                  left: 5.0,
                  right: 5.0,
                  top: 5.0,
                  bottom: 5.0,
                ),
                decoration: BoxDecoration(
                  color: AppColors.app_blue, //1487C9
                  borderRadius: BorderRadius.circular(15.0),
                ),
                child: Center(
                  child: Text(
                    "Submit",
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      fontSize: 15,
                        fontFamily: "JakartaMedium",
                        color: Colors.white),
                  ),
                ),
              ),
            ),
          ),
        );
      },);


  }
  Future<void> _showAttachmentSheet(BuildContext context) {
    return showModalBottomSheet(
      useSafeArea: true,
      isDismissible: true,
      isScrollControlled: true,
      showDragHandle: true,
      backgroundColor: Colors.white,
      enableDrag: true,
      context: context,
      builder: (context) {
        return StatefulBuilder(
          builder: (context, setState) {
            return SafeArea(
              child: Consumer<Pendingcomplaintsprovider>(
                builder: (context, provider, child) {
                  return Padding(

                    padding: EdgeInsets.only(
                      bottom:
                      MediaQuery
                          .of(
                        context,
                      )
                          .viewInsets
                          .bottom, // This handles keyboard
                    ),
                    child: Container(
                      margin: EdgeInsets.only(
                        bottom: 15,
                        left: 15,
                        right: 15,
                        top: 10,
                      ),

                      child: SingleChildScrollView(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Align(
                              alignment: Alignment.center,
                              child: Text(
                                "Select Source",
                                style: TextStyle(
                                  color: AppColors.app_blue,
                                  fontSize: 16
                                ),
                              ),
                            ),
                            SizedBox(height: 15,),
                            InkWell(
                              onTap: () {
                                Navigator.of(context).pop(false);
                                provider.imgFromGallery();
                              },
                              child: Container(
                                height: 35,
                                child: Text("Select photo from gallery"),
                              ),
                            ),
                            SizedBox(height: 10),
                            InkWell(
                              onTap: () {
                                Navigator.of(context).pop(false);
                                provider.imgFromCamera(
                                );
                              },
                              child: Container(
                                height: 35,
                                child: Text("Capture photo from camera"),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  );
                },
              ),
            );
          },
        );
      },
    );
  }

}
