import 'dart:convert';

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

import 'package:watermanagement/settings.dart';

import 'models/tankersview_model.dart';

class ConectedCustomers extends StatefulWidget {



  @override
  State<ConectedCustomers> createState() => _ConectedCustomersState();
}

class _ConectedCustomersState extends State<ConectedCustomers> {

  TextEditingController conectedCustomerNameController = TextEditingController();
  TextEditingController conectedCustomerPhoneController = TextEditingController();
  TextEditingController conectedCustomerBuildingName = TextEditingController();
  TextEditingController conectedCustomerAddress = TextEditingController();


  bool isPendingDataLoading=false;
  List<ConnectedCustomersModel> connectedCustomersList = [];
  bool isSereverIssuePending = false;
  bool isLoading=false;








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

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

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


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

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



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

      return  Column(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            Expanded(child:ListView.builder(
                padding:  EdgeInsets.all(0),
                itemCount: connectedCustomersList.length,
                itemBuilder: (BuildContext context, int index) {
                  return Card(
                    child: Padding(
                      padding:EdgeInsets.all(8) ,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Container(

                              child: Row(
                                children: [
                                  Column(
                                    mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: [
                                      Text('username :',
                                          style: labelTextStyle()),

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

                        ],

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