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/createoffers.dart';
import 'package:healthcare_pharmacy/getmedicines.dart';
import 'package:healthcare_pharmacy/models/approvedoffer_model.dart';
import 'package:healthcare_pharmacy/models/biddingrequest_model.dart';
import 'package:healthcare_pharmacy/settings.dart';

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

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

  @override
  State<ApprovedOffersData> createState() => _ApprovedOffersDataState();
}

class _ApprovedOffersDataState extends State<ApprovedOffersData> {
  bool isOfferDataLoading=false;
  bool isSereverIssue = false;
  bool isSereverIssueConnected = false;
  bool isSereverIssuePending = false;
  List<GetApprovedOffersDetailsModel> approvedOffersList = [];
  final ImagePicker _picker = ImagePicker();
  bool isActiveDataLoading=false;



  Future<void> getApprovedOffersViewData() async {
    isActiveDataLoading = true;
    try {
      var data = await AppSettings.getApprovedOffers();
      setState(() {
        List<dynamic> responseData = jsonDecode(data)['data'];
        approvedOffersList  = responseData
            .map((jsonObject) => GetApprovedOffersDetailsModel.fromJson(jsonObject))
            .toList();
        isActiveDataLoading = false;
      });

    } catch (error) {
      setState(() {
        isActiveDataLoading = false;
        isSereverIssueConnected = true;
      });
    }
  }



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

  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 renderzActiveUi(){
    if(approvedOffersList.length!=0){

      return  Column(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            Expanded(child:ListView.builder(
                padding:  EdgeInsets.all(0),
                itemCount: approvedOffersList .length,
                itemBuilder: (context,index){
                  var data=approvedOffersList[index];
                  return GestureDetector(
                    onTap: () {

                    },
                    child: Card(
                      child: ListTile(
                        leading: GestureDetector(
                          onTap: () {
                            showPicDialog(approvedOffersList[index].picture[0].url);
                          },
                          child: Container(
                            width: 100,
                            height: 300,
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(10),
                              image: DecorationImage(
                                fit: BoxFit.cover,
                                alignment: Alignment.center,
                                image: data.picture.isEmpty
                                    ? AssetImage('images/logo.png') as ImageProvider
                                    : NetworkImage(approvedOffersList[index].picture[0].url),
                              ),
                            ),
                          ),
                        ),


                        title: RichText(
                          text: TextSpan(
                            style: DefaultTextStyle.of(context).style,
                            children: <TextSpan>[
                              TextSpan(
                                text: 'Name: ',
                                style: TextStyle(
                                  color: Colors.black,
                                  fontWeight: FontWeight.bold,
                                ),
                              ),
                              TextSpan(
                                text: approvedOffersList[index].offer_name,
                                style: TextStyle(
                                  color: primaryColor,
                                ),
                              ),
                            ],
                          ),
                        ),
                        subtitle: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            // ... (other RichText widgets for Code, Description, Start Date, End Date)
                            RichText(
                              text: TextSpan(
                                style: DefaultTextStyle.of(context).style,
                                children: <TextSpan>[
                                  TextSpan(
                                    text: 'Code: ',
                                    style: TextStyle(
                                      color: Colors.black, // Change the color for "Description" text
                                      fontWeight: FontWeight.bold, // You can apply other styles too
                                    ),
                                  ),
                                  TextSpan(
                                    text: approvedOffersList[index].offer_code,
                                    style: TextStyle(
                                      color: primaryColor, // Change the color for description content
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            RichText(
                              text: TextSpan(
                                style: DefaultTextStyle.of(context).style,
                                children: <TextSpan>[
                                  TextSpan(
                                    text: 'Description: ',
                                    style: TextStyle(
                                      color: Colors.black, // Change the color for "Description" text
                                      fontWeight: FontWeight.bold, // You can apply other styles too
                                    ),
                                  ),
                                  TextSpan(
                                    text: approvedOffersList[index].description,
                                    style: TextStyle(
                                      color:primaryColor, // Change the color for description content
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            RichText(
                              text: TextSpan(
                                style: DefaultTextStyle.of(context).style,
                                children: <TextSpan>[
                                  TextSpan(
                                    text: 'Category: ',
                                    style: TextStyle(
                                      color: Colors.black, // Change the color for "Description" text
                                      fontWeight: FontWeight.bold, // You can apply other styles too
                                    ),
                                  ),
                                  TextSpan(
                                    text: approvedOffersList[index].category,
                                    style: TextStyle(
                                      color:primaryColor, // Change the color for description content
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            RichText(
                              text: TextSpan(
                                style: DefaultTextStyle.of(context).style,
                                children: <TextSpan>[
                                  TextSpan(
                                    text: 'Start Date: ',
                                    style: TextStyle(
                                      color: Colors.black, // Change the color for "Description" text
                                      fontWeight: FontWeight.bold, // You can apply other styles too
                                    ),
                                  ),
                                  TextSpan(
                                    text: approvedOffersList[index].starting_date,

                                    style: TextStyle(
                                      color:primaryColor, // Change the color for description content
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            RichText(
                              text: TextSpan(
                                style: DefaultTextStyle.of(context).style,
                                children: <TextSpan>[
                                  TextSpan(
                                    text: 'End Date  :',
                                    style: TextStyle(
                                      color: Colors.black, // Change the color for "Description" text
                                      fontWeight: FontWeight.bold, // You can apply other styles too
                                    ),
                                  ),
                                  TextSpan(
                                    text: approvedOffersList[index].ending_date,
                                    style: TextStyle(
                                      color:primaryColor, // Change the color for description content
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  );






                }) ),
            Padding(
              padding: EdgeInsets.fromLTRB(8, 8, 8, 8),
              child: CircleAvatar(
                backgroundColor: primaryColor,
                radius: 40,
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    IconButton(
                      iconSize: 40,
                      icon: const Icon(
                        Icons.add,
                        color: Colors.white,
                      ),
                      onPressed: () async{
                        Navigator.pop(context);
                        await Navigator.push(
                          context,
                          MaterialPageRoute(
                              builder: (context) => offers()),
                        );
                        //showBoreAddingDialog();
                      },
                    ),
                    /* Padding(
                      padding: EdgeInsets.fromLTRB(5, 0, 5, 5),
                      child: Text(
                        'Add Tanks ',
                        style: TextStyle(color: Colors.white),
                      ),
                    )*/
                  ],
                ),
              ),
            ),
          ]);
    }
    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('Click below icon to add new Offer'),
                SizedBox(
                  height: 20,
                ),
                CircleAvatar(
                  backgroundColor: primaryColor,
                  radius: 40,
                  child: IconButton(
                    iconSize: 40,
                    icon: const Icon(
                      Icons.add,
                      color: Colors.white,
                    ),
                    onPressed: () async {
                      Navigator.pop(context);
                      await Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) => offers()),
                      );
                    },
                  ),
                )
              ],
            ),
          )
      );
    }
  }


  /**/

  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Scaffold(
          appBar: AppSettings.appBar('Approved Ofers'),
          body: isActiveDataLoading?Center(
            child: CircularProgressIndicator(
              color: primaryColor,
              strokeWidth: 5.0,
            ),
          ):renderzActiveUi(),
        ));
  }
}