import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:supplier_new/common/settings.dart';


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

  @override
  State<FleetEmployees> createState() => _FleetEmployeesState();
}

class _FleetEmployeesState extends State<FleetEmployees> {
  final _formKey = GlobalKey<FormState>();

  // Controllers
  final _nameCtrl = TextEditingController();
  final _mobileCtrl = TextEditingController();
  final _altMobileCtrl = TextEditingController();

  // Dropdowns
  final List<String> licenseNumbers = [
    "UP3220050012345",
    "UP3220050012355",
    "UP3220050012365",
    "UP3220050012375",
  ];
  String? selectedLicense;

  final List<String> yearOptions = ["1", "2", "3", "4", "5"];
  String? selectedExperience;

  // Data bucket
  final List<Map<String, dynamic>> _drivers = [];

  // Validators
  String? _required(String? v, {String field = "This field"}) {
    if (v == null || v.trim().isEmpty) return "$field is required";
    return null;
  }

  String? _validatePhone(String? v, {String label = "Phone Number"}) {
    if (v == null || v.trim().isEmpty) return "$label is required";
    final digits = v.replaceAll(RegExp(r'\D'), '');
    if (digits.length != 10) return "Enter a 10-digit $label";
    if (!RegExp(r'^[6-9]\d{9}$').hasMatch(digits)) {
      return "$label must start with 6/7/8/9";
    }
    return null;
  }

  @override
  void dispose() {
    _nameCtrl.dispose();
    _mobileCtrl.dispose();
    _altMobileCtrl.dispose();
    super.dispose();
  }

  Map<String, dynamic> _buildPayload() => {
    "driver_name": _nameCtrl.text.trim(),
    "license_number": selectedLicense,
    "experience_years": selectedExperience,
    "phone": _mobileCtrl.text.trim(),
    "alt_phone": _altMobileCtrl.text.trim(),
  };

  void _clearForm() {
    _nameCtrl.clear();
    _mobileCtrl.clear();
    _altMobileCtrl.clear();
    selectedLicense = null;
    selectedExperience = null;
    setState(() {});
  }

  void _addDriver() async{
    final ok = _formKey.currentState?.validate() ?? false;
    setState(() {}); // ensure error texts render
    if (!ok ||
        selectedLicense == null ||
        selectedExperience == null ||
        selectedExperience!.isEmpty) {
      if (selectedExperience == null) {
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(content: Text("Please select License & Experience")),
        );
      }
      return;
    }
    var payload = new Map<String, dynamic>();
    payload["Name"] = _nameCtrl.text.toString();
    payload["license_number"] =selectedLicense.toString();
    payload["address"] = AppSettings.userAddress;
    payload["supplier_name"] = AppSettings.userName;
    payload["phone"] = _mobileCtrl.text.toString();
    payload["alternativeContactNumber"] ='';
    payload["years_of_experience"] =selectedExperience.toString();
    payload["status"] = 'string';



    bool tankStatus = await AppSettings.addDrivers(payload);

