import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:watermanagement/addtankers.dart';
import 'package:watermanagement/models/pending_suppliers_model.dart';

import 'package:watermanagement/settings.dart';

import 'models/tankersview_model.dart';

class PendingCustomers extends StatefulWidget {



  @override
  State<PendingCustomers> createState() => _PendingCustomersState();
}

class _PendingCustomersState extends State<PendingCustomers> {

  TextEditingController tankerNameController = TextEditingController();
  TextEditingController tankerPhoneController = TextEditingController();
  TextEditingController tankerAlterPhoneController = TextEditingController();

  bool isPendingDataLoading=false;
  List<PendingSuppliersModel> pendingSuppliersList = [];
  bool isSereverIssuePending = false;
  bool isLoading=false;








  Future<void> getPendingSuppliersData() async {
    isPendingDataLoading = true;

    try {
      var response = await AppSettings.getPendingSuppliers();

      setState(() {
        pendingSuppliersList =
            ((jsonDecode(response)['data']) as List).map((dynamic model) {
              return PendingSuppliersModel.fromJson(model);
            }).toList();
        isPendingDataLoading = false;
      });


    } catch (e) {
      setState(() {
        isPendingDataLoading = false;
        isSereverIssuePending = true;
      });
    }
  }

  @override
  void initState() {
    isLoading=true;
    getPendingSuppliersData();
    super.initState();
  }



  Widget renderzUi(){
    if(pendingSuppliersList.length!=0){

      return  Column(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            Expanded(child:ListView.builder(
                padding:  EdgeInsets.all(0),
                itemCount: pendingSuppliersList.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 * .50,
                              child: Row(
                                children: [
                                  Column(
                                    mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: [
                                      Text('username :',
                                          style: labelTextStyle()),

                                      SizedBox(height: 10,),
                                      Text('contactNumber:',
                                          style: labelTextStyle()),
                                      SizedBox(height: 10,),
                                      Text('address:',
                                          style: labelTextStyle()),
                                    ],
                                  ),
                                  SizedBox(width: 10,),
                                  Expanded(child:Column(
                                    mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: [
                                      Text(
                                          pendingSuppliersList[index]
                                              .customer_name
                                              .toUpperCase(),
                                          style: valuesTextStyle()),
                                      SizedBox(height: 10,),
                                      Text(
                                          pendingSuppliersList[index]
                                              .customer_phone_number
                                              .toUpperCase(),
                                          style: valuesTextStyle()),
                                      SizedBox(height: 10,),
                                      Text(
                                          pendingSuppliersList[index]
                                              .customer_address
                                              .toUpperCase(),
                                          style: valuesTextStyle())
                                    ],
                                  ),)
                                ],
                              )
                          ),


                         TextButton(
                                child:  Text(
                                  'Accept',
                                  style: TextStyle(fontSize: 15,
                                    color:primaryColor/*FilteredList[index].text_color*/ ),
                                ),
                                onPressed: () async{
                                  var payload = new Map<String, dynamic>();
                                  payload["supplierId"] =AppSettings.supplierId;
                                  payload["customerId"] = pendingSuppliersList[index].customer_id;
                                  bool requestStatus = await AppSettings.acceptRequest(payload);

                                  if(requestStatus){
                                    AppSettings.longSuccessToast("Request Accepted Successfully");
                                   // await getConnectedSuppliersData();
                                    await getPendingSuppliersData();

                                  }
                                  else{
                                  }

                                },
                              ),

                          TextButton(
                            child:  Text(
                              'Reject',
                              style: TextStyle(fontSize: 15,
                                  color:primaryColor/*FilteredList[index].text_color*/ ),
                            ),
                            onPressed: () async{
                                var payload = new Map<String, dynamic>();
                                payload["supplierId"] =AppSettings.supplierId;
                                payload["customerId"] = pendingSuppliersList[index].customer_id;

                                  bool requestStatus = await AppSettings.rejectRequest(payload);
                                  if(requestStatus){
                                    AppSettings.longSuccessToast("Request Sent Successfully");
                                    await getPendingSuppliersData();

                                  }
                                  else{

                                  }




                            },
                          ),
                        ],

                      ),
                    ),
                  );
                }) ),
          ]);
    }
    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: AppSettings.appBar('Pending Customers'),
          body: isPendingDataLoading?Center(
            child: CircularProgressIndicator(
              color: primaryColor,
              strokeWidth: 5.0,
            ),
          ):renderzUi(),
        ));
  }
}
