import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:healthcare_pharmacy/dashboard.dart';
import 'package:healthcare_pharmacy/settings.dart';
import 'package:intl/intl.dart';
import 'package:flutter_cupertino_datetime_picker/flutter_cupertino_datetime_picker.dart';


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

  @override
  State<offers> createState() => _offersState();
}

class _offersState extends State<offers> {

  TextEditingController offer_nameController = TextEditingController();
  TextEditingController offer_codeController = TextEditingController();
  TextEditingController descriptionController = TextEditingController();
  TextEditingController offerController = TextEditingController();
  TextEditingController starting_dateController = TextEditingController();
  TextEditingController ending_dateController = TextEditingController();
  var selIOS;


  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
          appBar: AppSettings.appBar('Cteate Offers'),
          body: Stack(children: <Widget>[

            GestureDetector(
              onTap: () {
                FocusManager.instance.primaryFocus?.unfocus();
              },
              child: SafeArea(
                  child: SingleChildScrollView(
                    child: Column(
                      children: <Widget>[

                        SizedBox(
                          height: 20,
                        ),
                        Container(
                          //width: double.infinity,
                            child: Image(
                              image: const AssetImage('images/logo.png'),
                              height: MediaQuery.of(context).size.height * .18,
                            )),

                        SizedBox(
                          height: 05,
                        ),
                        Container(
                          padding: const EdgeInsets.all(10),
                          child: TextFormField(
                            cursorColor: greyColor,
                            controller: offer_nameController,
                            textCapitalization: TextCapitalization.characters,
                            decoration: const InputDecoration(
                              prefixIcon: Icon(
                                Icons.person,
                                color: primaryColor,
                              ),
                              border: OutlineInputBorder(
                                  borderSide: BorderSide(color: primaryColor)),
                              focusedBorder: OutlineInputBorder(
                                borderSide: BorderSide(color: primaryColor),
                              ),
                              enabledBorder: OutlineInputBorder(
                                borderSide: BorderSide(color: primaryColor),
                              ),
                              labelText: 'Enter Offer Name',
                              labelStyle: TextStyle(
                                color: greyColor, //<-- SEE HERE
                              ),
                            ),
                          ),
                        ), //name
                        SizedBox(
                          height: 05,
                        ),
                        Container(
                          padding: const EdgeInsets.all(10),
                          child: TextFormField(
                            cursorColor: greyColor,
                            controller: offer_codeController,
                            textCapitalization: TextCapitalization.characters,
                            decoration: const InputDecoration(
                              prefixIcon: Icon(
                                Icons.numbers,
                                color: primaryColor,
                              ),
                              border: OutlineInputBorder(
                                  borderSide: BorderSide(color: primaryColor)),
                              focusedBorder: OutlineInputBorder(
                                borderSide: BorderSide(color: primaryColor),
                              ),
                              enabledBorder: OutlineInputBorder(
                                borderSide: BorderSide(color: primaryColor),
                              ),
                              labelText: 'Enter Offer Code',
                              labelStyle: TextStyle(
                                color: greyColor, //<-- SEE HERE
                              ),
                            ),
                          ),
                        ),
                        const SizedBox(
                          height: 5,
                        ),
                        Container(
                          padding: const EdgeInsets.all(10),
                          child: TextFormField(
                            cursorColor: greyColor,
                            controller: descriptionController,
                            textCapitalization: TextCapitalization.characters,
                            decoration: const InputDecoration(
                              prefixIcon: Icon(
                                Icons.description,
                                color: primaryColor,
                              ),
                              border: OutlineInputBorder(
                                  borderSide: BorderSide(color: primaryColor)),
                              focusedBorder: OutlineInputBorder(
                                borderSide: BorderSide(color: primaryColor),
                              ),
                              enabledBorder: OutlineInputBorder(
                                borderSide: BorderSide(color: primaryColor),
                              ),
                              labelText: 'Enter Offer Description',
                              labelStyle: TextStyle(
                                color: greyColor, //<-- SEE HERE
                              ),
                            ),
                          ),
                        ), //mobile

                        const SizedBox(
                          height: 5,
                        ),
                        Container(
                          padding: const EdgeInsets.all(10),
                          child: TextFormField(
                            cursorColor: greyColor,
                            controller: offerController,
                            keyboardType: TextInputType.text,
                            textCapitalization: TextCapitalization.characters,
                            decoration: const InputDecoration(
                              prefixIcon: Icon(
                                Icons.local_offer,
                                color: primaryColor,
                              ),
                              border: OutlineInputBorder(
                                  borderSide: BorderSide(color: primaryColor)),
                              focusedBorder: OutlineInputBorder(
                                borderSide: BorderSide(color: primaryColor),
                              ),
                              enabledBorder: OutlineInputBorder(
                                borderSide: BorderSide(color: primaryColor),
                              ),
                              labelText: 'Enter Offer',
                              labelStyle: TextStyle(
                                color: greyColor, //<-- SEE HERE
                              ),
                            ),
                          ),
                        ),
                        const SizedBox(
                          height: 5,
                        ),

                        Container(
                          padding: const EdgeInsets.fromLTRB(10, 10, 10, 0),
                          child: TextFormField(
                            readOnly: true,
                            cursorColor: greyColor,
                            controller: starting_dateController,
                            decoration: InputDecoration(
                              labelText: 'Select Start Date',
                              prefixIcon: const Icon(Icons.date_range, color: primaryColor,),
                              labelStyle: const TextStyle(
                                color: greyColor, //<-- SEE HERE
                              ),
                              border: const OutlineInputBorder(
                                  borderSide: BorderSide(color: primaryColor)),
                              focusedBorder: const OutlineInputBorder(
                                borderSide: BorderSide(color: primaryColor),
                              ),
                              enabledBorder: const OutlineInputBorder(
                                borderSide: BorderSide(color: primaryColor),
                              ),
                              suffixIcon: IconButton(
                                icon:  Icon(
                                  Icons.date_range,
                                ),
                                onPressed: () async {
                                  DatePicker.showDatePicker(
                                    context,
                                    dateFormat: 'dd MMMM yyyy HH:mm',
                                    initialDateTime: DateTime.now(),
                                    minDateTime:DateTime.now(),
                                    maxDateTime: DateTime.now().add(Duration(days: 365)),
                                    onMonthChangeStartWithFirstDate: true,
                                    pickerMode: DateTimePickerMode.datetime,
                                    pickerTheme: DateTimePickerTheme(
                                      // backgroundColor: Colors.white,
                                      cancelTextStyle: labelTextStyle(),
                                      confirmTextStyle: labelTextStyle(),
                                      // showTitle: true,
                                      //title: Text('Pick date and time'),
                                      itemTextStyle: valuesTextStyle(),
                                    ),
                                    onConfirm: (dateTime, List<int> index)async {
                                      DateTime selectdate = dateTime;
                                      setState(() {
                                        selIOS = DateFormat('dd-MMM-yyyy - HH:mm').format(selectdate);
                                      });

                                      if(selIOS!=''){
                                        setState(() {
                                          starting_dateController.text=selIOS.toString();
                                        });
                                      }
                                      else {
                                        AppSettings.longFailedToast('please select date');
                                      }
                                    },
                                  );
                                },
                              ),

                            ),

                          ),
                        ),

                        const SizedBox(
                          height: 5,
                        ),
                        Container(
                          padding: const EdgeInsets.fromLTRB(10, 10, 10, 0),
                          child: TextFormField(
                            readOnly: true,
                            cursorColor: greyColor,
                            controller: ending_dateController,
                            decoration: InputDecoration(
                              labelText: 'Select End Date',
                              prefixIcon: const Icon(Icons.date_range, color: primaryColor,),
                              labelStyle: const TextStyle(
                                color: greyColor, //<-- SEE HERE
                              ),
                              border: const OutlineInputBorder(
                                  borderSide: BorderSide(color: primaryColor)),
                              focusedBorder: const OutlineInputBorder(
                                borderSide: BorderSide(color: primaryColor),
                              ),
                              enabledBorder: const OutlineInputBorder(
                                borderSide: BorderSide(color: primaryColor),
                              ),
                              suffixIcon: IconButton(
                                icon:  Icon(
                                  Icons.date_range,
                                ),
                                onPressed: () async {
                                  DatePicker.showDatePicker(
                                    context,
                                    dateFormat: 'dd MMMM yyyy HH:mm',
                                    initialDateTime: DateTime.now(),
                                    minDateTime:DateTime.now(),
                                    maxDateTime: DateTime.now().add(Duration(days: 365)),
                                    onMonthChangeStartWithFirstDate: true,
                                    pickerMode: DateTimePickerMode.datetime,
                                    pickerTheme: DateTimePickerTheme(
                                      // backgroundColor: Colors.white,
                                      cancelTextStyle: labelTextStyle(),
                                      confirmTextStyle: labelTextStyle(),
                                      // showTitle: true,
                                      //title: Text('Pick date and time'),
                                      itemTextStyle: valuesTextStyle(),
                                    ),
                                    onConfirm: (dateTime, List<int> index)async {
                                      DateTime selectdate = dateTime;
                                      setState(() {
                                        selIOS = DateFormat('dd-MMM-yyyy - HH:mm').format(selectdate);
                                      });

                                      if(selIOS!=''){
                                        setState(() {
                                          ending_dateController.text=selIOS.toString();
                                        });
                                      }
                                      else {
                                        AppSettings.longFailedToast('please select date');
                                      }
                                    },
                                  );
                                },
                              ),

                            ),

                          ),
                        ),//address description

                        const SizedBox(
                          height:15,
                        ),
                        Container(
                            width:MediaQuery.of(context).size.width * .99,
                            height: 55,
                            padding: const EdgeInsets.fromLTRB(3, 0, 3, 0),
                            child: ElevatedButton(
                              style: ElevatedButton.styleFrom(
                                primary: buttonColor, // background
                                onPrimary: Colors.white, // foreground
                              ),
                              onPressed: () async {
                                if (offer_nameController.text != '' &&
                                    offer_codeController.text != '' &&
                                    descriptionController.text != ''&&
                                    offerController.text != ''&&
                                    starting_dateController.text != '' &&
                                    ending_dateController.text != '') {
                                  AppSettings.preLoaderDialog(context);

                                  var payload = new Map<String, dynamic>();
                                  payload["offer_name"] = offer_nameController.text.toString();
                                  payload["offer_code"] = offer_codeController.text.toString();
                                  payload["description"] =  descriptionController.text.toString();;
                                  payload["offer"] = offerController.text.toString();
                                  payload["starting_date"] = starting_dateController.text.toString();
                                  payload["ending_date"] = ending_dateController.text.toString();
                                  payload["offer_status"] ="active";
                                  bool offerStatus = await AppSettings.createOffers(payload);
                                  try{
                                    if (offerStatus) {
                                      Navigator.pop(context);
                                      Navigator.of(context,rootNavigator: true).pop();
                                     Navigator.push(
                                        context,
                                        MaterialPageRoute(builder: (context) => Dashboard()),
                                      );
                                      AppSettings.longSuccessToast("Offer Created Succesfully!!");
                                    } else {

                                      AppSettings.longFailedToast("Fields should not be empty !!");
                                    }
                                  }
                                  catch(exception){
                                    print(exception);
                                    AppSettings.longFailedToast("Please enter valid details");
                                  }
                                } else {
                                  AppSettings.longFailedToast("Offer Not Created!!");

                                }
                              },
                              child: Text('Create'),
                            )),//login button

                      ],
                    ),
                  )),
            ),
          ])), );
  }
}
