import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:healthcare_pharmacy/biddingrequests.dart';
import 'package:healthcare_pharmacy/dashboard.dart';
import 'package:healthcare_pharmacy/getmedicines.dart';
import 'package:healthcare_pharmacy/maps/app_colors.dart';
import 'package:healthcare_pharmacy/medicinedetailspage.dart';
import 'package:healthcare_pharmacy/models/biddingrequest_model.dart';
import 'package:healthcare_pharmacy/models/cartview_model.dart';
import 'package:healthcare_pharmacy/models/offersview_model.dart';
import 'package:healthcare_pharmacy/settings.dart';
import 'package:intl/intl.dart';
import 'package:flutter_cupertino_datetime_picker/flutter_cupertino_datetime_picker.dart';


class MedicineCartList extends StatefulWidget {
  var bookidcart;

  MedicineCartList({
    this.bookidcart
  });

  @override
  State<MedicineCartList> createState() => _MedicineCartListState();
}

class _MedicineCartListState extends State<MedicineCartList> with TickerProviderStateMixin {
  bool isSupplierDataLoading=false;
  bool isSereverIssue = false;
  bool isSereverIssueConnected = false;
  bool isSereverIssuePending = false;
  var startdate;
  var enddate;
   String totalPrice='';
   String medicinetime='';

  String bookingidstring='';
  bool isLoading=false;




  List<BiddingCartviewModel> offersviewList = [];
  TextEditingController medicine_nameController = TextEditingController();
  TextEditingController medicine_quantityController = TextEditingController();
  TextEditingController medicine_priceController = TextEditingController();
  TextEditingController medicine_timingsController = TextEditingController();


  Future<void> getCartViewData() async {
    isLoading = true;

    try {
      var response = await AppSettings.getCartDetails(widget.bookidcart).then((value){
        setState(() {
          // offersviewList = BiddingCartviewModel.fromJson(response['items']) as List<BiddingCartviewModel>;
          offersviewList =
              ((jsonDecode(value)['items']) as List).map((dynamic model) {
                return BiddingCartviewModel.fromJson(model);
              }).toList();

          totalPrice=jsonDecode(value)['totalPrice'].toString();

          print(medicinetime);
          print(totalPrice);
          isLoading = false;
        });
      });
      print("offersviewListdata${jsonDecode(response)}");

    } catch (e) {
      setState(() {
        isLoading = false;
        isSereverIssueConnected = true;
      });
    }
  }



  @override
  void initState() {
    // TODO: implement initState
    getCartViewData();
    super.initState();
  }

