import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:healthcare_delivery/common/settings.dart';
import 'package:healthcare_delivery/model/biddingrequest_model.dart';
import 'package:intl/intl.dart';
import 'package:url_launcher/url_launcher.dart';


class DelivryBookingData extends StatefulWidget {
  const DelivryBookingData({Key? key}) : super(key: key);

  @override
  State<DelivryBookingData> createState() => _DelivryBookingDataState();
}

class _DelivryBookingDataState extends State<DelivryBookingData> {
  bool isSupplierDataLoading=false;
  bool isSereverIssue = false;
  bool isSereverIssueConnected = false;
  bool isSereverIssuePending = false;
  String dropdownTypeOfPayment = 'Cash';
  String dueAmount = '';
  bool isDataLoading = false;

  TextEditingController amountPaidController = TextEditingController();

  List<demo> bookingDataList = [];
  List<demo> completedOrdersList = [];
  List<demo> activeOrdersList = [];
  bool isLoading=false;



  Future<void> getBokkingsData() async {
    isLoading = true;
    try {
      var response = await AppSettings.getdeliveryboybookings();
      setState(() {
        activeOrdersList =
            ((jsonDecode(response)['data']) as List).map((dynamic model) {
              return demo.fromJson(model);
            }).toList();
        isLoading = false;
      });


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


  @override
  void initState() {
    getBokkingsData();
    super.initState();
  }



  Widget activeOrders(){
    if (activeOrdersList.length != 0) {
      return ListView.builder(
          padding: EdgeInsets.all(8),
          itemCount: activeOrdersList.length,
          itemBuilder: (BuildContext context, int index) {
            return Card(
              child: Padding(
                padding: EdgeInsets.all(8),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Container(
                      // width: MediaQuery.of(context).size.width * .75,
                      child: Column(
                        mainAxisAlignment:
                        MainAxisAlignment.spaceBetween,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Row(
                            children: [
                              Text('PharmacyName :',
                                  style: labelTextStyle()),
                              SizedBox(
                                width: 5.0,
                              ),
                              Text(
                                  activeOrdersList[index]
                                      .pharmacyName
                                      .toUpperCase(),
                                  style: valuesTextStyle()),
                            ],
                          ),
                          Row(
                            children: [
                              Text('Pharmacy Phone :',
                                  style: labelTextStyle()),
                              SizedBox(
                                width: 5.0,
                              ),
                              Text(
                                  activeOrdersList [index]
                                      .pharmacyPhone
                                      .toString(),
                                  style: valuesTextStyle()),
                            ],
                          ),
                          Row(
                            children: [
                              Text('User Name :',
                                  style: labelTextStyle()),
                              SizedBox(
                                width: 5.0,
                              ),
                              Text(activeOrdersList[index].firstName,
                                  style: valuesTextStyle()),
                            ],
                          ),
                          Row(
                            children: [
                              Text('User Phone :',
                                  style: labelTextStyle()),
                              SizedBox(
                                width: 5.0,
                              ),
                              Text(activeOrdersList[index].contactNumber,
                                  style: valuesTextStyle()),
                            ],
                          ),
                          Row(
                            children: [
                              Text('Address :',
                                  style: labelTextStyle()),
                              SizedBox(
                                width: 5.0,
                              ),
                              Text(activeOrdersList[index].address,
                                  style: valuesTextStyle()),
                            ],
                          ),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.start,
                            crossAxisAlignment:
                            CrossAxisAlignment.start,
                            children: [
                              Column(
                                children: [
                                  IconButton(
                                    iconSize: 30,
                                    icon: const Icon(
                                      Icons.phone,
                                      color: primaryColor,
                                    ),
                                    onPressed: () async {
                                      final Uri _phoneUri = Uri(
                                          scheme: "tel",
                                          path: activeOrdersList[index]
                                              .contactNumber);
                                      try {
                                        await launch(
                                            _phoneUri.toString());
                                      } catch (error) {
                                        throw ("Cannot dial");
                                      }
                                    },
                                  ),
                                  Text(
                                    'Call now',
                                    style: iconBelowTextStyle(),
                                  ),
                                ],
                              ),
                              SizedBox(
                                width: 30,
                              ),
                              Column(
                                children: [
                                  IconButton(
                                    iconSize: 30,
                                    icon: const Icon(
                                      Icons.start,
                                      color: primaryColor,
                                    ),
                                    onPressed: () {

                                      /*  Navigator.push(
                                          context,
                                          new MaterialPageRoute(
                                              builder: (__) => new OrderTrackingPage(lat:activeOrdersList[index].lat,lng:activeOrdersList[index].lng)));*/


                                    },
                                  ),
                                  Text(
                                    'Start Trip',
                                    style: iconBelowTextStyle(),
                                  ),
                                ],
                              ),
                              SizedBox(
                                width: 30,
                              ),

                            ],
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            );
          });
    }
    else {
      return Center(
          child: Padding(
            padding: EdgeInsets.fromLTRB(0, 40, 0, 0),
            child: isSereverIssueConnected
                ? Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Image(
                  image: AssetImage('images/logo.png'),
                  //  height: MediaQuery.of(context).size.height * .10,
                ),
                SizedBox(
                  height: 20,
                ),
                Text(
                  'No Data',style:serverIssueTextStyle() ,),
              ],
            )
                :  Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Image(
                  image: AssetImage('images/no_data.png'),
                  //  height: MediaQuery.of(context).size.height * .10,
                ),
                SizedBox(
                  height: 20,
                ),
                Text(
                  'No Connected Tankers',style:serverIssueTextStyle() ,),
              ],
            ),
          ));
    }
  }

  /**/

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppSettings.appBar('BiddingRequests'),
      body: isDataLoading?Center(
        child: CircularProgressIndicator(
          color: primaryColor,
          strokeWidth: 5.0,
        ),
      ): activeOrders(),
    );
  }
}
