import '../../widget/sankbar_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:intl/intl.dart';
import 'package:url_launcher/url_launcher.dart';

import '../../app/constant.dart';
import '../../app/route.dart';
import '../../provider/current_user_provider.dart';
import '../../widget/custom_drawer.dart';

class ReportScreen extends HookConsumerWidget {
  const ReportScreen({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    String formattedDate = DateFormat('dd-MM-yyyy').format(DateTime.now());
    final ctrlTglAwal = useTextEditingController(text: formattedDate);

    final tglAwal = useState<DateTime>(DateTime.now());
    final tglAwalTmp = useState<String>("");

    final ctrlTglAkhir = useTextEditingController(text: formattedDate);

    final tglAkhir = useState<DateTime>(DateTime.now());
    final tglAkhirTmp = useState<String>("");

    useEffect(() {
      WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
        final userID = ref.read(currentUserProvider)?.model.M_UserID ?? "0";
        if (userID == "0") {
          //not login
          Navigator.of(context)
              .pushNamedAndRemoveUntil(loginRoute, (route) => true);

          //  Navigator.popAndPushNamed(context, loginRoute);
          return;
        }
      });
      return () {};
    }, []);

    return Padding(
      padding: EdgeInsets.only(
        top: Constant.getActualYPhone(context: context, y: 30),
      ),
      child: Scaffold(
        appBar: AppBar(
          title: Text('Report'),
        ),
        drawer: CustomDrawer(),
        body: SafeArea(
          child: SingleChildScrollView(
            child: Padding(
              padding: EdgeInsets.all(20),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    'Tanggal Awal',
                    style: Constant.body1(context: context).copyWith(
                        fontWeight: FontWeight.w600, color: Constant.textBlack),
                  ),
                  SizedBox(
                    height: Constant.getActualYPhone(context: context, y: 10),
                  ),
                  // Tanggal Awal
                  Row(
                    children: [
                      Expanded(
                        child: TextField(
                          controller: ctrlTglAwal,
                          decoration: InputDecoration(
                            hintStyle:
                                Constant.body2_400(context: context).copyWith(
                              color: Colors.orange,
                            ),
                            labelStyle:
                                Constant.body2_400(context: context).copyWith(
                              color: Colors.orange,
                            ),
                            border: OutlineInputBorder(
                              borderSide: BorderSide(
                                color: Colors.orange,
                                width: 1,
                              ),
                            ),
                            focusedBorder: OutlineInputBorder(
                              borderSide: BorderSide(
                                color: Colors.orange,
                                width: 1,
                              ),
                            ),
                            // labelText: "Tanggal Awal",
                            hintText: 'Tanggal Awal',
                            // suffixIcon: isLoadingFilterScope.value
                            //     ? SizedBox(
                            //         width: Constant.getActualXPhone(
                            //           context: context,
                            //           x: 4,
                            //         ),
                            //         height: Constant.getActualYPhone(
                            //           context: context,
                            //           y: 4,
                            //         ),
                            //         child: CircularProgressIndicator(
                            //           color: Constant.textRed,
                            //         ),
                            //       )
                            //     : Icon(
                            //         Icons.calendar_month_sharp,
                            //         color: Constant.colorIconDate,
                            //       ),
                          ),
                          onTap: () async {
                            final selectedDateAwal = await showDatePicker(
                              // locale: const Locale("en-CA"),
                              // locale: ,
                              context: context,
                              initialEntryMode:
                                  DatePickerEntryMode.calendarOnly,
                              firstDate: DateTime(2000),
                              lastDate: DateTime(2100),

                              initialDate: (ctrlTglAwal.text.isEmpty)
                                  ? DateTime.now()
                                  : tglAwal.value,
                            );

                            if (selectedDateAwal != null) {
                              String formattedDate = DateFormat('dd-MM-yyyy')
                                  .format(selectedDateAwal);
                              // ctrlTglAwal.text =
                              //     selectedDateAwal.toString().split(' ')[0];
                              ctrlTglAwal.text = formattedDate;
                              tglAwal.value = selectedDateAwal;
                              tglAwalTmp.value = selectedDateAwal.toString();
                            }

                            if (selectedDateAwal == null) {
                              print('cancel button');
                              return;
                            }
                          },
                        ),
                      ),
                    ],
                  ),

                  SizedBox(
                    height: Constant.getActualYPhone(context: context, y: 20),
                  ),

                  Text(
                    'Tanggal Akhir',
                    style: Constant.body1(context: context).copyWith(
                        fontWeight: FontWeight.w600, color: Constant.textBlack),
                  ),
                  SizedBox(
                    height: Constant.getActualYPhone(context: context, y: 10),
                  ),
                  // Tanggal Akhir
                  Row(
                    children: [
                      Expanded(
                        child: TextField(
                          controller: ctrlTglAkhir,
                          decoration: InputDecoration(
                            hintStyle:
                                Constant.body2_400(context: context).copyWith(
                              color: Colors.orange,
                            ),
                            labelStyle:
                                Constant.body2_400(context: context).copyWith(
                              color: Colors.orange,
                            ),
                            border: OutlineInputBorder(
                              borderSide: BorderSide(
                                color: Colors.orange,
                                width: 1,
                              ),
                            ),
                            focusedBorder: OutlineInputBorder(
                              borderSide: BorderSide(
                                color: Colors.orange,
                                width: 1,
                              ),
                            ),
                            // labelText: "Tanggal Awal",
                            hintText: 'Tanggal Akhir',
                            // suffixIcon: isLoadingFilterScope.value
                            //     ? SizedBox(
                            //         width: Constant.getActualXPhone(
                            //           context: context,
                            //           x: 4,
                            //         ),
                            //         height: Constant.getActualYPhone(
                            //           context: context,
                            //           y: 4,
                            //         ),
                            //         child: CircularProgressIndicator(
                            //           color: Constant.textRed,
                            //         ),
                            //       )
                            //     : Icon(
                            //         Icons.calendar_month_sharp,
                            //         color: Constant.colorIconDate,
                            //       ),
                          ),
                          onTap: () async {
                            final selectedDateAkhir = await showDatePicker(
                              // locale: const Locale("en-CA"),
                              // locale: ,
                              context: context,
                              initialEntryMode:
                                  DatePickerEntryMode.calendarOnly,
                              firstDate: DateTime(2000),
                              lastDate: DateTime(2100),

                              initialDate: (ctrlTglAkhir.text.isEmpty)
                                  ? DateTime.now()
                                  : tglAkhir.value,
                            );

                            if (selectedDateAkhir != null) {
                              String formattedDate = DateFormat('dd-MM-yyyy')
                                  .format(selectedDateAkhir);
                              // ctrlTglAkhir.text =
                              //     selectedDateAkhir.toString().split(' ')[0];
                              ctrlTglAkhir.text = formattedDate;
                              tglAkhir.value = selectedDateAkhir;
                              tglAkhirTmp.value = selectedDateAkhir.toString();
                            }

                            if (selectedDateAkhir == null) {
                              print('cancel button');
                              return;
                            }
                          },
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ),
        ),
        bottomNavigationBar: Padding(
          padding: EdgeInsets.only(
            // right: Constant.getActualXPhone(context: context, x: 27),
            // left: Constant.getActualXPhone(context: context, x: 27),
            // bottom: Constant.getActualYPhone(context: context, y: 32),
            top: Constant.getActualYPhone(context: context, y: 10),
          ),
          child: BottomAppBar(
            child: Container(
              height: 150,
              child: Padding(
                padding: EdgeInsets.only(
                  // right: Constant.getActualXPhone(context: context, x: 27),
                  // left: Constant.getActualXPhone(context: context, x: 27),
                  // bottom: Constant.getActualYPhone(context: context, y: 32),
                  top: Constant.getActualYPhone(context: context, y: 24),
                ),
                child: Column(
                  children: [
                    // Excel
                    Container(
                      width: Constant.getActualXPhone(context: context, x: 336),
                      height: Constant.getActualYPhone(context: context, y: 42),
                      child: ElevatedButton(
                        style: ButtonStyle(
                          backgroundColor: MaterialStateColor.resolveWith(
                              (states) => Colors.white),
                          // side: MaterialStateBorderSide.resolveWith(
                          //   (states) => BorderSide(color: Colors.green),
                          // ),

                          // backgroundColor: MaterialStateColor.resolveWith(
                          //     (st) => Constant.pcBtnBackgroundColor),
                          shape:
                              MaterialStateProperty.all<RoundedRectangleBorder>(
                            RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(8),
                              side: BorderSide(
                                color: Colors.green,
                              ),
                            ),
                          ),
                          shadowColor:
                              MaterialStateProperty.all(Color(0xffff48423d)),
                          elevation: MaterialStateProperty.all(4.0),
                        ),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Container(
                              width: Constant.getActualXPhone(
                                  context: context, x: 16),
                              height: Constant.getActualYPhone(
                                  context: context, y: 16),
                              // decoration: BoxDecoration(color: Colors.grey),
                              child: Image.asset(
                                "images/logo_excel.png",
                                fit: BoxFit.fill,
                                // scale: 1,
                              ),
                            ),
                            SizedBox(
                              height: Constant.getActualXPhone(
                                  context: context, x: 8),
                            ),
                            Text(
                              'Download Report (xls) ',
                              style: Constant.body1(context: context).copyWith(
                                fontWeight: FontWeight.w600,
                                color: Colors.green,
                              ),
                            ),
                          ],
                        ),
                        onPressed: () {},
                      ),
                    ),

                    SizedBox(
                      height: Constant.getActualYPhone(context: context, y: 20),
                    ),

                    // PDF
                    Container(
                      width: Constant.getActualXPhone(context: context, x: 336),
                      height: Constant.getActualYPhone(context: context, y: 42),
                      child: ElevatedButton(
                        style: ButtonStyle(
                          backgroundColor: MaterialStateColor.resolveWith(
                              (st) => Constant.pcBtnBackgroundColor),
                          shape:
                              MaterialStateProperty.all<RoundedRectangleBorder>(
                            RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(8),
                              side: BorderSide(
                                color: Constant.pcBtnBackgroundColor,
                              ),
                            ),
                          ),
                          shadowColor:
                              MaterialStateProperty.all(Color(0xffff48423d)),
                          elevation: MaterialStateProperty.all(4.0),
                        ),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Container(
                              width: Constant.getActualXPhone(
                                  context: context, x: 16),
                              height: Constant.getActualYPhone(
                                  context: context, y: 16),
                              // decoration: BoxDecoration(color: Colors.grey),
                              child: Image.asset(
                                "images/logo_pdf.png",
                                fit: BoxFit.fill,
                                // scale: 1,
                              ),
                            ),
                            SizedBox(
                              height: Constant.getActualXPhone(
                                  context: context, x: 8),
                            ),
                            Text(
                              'Download Report (PDF)',
                              style: Constant.body1(context: context).copyWith(
                                  fontWeight: FontWeight.w600,
                                  color: Constant.white),
                            ),
                          ],
                        ),
                        onPressed: () async {
                          String url =
                              "https://pub.dev/packages?q=url+launcher";
                          if (!await launchUrl(Uri.parse(url))) {
                            // throw Exception('Could not launch $url');
                            SanckbarWidget(context, 'Could not launch $url',
                                snackbarType.error);
                          }
                        },
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}