  Widget renderzUi() {
    if (offersviewList.length != 0) {
      return Column(crossAxisAlignment: CrossAxisAlignment.end, children: [
        Expanded(
            child: ListView.builder(
                padding: EdgeInsets.all(0),
                itemCount: offersviewList.length,
                itemBuilder: (BuildContext context, int index) {
                  return Card(
                    child: Padding(
                      padding: EdgeInsets.all(8),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Expanded(
                            child: Container(
                                width: MediaQuery.of(context).size.width * .70,
                                child: Row(
                                  children: [
                                    Column(
                                      mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                      crossAxisAlignment:
                                      CrossAxisAlignment.start,
                                      children: [
                                        Text('MedicineName:',
                                            style: labelTextStyle()),
                                        SizedBox(
                                          height: 10,
                                        ),
                                        Text(
                                          'Medicine Timings:',
                                            style: labelTextStyle()),
                                        SizedBox(
                                          height: 10,
                                        ),
                                        Text('MedicineQuantity:',
                                            style: labelTextStyle()),
                                        SizedBox(
                                          height: 10,
                                        ),
                                        Text('MedicinePrice:',
                                            style: labelTextStyle()),
                                      ],
                                    ),
                                    SizedBox(
                                      width: 10,
                                    ),
                                    Column(
                                      mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                      crossAxisAlignment:
                                      CrossAxisAlignment.start,
                                      children: [
                                        SizedBox(
                                          height: 12,
                                        ),
                                        Text(
                                            offersviewList[index].medicinename_bidding!
                                                .toUpperCase(),
                                            style: valuesTextStyle()),
                                        SizedBox(
                                          height: 12,
                                        ),
                                        Text(
                                            offersviewList[index].medicine_timings![0].medicineTimings!.toUpperCase(),
                                            style: valuesTextStyle()),
                                        SizedBox(
                                          height: 10,
                                        ),
                                        Text(
                                            offersviewList[index].quantity_bidding.toString()
                                                .toUpperCase(),
                                            style: valuesTextStyle()),
                                        SizedBox(
                                          height: 10,
                                        ),
                                        Text(
                                            offersviewList[index].price_bidding.toString()
                                                .toUpperCase(),
                                            style: valuesTextStyle()),
                                        SizedBox(
                                          height: 10,
                                        ),
                                      ],
                                    ),
                                  ],
                                )),
                          ),

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

        Container(
          width: 400,
          height: 50,
          padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
          child:TextButton(
            onPressed: () {
              // Add your button click logic here
            },
            child: Text(
              'Total Price:$totalPrice',
              style: TextStyle(
                fontSize: 20,
                color: AppColors.primaryColor, // Text color
                decoration: TextDecoration.underline, // Underline the text
                fontWeight: FontWeight.bold, // Bold text
              ),
            ),
          ),),
        SizedBox(
          height: 10,
        ),
        Container(
            width: 400,
            height: 50,
            padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
            child: ElevatedButton(
              style: ElevatedButton.styleFrom(
                primary: primaryColor, // background
                onPrimary: Colors.white, // foreground
              ),
              onPressed: ()  async{

              },
              child:  Text('CheckOut'),
            )),



        /*Column(
         children: [
           Padding(padding: const EdgeInsets.fromLTRB(10, 10,10,10),
             child: Row(
               mainAxisAlignment: MainAxisAlignment.center,
               children: <Widget>[
                 const Text(
                   'Add More',
                   style: TextStyle(
                     color: Colors.white,
                   ),
                 ),
                 TextButton(
                   child: const Text(
                     'Sign Up',
                     style: TextStyle(fontSize: 15,
                         decoration: TextDecoration.underline,color: Colors.white),
                   ),
                   onPressed: () {
                    *//* Navigator.push(
                       context,
                       MaterialPageRoute(
                           builder: (context) => const SignUp()),
                     );*//*
                     //signup screen
                   },
                 )
               ],
             ),)
           Container(
             width: 400,
             height: 50,
             padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
             child:TextButton(
               onPressed: () {
                 // Add your button click logic here
               },
               child: Text(
                 'Total Price:$totalPrice',
                 style: TextStyle(
                   fontSize: 20,
                   color: AppColors.primaryColor, // Text color
                   decoration: TextDecoration.underline, // Underline the text
                   fontWeight: FontWeight.bold, // Bold text
                 ),
               ),
             ),),
           SizedBox(
             height: 10,
           ),
           Container(
               width: 400,
               height: 50,
               padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
               child: ElevatedButton(
                 style: ElevatedButton.styleFrom(
                   primary: primaryColor, // background
                   onPrimary: Colors.white, // foreground
                 ),
                 onPressed: ()  async{

                 },
                 child:  Text('CheckOut'),
               )),
         ],
        )*/



      ]);
    } else {
      return Center(
          child: Padding(
            padding: EdgeInsets.fromLTRB(0, 40, 0, 0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                SizedBox(
                  height: MediaQuery.of(context).size.height * .25,
                ),
                Text('No Data Found'),
                SizedBox(
                  height: 20,
                ),
              ],
            ),
          ));
    }
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Scaffold(
          appBar: AppBar(
            // Add a back button to the leading property
            backgroundColor: primaryColor,
            leading: IconButton(
              icon: Icon(Icons.arrow_back), // You can use a different back icon if needed
              onPressed: () {
                // Implement the navigation logic to go back
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => BiddingRequests()),
                ); // This will pop the current route and go back
              },
            ),
            title: Text('Cart Details'),
          ),
          body: isLoading?Center(
            child: CircularProgressIndicator(
              color: primaryColor,
              strokeWidth: 5.0,
            ),
          ):renderzUi(),
        ));
  }
}