import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:mitra_corporate/provider/auth_provider.dart';
import 'package:mitra_corporate/screen/surat_jalan/send_qr_code_provider.dart';

import '../../app/constant.dart';
import '../../widgets/custom_snackbar_widget.dart';

class DialogSendQr extends HookConsumerWidget {
  const DialogSendQr(
      {super.key, required this.arrOrderID, required this.orderNumber});
  final List<String> arrOrderID;
  final String orderNumber;

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final auth = ref.watch(authProvider);

    final rb = useState("Y");

    sendQrCode() {
      ref
          .read(SendQrCodeProvider.notifier)
          .senQrCode(token: auth?.token ?? "", arrOrderID: arrOrderID);
    }

    ref.listen(
      SendQrCodeProvider,
      (pref, next) {
        if (next is SendQrCodeStateInit) {
        } else if (next is SendQrCodeStateLoading) {
        } else if (next is SendQrCodeStateError) {
          SanckbarWidget(
              context,
              "Gagal Menyimpan Data${jsonEncode(next.message)}",
              snackbarType.error);
          Constant.autoLogout(context: context, msg: next.message);
        } else if (next is SendQrCodeStateDone) {
          Navigator.pop(context);
          // print(jsonEncode(next.model));

          if (next.model != 'OK') {
            SanckbarWidget(context, "Gagal Menyimpan Data", snackbarType.error);
          } else {
            SanckbarWidget(
                context,
                "Berhasil Menyimpan Data, Qr Code Akan Dikirimkan",
                snackbarType.success);
          }
        }
      },
    );

    return AlertDialog(
      contentPadding: EdgeInsets.symmetric(
          horizontal: Constant.getActualX(context: context, x: 24),
          vertical: Constant.getActualY(context: context, y: 24)),
      shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.all(Radius.circular(12))),
      title: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            'Kirim QR Code',
            textAlign: TextAlign.center,
            style: Constant.h4_600(context: context)
                .copyWith(fontWeight: FontWeight.w700),
          ),
          IconButton(
              onPressed: () {
                Navigator.pop(context);
              },
              icon: Icon(Icons.close_rounded))
        ],
      ),
      content: Container(
          constraints: BoxConstraints(
            minHeight: Constant.getActualY(context: context, y: 100),
            maxHeight: Constant.getActualY(context: context, y: 362),
          ),
          height: Constant.getActualY(context: context, y: 320),
          child: Column(
            // crossAxisAlignment: CrossAxisAlignment.center,
            // mainAxisAlignment: MainAxisAlignment.center,
            children: [
              SizedBox(
                height: Constant.getActualY(context: context, y: 30),
              ),
              Text(
                "Surat Jalan Berhasil Dibuat",
                style: Constant.body1_600(context: context)
                    .copyWith(fontWeight: FontWeight.w600),
              ),
              SizedBox(
                height: Constant.getActualY(context: context, y: 12),
              ),
              Container(
                width: Constant.getActualX(context: context, x: 263),
                padding: EdgeInsets.symmetric(
                    horizontal: Constant.getActualX(context: context, x: 24),
                    vertical: Constant.getActualY(context: context, y: 12)),
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(12),
                    border: Border.all(color: Colors.grey.shade300)),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      "Nomor Surat Jalan",
                      style: Constant.body3_500(context: context)
                          .copyWith(color: Constant.textGrey),
                    ),
                    Text(
                      orderNumber,
                      style: Constant.body3_500(context: context)
                          .copyWith(fontWeight: FontWeight.w600),
                    )
                  ],
                ),
              ),
              SizedBox(
                height: Constant.getActualY(context: context, y: 12),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Container(
                    // width: Constant.getActualX(context: context, x: 48),
                    child: RadioMenuButton<String>(
                        style: ButtonStyle(
                            backgroundColor: WidgetStateColor.resolveWith(
                              (states) => Colors.white,
                            ),
                            shadowColor: WidgetStateColor.resolveWith(
                                (states) => Colors.white),
                            overlayColor: WidgetStateColor.resolveWith(
                                (states) => Colors.white)),
                        value: 'Y',
                        groupValue: rb.value,
                        onChanged: (value) {
                          if (rb.value == value) {
                            rb.value = 'N';
                          } else {
                            rb.value = value!;
                          }
                        },
                        child: Text(
                          "Kirim Whatsapp QR Code",
                          style: Constant.body3_400(context: context),
                        )),
                  ),
                  // Text(
                  //   "Kirim Whatsapp QR Code",
                  //   style: Constant.body3_400(context: context),
                  // )
                ],
              ),
              SizedBox(
                height: Constant.getActualY(context: context, y: 37),
              ),
              ElevatedButton(
                  onPressed: rb.value == 'Y'
                      ? () {
                          sendQrCode();
                        }
                      : null,
                  style: ElevatedButton.styleFrom(
                      minimumSize: Size(
                          Constant.getActualX(context: context, x: 328),
                          Constant.getActualY(context: context, y: 64)),
                      elevation: 5,
                      backgroundColor: Constant.primaryRed,
                      shadowColor: Constant.primaryRed,
                      shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(8))),
                  child: Container(
                    margin: EdgeInsets.symmetric(
                        vertical: Constant.getActualY(context: context, y: 11)),
                    child: Text(
                      "Kirim QR Code",
                      style: Constant.button_large(context: context)
                          .copyWith(color: Colors.white),
                    ),
                  )),
              // SizedBox(
              //   height: Constant.getActualY(context: context, y: 12),
              // ),
              // Row(
              //   children: [
              //     Text(
              //       "Belum menerima QR Code ?",
              //       style: Constant.caption1_600(context: context)
              //           .copyWith(color: Constant.textGrey),
              //     ),
              //     TextButton(
              //         onPressed: rb.value == 'Y' ? () {} : null,
              //         child: Text("Kirim Ulang",
              //             style: Constant.caption1_600(context: context)
              //                 .copyWith()))
              //   ],
              // )
            ],
          )),
    );
  }
}
