// ignore_for_file: sized_box_for_whitespace

import 'package:flutter/material.dart';
import 'package:tour_travel_agr/helper/components_widget/custom_appbar.dart';
import 'package:tour_travel_agr/helper/components_widget/modal_select_date.dart';
import 'package:tour_travel_agr/helper/components_widget/transaction_list.dart';
import 'package:tour_travel_agr/helper/function/replace_date.dart';
import 'package:tour_travel_agr/helper/prefs.dart';
import 'package:tour_travel_agr/helper/widget_responsive.dart';
import 'package:tour_travel_agr/resource/assets.dart';
import 'package:tour_travel_agr/resource/colors.dart';
import 'package:tour_travel_agr/resource/constanta_string.dart';
import 'package:tour_travel_agr/resource/font.dart';
import 'package:tour_travel_agr/resource/size.dart';
import 'package:tour_travel_agr/resource/style.dart';

class ReimbursementView extends StatelessWidget {
  const ReimbursementView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ColorManager.backgroundColor,
      body: ScreenResponsive(
        widget: const BodyWidget(),
        widthScreen: MediaQuery.of(context).size.width,
      ),
    );
  }
}

class BodyWidget extends StatefulWidget {
  const BodyWidget({
    super.key,
  });

  @override
  State<BodyWidget> createState() => _BodyWidgetState();
}

class _BodyWidgetState extends State<BodyWidget> {
  String dateSelected = dateLocal(DateTime.now().toLocal().toString());

  bool? todayActive;
  bool? customDateActive;
  String? startDate;
  String? endDate;

  @override
  void initState() {
    resetFilter();
    super.initState();
  }

  @override
  void dispose() {
    setReimburseDate("");
    super.dispose();
  }

  void resetFilter() {
    String reimburseDate = getReimburseDate();
    if (reimburseDate != '') {
      List<String> split = reimburseDate.split(" - ");
      if (split.length > 1) {
        startDate = split[0];
        endDate = split[1];
        customDateActive = true;
        todayActive = false;
      } else {
        customDateActive = false;
        todayActive = true;
        startDate = split[0];
        endDate = split[0];
      }
    } else {
      startDate = dateLocal(DateTime.now().toLocal().toString());
      endDate = dateLocal(DateTime.now().toLocal().toString());
      customDateActive = false;
      todayActive = true;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(
        top: AppPadding.safeAreaTop(context),
        left: AppPadding.p20,
        right: AppPadding.p20,
        bottom: AppPadding.safeAreaBot(context),
      ),
      child: Column(
        children: [
          const CustomAppBar(text: "Reimbursement"),
          Container(
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(
                5,
              ),
              border: Border.all(
                color: ColorManager.grey.withOpacity(
                  0.25,
                ),
              ),
            ),
            margin: EdgeInsets.only(
              top: AppMargin.m16,
            ),
            padding: EdgeInsets.symmetric(
              horizontal: AppPadding.p20,
              vertical: AppPadding.p9,
            ),
            child: Center(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    dateSelected,
                    style: getSemiBoldStyle(
                      color: Colors.black,
                      fontFamily: FontConstants.openSans,
                    ),
                  ),
                  GestureDetector(
                    onTap: () {
                      showModalBottomSheet(
                          backgroundColor: ColorManager.backgroundColor,
                          isScrollControlled: true,
                          context: context,
                          builder: (context) {
                            return ModalSelectDate(
                              onTapWidget: () {
                                Navigator.pop(context);
                                resetFilter();
                                setState(() {
                                  dateSelected = getReimburseDate();
                                });
                              },
                              todayActive: todayActive!,
                              customDateActive: customDateActive!,
                              startDate: startDate,
                              endDate: endDate,
                              fromWidget: ConstantString.fromWidgetReimburse,
                            );
                          });
                    },
                    child: Image(
                      image: AssetImage(
                        Assets.calenderIcon,
                      ),
                    ),
                  )
                ],
              ),
            ),
          ),
          const SizedBox(
            height: 12,
          ),
          Expanded(
            child: ListView.builder(
              padding: EdgeInsets.zero,
              itemCount: 10,
              itemBuilder: (c, index) {
                return const ListTransaction(
                  date: "24 Maret 2023",
                  title: "15 Transaksi",
                  subtitle: "Rp 320.000",
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}
