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:intl/intl.dart';
import 'package:mitra_corporate/model/registration_model.dart';
import 'package:mitra_corporate/model/search_order_model.dart';
import 'package:mitra_corporate/model/test_model.dart';
import 'package:mitra_corporate/provider/order_provider.dart';
import 'package:mitra_corporate/screen/order/cancel_order_provider.dart';
import 'package:mitra_corporate/screen/order/dialog_cancel_order.dart';
import 'package:mitra_corporate/screen/order/dialog_edit.dart';
import 'package:mitra_corporate/screen/order/search_order_provider.dart';
import 'package:loading_animation_widget/loading_animation_widget.dart';

import '../../app/constant.dart';
import '../../provider/auth_provider.dart';
import '../../provider/registrasi_provider.dart';
import '../../widgets/custom_snackbar_widget.dart';
import '../../widgets/header.dart';
import '../../widgets/qr_status_chip.dart';
import '../registrasi/registrasi_prvider.dart';
import '../registrasi/table_pasien.dart';
import 'order_filter.dart';

class OrderScreen extends HookConsumerWidget {
  const OrderScreen({super.key});
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final startDateCtr = useTextEditingController(
        text: DateFormat('dd-MM-yyyy').format(DateTime.now()));
    final startDateState = useState(DateTime.now());
    final endDateCtr = useTextEditingController(
        text: DateFormat('dd-MM-yyyy').format(DateTime.now()));
    final endDateState = useState(DateTime.now());
    final keywordCtr = useTextEditingController(text: "");
    final auth = ref.watch(authProvider);
    final keyword = useState("");
    final rowsPerPage = useState(10);
    final searchLoading = useState(false);
    final cancelLoading = useState(false);
    final dataStart = useState(1);
    final dataEnd = useState(1);
    final currPage = useState(1);
    final listTest = useState<List<TestModel>>(List.empty());
    final orderList = useState<SearchOrderModel>(
        SearchOrderModel(orders: [], total: "0", totalPage: 0));
    keywordCtr.addListener(
      () {
        keyword.value = keywordCtr.text;
      },
    );
    refresh() {
      var ed =
          "${DateFormat('yyyy-MM-dd').format(endDateState.value)} 23:59:59";
      var sd =
          "${DateFormat('yyyy-MM-dd').format(startDateState.value)} 00:00:00";
      print("ed : $ed");
      print("sd : $sd");

      ref.read(SearchOrderProvider.notifier).getData(
          token: auth?.token ?? "",
          companyID: auth?.mUserMCompanyID ?? "",
          rpp: rowsPerPage.value.toString(),
          keyword: keyword.value,
          page: currPage.value.toString(),
          startDate: sd,
          endDate: ed);
    }

