import 'package:flutter/material.dart';

import '../model/dropdown_option.dart';
import '../model/enquiry_model.dart';
import '../widgets/searchable_dropdown.dart';

class EnquiryForm extends StatefulWidget {
  final Function(EnquiryModel) onSubmit;
  final List<DropdownOption> partners;
  final List<DropdownOption> insurers;
  final List<DropdownOption> assignees;

  const EnquiryForm({
    super.key,
    required this.onSubmit,
    required this.partners,
    required this.insurers,
    required this.assignees,
  });

  @override
  State<EnquiryForm> createState() => _EnquiryFormState();
}

class _EnquiryFormState extends State<EnquiryForm> {
  final _formKey = GlobalKey<FormState>();
  String? _selectedPartner;
  String? _selectedInsurer;
  String? _selectedAssignee;
  final _insuredNameController = TextEditingController();
  final _regNoController = TextEditingController();

  @override
  void dispose() {
    _insuredNameController.dispose();
    _regNoController.dispose();
    super.dispose();
  }

  void _submit() {
    if (_formKey.currentState!.validate()) {
      final enquiry = EnquiryModel(
        receivedDate: DateTime.now(),
        partner: _selectedPartner!,
        assignedTo: _selectedAssignee!,
        broker: 'Nhance',
        insurer: _selectedInsurer!,
        insuredName: _insuredNameController.text,
        vehicleNo: _regNoController.text,
        vehicleType: '',
        email: '',
        mobile: '',
        remarks: '',
        status: 'awaiting',
      );

      widget.onSubmit(enquiry);

      // Reset form
      _formKey.currentState!.reset();
      _insuredNameController.clear();
      _regNoController.clear();
      setState(() {
        _selectedPartner = null;
        _selectedInsurer = null;
        _selectedAssignee = null;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return SelectionArea(
        child:Container(
      margin: const EdgeInsets.all(20),
      padding: const EdgeInsets.all(20),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(6),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.1),
            blurRadius: 4,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Form(
        key: _formKey,
        child: Wrap(
          spacing: 12,
          runSpacing: 12,
          alignment: WrapAlignment.start,
          children: [
            SizedBox(
              width: 180,
              child: SearchableDropdown(
                label: 'Partner',
                value: _selectedPartner,
                items: widget.partners,
                onChanged: (value) => setState(() => _selectedPartner = value),
                validator: (value) => value == null ? 'Required' : null,
              ),
            ),
            SizedBox(
              width: 180,
              child: SearchableDropdown(
                label: 'Insurer',
                value: _selectedInsurer,
                items: widget.insurers,
                onChanged: (value) => setState(() => _selectedInsurer = value),
                validator: (value) => value == null ? 'Required' : null,
              ),
            ),
            SizedBox(
              width: 180,
              child: TextFormField(
                controller: _insuredNameController,
                decoration: const InputDecoration(
                  labelText: 'Insured Name',
                  border: OutlineInputBorder(),
                  contentPadding: EdgeInsets.symmetric(
                    horizontal: 10,
                    vertical: 8,
                  ),
                ),
                style: const TextStyle(fontSize: 13),
                validator: (value) =>
                    value?.isEmpty ?? true ? 'Required' : null,
              ),
            ),
            SizedBox(
              width: 180,
              child: TextFormField(
                controller: _regNoController,
                decoration: const InputDecoration(
                  labelText: 'Reg No',
                  border: OutlineInputBorder(),
                  contentPadding: EdgeInsets.symmetric(
                    horizontal: 10,
                    vertical: 8,
                  ),
                ),
                style: const TextStyle(fontSize: 13),
                validator: (value) =>
                    value?.isEmpty ?? true ? 'Required' : null,
              ),
            ),
            SizedBox(
              width: 180,
              child: SearchableDropdown(
                label: 'Assigned To',
                value: _selectedAssignee,
                items: widget.assignees,
                onChanged: (value) => setState(() => _selectedAssignee = value),
                validator: (value) => value == null ? 'Required' : null,
              ),
            ),
            ElevatedButton(
              onPressed: _submit,
              style: ElevatedButton.styleFrom(
                backgroundColor: const Color(0xFF4a90e2),
                foregroundColor: Colors.white,
                padding: const EdgeInsets.symmetric(
                  horizontal: 24,
                  vertical: 16,
                ),
              ),
              child: const Text('Create', style: TextStyle(fontSize: 13)),
            ),
          ],
        ),
      ),
    ));
  }
}
