import 'package:siro_driver/constant/style.dart';
import 'package:siro_driver/views/widgets/elevated_btn.dart';
import 'package:siro_driver/views/widgets/my_scafold.dart';
import 'package:siro_driver/views/widgets/my_textField.dart';
import 'package:siro_driver/views/widgets/mycircular.dart';
import 'package:siro_driver/views/widgets/mydialoug.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:siro_driver/constant/currency.dart';

import '../../../controller/home/payment/captain_wallet_controller.dart';

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

  @override
  Widget build(BuildContext context) {
    Get.put(CaptainWalletController());
    return MyScafolld(
        title: "Transfer budget".tr,
        body: [
          GetBuilder<CaptainWalletController>(
              builder: (captainWalletController) {
            return Padding(
              padding: const EdgeInsets.all(8.0),
              child: Container(
                decoration: AppStyle.boxDecoration1,
                height: Get.height * .7,
                width: double.infinity,
                child: Form(
                    key: captainWalletController.formKeyTransfer,
                    child: Column(
                      children: [
                        const SizedBox(
                          height: 20,
                        ),
                        MyTextForm(
                            controller: captainWalletController
                                .newDriverPhoneController,
                            label: 'phone number of driver'.tr,
                            hint: 'phone number of driver',
                            type: TextInputType.phone),
                        MyTextForm(
                            controller: captainWalletController
                                .amountFromBudgetController,
                            label: 'insert amount'.tr,
                            hint:
                                '${'You have in account'.tr} ${captainWalletController.totalAmountVisa}',
                            type: TextInputType.number),
                        captainWalletController.isNewTransfer
                            ? const MyCircularProgressIndicator()
                            : captainWalletController
                                    .amountToNewDriverMap.isEmpty
                                ? MyElevatedButton(
                                    title: 'Next'.tr,
                                    onPressed: () async {
                                      await captainWalletController
                                          .detectNewDriverFromMyBudget();
                                    })
                                : const SizedBox(),
                        captainWalletController.amountToNewDriverMap.isNotEmpty
                            ? Padding(
                                padding: const EdgeInsets.all(8.0),
                                child: Column(
                                  children: [
                                    Container(
                                      width: double.maxFinite,
                                      decoration: AppStyle.boxDecoration1,
                                      child: Text(
                                        'Name :'.tr +
                                            captainWalletController
                                                .amountToNewDriverMap[0]['name']
                                                .toString(),
                                        textAlign: TextAlign.center,
                                        style: AppStyle.title,
                                      ),
                                    ),
                                    const SizedBox(
                                      height: 5,
                                    ),
                                    Container(
                                      width: double.maxFinite,
                                      decoration: AppStyle.boxDecoration1,
                                      child: Text(
                                        "${"NationalID".tr} ${captainWalletController.amountToNewDriverMap[0]['national_number']}",
                                        style: AppStyle.title,
                                        textAlign: TextAlign.center,
                                      ),
                                    ),
                                    const SizedBox(
                                      height: 5,
                                    ),
                                    Container(
                                      width: double.maxFinite,
                                      decoration: AppStyle.boxDecoration1,
                                      child: Text(
                                        "${"amount".tr} ${captainWalletController.amountFromBudgetController.text} ${CurrencyHelper.currency}",
                                        style: AppStyle.title,
                                        textAlign: TextAlign.center,
                                      ),
                                    ),
                                    const SizedBox(
                                      height: 5,
                                    ),
                                    Container(
                                      width: double.maxFinite,
                                      decoration: AppStyle.boxDecoration1,
                                      child: Text(
                                        "${"Transfer Fee".tr}: ${captainWalletController.transferFee} ${CurrencyHelper.currency}",
                                        style: AppStyle.title,
                                        textAlign: TextAlign.center,
                                      ),
                                    ),
                                    const SizedBox(
                                      height: 15,
                                    ),
                                    captainWalletController
                                            .amountToNewDriverMap.isNotEmpty
                                        ? MyElevatedButton(
                                            title: 'Transfer'.tr,
                                            onPressed: () async {
                                              double amount = double.tryParse(captainWalletController.amountFromBudgetController.text) ?? 0.0;
                                              double totalAmount = double.tryParse(captainWalletController.totalAmountVisa) ?? 0.0;
                                              double fee = captainWalletController.transferFee;
                                              double minTransfer = captainWalletController.minTransferAmount;

                                              if (amount < minTransfer) {
                                                MyDialog().getDialog(
                                                    "Error".tr,
                                                    "${"Minimum transfer amount is".tr} $minTransfer ${CurrencyHelper.currency}", () {
                                                  Get.back();
                                                });
                                              } else if (amount > (totalAmount - fee)) {
                                                MyDialog().getDialog(
                                                    "Insufficient Balance".tr,
                                                    "${"You must leave at least".tr} $fee ${CurrencyHelper.currency} ${"for transfer fees".tr}", () {
                                                  Get.back();
                                                });
                                              } else {
                                                await captainWalletController
                                                    .addTransferDriversWallet(
                                                  'TransferFrom',
                                                  'TransferTo',
                                                );
                                              }
                                            })
                                        : const SizedBox()
                                  ],
                                ),
                              )
                            : const SizedBox()
                      ],
                    )),
              ),
            );
          }),
        ],
        isleading: true);
  }
}
