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

import '../../app/constant.dart';
import '../../model/surat_jalan_detail_model.dart';
import '../../provider/auth_provider.dart';
import '../../provider/menu_provider.dart';
import '../../provider/surat_jalan_provider.dart';
import '../../widgets/custom_snackbar_widget.dart';
import '../../widgets/header.dart';
import '../../widgets/dialog_print.dart';
import '../surat_jalan/get_surat_jalan_detail_provider.dart';

class DetailResultScreen extends HookConsumerWidget {
  const DetailResultScreen({super.key});
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final sjNumber = ref.watch(suratJalanNumber);
    final id = ref.watch(suratJalanID);
    final auth = ref.watch(authProvider);
    final loading = useState(false);
    final data = useState<List<SuratJalanDetailModel>>(List.empty());
    useEffect(() {
      WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
        ref
            .read(GetSuratJalanDetailProvider.notifier)
            .getData(token: auth?.token ?? "", deliveryID: id);
      });
      return () {};
    }, [id]);
    ref.listen(
      GetSuratJalanDetailProvider,
      (pref, next) {
        if (next is GetSuratJalanDetailStateInit) {
          loading.value = true;
        } else if (next is GetSuratJalanDetailStateLoading) {
          loading.value = true;
        } else if (next is GetSuratJalanDetailStateError) {
          loading.value = false;
          SanckbarWidget(context, next.message, snackbarType.error);
          Constant.autoLogout(context: context, msg: next.message);
        } else if (next is GetSuratJalanDetailStateDone) {
          // print(jsonEncode(next.model));
          data.value = next.model;

          loading.value = false;
        }
      },
    );
    return Material(
      child: Column(
        children: [
          Header(),
          Container(
            child: Padding(
              padding: const EdgeInsets.all(32.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      IconButton(
                          onPressed: () {
                            ref.read(currentMenuProvider.notifier).state = 4;
                          },
                          icon: Icon(EvaIcons.arrowBackOutline)),
                      Text(sjNumber,
                          style: Constant.h4_600(context: context)
                              .copyWith(color: Constant.textBlack)),
                    ],
                  ),

                  SizedBox(
                      height: Constant.getActualY(context: context, y: 24)),
                  // SizedBox(
                  //     height: Constant.getActualY(context: context, y: 24)),
                  Material(
                    elevation: 10,
                    borderRadius: BorderRadius.all(Radius.circular(8)),
                    child: Column(
                      children: [
                        Container(
                          height: Constant.getActualY(context: context, y: 64),
                          decoration: BoxDecoration(
                            color: Constant.grey_200,
                          ),
                          child: Row(
                            children: [
                              Expanded(
                                flex: 2,
                                child: Container(
                                  alignment: Alignment.centerLeft,
                                  // width: Constant.getActualX(context: context, x: 100),
                                  height: Constant.getActualY(
                                      context: context, y: 64),
                                  color: Constant.grey_200,
                                  child: Padding(
                                    padding: const EdgeInsets.only(
                                        top: 12,
                                        bottom: 12,
                                        left: 20,
                                        right: 20),
                                    child: Text('Tanggal',
                                        style:
                                            Constant.body3_600(context: context)
                                                .copyWith(
                                                    color: Constant.textGrey)),
                                  ),
                                ),
                              ),
                              Expanded(
                                flex: 2,
                                child: Container(
                                  alignment: Alignment.centerLeft,
                                  // width: Constant.getActualX(context: context, x: 115),
                                  height: Constant.getActualY(
                                      context: context, y: 64),
                                  color: Constant.grey_200,
                                  child: Padding(
                                    padding: const EdgeInsets.only(
                                        top: 12,
                                        bottom: 12,
                                        left: 20,
                                        right: 20),
                                    child: Text('No Lab',
                                        style:
                                            Constant.body3_600(context: context)
                                                .copyWith(
                                                    color: Constant.textGrey)),
                                  ),
                                ),
                              ),
                              Expanded(
                                flex: 2,
                                child: Container(
                                  alignment: Alignment.centerLeft,
                                  // width: Constant.getActualX(context: context, x: 196),
                                  height: Constant.getActualY(
                                      context: context, y: 64),
                                  color: Constant.grey_200,
                                  child: Padding(
                                    padding: const EdgeInsets.only(
                                        top: 12,
                                        bottom: 12,
                                        left: 20,
                                        right: 20),
                                    child: Text('Nama Pasien',
                                        style:
                                            Constant.body3_600(context: context)
                                                .copyWith(
                                                    color: Constant.textGrey)),
                                  ),
                                ),
                              ),
                              Expanded(
                                flex: 1,
                                child: Container(
                                  alignment: Alignment.centerLeft,
                                  // width: Constant.getActualX(context: context, x: 100),
                                  height: Constant.getActualY(
                                      context: context, y: 64),
                                  color: Constant.grey_200,
                                  child: Padding(
                                    padding: const EdgeInsets.only(
                                        top: 12,
                                        bottom: 12,
                                        left: 20,
                                        right: 20),
                                    child: Text('Receive',
                                        overflow: TextOverflow.ellipsis,
                                        style:
                                            Constant.body3_600(context: context)
                                                .copyWith(
                                                    color: Constant.textGrey)),
                                  ),
                                ),
                              ),
                              Expanded(
                                flex: 1,
                                child: Container(
                                  alignment: Alignment.centerLeft,
                                  // width: Constant.getActualX(context: context, x: 100),
                                  height: Constant.getActualY(
                                      context: context, y: 64),
                                  color: Constant.grey_200,
                                  child: Padding(
                                    padding: const EdgeInsets.only(
                                        top: 12,
                                        bottom: 12,
                                        left: 20,
                                        right: 20),
                                    child: Text('Proses',
                                        overflow: TextOverflow.ellipsis,
                                        style:
                                            Constant.body3_600(context: context)
                                                .copyWith(
                                                    color: Constant.textGrey)),
                                  ),
                                ),
                              ),
                              Expanded(
                                flex: 1,
                                child: Container(
                                  alignment: Alignment.centerLeft,
                                  // width: Constant.getActualX(context: context, x: 80),
                                  height: Constant.getActualY(
                                      context: context, y: 64),
                                  color: Constant.grey_200,
                                  child: Padding(
                                    padding: const EdgeInsets.only(
                                        top: 12,
                                        bottom: 12,
                                        left: 20,
                                        right: 20),
                                    child: Text('Done',
                                        overflow: TextOverflow.ellipsis,
                                        style:
                                            Constant.body3_600(context: context)
                                                .copyWith(
                                                    color: Constant.textGrey)),
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                        Container(
                          color: Colors.white,
                          height: Constant.getActualY(context: context, y: 700),
                          child: loading.value
                              ? Center(
                                  child: LoadingAnimationWidget.discreteCircle(
                                      color: Constant.primaryBlue, size: 40),
                                )
                              : ListView(
                                  children: data.value
                                      .map(
                                        (e) => Row(
                                          children: [
                                            Expanded(
                                              flex: 2,
                                              child: Container(
                                                alignment: Alignment.centerLeft,
                                                // width: Constant.getActualX(context: context, x: 100),
                                                height: Constant.getActualY(
                                                    context: context, y: 64),

                                                child: Padding(
                                                  padding:
                                                      const EdgeInsets.only(
                                                          top: 12,
                                                          bottom: 12,
                                                          left: 20,
                                                          right: 20),
                                                  child: Text(e.date ?? "",
                                                      style: Constant.body3_600(
                                                              context: context)
                                                          .copyWith(
                                                              color: Constant
                                                                  .textBlack)),
                                                ),
                                              ),
                                            ),
                                            Expanded(
                                              flex: 2,
                                              child: Container(
                                                alignment: Alignment.centerLeft,
                                                width: Constant.getActualX(
                                                    context: context, x: 115),
                                                height: Constant.getActualY(
                                                    context: context, y: 64),
                                                child: Padding(
                                                  padding:
                                                      const EdgeInsets.only(
                                                          top: 12,
                                                          bottom: 12,
                                                          left: 20,
                                                          right: 20),
                                                  child: Text(
                                                      e.orderNumber ?? "",
                                                      style: Constant.body3_600(
                                                              context: context)
                                                          .copyWith(
                                                              color: Constant
                                                                  .textBlack)),
                                                ),
                                              ),
                                            ),
                                            Expanded(
                                              flex: 2,
                                              child: Container(
                                                alignment: Alignment.centerLeft,
                                                width: Constant.getActualX(
                                                    context: context, x: 196),
                                                height: Constant.getActualY(
                                                    context: context, y: 64),
                                                child: Padding(
                                                  padding:
                                                      const EdgeInsets.only(
                                                          top: 12,
                                                          bottom: 12,
                                                          left: 20,
                                                          right: 20),
                                                  child: Text(
                                                      e.patientName ?? "",
                                                      style: Constant.body3_600(
                                                              context: context)
                                                          .copyWith(
                                                              color: Constant
                                                                  .textBlack)),
                                                ),
                                              ),
                                            ),
                                            Expanded(
                                              flex: 1,
                                              child: Container(
                                                alignment: Alignment.centerLeft,
                                                width: Constant.getActualX(
                                                    context: context, x: 100),
                                                height: Constant.getActualY(
                                                    context: context, y: 64),
                                                child: Padding(
                                                    padding:
                                                        const EdgeInsets.only(
                                                            top: 12,
                                                            bottom: 12,
                                                            left: 20,
                                                            right: 20),
                                                    child: e.status == "R" ||
                                                            e.status == "T" ||
                                                            e.status == "P" ||
                                                            e.status == "Q" ||
                                                            e.status == "D"
                                                        ? Icon(
                                                            EvaIcons
                                                                .checkmarkCircle2,
                                                            size: 24,
                                                            color:
                                                                Constant.green,
                                                          )
                                                        : null),
                                              ),
                                            ),
                                            Expanded(
                                              flex: 1,
                                              child: Container(
                                                alignment: Alignment.centerLeft,
                                                width: Constant.getActualX(
                                                    context: context, x: 100),
                                                height: Constant.getActualY(
                                                    context: context, y: 64),
                                                child: Padding(
                                                    padding:
                                                        const EdgeInsets.only(
                                                            top: 12,
                                                            bottom: 12,
                                                            left: 20,
                                                            right: 20),
                                                    child: e.status == "P" ||
                                                            e.status == "Q" ||
                                                            e.status == "D"
                                                        ? Icon(
                                                            EvaIcons
                                                                .checkmarkCircle2,
                                                            size: 24,
                                                            color:
                                                                Constant.green,
                                                          )
                                                        : null),
                                              ),
                                            ),
                                            Expanded(
                                              flex: 1,
                                              child: Container(
                                                alignment: Alignment.centerLeft,
                                                width: Constant.getActualX(
                                                    context: context, x: 80),
                                                height: Constant.getActualY(
                                                    context: context, y: 64),
                                                child: Padding(
                                                    padding:
                                                        const EdgeInsets.only(
                                                            top: 12,
                                                            bottom: 12,
                                                            left: 20,
                                                            right: 20),
                                                    child: e.status == "D"
                                                        ? InkWell(
                                                            onTap: () {
                                                              showDialog(
                                                                  context:
                                                                      context,
                                                                  // barrierDismissible: false,
                                                                  builder:
                                                                      ((context) =>
                                                                          Container(
                                                                            child:
                                                                                DialogPrint(url: "http://devone.aplikasi.web.id/birt/run?__report=report/one/lab/rpt_fo_001.rptdesign&__format=pdf&username=ADMIN&PID=131868?t=1696927533769"),
                                                                          )));
                                                            },
                                                            child: Tooltip(
                                                              message:
                                                                  "Cetak laporan",
                                                              child: Icon(
                                                                EvaIcons
                                                                    .printer,
                                                                size: 24,
                                                                color: Constant
                                                                    .primaryBlue,
                                                              ),
                                                            ),
                                                          )
                                                        : null),
                                              ),
                                            ),
                                          ],
                                        ),
                                      )
                                      .toList(),
                                ),
                        ),
                      ],
                    ),
                  )
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
