import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:hallo_doctor_doctor_app/app/styles/styles.dart';
import '../controllers/withdraw_method_controller.dart';

class AddBankPage extends StatelessWidget {
  final _formKey = GlobalKey<FormState>();
  final TextEditingController _nameController = TextEditingController();
  final TextEditingController _accountNumberController =
      TextEditingController();
  final TextEditingController _accountHolderController =
      TextEditingController();
  final TextEditingController _bankNameController = TextEditingController();
  final TextEditingController _branchCodeController = TextEditingController();
  final TextEditingController _swiftCodeController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          'Add Bank Account'.tr,
          style: Styles.appBarTextStyle,
        ),
        centerTitle: true,
        backgroundColor: Colors.white,
      ),
      body: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.all(20.0),
          child: Form(
            key: _formKey,
            child: Column(
              children: [
                TextFormField(
                  controller: _nameController,
                  decoration: InputDecoration(
                    labelText: 'Account Name'.tr,
                    hintText: 'Enter name for this account'.tr,
                  ),
                  validator: (value) {
                    if (value == null || value.isEmpty) {
                      return 'Please enter account name'.tr;
                    }
                    return null;
                  },
                ),
                SizedBox(height: 20),
                TextFormField(
                  controller: _accountNumberController,
                  decoration: InputDecoration(
                    labelText: 'Account Number'.tr,
                    hintText: 'Enter your bank account number'.tr,
                  ),
                  validator: (value) {
                    if (value == null || value.isEmpty) {
                      return 'Please enter account number'.tr;
                    }
                    return null;
                  },
                  keyboardType: TextInputType.number,
                ),
                SizedBox(height: 20),
                TextFormField(
                  controller: _accountHolderController,
                  decoration: InputDecoration(
                    labelText: 'Account Holder Name'.tr,
                    hintText: 'Enter account holder name'.tr,
                  ),
                  validator: (value) {
                    if (value == null || value.isEmpty) {
                      return 'Please enter account holder name'.tr;
                    }
                    return null;
                  },
                ),
                SizedBox(height: 20),
                TextFormField(
                  controller: _bankNameController,
                  decoration: InputDecoration(
                    labelText: 'Bank Name'.tr,
                    hintText: 'Enter your bank name'.tr,
                  ),
                  validator: (value) {
                    if (value == null || value.isEmpty) {
                      return 'Please enter bank name'.tr;
                    }
                    return null;
                  },
                ),
                SizedBox(height: 20),
                TextFormField(
                  controller: _branchCodeController,
                  decoration: InputDecoration(
                    labelText: 'Branch Code'.tr,
                    hintText: 'Enter branch code (optional)'.tr,
                  ),
                ),
                SizedBox(height: 20),
                TextFormField(
                  controller: _swiftCodeController,
                  decoration: InputDecoration(
                    labelText: 'SWIFT/BIC Code'.tr,
                    hintText: 'Enter SWIFT/BIC code (optional)'.tr,
                  ),
                ),
                SizedBox(height: 40),
                Container(
                  width: double.infinity,
                  child: ElevatedButton(
                    onPressed: () async {
                      if (_formKey.currentState!.validate()) {
                        final controller = Get.find<WithdrawMethodController>();
                        controller.addBankAccount(
                          name: _nameController.text,
                          accountNumber: _accountNumberController.text,
                          accountHolderName: _accountHolderController.text,
                          bankName: _bankNameController.text,
                          branchCode: _branchCodeController.text.isEmpty
                              ? null
                              : _branchCodeController.text,
                          swiftCode: _swiftCodeController.text.isEmpty
                              ? null
                              : _swiftCodeController.text,
                        );
                      }
                    },
                    child: Text(
                      'Add Bank Account'.tr,
                      style: GoogleFonts.nunito(
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
