import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:healthcare_pharmacy/models/offersview_model.dart';
import 'package:healthcare_pharmacy/createoffers.dart';
import 'package:healthcare_pharmacy/settings.dart';
import 'package:intl/intl.dart';
import 'package:flutter_cupertino_datetime_picker/flutter_cupertino_datetime_picker.dart';


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

  @override
  State<InActiveOffersView> createState() => _InActiveOffersViewState();
}

class _InActiveOffersViewState extends State<InActiveOffersView> with TickerProviderStateMixin {
  bool isSupplierDataLoading=false;
  bool isSereverIssue = false;
  bool isSereverIssueConnected = false;
  bool isSereverIssuePending = false;
  var startdate;
  var enddate;


  List<GetOffersDetailsModel> offersviewList = [];
  TextEditingController offerNameController = TextEditingController();
  TextEditingController offerCodeController = TextEditingController();
  TextEditingController offerDescriptionController = TextEditingController();
  TextEditingController offerDiscountController = TextEditingController();
  TextEditingController offerStartDateController = TextEditingController();
  TextEditingController offerEndDateController = TextEditingController();


  bool isLoading=false;

  Future<void> getOffersViewData() async {
    isLoading = true;
    try {
      var response = await AppSettings.getinactiveOffers();
      setState(() {
        offersviewList =
            ((jsonDecode(response)['data']) as List).map((dynamic model) {
              return GetOffersDetailsModel.fromJson(model);
            }).toList();
        isLoading = false;
      });


    } catch (e) {
      setState(() {
        isLoading = false;
        isSereverIssueConnected = true;
      });
    }
  }



  @override
  void initState() {
    // TODO: implement initState
    isLoading=true;
    getOffersViewData();
    super.initState();
  }

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

                          Container(
                            //width: MediaQuery.of(context).size.width * .55,
                            child: Column(
                              children: [
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Column(
                                      mainAxisAlignment: MainAxisAlignment.start,
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      children: [
                                        Text('OfferName :',style: labelTextStyle()),
                                        Text('OfferCode :',style: labelTextStyle()),
                                        Text('OfferDes :' ,style: labelTextStyle()),
                                        Text('OfferDis(%) :' ,style: labelTextStyle()),
                                        Text('StartDate :',style: labelTextStyle()),
                                        Text('EndDate   :' ,style: labelTextStyle()),

                                      ],
                                    ),
                                    SizedBox(width: 5,),
                                    Column(
                                      mainAxisAlignment: MainAxisAlignment.start,
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      children: [
                                        Text(offersviewList[index].offer_name??"",style: valuesTextStyle()),
                                        Text(offersviewList[index].offer_code??"",style: valuesTextStyle()),
                                        Text(offersviewList[index].description??"",style: valuesTextStyle()),
                                        Text(offersviewList[index].starting_date??"",style: valuesTextStyle()),
                                        Text(offersviewList[index].ending_date??"",style: valuesTextStyle())

                                      ],
                                    ),
                                  ],
                                ),
                              ],
                            ),
                          ),

                          Expanded(child:TextButton(
                             onPressed: () {
                               showDialog(
                                 //if set to true allow to close popup by tapping out of the popup
                                 //barrierDismissible: false,
                                 context: context,
                                 builder: (BuildContext context) => AlertDialog(
                                   title: const Text('Do you want to Re_Active offer ??',
                                       style: TextStyle(
                                         color: primaryColor,
                                         fontSize: 16,
                                       )),
                                   actionsAlignment: MainAxisAlignment.spaceBetween,
                                   actions: [
                                     TextButton(
                                       onPressed: ()async {
                                         bool deleteTankStatus = await AppSettings.reactiveOffers(offersviewList[index].offer_code);

                                         if(deleteTankStatus){
                                           getOffersViewData();
                                           AppSettings.longSuccessToast('offer Re_Active successfully');
                                           Navigator.of(context).pop(true);

                                         }
                                         else{
                                           AppSettings.longFailedToast('offer Re_Active failed');
                                         }
                                       },
                                       child: const Text('Yes',
                                           style: TextStyle(
                                             color: primaryColor,
                                             fontSize: 18,
                                           )),
                                     ),
                                     TextButton(
                                       onPressed: () {
                                         Navigator.of(context).pop(true);
                                       },
                                       child: const Text('No',
                                           style: TextStyle(
                                             color: primaryColor,
                                             fontSize: 18,
                                           )),
                                     ),
                                   ],
                                 ),
                               );
                           },
                           child: const Text(
                           'RE_ACTIVE',
                            style: TextStyle(
                           color: primaryColor,
                            fontSize: 15,
                          fontWeight: FontWeight.bold
                          /* decoration: TextDecoration.underline,*/

                               ),
                           ),
                           ),

                          ),

                        ],

                      ),
                    ),
                  );
                }) ),
            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('InActive OfersView'),
          body: isLoading?Center(
            child: CircularProgressIndicator(
              color: primaryColor,
              strokeWidth: 5.0,
            ),
          ):renderzUi(),
        ));
  }
}