// lib/features/admin/tabs/positions/assign_positions_sub_tab.dart

import 'package:flutter/material.dart';

const Color _standardGold = Color.fromARGB(255, 204, 117, 4);
const Color _darkGreen = Color.fromARGB(255, 2, 105, 48);

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

  @override
  State<AssignPositionsSubTab> createState() => _AssignPositionsSubTabState();
}

class _AssignPositionsSubTabState extends State<AssignPositionsSubTab> {
  String? _selectedStaff;
  String? _selectedPosition;

  // Ideally, wire these up to your real backend data
  final List<String> _dummyStaff = ['Emmanuel C.', 'John Doe', 'Jane Smith', 'Dr. Alice'];
  final List<String> _dummyPositions = ['Senior Lecturer', 'Librarian', 'Head of IT', 'Security Officer'];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: SingleChildScrollView(
        padding: EdgeInsets.all(MediaQuery.sizeOf(context).width < 600 ? 16.0 : 32.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              'ASSIGN STAFF POSITIONS',
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.w900, color: Colors.grey.shade800, letterSpacing: 1.5),
            ),
            const SizedBox(height: 32),
            
            Container(
              padding: EdgeInsets.all(MediaQuery.sizeOf(context).width < 600 ? 16 : 32),
              decoration: BoxDecoration(
                color: Colors.white,
                border: Border.all(color: Colors.grey.shade300),
                borderRadius: BorderRadius.zero,
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  const Text('Select Staff Member', style: TextStyle(fontWeight: FontWeight.bold, color: _darkGreen)),
                  const SizedBox(height: 8),
                  DropdownButtonFormField<String>(
                    initialValue: _selectedStaff,
                    hint: const Text('Search & Select Staff...'),
                    decoration: const InputDecoration(
                      border: OutlineInputBorder(borderRadius: BorderRadius.zero),
                      enabledBorder: OutlineInputBorder(borderRadius: BorderRadius.zero, borderSide: BorderSide(color: Colors.grey)),
                      focusedBorder: OutlineInputBorder(borderRadius: BorderRadius.zero, borderSide: BorderSide(color: _darkGreen, width: 2)),
                      filled: true,
                      fillColor: Colors.white,
                    ),
                    items: _dummyStaff.map((s) => DropdownMenuItem(value: s, child: Text(s))).toList(),
                    onChanged: (val) => setState(() => _selectedStaff = val),
                  ),
                  
                  const SizedBox(height: 32),

                  const Text('Select Position to Assign', style: TextStyle(fontWeight: FontWeight.bold, color: _darkGreen)),
                  const SizedBox(height: 8),
                  DropdownButtonFormField<String>(
                    initialValue: _selectedPosition,
                    hint: const Text('Search & Select Position...'),
                    decoration: const InputDecoration(
                      border: OutlineInputBorder(borderRadius: BorderRadius.zero),
                      enabledBorder: OutlineInputBorder(borderRadius: BorderRadius.zero, borderSide: BorderSide(color: Colors.grey)),
                      focusedBorder: OutlineInputBorder(borderRadius: BorderRadius.zero, borderSide: BorderSide(color: _darkGreen, width: 2)),
                      filled: true,
                      fillColor: Colors.white,
                    ),
                    items: _dummyPositions.map((p) => DropdownMenuItem(value: p, child: Text(p))).toList(),
                    onChanged: (val) => setState(() => _selectedPosition = val),
                  ),

                  const SizedBox(height: 48),

                  SizedBox(
                    width: double.infinity,
                    child: ElevatedButton.icon(
                      onPressed: (_selectedStaff != null && _selectedPosition != null) 
                          ? () {
                              ScaffoldMessenger.of(context).showSnackBar(
                                SnackBar(
                                  content: Text('$_selectedStaff successfully assigned to $_selectedPosition!'),
                                  backgroundColor: _darkGreen,
                                  shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
                                  behavior: SnackBarBehavior.floating,
                                )
                              );
                              // Reset the fields after submission
                              setState(() {
                                _selectedStaff = null;
                                _selectedPosition = null;
                              });
                            }
                          : null,
                      icon: const Icon(Icons.verified_user_rounded, color: Colors.white),
                      label: const Text('ASSIGN POSITION TO STAFF', style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold, letterSpacing: 1.0)),
                      style: ElevatedButton.styleFrom(
                        backgroundColor: _standardGold,
                        disabledBackgroundColor: Colors.grey.shade300,
                        padding: const EdgeInsets.symmetric(vertical: 24),
                        shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
                        elevation: 0,
                      ),
                    ),
                  )
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}