import 'dart:convert';

import 'package:doctor/common/settings.dart';
import 'package:doctor/connected_patients/patient_records.dart';
import 'package:doctor/models/connected_patients_model.dart';
import 'package:flutter/material.dart';

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

  @override
  State<ConnectedPatients> createState() => _ConnectedPatientsState();
}

class _ConnectedPatientsState extends State<ConnectedPatients> {


  bool isConnectedPatientsDataLoading=false;
  bool isSereverIssue=false;
  List<GetConnectedPatientsModel> connectedPatientsList = [];

  Future<void> getAllConnectedPatients() async {
    isConnectedPatientsDataLoading=true;
    try {
      var response = await AppSettings.getAllConectedPatients();

      setState(() {
        connectedPatientsList = ((jsonDecode(response)) as List)
            .map((dynamic model) {
          return GetConnectedPatientsModel.fromJson(model);
        }).toList();
        connectedPatientsList=connectedPatientsList.reversed.toList();
        isConnectedPatientsDataLoading = false;
      });
    } catch (e) {
      setState(() {
        isConnectedPatientsDataLoading = false;
        isSereverIssue = true;
      });
    }
  }

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

  Widget _patients(){
    if(connectedPatientsList.length!=0){
      return ListView.builder(
          padding:  EdgeInsets.all(0),
          itemCount: connectedPatientsList.length,
          itemBuilder: (BuildContext context, int index) {
            return GestureDetector(
              onTap: (){
                Navigator.push(
                    context,
                    new MaterialPageRoute(
                        builder: (__) => new PatientRecords(patientDetails: connectedPatientsList[index],)));


              },
              child: Card(

                //color: prescriptionsList[index].cardColor,
                child: Padding(
                  padding:EdgeInsets.all(8) ,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [

                          Container(


                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.start,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Column(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text(
                                      'Patient Name',
                                      style: labelTextStyle(),
                                    ),
                                    SizedBox(height:MediaQuery.of(context).size.height * .01,),
                                    Text(
                                      'Phone Number',
                                      style: labelTextStyle(),
                                    ),
                                    SizedBox(height:MediaQuery.of(context).size.height * .01,),
                                    Text(
                                      'Age',
                                      style: labelTextStyle(),
                                    ),
                                    SizedBox(height:MediaQuery.of(context).size.height * .01,),
                                    Text(
                                      'Gender',
                                      style: labelTextStyle(),
                                    ),
                                    SizedBox(height:MediaQuery.of(context).size.height * .01,),

                                  ],
                                ),
                                SizedBox(width:MediaQuery.of(context).size.width * .01,),
                                Column(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text(
                                      ':',
                                      style: labelTextStyle(),
                                    ),
                                    SizedBox(height:MediaQuery.of(context).size.height * .01,),
                                    Text(
                                      ':',
                                      style: labelTextStyle(),
                                    ),
                                    SizedBox(height:MediaQuery.of(context).size.height * .01,),
                                    Text(
                                      ':',
                                      style: labelTextStyle(),
                                    ),
                                    SizedBox(height:MediaQuery.of(context).size.height * .01,),
                                    Text(
                                      ':',
                                      style: labelTextStyle(),
                                    ),

                                  ],
                                ),
                                SizedBox(width:MediaQuery.of(context).size.width * .01,),
                                Column(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text(connectedPatientsList[index].user_name.toString().toUpperCase(),style: valuesTextStyle()),
                                    SizedBox(height:MediaQuery.of(context).size.height * .01,),
                                    Text(
                                      connectedPatientsList[index].phone_number,
                                      style: valuesTextStyle(),
                                    ),
                                    SizedBox(height:MediaQuery.of(context).size.height * .01,),
                                    Text(
                                      connectedPatientsList[index].age+' Yrs',
                                      style: valuesTextStyle(),
                                    ),
                                    SizedBox(height:MediaQuery.of(context).size.height * .01,),
                                    Text(
                                      connectedPatientsList[index].gender,
                                      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 Connected patients available'),
                SizedBox(
                  height: 20,
                ),
                CircleAvatar(
                  backgroundColor: primaryColor,
                  radius: 40,
                  child: IconButton(
                    iconSize: 40,
                    icon: const Icon(
                      Icons.info,
                      color: Colors.white,
                    ),
                    onPressed: () async {
                      /*Navigator.push(context, MaterialPageRoute(builder: (context) => AddReports())).then((value) {
                        getAllRecords();
                      });*/
                    },
                  ),
                )
              ],
            ),
          )
      );
    }

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppSettings.appBar('Connected Patients'),
      body: Container(
        child: isConnectedPatientsDataLoading?Center(
          child: CircularProgressIndicator(
            color: primaryColor,
            strokeWidth: 5.0,
          ),
        ): _patients(),
      ),
    );
  }
}
