import 'dart:convert';

import 'package:doctor/common/settings.dart';
import 'package:doctor/patient_dashboard/BMI/bmi_history.dart';
import 'package:doctor/patient_dashboard/BP/bp_history.dart';
import 'package:doctor/patient_dashboard/Reports/allreports.dart';
import 'package:doctor/patient_dashboard/Sugar/sugar_history.dart';
import 'package:doctor/patient_dashboard/prescriptions/prescriptions.dart';
import 'package:doctor/patient_dashboard/problems/all_problems.dart';
import 'package:flutter/material.dart';

class PatientDashboardDetails extends StatefulWidget {
  String? mobileNumber;
  PatientDashboardDetails({this.mobileNumber});

  @override
  State<PatientDashboardDetails> createState() => _PatientDashboardDetailsState();
}

class _PatientDashboardDetailsState extends State<PatientDashboardDetails> {

  bool isDataLoading=false;
  bool isSereverIssue=false;
  String? customerId;
  String? patientName;
  String? patientAge;
  String? patientGender;
  String? patientContactNumber;
  final List<String> images = [
    'images/bp.png',
    'images/sugar.png',
    'images/bmi.png',
    'images/health_records.png',
    'images/prescriptions.png',
    'images/medicines.png',
    'images/new_problem.png',
    'images/get_checkup.png',
    'images/resources.png',
  ];
  final List<String> labels = [
    'Bp',
    'Sugar',
    'BMI',
    'Health Records',
    'Prescriptions',
    'Medicines',
    'New Problem',
    'Get Checkup',
    'Resources',
  ];
  Map<String,dynamic> patDetails={};

  Future<void> getPatientInformation() async {
    isDataLoading=true;
    try {
      var response = await AppSettings.getPatientInformationBasedOnMobileNumber(widget.mobileNumber);
      print(jsonDecode(response));
      setState(() {
        customerId=jsonDecode(response)[0]['customerId'];
        patientName=jsonDecode(response)[0]['profile']['firstName'];
        patientAge=jsonDecode(response)[0]['age'].toString();
        patientGender=jsonDecode(response)[0]['gender'];
        patientContactNumber=jsonDecode(response)[0]['profile']['contactNumber'];
        isDataLoading = false;
      });
    } catch (e) {
      setState(() {
        isDataLoading = false;
        isSereverIssue = true;
      });
    }
  }

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


  _onTap(int index){
    if(index==0){
      Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) =>  BPHistory(customerId:customerId,)),
      );
    }
    else if(index==1){
      Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) =>  SugarHistory(customerId:customerId,)),
      );
    }
    else if(index==2){
      Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) =>  BMIHistory(customerId:customerId,)),
      );
    }
    else if(index==3){
      Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) =>  AllReports(customerId:customerId,patName: patientName,patAge: patientAge,patGender: patientGender,)),
      );
    }
    else if(index==4){
      Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) =>  Prescriptions(customerId:customerId,patName: patientName,patAge: patientAge,patGender: patientGender,)),
      );
    }
    else if(index==6){
      Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) =>  AllProblemsReportMyself(customerId:customerId,patName: patientName,patAge: patientAge,patGender: patientGender,)),
      );
    }
    else if(index==7){
      Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) =>  AllProblemsReportMyself(customerId:customerId,patName: patientName,patAge: patientAge,patGender: patientGender,)),
      );
    }
  }


Widget _dashboard(){
    return Column(
      children: [
        Padding(padding: EdgeInsets.all(10),
        child: Align(child: Text('Patient Information',style: TextStyle(color: primaryColor,fontSize: 16),),
            alignment: Alignment.topLeft),),
       Padding(padding: EdgeInsets.all(10),
       child:  Card(child: Row(
         mainAxisAlignment: MainAxisAlignment.start,
         crossAxisAlignment: CrossAxisAlignment.start,
         children: [
           Column(
             mainAxisAlignment: MainAxisAlignment.start,
             crossAxisAlignment: CrossAxisAlignment.start,
             children: [
               Text('Name'),
               Text('Age'),
               Text('Gender'),
               Text('Contact Number')
             ],
           ),
           SizedBox(
             width:MediaQuery.of(context).size.width * .02,
           ),
           Column(
             mainAxisAlignment: MainAxisAlignment.start,
             crossAxisAlignment: CrossAxisAlignment.start,
             children: [
               Text(':'),
               Text(':'),
               Text(':'),
               Text(':')
             ],
           ),
           SizedBox(
             width:MediaQuery.of(context).size.width * .02,
           ),
           Column(
             mainAxisAlignment: MainAxisAlignment.start,
             crossAxisAlignment: CrossAxisAlignment.start,
             children: [
               Text(patientName!),
               Text(patientAge!),
               Text(patientGender!),
               Text(patientContactNumber!)
             ],
           ),
         ],
       ),),),
        Expanded(child: Container(

            child: Padding(
              padding: EdgeInsets.all(10),
              child: GridView.builder(
                itemCount: images.length,
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 3,
                  crossAxisSpacing: 4.0,
                  mainAxisSpacing: 4.0,
                ),
                itemBuilder: (BuildContext context, int index) {
                  return GestureDetector(
                      onTap: () {
                        _onTap(index);
                      },
                      child: Column(
                        children: [
                          Expanded(child: Container(
                            decoration: BoxDecoration(
                                color: secondaryColor,
                                shape: BoxShape.rectangle,
                                borderRadius: BorderRadius.circular(30),
                                image: DecorationImage(
                                    image: AssetImage(images[index],) , // picked file
                                    fit: BoxFit.fitWidth)),
                            //color: secondaryColor,

                          )),
                          Padding(padding: EdgeInsets.all(10),
                            child: Text(labels[index]),),
                          /*Divider(
                  color: Colors.grey,
                  height: 1.0,
                ),*/
                        ],
                      )
                  );
                },
              ),
            )
        ))
      ],
    );
}


  @override
  Widget build(BuildContext context) {
    return Scaffold(

      appBar: AppSettings.appBar('Patient Dashboard Details'),
      body: Container(
        child: isDataLoading?Center(
          child: CircularProgressIndicator(
            color: primaryColor,
            strokeWidth: 5.0,
          ),
        ): _dashboard(),
      ),
    );
  }
}
