import 'package:flutter/material.dart';
import 'package:test_sa/views/app_style/colors.dart';
import 'package:test_sa/views/app_style/sizing.dart';

import '../../../models/service_request/search_work_order.dart';

class AssistantEmployeeMenu extends StatefulWidget {
  final List<AssistantEmployees> statuses;
  final AssistantEmployees initialStatus;
  final Function(AssistantEmployees) onSelect;

  const AssistantEmployeeMenu({Key key, this.statuses, this.onSelect, this.initialStatus}) : super(key: key);

  @override
  _SingleAssistantEmployeeMenuState createState() => _SingleAssistantEmployeeMenuState();
}

class _SingleAssistantEmployeeMenuState extends State<AssistantEmployeeMenu> {
  AssistantEmployees _selectedStatus;

  @override
  void setState(VoidCallback fn) {
    if (mounted) super.setState(fn);
  }

  @override
  void didUpdateWidget(covariant AssistantEmployeeMenu oldWidget) {
    if (widget.initialStatus != null) {
      final result = widget.statuses?.where((element) {
        return element?.user?.id == widget.initialStatus?.user?.id;
      });
      if (result.isNotEmpty) {
        _selectedStatus = result.first;
      } else {
        _selectedStatus = null;
      }
      if ((widget.initialStatus?.user?.id ?? "") != (_selectedStatus?.user?.id ?? "")) {
        widget.onSelect(_selectedStatus);
      }
    } else {
      _selectedStatus = null;
    }
    super.didUpdateWidget(oldWidget);
  }

  @override
  void initState() {
    if (widget.initialStatus != null) {
      final result = widget.statuses?.where((element) {
        return element?.user?.id == widget.initialStatus?.user?.id;
      });
      if (result.isNotEmpty) _selectedStatus = result.first;
      if (widget.initialStatus?.user?.id != _selectedStatus?.user?.id) {
        widget.onSelect(_selectedStatus);
      }
    }

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 16),
      decoration: BoxDecoration(
        color: AColors.inputFieldBackgroundColor,
        border: Border.all(
          color: Color(0xffefefef),
        ),
        borderRadius: BorderRadius.circular(AppStyle.borderRadius * AppStyle.getScaleFactor(context)),
        // boxShadow: const [
        //   AppStyle.boxShadow
        // ]
      ),
      child: DropdownButton<AssistantEmployees>(
        value: _selectedStatus,
        iconSize: 24,
        icon: const Icon(Icons.keyboard_arrow_down_rounded),
        elevation: 0,
        isExpanded: true,
        hint: Text(
          "Select",
          style: Theme.of(context).textTheme.subtitle1,
        ),
        style: TextStyle(color: Theme.of(context).primaryColor),
        underline: const SizedBox.shrink(),
        onChanged: (AssistantEmployees newValue) {
          setState(() {
            _selectedStatus = newValue;
          });
          widget.onSelect(newValue);
        },
        items: widget.statuses.map<DropdownMenuItem<AssistantEmployees>>((AssistantEmployees value) {
          return DropdownMenuItem<AssistantEmployees>(
            value: value,
            child: Text(
              value.user?.name ?? "NULL",
              style: Theme.of(context).textTheme.titleMedium.copyWith(
                    color: Theme.of(context).primaryColor,
                    fontSize: 11,
                    //fontWeight: FontWeight.bold
                  ),
            ),
          );
        }).toList(),
      ),
    );
  }
}
