import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:geolocator/geolocator.dart';
import 'package:healthcare_pharmacy/getmedicines.dart';
import 'package:healthcare_pharmacy/maps/app_colors.dart';
import 'package:healthcare_pharmacy/models/biddingrequest_model.dart';
import 'package:healthcare_pharmacy/models/companyoffer_model.dart';
import 'package:healthcare_pharmacy/settings.dart';

import 'package:image_picker/image_picker.dart';
import 'package:photo_view/photo_view.dart';

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

  @override
  State<GetAllPharmacies> createState() => _GetAllPharmaciesState();
}

class _GetAllPharmaciesState extends State<GetAllPharmacies> {
  String Url = '';
  List<GetAllPharmacyModel> pharmacyList = [];
  List<GetAllPharmacyModel> prescriptionsListOriginal = [];
  bool isPrescriptionsDataLoading = false;
  bool isSereverIssue = false;
  bool isLoading=false;
  List<String> checked = [];


  Future<void> getAllPharmacies() async {
    isPrescriptionsDataLoading=true;
    try {
      var response = await AppSettings.getPharmacyData();
      print(response);
      setState(() {
        pharmacyList =
            ((jsonDecode(response)['data']) as List).map((dynamic model) {
              return GetAllPharmacyModel.fromJson(model);
            }).toList();
        isPrescriptionsDataLoading = false;
      });


    } catch (e) {
      setState(() {
        isLoading = false;
        isPrescriptionsDataLoading = false;
      });
    }
  }

  @override
  void initState() {
    getAllPharmacies();
    //getAllPharmaciesData(dropdownArea);
    super.initState();
  }

  void _onCheckboxChanged(String id, bool value) {
    setState(() {
      if (value) {
        checked.add(id);
      } else {
        checked.remove(id);
      }
    });
  }

  showPicDialog(var imageUrl){
    return showDialog(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return StatefulBuilder(
            builder: (BuildContext context, StateSetter setState) {
              return AlertDialog(
                title: const Text(''),
                content: SingleChildScrollView(
                  child: ListBody(
                    children: <Widget>[
                      Container(
                          width: MediaQuery.of(context).size.width * .10,
                          height: MediaQuery.of(context).size.height * .50,
                          child: PhotoView(
                            imageProvider: NetworkImage(imageUrl) as ImageProvider,
                            maxScale: PhotoViewComputedScale.contained * 4.0,
                            minScale: PhotoViewComputedScale.contained,
                            initialScale: PhotoViewComputedScale.contained,
                            basePosition: Alignment.center,

                          )
                      )
                    ],
                  ),
                ),
                actions: <Widget>[
                  TextButton(
                    child: Text('Close', style: textButtonStyle()),
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                  ),
                ],
              );
            });
      },
    );
  }


  Widget _allPharmacies() {
    if (pharmacyList.length != 0) {
      return Column(
        crossAxisAlignment: CrossAxisAlignment.end,
        children: [
          Expanded(
            child: ListView.builder(
              padding: EdgeInsets.all(0),
              itemCount: pharmacyList.length,
              itemBuilder: (BuildContext context, int index) {
                return Card(
                  child: Padding(
                    padding: EdgeInsets.all(8),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        GestureDetector(
                          child: Container(
                            width: MediaQuery.of(context).size.width * .18,
                            height: MediaQuery.of(context).size.height * .10,
                            decoration: BoxDecoration(
                              shape: BoxShape.circle,
                              image: DecorationImage(
                                image: (AppSettings.updatedImage != null)
                                    ? FileImage(AppSettings.updatedImage!)
                                as ImageProvider
                                    : AssetImage("images/mobilebg.png"), // picked file
                                fit: BoxFit.cover,
                              ),
                            ),
                          ),
                          onTap: () {
                            // showPicDialog(prescriptionsList[index].prescription_url);
                          },
                        ),
                        SizedBox(width: MediaQuery.of(context).size.width * .02,),
                        Container(
                          width: MediaQuery.of(context).size.width * .55,
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.start,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                pharmacyList[index].pharmacyname_offer
                                    .toString()
                                    .toUpperCase(),
                                style: valuesTextStyle(),
                              ),
                              Text(
                                pharmacyList[index].pharmacyId_offer
                                    .toString()
                                    .toUpperCase(),
                                style: valuesTextStyle(),
                              ),
                              Text(
                                pharmacyList[index].phone_offer
                                    .toString()
                                    .toUpperCase(),
                                style: valuesTextStyle(),
                              ),
                              Text(
                                pharmacyList[index].description_offer
                                    .toString()
                                    .toUpperCase(),
                                style: valuesTextStyle(),
                              ),
                            ],
                          ),
                        ),
                        Checkbox(
                          value: checked.contains(
                              pharmacyList[index].pharmacyId_offer.toString()),
                          onChanged: (value) {
                            _onCheckboxChanged(
                                pharmacyList[index].pharmacyId_offer.toString(),
                                value ?? false);
                          },
                        ),
                      ],
                    ),
                  ),
                );
              },
            ),
          ),

         /* Container(
            width: 400,
            height: 50,
            padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
            child:TextButton(
              onPressed: () {
                // Add your button click logic here
              },
              child: Text(
                'Total Price:$checked.toString()',
                style: TextStyle(
                  fontSize: 20,
                  color: AppColors.primaryColor, // Text color
                  decoration: TextDecoration.underline, // Underline the text
                  fontWeight: FontWeight.bold, // Bold text
                ),
              ),
            ),),*/
          SizedBox(
            height: 10,
          ),
          Container(
              width: 400,
              height: 50,
              padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
              child: ElevatedButton(
                style: ElevatedButton.styleFrom(
                  primary: primaryColor, // background
                  onPrimary: Colors.white, // foreground
                ),
                onPressed: ()  async{
                  ScaffoldMessenger.of(context).showSnackBar(
                    SnackBar(
                      content: Text(checked.toString()),
                    ),
                  );
                },
                child:  Text('Submit'),
              )),
        ],
      );
    } 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,),
            ],
          ),
        ),
      );
    }
  }



  /**/

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