import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:siro_admin/controller/functions/crud.dart';
import '../../../constant/links.dart';
import '../../../constant/style.dart';
import '../../../controller/admin/captain_admin_controller.dart';
import '../../widgets/elevated_btn.dart';
import '../../widgets/my_scafold.dart';
import '../../widgets/my_textField.dart';

class FormCaptain extends StatefulWidget {
  const FormCaptain({super.key});

  @override
  State<FormCaptain> createState() => _FormCaptainState();
}

class _FormCaptainState extends State<FormCaptain> {
  final CaptainAdminController controller = Get.find();
  final _formKey = GlobalKey<FormState>();

  late TextEditingController firstNameController;
  late TextEditingController lastNameController;
  late TextEditingController phoneController;

  bool isEditMode = false;
  Map<String, dynamic>? captainData;

  @override
  void initState() {
    super.initState();
    if (Get.arguments != null && Get.arguments['isEditMode'] == true) {
      isEditMode = true;
      captainData = Get.arguments['captainData'];
      firstNameController =
          TextEditingController(text: captainData?['first_name'] ?? '');
      lastNameController =
          TextEditingController(text: captainData?['last_name'] ?? '');
      phoneController =
          TextEditingController(text: captainData?['phone'] ?? '');
    } else {
      firstNameController = TextEditingController();
      lastNameController = TextEditingController();
      phoneController = TextEditingController();
    }
  }

  @override
  void dispose() {
    firstNameController.dispose();
    lastNameController.dispose();
    phoneController.dispose();
    super.dispose();
  }

  Future<void> _saveForm() async {
    if (_formKey.currentState!.validate()) {
      // Create a map of the updated data
      Map<String, dynamic> updatedData = {
        'id': captainData?['id'], // Important for the WHERE clause in SQL
        // 'first_name': firstNameController.text,
        // 'last_name': lastNameController.text,
        'phone': phoneController.text,
      };
      var res = await CRUD()
          .post(link: AppLink.updateDriverFromAdmin, payload: updatedData);

      if (res != 'failure') {
        print('Updating data: $updatedData');

        Get.back(); // Go back after saving
        Get.snackbar('Success', 'Captain data updated successfully!');
      }
      // controller.updateCaptain(updatedData);
    }
  }

  @override
  Widget build(BuildContext context) {
    return MyScafolld(
      title: 'Edit Captain'.tr,
      isleading: true,
      body: [
        Padding(
          padding: const EdgeInsets.all(16.0),
          child: Form(
            key: _formKey,
            child: ListView(
              children: [
                MyTextForm(
                  controller: firstNameController,
                  label: 'First Name'.tr,
                  hint: 'Enter first name'.tr,
                  type: TextInputType.name,
                ),
                const SizedBox(height: 16),
                MyTextForm(
                  controller: lastNameController,
                  label: 'Last Name'.tr,
                  hint: 'Enter last name'.tr,
                  type: TextInputType.name,
                ),
                const SizedBox(height: 16),
                MyTextForm(
                  controller: phoneController,
                  label: 'Phone Number'.tr,
                  hint: 'Enter phone number'.tr,
                  type: TextInputType.phone,
                ),
                const SizedBox(height: 32),
                MyElevatedButton(
                  title: 'Update'.tr,
                  onPressed: _saveForm,
                ),
              ],
            ),
          ),
        ),
      ],
    );
  }
}
