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

import '../../app/constant.dart';
import '../../provider/auth_provider.dart';
import '../../widgets/custom_dialog_builder.dart';

class OrderFilter extends HookConsumerWidget {
  const OrderFilter({
    super.key,
    required this.startDateCtr,
    required this.startDateState,
    required this.endDateCtr,
    required this.endDateState,
    required this.keywordCtr,
  });

  final TextEditingController startDateCtr;
  final ValueNotifier<DateTime> startDateState;
  final TextEditingController endDateCtr;
  final ValueNotifier<DateTime> endDateState;
  final TextEditingController keywordCtr;

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final auth = ref.watch(authProvider);
    final username = auth?.mUserUsername ?? "-";

    showDialogBuilder() {
      var endDateParam =
          DateFormat('yyyy-MM-dd').format(endDateState.value).toString();
      var startDateParam =
          DateFormat('yyyy-MM-dd').format(startDateState.value).toString();
      var companyID = auth?.mUserMCompanyID ?? "";
      print("endDateParam : $endDateParam");
      print("startDateParam : $startDateParam");

      CustomDialogBuilder.dialogBuilderWebPDF(
          context,
          // startDateCtr.text.toString(),
          // endDateCtr.text.toString(),
          startDateParam,
          endDateParam,
          keywordCtr.text.toString(),
          username,
          companyID);
      // launchUrlString("http://10.9.8.249/birt/run?__report=report/one/rekap/rpt_sales_001.rptdesign&__format=xls&PStartDate=2023-01-01&PEndDate=2023-01-31&username=adminsas&tm=1681262919125");
    }

    return Padding(
      padding: const EdgeInsets.all(0),
      child: Row(
        children: [
          Expanded(
            flex: 3,
            child: TextField(
              style: Constant.body1_600(context: context),
              controller: startDateCtr,
              decoration: InputDecoration(
                  suffixIcon: Icon(EvaIcons.calendar),
                  border: OutlineInputBorder(
                    borderSide: BorderSide(color: Constant.primaryBlue),
                    borderRadius: BorderRadius.circular(8),
                  ),
                  labelStyle: Constant.body1_600(context: context),
                  labelText: "Tanggal Awal" //label text of field
                  ),
              readOnly: true,
              onTap: () async {
                DateTime? pickedDate = await showDatePicker(
                    // locale: Locale("id"),
                    confirmText: "OK",
                    cancelText: "Batal",
                    context: context,
                    initialDate: startDateState.value,
                    firstDate: DateTime(1800),
                    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('dd-MM-yyyy').format(pickedDate);
                  print(
                      formattedDate); //formatted date output using intl package =>  2021-03-16
                  startDateCtr.text =
                      formattedDate; //set output date to TextField value.
                  startDateState.value = pickedDate;
                } else {}
              },
            ),
          ),
          SizedBox(
            width: Constant.getActualX(context: context, x: 24),
          ),
          Expanded(
            flex: 3,
            child: TextField(
              style: Constant.body1_600(context: context),
              controller: endDateCtr,
              decoration: InputDecoration(
                  suffixIcon: Icon(EvaIcons.calendar),
                  border: OutlineInputBorder(
                    borderSide: BorderSide(color: Constant.primaryBlue),
                    borderRadius: BorderRadius.circular(8),
                  ),
                  labelStyle: Constant.body1_600(context: context),
                  labelText: "Tanggal Akhir" //label text of field
                  ),
              readOnly: true,
              onTap: () async {
                DateTime? pickedDate = await showDatePicker(
                    // locale: Locale("id"),
                    confirmText: "OK",
                    cancelText: "Batal",
                    context: context,
                    initialDate: endDateState.value,
                    firstDate: DateTime(1800),
                    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('dd-MM-yyyy').format(pickedDate);
                  print(
                      formattedDate); //formatted date output using intl package =>  2021-03-16
                  endDateCtr.text =
                      formattedDate; //set output date to TextField value.
                  endDateState.value = pickedDate;
                } else {}
              },
            ),
          ),
          SizedBox(
            width: Constant.getActualX(context: context, x: 24),
          ),
          Expanded(
            flex: 6,
            child: TextField(
                style: Constant.body1_600(context: context),
                controller: keywordCtr,
                decoration: InputDecoration(
                    border: OutlineInputBorder(
                      borderSide: BorderSide(color: Constant.primaryBlue),
                      borderRadius: BorderRadius.circular(8),
                    ),
                    hintText: "No Reg / Nama",
                    hintStyle: Constant.body1_600(context: context),
                    labelStyle: Constant.body1_600(context: context),
                    labelText: "No Reg / Nama" //label text of field
                    )),
          ),
          SizedBox(
            width: Constant.getActualX(context: context, x: 24),
          ),
          // button report pdf
          Expanded(
            flex: 1,
            child: SizedBox(
              width: Constant.getActualX(
                context: context,
                x: 171,
              ),
              height: Constant.getActualY(context: context, y: 56),
              child: ElevatedButton(
                onPressed: () {
                  showDialogBuilder();
                },
                style: ButtonStyle(
                  backgroundColor: WidgetStateColor.resolveWith(
                    (st) => Constant.primaryRed,
                  ),
                  shape: WidgetStateProperty.all<RoundedRectangleBorder>(
                    RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(8),
                      side: BorderSide(
                        color: Colors.transparent,
                      ),
                    ),
                  ),
                  // shadowColor: MaterialStateProperty.all(
                  //     Color(0xffff48423d)),
                  elevation: WidgetStateProperty.all(1.0),
                ),
                // child: Align(
                //   alignment: Alignment.center,
                //   child: Text(
                //     'Cetak',
                //     style: Constant.body3_600(context: context).copyWith(
                //       color: Constant.textWhite,
                //     ),
                //   ),
                // ),
                child: Icon(
                  color: Colors.white,
                  EvaIcons.printerOutline,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
