import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:intl/intl.dart';

import 'package:loading_animation_widget/loading_animation_widget.dart';

import '../app/constant.dart';

import '../provider/current_user_provider.dart';

import '../screen/home_screen/pending_work_provider.dart';

import '../screen/work_list_screen/work_list_provider.dart';
import 'open_pdf_widget.dart';

Future<void> RptDate(BuildContext context, WidgetRef ref, String courierID) {
  return showDialog<void>(
    context: context,
    barrierDismissible: true, // user must tap button!
    builder: (BuildContext context) {
      return AlertDialog(
        title: Text(
          'Pilih Tanggal',
          style: Constant.heading4(context: context).copyWith(
              color: Constant.primaryBlue, fontWeight: FontWeight.w600),
        ),
        shape: const RoundedRectangleBorder(
            borderRadius: BorderRadius.all(Radius.circular(12))),
        content: HookConsumer(builder: (context, WidgetRef ref, z) {
          final dpKey = useState(1);
          final dateInput = useTextEditingController(
              text: DateFormat('yyyy-MM-dd').format(DateTime.now()));
          useEffect(() {
            WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
              var ky = dpKey.value;
              dpKey.value = ky + 1;
            });
            return () {};
          }, []);

          return SingleChildScrollView(
              child: Container(
                  width: double.infinity,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      // SizedBox(
                      //   height: Constant.getActualY(context: context, y: 12),
                      // ),
                      TextField(
                        controller: dateInput,
                        decoration: InputDecoration(
                            suffixIcon: Icon(Icons.calendar_today),
                            border: OutlineInputBorder(),
                            labelText: "Pilih Tanggal" //label text of field
                            ),
                        readOnly: true,
                        onTap: () async {
                          DateTime? pickedDate = await showDatePicker(
                              context: context,
                              initialDate: DateTime.parse(dateInput.text),
                              firstDate: DateTime(1950),
                              initialEntryMode:
                                  DatePickerEntryMode.calendarOnly,
                              //DateTime.now() - not to allow to choose before today.
                              lastDate: DateTime(2100));

                          if (pickedDate != null) {
                            print(
                                pickedDate); //pickedDate output format => 2021-03-10 00:00:00.000
                            String formattedDate =
                                DateFormat('yyyy-MM-dd').format(pickedDate);
                            print(
                                formattedDate); //formatted date output using intl package =>  2021-03-16
                            dateInput.text =
                                formattedDate; //set output date to TextField value.
                          } else {}
                        },
                      ),

                      SizedBox(
                        height: Constant.getActualY(context: context, y: 12),
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.end,
                        children: [
                          ElevatedButton(
                              onPressed: () {
                                Navigator.pop(context);
                                Navigator.push(
                                    context,
                                    MaterialPageRoute(
                                      builder: (context) => PdfViewerPage(
                                          date: dateInput.text,
                                          courierID: courierID),
                                    ));
                                // print(dateInput.text);
                              },
                              style: ElevatedButton.styleFrom(
                                  backgroundColor: Constant.primaryBlue,
                                  shape: RoundedRectangleBorder(
                                      borderRadius: BorderRadius.circular(8))),
                              child: Text("Buka Rpt")),
                        ],
                      ),
                    ],
                  )));
        }),
      );
    },
  );
}