    try {
      if (tankStatus) {
        AppSettings.longSuccessToast("Tanker Created Successfully");
        _nameCtrl.text = '';
        Navigator.pop(context,true);

      }
      else {
        AppSettings.longFailedToast("Tanker Creation failed");
      }
    } catch (exception) {
      print(exception);
    }
    _clearForm();
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text("Driver added (${_drivers.length})")),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: Colors.white,
        surfaceTintColor: Colors.transparent,
        elevation: 0,
        scrolledUnderElevation: 0,
        title: const Text("Complete Profile"),
        actions: [
          Padding(
            padding: const EdgeInsets.fromLTRB(10, 10, 0, 10),
            child: IconButton(
              splashRadius: 20,
              padding: EdgeInsets.zero,
              icon: const Image(image: AssetImage('images/calendar_appbar.png'), width: 22, height: 22),
              onPressed: () {},
            ),
          ),
          Padding(
            padding: const EdgeInsets.fromLTRB(0, 10, 10, 10),
            child: IconButton(
              splashRadius: 20,
              padding: EdgeInsets.zero,
              icon: Image.asset('images/notification_appbar.png', width: 22, height: 22),
              onPressed: () {},
            ),
          ),
        ],
      ),
      body: SafeArea(
        child: Form(
          key: _formKey,
          child: ListView(
            padding: const EdgeInsets.fromLTRB(20, 10, 20, 24),
            children: [
              // Step indicator
              Text(
                "Step 1/5",
                style: fontTextStyle(16, const Color(0xFFC3C4C4), FontWeight.w500),
              ),
              const SizedBox(height: 16),

              // Header block
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text("EMPLOYEES", style: fontTextStyle(20, const Color(0xFF515253), FontWeight.w600)),
                  const SizedBox(height: 8),
                  Container(
                    width: 24,
                    height: 24,
                    decoration: const BoxDecoration(
                      image: DecorationImage(image: AssetImage('images/manage-users.png'), fit: BoxFit.contain),
                    ),
                  ),
                ],
              ),
              const SizedBox(height: 6),
              Text(
                "Details about your water tanker fleet",
                style: fontTextStyle(14, const Color(0xFF939495), FontWeight.w500),
              ),
              const SizedBox(height: 16),

              // Section header (just the bar)
              _SectionHeaderBar(
                title: "DRIVER #1",
                icon: Image.asset('images/arrow-up.png', width: 16, height: 16),
                radius: 20,
              ),
              const SizedBox(height: 12),

              // === Fields
              _LabeledField(
                label: "Driver Name *",
                child: TextFormField(
                  controller: _nameCtrl,
                  validator: (v) => _required(v, field: "Driver Name"),
                  decoration: const InputDecoration(
                    hintText: "Full Name",
                    border: OutlineInputBorder(),
                    isDense: true,
                  ),
                  textInputAction: TextInputAction.next,
                ),
              ),

              _LabeledField(
                label: "Driver License Number *",
                child: DropdownButtonFormField<String>(
                  value: selectedLicense,
                  items: licenseNumbers
                      .map((t) => DropdownMenuItem(value: t, child: Text(t)))
                      .toList(),
                  onChanged: (v) => setState(() => selectedLicense = v),
                  validator: (v) => v == null || v.isEmpty ? "Driver License required" : null,
                  isExpanded: true,
                  alignment: Alignment.centerLeft,
                  hint: Text(
                    "Select License Number",
                    style: fontTextStyle(14, const Color(0xFF939495), FontWeight.w400),
                  ),
                  icon: Image.asset('images/downarrow.png', width: 16, height: 16),
                  decoration: const InputDecoration(
                    border: OutlineInputBorder(),
                    isDense: false,
                    contentPadding: EdgeInsets.symmetric(horizontal: 12, vertical: 14),
                  ),
                ),
              ),

              _LabeledField(
                label: "Years of Experience *",
                child: DropdownButtonFormField<String>(
                  value: selectedExperience,
                  items: yearOptions
                      .map((t) => DropdownMenuItem(value: t, child: Text(t)))
                      .toList(),
                  onChanged: (v) => setState(() => selectedExperience = v),
                  validator: (v) => v == null || v.isEmpty ? "Experience is required" : null,
                  isExpanded: true,
                  alignment: Alignment.centerLeft,
                  hint: Text(
                    "Years",
                    style: fontTextStyle(14, const Color(0xFF939495), FontWeight.w400),
                  ),
                  icon: Image.asset('images/downarrow.png', width: 16, height: 16),
                  decoration: const InputDecoration(
                    border: OutlineInputBorder(),
                    isDense: false,
                    contentPadding: EdgeInsets.symmetric(horizontal: 12, vertical: 14),
                  ),
                ),
              ),

              _LabeledField(
                label: "Phone Number *",
                child: TextFormField(
                  controller: _mobileCtrl,
                  validator: (v) => _validatePhone(v, label: "Phone Number"),
                  keyboardType: TextInputType.phone,
                  inputFormatters: [
                    FilteringTextInputFormatter.digitsOnly,
                    LengthLimitingTextInputFormatter(10),
                  ],
                  decoration: InputDecoration(
                    hintText: "Mobile Number",
                    hintStyle: fontTextStyle(14, const Color(0xFF939495), FontWeight.w400),
                    border: const OutlineInputBorder(),
                    isDense: true,
                  ),
                  textInputAction: TextInputAction.next,
                ),
              ),

              _LabeledField(
                label: "Alternate Phone Number",
                child: TextFormField(
                  controller: _altMobileCtrl,
                  validator: (v) {
                    if (v == null || v.trim().isEmpty) return null; // optional
                    return _validatePhone(v, label: "Alternate Phone Number");
                  },
                  keyboardType: TextInputType.phone,
                  inputFormatters: [
                    FilteringTextInputFormatter.digitsOnly,
                    LengthLimitingTextInputFormatter(10),
                  ],
                  decoration: InputDecoration(
                    hintText: "Mobile Number",
                    hintStyle: fontTextStyle(14, const Color(0xFF939495), FontWeight.w400),
                    border: const OutlineInputBorder(),
                    isDense: true,
                  ),
                  textInputAction: TextInputAction.next,
                ),
              ),

              const SizedBox(height: 20),

              // Actions
              Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  SizedBox(
                    width: double.infinity,
                    child: OutlinedButton.icon(
                      onPressed: _addDriver,
                      icon: Image.asset('images/Add_icon.png', width: 16, height: 16),
                      label: Text(
                        "Add Driver",
                        style: fontTextStyle(14, const Color(0xFF646566), FontWeight.w600),
                      ),
                    ),
                  ),
                  const SizedBox(height: 12),
                  SizedBox(
                    width: double.infinity,
                    child: ElevatedButton(
                      style: ElevatedButton.styleFrom(
                        backgroundColor: const Color(0xFF8270DB),
                        foregroundColor: Colors.white,
                        padding: const EdgeInsets.symmetric(vertical: 14),
                        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(24)),
                      ),
                      onPressed: () {
                        // // TODO: Navigate to the next step/screen
                        // Navigator.push(context, MaterialPageRoute(builder: (_) => const SourceLocation()));
                        // // ScaffoldMessenger.of(context).showSnackBar(
                        // //   SnackBar(content: Text("Saved ${_drivers.length} driver(s). Proceeding…")),
                        // // );
                      },
                      child: Text(
                        "Continue",
                        style: fontTextStyle(14, Colors.white, FontWeight.w400),
                      ),
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}

// ======= UI helpers =======

class _SectionHeaderBar extends StatelessWidget {
  final String title;
  final Widget? icon;
  final Color backgroundColor;
  final Color borderColor;
  final double radius;

  const _SectionHeaderBar({
    required this.title,
    this.icon,
    this.backgroundColor = const Color(0xFFEEEEEE),
    this.borderColor = const Color(0xFFE5E7EB),
    this.radius = 12,
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: backgroundColor,
        border: Border.all(color: borderColor, width: 1),
        borderRadius: BorderRadius.circular(radius),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.04),
            blurRadius: 6,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
      child: Row(
        children: [
          Expanded(
            child: Text(
              title,
              style: fontTextStyle(12, const Color(0xFF2D2E30), FontWeight.w600),
            ),
          ),
          if (icon != null) icon!,
        ],
      ),
    );
  }
}

class _LabeledField extends StatelessWidget {
  final String label;
  final Widget child;
  final String? Function()? validator; // (kept from your earlier helper; not used here)

  const _LabeledField({
    required this.label,
    required this.child,
    this.validator,
  });

  @override
  Widget build(BuildContext context) {
    final errorText = validator != null ? validator!() : null;

    return Padding(
      padding: const EdgeInsets.only(bottom: 14.0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(label, style: fontTextStyle(12, const Color(0xFF515253), FontWeight.w600)),
          const SizedBox(height: 6),
          child,
          if (errorText != null)
            Padding(
              padding: const EdgeInsets.only(top: 6),
              child: Text(
                errorText,
                style: fontTextStyle(14, const Color(0xFF939495), FontWeight.w400),
              ),
            ),
        ],
      ),
    );
  }
}