    useEffect(() {
      WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
        final debouncer = Debouncer(milliseconds: 100);
        debouncer.run(() {
          // ref.read(SearchPatientProvider.notifier).getData(
          //     token: auth?.token ?? "",
          //     page: currPage.value.toString(),
          //     keyword: keyword,
          //     rpp: rowsPerPage.value.toString(),
          //     companyID: auth?.mUserMCompanyID ?? "");

          final debouncer = Debouncer(milliseconds: 100);
          var ed =
              "${DateFormat('yyyy-MM-dd').format(endDateState.value)} 23:59:59";
          var sd =
              "${DateFormat('yyyy-MM-dd').format(startDateState.value)} 00:00:00";
          print("ed : $ed");
          print("sd : $sd");
          // debouncer.run(() {
          ref.read(SearchOrderProvider.notifier).getData(
              token: auth?.token ?? "",
              companyID: auth?.mUserMCompanyID ?? "",
              rpp: rowsPerPage.value.toString(),
              keyword: keyword.value,
              page: currPage.value.toString(),
              startDate: sd,
              endDate: ed);
          // });
        });
      });
      return () {};
    }, [currPage.value]);
    useEffect(() {
      WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
        final debouncer = Debouncer(milliseconds: 100);
        currPage.value = 1;
        var ed =
            "${DateFormat('yyyy-MM-dd').format(endDateState.value)} 23:59:59";
        var sd =
            "${DateFormat('yyyy-MM-dd').format(startDateState.value)} 00:00:00";
        print("ed : $ed");
        print("sd : $sd");
        debouncer.run(() {
          ref.read(SearchOrderProvider.notifier).getData(
              token: auth?.token ?? "",
              companyID: auth?.mUserMCompanyID ?? "",
              rpp: rowsPerPage.value.toString(),
              keyword: keyword.value,
              page: "1",
              startDate: sd,
              endDate: ed);
        });
      });
      return () {};
    }, [
      keyword.value,
      rowsPerPage.value,
      startDateState.value,
      endDateState.value
    ]);
    useEffect(() {
      WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
        ref.read(GetFPPProvider.notifier).getData(
            mouID: auth?.mUserMMouID ?? "0", token: auth?.token ?? "0");
      });
      return () {};
    }, []);

    ref.listen(
      SearchOrderProvider,
      (pref, next) {
        if (next is SearchOrderStateInit) {
          searchLoading.value = true;
        } else if (next is SearchOrderStateLoading) {
          searchLoading.value = true;
        } else if (next is SearchOrderStateError) {
          searchLoading.value = false;
          SanckbarWidget(context, next.message, snackbarType.error);
          Constant.autoLogout(context: context, msg: next.message);
        } else if (next is SearchOrderStateDone) {
          orderList.value = next.model;
          // print(jsonEncode(next.model));

          dataStart.value =
              (currPage.value * rowsPerPage.value) - rowsPerPage.value + 1;
          dataEnd.value = currPage.value == next.model.totalPage
              ? int.parse(next.model.total ?? "0")
              : dataStart.value + rowsPerPage.value - 1;

          searchLoading.value = false;
        }
      },
    );
    ref.listen(
      GetFPPProvider,
      (pref, next) {
        if (next is GetFPPStateInit) {
          searchLoading.value = true;
        } else if (next is GetFPPStateLoading) {
          searchLoading.value = true;
        } else if (next is GetFPPStateError) {
          searchLoading.value = false;
          SanckbarWidget(context, next.message, snackbarType.error);
          Constant.autoLogout(context: context, msg: next.message);
        } else if (next is GetFPPStateDone) {
          listTest.value = next.model;
          // print(jsonEncode(next.model));

          // tabController.length

          searchLoading.value = false;
        }
      },
    );

    cancelOrder(orderID) {
      ref
          .read(CancelOrderProvider.notifier)
          .cancel(token: auth?.token ?? "0", orderID: orderID);
    }

    ref.listen(
      CancelOrderProvider,
      (pref, next) {
        if (next is CancelOrderStateInit) {
          cancelLoading.value = true;
        } else if (next is CancelOrderStateLoading) {
          cancelLoading.value = true;
        } else if (next is CancelOrderStateError) {
          cancelLoading.value = false;
          SanckbarWidget(context, next.message, snackbarType.error);
          Constant.autoLogout(context: context, msg: next.message);
        } else if (next is CancelOrderStateDone) {
          refresh();
          Navigator.pop(context);
          // print(jsonEncode(next.model));
          SanckbarWidget(
              context, "Berhasil Membatalkan order", snackbarType.success);
          cancelLoading.value = false;
        }
      },
    );

    return Material(
        child: Column(children: [
      Header(),
      Padding(
        padding: const EdgeInsets.all(32.0),
        child: Column(
          children: [
            Container(
              alignment: Alignment.centerLeft,
              // width: Constant.getActualX(context: context, x: 1079),
              height: Constant.getActualY(context: context, y: 40),
              child: Text('Order',
                  style: Constant.h3_400(context: context)
                      .copyWith(color: Constant.textBlack)),
            ),
            SizedBox(height: Constant.getActualY(context: context, y: 42)),
            Container(
              decoration: BoxDecoration(
                  color: Colors.white, borderRadius: BorderRadius.circular(12)),
              padding: EdgeInsets.symmetric(
                horizontal: Constant.getActualX(context: context, x: 24),
                vertical: Constant.getActualY(context: context, y: 32),
              ),
              child: Column(
                children: [
                  OrderFilter(
                    startDateCtr: startDateCtr,
                    startDateState: startDateState,
                    endDateCtr: endDateCtr,
                    endDateState: endDateState,
                    keywordCtr: keywordCtr,
                  ),
                  SizedBox(
                      height: Constant.getActualY(context: context, y: 24)),
                  Material(
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(8)),
                    child: Column(
                      children: [
                        Row(
                          children: [
                            Expanded(
                                flex: 1,
                                child: Padding(
                                  padding: const EdgeInsets.all(0),
                                  child: Container(
                                    alignment: Alignment.centerLeft,
                                    // width: Constant.getActualX(context: context, x: 171),
                                    // height: Constant.getActualY(
                                    //     context: context, y: 64),
                                    // color: Constant.grey_200,
                                    child: Padding(
                                      padding: const EdgeInsets.only(
                                          top: 12,
                                          bottom: 12,
                                          left: 24,
                                          right: 6),
                                      child: Text('No',
                                          overflow: TextOverflow.ellipsis,
                                          style: Constant.body3_600(
                                                  context: context)
                                              .copyWith(
                                                  color: Constant.textGrey)),
                                    ),
                                  ),
                                )),
                            Expanded(
                              flex: 2,
                              child: Container(
                                alignment: Alignment.centerLeft,
                                // width: Constant.getActualX(context: context, x: 171),
                                // height: Constant.getActualY(
                                //     context: context, y: 64),
                                // color: Constant.grey_200,
                                child: Padding(
                                  padding: const EdgeInsets.only(
                                      top: 12, bottom: 12, left: 10, right: 6),
                                  child: Text('Nomor Reg',
                                      overflow: TextOverflow.ellipsis,
                                      style: Constant.body3_600(
                                              context: context)
                                          .copyWith(color: Constant.textGrey)),
                                ),
                              ),
                            ),
                            Expanded(
                              flex: 2,
                              child: Container(
                                alignment: Alignment.centerLeft,
                                // width: Constant.getActualX(context: context, x: 171),
                                // height: Constant.getActualY(
                                //     context: context, y: 64),
                                // color: Constant.grey_200,
                                child: Padding(
                                  padding: const EdgeInsets.only(
                                      top: 12, bottom: 12, left: 6, right: 6),
                                  child: Text('Nama',
                                      overflow: TextOverflow.ellipsis,
                                      style: Constant.body3_600(
                                              context: context)
                                          .copyWith(color: Constant.textGrey)),
                                ),
                              ),
                            ),
                            Expanded(
                              flex: 2,
                              child: Container(
                                alignment: Alignment.centerLeft,
                                // width: Constant.getActualX(context: context, x: 171),
                                // height: Constant.getActualY(
                                //     context: context, y: 64),
                                // color: Constant.grey_200,
                                child: Padding(
                                  padding: const EdgeInsets.only(
                                      top: 12, bottom: 12, left: 6, right: 24),
                                  child: Text('No. Hp',
                                      overflow: TextOverflow.ellipsis,
                                      style: Constant.body3_600(
                                              context: context)
                                          .copyWith(color: Constant.textGrey)),
                                ),
                              ),
                            ),
                            Expanded(
                              flex: 3,
                              child: Container(
                                alignment: Alignment.centerLeft,
                                // width: Constant.getActualX(context: context, x: 171),
                                // height: Constant.getActualY(
                                //     context: context, y: 64),
                                // color: Constant.grey_200,
                                child: Padding(
                                  padding: const EdgeInsets.only(
                                      top: 12, bottom: 12, left: 6, right: 6),
                                  child: Text('Pemeriksaan',
                                      overflow: TextOverflow.ellipsis,
                                      style: Constant.body3_600(
                                              context: context)
                                          .copyWith(color: Constant.textGrey)),
                                ),
                              ),
                            ),
                            Expanded(
                              flex: 2,
                              child: Container(
                                alignment: Alignment.centerLeft,
                                // width: Constant.getActualX(context: context, x: 171),
                                // height: Constant.getActualY(
                                //     context: context, y: 64),
                                // color: Constant.grey_200,
                                child: Padding(
                                  padding: const EdgeInsets.only(
                                      top: 12, bottom: 12, left: 6, right: 6),
                                  child: Text('QR Code',
                                      overflow: TextOverflow.ellipsis,
                                      style: Constant.body3_600(
                                              context: context)
                                          .copyWith(color: Constant.textGrey)),
                                ),
                              ),
                            ),
                            Expanded(
                              flex: 2,
                              child: Container(
                                alignment: Alignment.centerLeft,
                                // width: Constant.getActualX(context: context, x: 171),
                                // height: Constant.getActualY(
                                //     context: context, y: 64),
                                // color: Constant.grey_200,
                                child: Padding(
                                  padding: const EdgeInsets.only(
                                      top: 12, bottom: 12, left: 6, right: 6),
                                  child: Text('Status Registrasi',
                                      overflow: TextOverflow.ellipsis,
                                      maxLines: 2,
                                      style: Constant.body3_600(
                                              context: context)
                                          .copyWith(color: Constant.textGrey)),
                                ),
                              ),
                            ),
                            Expanded(
                              flex: 2,
                              child: Container(
                                alignment: Alignment.centerLeft,
                                // width: Constant.getActualX(context: context, x: 171),
                                // height: Constant.getActualY(
                                //     context: context, y: 64),
                                // color: Constant.grey_200,
                                child: Padding(
                                  padding: const EdgeInsets.only(
                                      top: 12, bottom: 12, left: 6, right: 6),
                                  child: Text('Status Proses',
                                      overflow: TextOverflow.ellipsis,
                                      maxLines: 2,
                                      style: Constant.body3_600(
                                              context: context)
                                          .copyWith(color: Constant.textGrey)),
                                ),
                              ),
                            ),
                            Expanded(
                              flex: 2,
                              child: Container(
                                alignment: Alignment.centerLeft,
                                // width: Constant.getActualX(context: context, x: 171),
                                // height: Constant.getActualY(
                                //     context: context, y: 64),
                                // color: Constant.grey_200,
                                child: Padding(
                                  padding: const EdgeInsets.only(
                                      top: 12, bottom: 12, left: 6, right: 6),
                                  child: Text('Status Hasil',
                                      overflow: TextOverflow.ellipsis,
                                      maxLines: 2,
                                      style: Constant.body3_600(
                                              context: context)
                                          .copyWith(color: Constant.textGrey)),
                                ),
                              ),
                            ),
                            Expanded(
                              flex: 1,
                              child: Container(
                                alignment: Alignment.centerLeft,
                                // width: Constant.getActualX(context: context, x: 171),
                                // height: Constant.getActualY(
                                //     context: context, y: 64),
                                // color: Constant.grey_200,
                                child: Padding(
                                  padding: const EdgeInsets.only(
                                      top: 12, bottom: 12, left: 12, right: 12),
                                  child: Text('Aksi',
                                      overflow: TextOverflow.ellipsis,
                                      style: Constant.body3_600(
                                              context: context)
                                          .copyWith(color: Constant.textGrey)),
                                ),
                              ),
                            )
                          ],
                        ),
                        Divider(),
                        Container(
                          color: Colors.white,
                          height: Constant.getActualY(context: context, y: 400),
                          child: searchLoading.value
                              ? Center(
                                  child: LoadingAnimationWidget.discreteCircle(
                                      color: Constant.primaryBlue, size: 40))
                              : ListView(
                                  children: orderList.value.orders!
                                      .asMap()
                                      .entries
                                      .map((e) => Column(
                                            children: [
                                              TextButton(
                                                style: TextButton.styleFrom(
                                                    shape:
                                                        RoundedRectangleBorder(
                                                            borderRadius:
                                                                BorderRadius
                                                                    .circular(
                                                                        8)),
                                                    foregroundColor:
                                                        Constant.primaryBlue),
                                                onPressed: e.value.status == 'N'
                                                    ? () {}
                                                    : null,
                                                child: Row(
                                                  children: [
                                                    Expanded(
                                                        flex: 1,
                                                        child: Padding(
                                                          padding:
                                                              const EdgeInsets
                                                                  .all(8.0),
                                                          child: Container(
                                                            alignment: Alignment
                                                                .centerLeft,
                                                            // width: Constant.getActualX(context: context, x: 171),
                                                            // height: Constant.getActualY(
                                                            //     context: context, y: 64),
                                                            // color: Colors.white,
                                                            child: Padding(
                                                                padding:
                                                                    const EdgeInsets
                                                                        .only(
                                                                        top: 12,
                                                                        bottom:
                                                                            12,
                                                                        left: 6,
                                                                        right:
                                                                            6),
                                                                child: Text(
                                                                  (e.key +
                                                                          dataStart
                                                                              .value)
                                                                      .toString(),
                                                                  // overflow:
                                                                  //     TextOverflow
                                                                  //         .ellipsis,
                                                                  style: Constant.body3_600(
                                                                          context:
                                                                              context)
                                                                      .copyWith(
                                                                          color:
                                                                              Colors.black),
                                                                )),
                                                          ),
                                                        )),
                                                    Expanded(
                                                      flex: 2,
                                                      child: Container(
                                                        alignment: Alignment
                                                            .centerLeft,
                                                        // width: Constant.getActualX(context: context, x: 171),
                                                        // height: Constant.getActualY(
                                                        //     context: context, y: 64),
                                                        // color: Colors.white,
                                                        child: Padding(
                                                          padding:
                                                              const EdgeInsets
                                                                  .only(
                                                                  top: 12,
                                                                  bottom: 12,
                                                                  left: 6,
                                                                  right: 6),
                                                          child: Row(
                                                            children: [
                                                              Text(
                                                                  e.value.orderNumber ??
                                                                      "",
                                                                  style: Constant.body3_600(
                                                                          context:
                                                                              context)
                                                                      .copyWith(
                                                                          color:
                                                                              Colors.black)),
                                                            ],
                                                          ),
                                                        ),
                                                      ),
                                                    ),
                                                    Expanded(
                                                      flex: 2,
                                                      child: Container(
                                                        alignment: Alignment
                                                            .centerLeft,
                                                        // width: Constant.getActualX(context: context, x: 171),
                                                        // height: Constant.getActualY(
                                                        //     context: context, y: 64),
                                                        // color: Colors.white,
                                                        child: Padding(
                                                          padding:
                                                              const EdgeInsets
                                                                  .only(
                                                                  top: 12,
                                                                  bottom: 12,
                                                                  left: 6,
                                                                  right: 6),
                                                          child: Text(
                                                              e.value.patientName ??
                                                                  "",
                                                              style: Constant
                                                                      .body3_600(
                                                                          context:
                                                                              context)
                                                                  .copyWith(
                                                                      color: Colors
                                                                          .black)),
                                                        ),
                                                      ),
                                                    ),
                                                    Expanded(
                                                      flex: 2,
                                                      child: Container(
                                                        alignment: Alignment
                                                            .centerLeft,
                                                        // width: Constant.getActualX(context: context, x: 171),
                                                        // height: Constant.getActualY(
                                                        //     context: context, y: 64),
                                                        // color: Colors.white,
                                                        child: Padding(
                                                          padding:
                                                              const EdgeInsets
                                                                  .only(
                                                                  top: 12,
                                                                  bottom: 12,
                                                                  left: 6,
                                                                  right: 6),
                                                          child: Text(
                                                              e.value.hp ?? "",
                                                              style: Constant
                                                                      .body3_600(
                                                                          context:
                                                                              context)
                                                                  .copyWith(
                                                                      color: Colors
                                                                          .black)),
                                                        ),
                                                      ),
                                                    ),
                                                    Expanded(
                                                      flex: 3,
                                                      child: Container(
                                                        alignment: Alignment
                                                            .centerLeft,
                                                        // width: Constant.getActualX(context: context, x: 171),
                                                        // height: Constant.getActualY(
                                                        //     context: context, y: 64),
                                                        // color: Constant.grey_200,
                                                        child: Padding(
                                                            padding:
                                                                const EdgeInsets
                                                                    .only(
                                                                    top: 12,
                                                                    bottom: 12,
                                                                    left: 6,
                                                                    right: 6),
                                                            child: Wrap(
                                                              runSpacing: 10,
                                                              spacing: 10,
                                                              children:
                                                                  e.value.tests!
                                                                      .map(
                                                                        (j) => Chip(
                                                                            shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
                                                                            label: Text(
                                                                              j,
                                                                              style: Constant.caption1_600(context: context),
                                                                            )),
                                                                      )
                                                                      .toList(),
                                                            )),
                                                      ),
                                                    ),
                                                    //Status Qr
                                                    Expanded(
                                                      flex: 2,
                                                      child: Container(
                                                        alignment: Alignment
                                                            .centerLeft,
                                                        // width: Constant.getActualX(context: context, x: 171),
                                                        // height: Constant.getActualY(
                                                        //     context: context, y: 64),
                                                        // color: Constant.grey_200,
                                                        child: Padding(
                                                            padding:
                                                                const EdgeInsets
                                                                    .only(
                                                                    top: 12,
                                                                    bottom: 12,
                                                                    left: 6,
                                                                    right: 6),
                                                            child: e.value
                                                                        .isQr ==
                                                                    "Y"
                                                                ? QRStatusChip(
                                                                    status: e
                                                                        .value
                                                                        .statusQr!,
                                                                  )
                                                                : null),
                                                      ),
                                                    ),
                                                    // Status registrasi
                                                    Expanded(
                                                      flex: 2,
                                                      child: Container(
                                                        alignment: Alignment
                                                            .centerLeft,
                                                        // width: Constant.getActualX(context: context, x: 171),
                                                        // height: Constant.getActualY(
                                                        //     context: context, y: 64),
                                                        // color: Constant.grey_200,
                                                        child: Padding(
                                                          padding:
                                                              const EdgeInsets
                                                                  .only(
                                                                  top: 12,
                                                                  bottom: 12,
                                                                  left: 6,
                                                                  right: 6),
                                                          child: e.value
                                                                          .statusPemeriksaan ==
                                                                      'Y' ||
                                                                  e.value.statusPemeriksaan ==
                                                                      'P' ||
                                                                  e.value.statusPemeriksaan ==
                                                                      'R'
                                                              ? Tooltip(
                                                                  message:
                                                                      "Sudah Registrasi",
                                                                  child: Icon(
                                                                    EvaIcons
                                                                        .checkmarkCircle2,
                                                                    color: Colors
                                                                        .green,
                                                                  ),
                                                                )
                                                              : null,
                                                        ),
                                                      ),
                                                    ),
                                                    //Status proses
                                                    Expanded(
                                                      flex: 2,
                                                      child: Container(
                                                        alignment: Alignment
                                                            .centerLeft,
                                                        // width: Constant.getActualX(context: context, x: 171),
                                                        // height: Constant.getActualY(
                                                        //     context: context, y: 64),
                                                        // color: Constant.grey_200,
                                                        child: Padding(
                                                          padding:
                                                              const EdgeInsets
                                                                  .only(
                                                                  top: 12,
                                                                  bottom: 12,
                                                                  left: 6,
                                                                  right: 6),
                                                          child: e.value.statusPemeriksaan ==
                                                                      'P' ||
                                                                  e.value.statusPemeriksaan ==
                                                                      'R'
                                                              ? Tooltip(
                                                                  message:
                                                                      "Sudah Di proses",
                                                                  child: Icon(
                                                                    EvaIcons
                                                                        .checkmarkCircle2,
                                                                    color: Colors
                                                                        .green,
                                                                  ),
                                                                )
                                                              : null,
                                                        ),
                                                      ),
                                                    ),
                                                    //status hasil
                                                    Expanded(
                                                      flex: 2,
                                                      child: Container(
                                                        alignment: Alignment
                                                            .centerLeft,
                                                        // width: Constant.getActualX(context: context, x: 171),
                                                        // height: Constant.getActualY(
                                                        //     context: context, y: 64),
                                                        // color: Constant.grey_200,
                                                        child: Padding(
                                                          padding:
                                                              const EdgeInsets
                                                                  .only(
                                                                  top: 12,
                                                                  bottom: 12,
                                                                  left: 6,
                                                                  right: 6),
                                                          child:
                                                              e.value.statusPemeriksaan ==
                                                                      'R'
                                                                  ? Tooltip(
                                                                      message:
                                                                          "Sudah Ada Hasil",
                                                                      child:
                                                                          Icon(
                                                                        EvaIcons
                                                                            .checkmarkCircle2,
                                                                        color: Colors
                                                                            .green,
                                                                      ),
                                                                    )
                                                                  : null,
                                                        ),
                                                      ),
                                                    ),
                                                    Expanded(
                                                      flex: 1,
                                                      child: Container(
                                                        alignment:
                                                            Alignment.center,
                                                        // width: Constant.getActualX(context: context, x: 171),
                                                        // height: Constant.getActualY(
                                                        //     context: context, y: 64),
                                                        // color: Constant.grey_200,
                                                        child:
                                                            (e.value.status !=
                                                                    'N')
                                                                ? Tooltip(
                                                                    message:
                                                                        "${e.value.orderNumber} sudah dibuat surat jalan",
                                                                    child: Icon(
                                                                      EvaIcons
                                                                          .lockOutline,
                                                                      color: Colors
                                                                          .red,
                                                                    ),
                                                                  )
                                                                : Wrap(
                                                                    children: [
                                                                      Tooltip(
                                                                        message:
                                                                            "Edit order ${e.value.orderNumber}",
                                                                        child: IconButton(
                                                                            onPressed: () {
                                                                              ref.read(dialogOrderActionProvider.notifier).state = 'edit';
                                                                              ref.read(selectedPacketEditProvider.notifier).state = e.value.paket ?? [];
                                                                              ref.read(currentPacketProvider.notifier).state = e.value.paketDetail ?? [];
                                                                              List<String> tests = List.empty(growable: true);
                                                                              for (var element in listTest.value) {
                                                                                element.items?.forEach((i) {
                                                                                  bool cek = e.value.testsID!.contains(i.testID);
                                                                                  if (cek) {
                                                                                    var ts = "${element.tabId}|${i.testID}";
                                                                                    tests.add(ts);
                                                                                  }
                                                                                });
                                                                              }
                                                                              List<Specimens> sp = List.empty(growable: true);
                                                                              e.value.sample?.forEach((element) {
                                                                                List splitted = element.split('|');
                                                                                if (splitted.length > 3) {
                                                                                  sp.add(Specimens(detailID: splitted[0], id: splitted[1], name: splitted[2], amount: splitted[3], ctr: TextEditingController(text: splitted[3])));
                                                                                }
                                                                              });
                                                                              List<Bahan> bhn = List.empty(growable: true);
                                                                              e.value.bahan?.forEach((element) {
                                                                                List splitted = element.split('|');
                                                                                if (splitted.length > 3) {
                                                                                  bhn.add(Bahan(detailID: splitted[0], id: splitted[1], name: splitted[2], amount: splitted[3], ctr: TextEditingController(text: splitted[3])));
                                                                                }
                                                                              });
                                                                              ref.read(selectedTestEditProvider.notifier).state = tests;
                                                                              ref.read(selectedtestData.notifier).state = e.value.testDetail ?? [];
                                                                              RegistrationModel a = RegistrationModel(
                                                                                  orderID: e.value.orderId,
                                                                                  specimens: sp,
                                                                                  bahan: bhn,
                                                                                  patientId: e.value.patientId,
                                                                                  patientData: PatientData(
                                                                                    noRM: e.value.noRM,
                                                                                    address: e.value.address,
                                                                                    diagnosis: e.value.diagnosis,
                                                                                    dob: e.value.dob,
                                                                                    gender: e.value.sexID,
                                                                                    hp: e.value.hp,
                                                                                    name: e.value.patientName,
                                                                                    nik: e.value.NIK,
                                                                                    nip: e.value.NIP,
                                                                                    note: e.value.note,
                                                                                    prefix: e.value.prefix,
                                                                                    saluation: e.value.title,
                                                                                    suffix: e.value.suffix,
                                                                                    withoutNIK: 'N',
                                                                                    jabatan: e.value.jabatan,
                                                                                    kedudukan: e.value.kedudukan,
                                                                                    lokasi: e.value.lokasi,
                                                                                    pekerjaan: e.value.pekerjaan,
                                                                                  ));
                                                                              ref.read(EditDataProvider.notifier).state = a;
                                                                              showDialog(context: context, barrierDismissible: false, builder: ((context) => DialogEdit())).then((value) => refresh());
                                                                            },
                                                                            icon: Icon(
                                                                              EvaIcons.edit2,
                                                                              color: Constant.textGrey,
                                                                            )),
                                                                      ),
                                                                      Tooltip(
                                                                        message:
                                                                            "Batalkan order",
                                                                        child: IconButton(
                                                                            onPressed: () {
                                                                              showDialog<String>(
                                                                                context: context,
                                                                                barrierDismissible: !cancelLoading.value,
                                                                                builder: (BuildContext context) => DialogCancelOrder(cancel: cancelOrder, orderID: e.value.orderId ?? "0", loading: cancelLoading.value, orderNumber: e.value.orderNumber ?? "", name: e.value.patientName ?? ""),
                                                                              ).whenComplete(() {
                                                                                // onRefresh();
                                                                              });
                                                                            },
                                                                            icon: Icon(
                                                                              EvaIcons.closeCircle,
                                                                              color: Constant.textGrey,
                                                                            )),
                                                                      )
                                                                    ],
                                                                  ),
                                                      ),
                                                    ),
                                                  ],
                                                ),
                                              ),
                                              Divider()
                                            ],
                                          ))
                                      .toList(),
                                ),
                        ),
                        Divider(
                          indent: 0,
                          endIndent: 0,
                          height: 0,
                          thickness: 1,
                        ),
                        Container(
                          color: Colors.white,
                          height: Constant.getActualY(context: context, y: 60),
                          child: Row(
                            children: [
                              Spacer(),
                              Text(
                                "Rows per page :",
                                style: Constant.body3_400(context: context),
                              ),
                              SizedBox(
                                width: Constant.getActualX(
                                    context: context, x: 24),
                              ),
                              DropdownButton<int>(
                                  value: rowsPerPage.value,
                                  dropdownColor: Colors.white,
                                  elevation: 0,
                                  style: Constant.body3_400(context: context)
                                      .copyWith(color: Colors.black),
                                  underline: Container(),
                                  icon: Icon(Icons.keyboard_arrow_down_rounded),
                                  focusColor: Colors.white,
                                  items: [
                                    DropdownMenuItem(
                                      value: 10,
                                      child: Text("10"),
                                    ),
                                    DropdownMenuItem(
                                      value: 15,
                                      child: Text("15"),
                                    ),
                                    DropdownMenuItem(
                                      value: 20,
                                      child: Text("20"),
                                    ),
                                    DropdownMenuItem(
                                      value: 30,
                                      child: Text("30"),
                                    ),
                                    DropdownMenuItem(
                                      value: 40,
                                      child: Text("40"),
                                    ),
                                    DropdownMenuItem(
                                      value: 50,
                                      child: Text("50"),
                                    ),
                                  ],
                                  onChanged: (value) {
                                    rowsPerPage.value = value!;
                                  }),
                              SizedBox(
                                width: Constant.getActualX(
                                    context: context, x: 24),
                              ),
                              Text(
                                "${dataStart.value} - ${dataEnd.value} of ${orderList.value.total}",
                                style: Constant.body3_400(context: context),
                              ),
                              SizedBox(
                                width: Constant.getActualX(
                                    context: context, x: 24),
                              ),
                              IconButton(
                                  iconSize: 20,
                                  onPressed: currPage.value > 1
                                      ? () {
                                          currPage.value = currPage.value - 1;
                                        }
                                      : null,
                                  icon: Icon(Icons.arrow_back_ios_new_rounded)),
                              SizedBox(
                                width: Constant.getActualX(
                                    context: context, x: 10),
                              ),
                              IconButton(
                                  iconSize: 20,
                                  onPressed: currPage.value <
                                          orderList.value.totalPage!.toInt()
                                      ? () {
                                          currPage.value = currPage.value + 1;
                                        }
                                      : null,
                                  icon: Icon(Icons.arrow_forward_ios_outlined)),
                              SizedBox(
                                width: Constant.getActualX(
                                    context: context, x: 24),
                              )
                            ],
                          ),
                        )
                      ],
                    ),
                  )
                ],
              ),
            ),
          ],
        ),
      ),
    ]));
  }
}
