import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import '/models/history_model.dart';
import '/screen/detail_history_screen/detail_history_screen.dart';
import '/widget/chip_type.dart';
import 'package:intl/intl.dart';

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

class HistoryListCard extends HookConsumerWidget {
  const HistoryListCard({
    super.key,
    required this.ListRiwayat,
  });

  final List<HistoryModel> ListRiwayat;

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final mCourirID =
        ref.watch(currentUserProvider)?.model.user?.mCourierID ?? "0";
    return ListView.builder(
      itemCount: ListRiwayat.length,
      itemBuilder: (context, index) {
        var data = ListRiwayat[index];
        String originalDateString = data.cdate.toString();
        DateTime originalDate =
            DateFormat('dd-MM-yyyy HH:mm:ss').parse(originalDateString);
        // String formattedDate = DateFormat.yMMMMd('id').format(originalDate);
        String formattedDate = DateFormat('d MMM HH:mm').format(originalDate);
        // Output: 30 Mei 2023
        return Container(
          margin: EdgeInsets.only(
              top: Constant.getActualY(context: context, y: 24)),
          padding: EdgeInsets.symmetric(
              horizontal: Constant.getActualX(context: context, x: 31)),
          child: ElevatedButton(
            onPressed: () {
              showModalBottomSheet(
                enableDrag: true,
                isScrollControlled: true,
                shape: const RoundedRectangleBorder(
                  borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(16),
                      topRight: Radius.circular(16)),
                ),
                backgroundColor: Theme.of(context).scaffoldBackgroundColor,
                context: context,
                builder: (context) {
                  return DraggableScrollableSheet(
                      initialChildSize: 0.9,
                      maxChildSize: 0.95,
                      minChildSize: 0.5,
                      expand: false,
                      builder: (_, scrollController) => Stack(
                            children: [
                              Container(
                                padding: EdgeInsets.symmetric(
                                    horizontal: Constant.getActualX(
                                        context: context, x: 125)),
                                child: Divider(
                                  thickness: 3,
                                  color: Constant.textPrimary,
                                ),
                              ),
                              SingleChildScrollView(
                                  controller: scrollController,
                                  child: DetailHistoryScreen(
                                    data: {
                                      "id": "1",
                                      "alamat": "Cabang Kertajaya",
                                      "jarak": "1,2 KM",
                                      "tanggal": "02 Desember 14:50",
                                      "tipe": "Pengantaran hasil Instansi",
                                      "tipe_id": 4
                                    },
                                    prop: DetailHistoryProp(
                                        data.tipeid.toString(),
                                        mCourirID,
                                        data.id.toString()),
                                  )),
                            ],
                          ));
                },
              );
            },
            style: ElevatedButton.styleFrom(
                padding: const EdgeInsets.all(10),
                elevation: 0,
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(16))),
                backgroundColor: data.ispending == 'N'
                    ? Constant.historyCardColor
                    : Constant.historyCardColorPending,
                foregroundColor: Colors.grey),
            child: Column(
              children: [
                SizedBox(
                  height: Constant.getActualY(context: context, y: 14),
                ),
                Row(
                  children: [
                    Image.asset(
                      "assets/icon_riwayat_motor.png",
                      width: Constant.getActualX(context: context, x: 64),
                      height: Constant.getActualX(context: context, x: 64),
                    ),
                    SizedBox(
                      width: Constant.getActualX(context: context, x: 12),
                    ),
                    Container(
                      height: Constant.getActualX(context: context, x: 70),
                      width: Constant.getActualX(context: context, x: 232),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        crossAxisAlignment: CrossAxisAlignment.end,
                        children: [
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            crossAxisAlignment: CrossAxisAlignment.end,
                            children: [
                              Container(
                                constraints: BoxConstraints(
                                    maxWidth: Constant.getActualX(
                                        context: context, x: 130)),
                                child: Column(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text(
                                      data.noSuratJalan.toString(),
                                      textAlign: TextAlign.start,
                                      style: Constant.caption1(context: context)
                                          .copyWith(
                                              fontWeight: FontWeight.w600,
                                              color: Constant.primaryBlue),
                                    ),
                                    Text(
                                      data.nama.toString(),
                                      maxLines: 2,
                                      overflow: TextOverflow.ellipsis,
                                      style: Constant.caption2(context: context)
                                          .copyWith(
                                              fontWeight: FontWeight.w600,
                                              color: Constant.textBlack),
                                    ),
                                  ],
                                ),
                              ),
                              Container(
                                constraints: BoxConstraints(
                                    maxWidth: Constant.getActualX(
                                        context: context, x: 100)),
                                child: Text(
                                  "${data.distance ?? '0'} Km",
                                  style: Constant.heading3(context: context)
                                      .copyWith(
                                          fontWeight: FontWeight.w600,
                                          color: Constant.textBlack),
                                ),
                              )
                            ],
                          ),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Expanded(
                                child: Text(
                                  formattedDate,
                                  maxLines: 2,
                                  overflow: TextOverflow.ellipsis,
                                  style: Constant.caption1(context: context)
                                      .copyWith(
                                          fontWeight: FontWeight.w500,
                                          color: Constant.textBlack),
                                ),
                              ),
                              Container(
                                constraints: BoxConstraints(
                                    maxWidth: Constant.getActualX(
                                        context: context, x: 150)),
                                child: ChipType(
                                  name: data.tipe.toString(),
                                  tipe: data.tipeid!,
                                  isDetailHistory: false,
                                ),
                              )
                            ],
                          )
                        ],
                      ),
                    ),
                  ],
                ),
                SizedBox(
                  height: Constant.getActualY(context: context, y: 14),
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
