import 'package:farm_tpf/custom_model/action_form/CommonData.dart';
import 'package:farm_tpf/presentation/screens/actions/state_management_helper/change_dropdown_controller.dart';
import 'package:farm_tpf/presentation/screens/resources/sc_common_data_helper.dart';
import 'package:farm_tpf/utils/validators.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class DropdownSupplyWidget extends StatefulWidget {
  final String? titleName;
  final String condition;
  final String hint;
  final String? value;
  final Function(CommonData) onPressed;
  final String? invalidMessage;
  final String tag;
  final String tbSupply;
  DropdownSupplyWidget({
    this.titleName,
    required this.hint,
    required this.tag,
    this.value,
    required this.condition,
    required this.onPressed,
    this.invalidMessage,
    required this.tbSupply,
  });

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

class _DropdownSupplyWidgetState extends State<DropdownSupplyWidget> {
  ChangeDropdownController? controller;
  @override
  void initState() {
    super.initState();
    controller = Get.put(ChangeDropdownController(), tag: widget.tag);
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ChangeDropdownController>(
        init: controller,
        tag: widget.tag,
        builder: (data) {
          return SizedBox(
            width: double.infinity,
            height: Validators.stringNotNullOrEmpty(widget.invalidMessage ?? '') ? 85 : 65,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Validators.stringNotNullOrEmpty(data?.currentData?.name ?? '')
                    ? Text(
                        widget.hint ?? '',
                        style: TextStyle(
                            color: Validators.stringNotNullOrEmpty(widget.invalidMessage ?? '') ? Colors.red : Colors.black54, fontSize: 13.0),
                      )
                    : Text(
                        '',
                        style: TextStyle(
                            color: Validators.stringNotNullOrEmpty(widget.invalidMessage ?? '') ? Colors.red : Colors.black54, fontSize: 13.0),
                      ),
                SizedBox(
                  width: double.infinity,
                  height: 44,
                  child: TextButton(
                      onPressed: () {
                        Navigator.of(context)
                            .push(MaterialPageRoute(
                                builder: (_) => CommonDataHelperScreen(
                                    titleName: widget.titleName ?? '',
                                    tbSupply: widget.tbSupply ?? '',
                                    condition: widget.condition ?? '',
                                    selectedId: data?.selectedId ?? -1,
                                    currentItems: [],
                                    currentEditId: -1),
                                fullscreenDialog: false))
                            .then((value) {
                          if (value != null) {
                            var result = value as CommonData;
                            controller?.change(result);
                            widget.onPressed(result);
                          }
                        });
                      },
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Container(
                              padding: EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0, left: 0.0),
                              decoration: BoxDecoration(
                                border: Border(
                                    bottom: BorderSide(
                                        width: 0.5,
                                        color: Validators.stringNotNullOrEmpty(widget.invalidMessage ?? '') ? Colors.red : Colors.black54)),
                              ),
                              child: Row(
                                children: [
                                  Expanded(
                                    child: Validators.stringNotNullOrEmpty(data?.currentData?.name ?? '')
                                        ? Text(data?.currentData?.name ?? '', style: TextStyle(fontSize: 16.0, color: Colors.black))
                                        : Text(widget.hint, style: TextStyle(fontSize: 16.0, color: Colors.black54)),
                                  ),
                                  Icon(
                                    Icons.arrow_drop_down,
                                    color: Colors.grey,
                                  ),
                                ],
                              ))
                        ],
                      )),
                ),
                Validators.stringNotNullOrEmpty(widget.invalidMessage ?? '')
                    ? Text(
                        widget.invalidMessage ?? '',
                        style: TextStyle(fontSize: 12.0, color: Colors.red, fontWeight: FontWeight.normal),
                        textAlign: TextAlign.left,
                      )
                    : SizedBox(),
              ],
            ),
          );
        });
  }
}
