import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/providers/api/user_provider.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/int_extensions.dart';
import 'package:test_sa/extensions/string_extensions.dart';
import 'package:test_sa/extensions/text_extensions.dart';
import 'package:test_sa/extensions/widget_extensions.dart';
import 'package:test_sa/models/service_request/supp_engineer_work_orders.dart';
import 'package:test_sa/models/service_request/supplier_engineer_model.dart';
import 'package:test_sa/providers/work_order/supplier_engineer_provider.dart';

import '../../../../controllers/providers/api/service_requests_provider.dart';
import '../../../../models/lookup.dart';
import '../../../../new_views/app_style/app_color.dart';
import '../../../../new_views/common_widgets/app_filled_button.dart';
import '../../../../new_views/common_widgets/app_text_form_field.dart';
import '../../../../new_views/common_widgets/single_item_drop_down_menu.dart';

class AddSupplierEngineerBottomSheet extends StatefulWidget {
  final int supplierId;

  const AddSupplierEngineerBottomSheet(this.supplierId, {Key? key}) : super(key: key);

  @override
  State<AddSupplierEngineerBottomSheet> createState() => _AddSupplierEngineerBottomSheetState();
}

class _AddSupplierEngineerBottomSheetState extends State<AddSupplierEngineerBottomSheet> {
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

  Lookup? suppEngRole;

  SupplierEngineer engineer = SupplierEngineer();

  @override
  void initState() {
    super.initState();
    engineer.id = 0;
    engineer.supplierId = widget.supplierId;
  }

  @override
  Widget build(BuildContext context) {
    final userProvider = Provider.of<UserProvider>(context, listen: false);
    return Wrap(
      children: [
        Container(
          clipBehavior: Clip.antiAlias,
          margin: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
          decoration: BoxDecoration(
            color: Theme.of(context).scaffoldBackgroundColor,
            borderRadius: const BorderRadius.only(topRight: Radius.circular(20), topLeft: Radius.circular(20)),
          ),
          padding: EdgeInsets.symmetric(horizontal: 16.toScreenWidth, vertical: 8.toScreenHeight),
          child: Form(
            key: _formKey,
            child: SingleChildScrollView(
              child: Column(
                children: [
                  Container(
                    width: 40.toScreenWidth,
                    height: 5.toScreenHeight,
                    decoration: BoxDecoration(color: AppColor.neutral40, borderRadius: BorderRadius.circular(30)),
                  ),
                  Align(
                    alignment: AlignmentDirectional.centerStart,
                    child: "Add Engineer For Supplier".heading5(context).custom(fontWeight: FontWeight.w600).paddingOnly(top: 16, bottom: 16),
                  ),
                  8.height,
                  AppTextFormField(
                    labelText: "Engineer Supplier Name".addTranslation,
                    textInputType: TextInputType.name,
                    onChange: (text) {
                      engineer.personName = text.isEmpty ? null : text;
                    },
                    onSaved: (text) {
                      engineer.personName = text.isEmpty ? null : text;
                    },
                  ),
                  8.height,
                  SingleItemDropDownMenu<Lookup, SupplierEngineerProvider>(
                    context: context,
                    title: "Role",
                    initialValue: suppEngRole,
                    onSelect: (value) {
                      if (value != null) {
                        suppEngRole = value;
                      }
                    },
                  ),
                  8.height,
                  AppTextFormField(
                    labelText: "Contact".addTranslation,
                    textInputType: TextInputType.name,
                    onChange: (text) {
                      engineer.contact = text.isEmpty ? null : text;
                    },
                    onSaved: (text) {
                      engineer.contact = text.isEmpty ? null : text;
                    },
                  ),
                  8.height,
                  AppTextFormField(
                    labelText: "Email".addTranslation,
                    textInputType: TextInputType.emailAddress,
                    onChange: (text) {
                      engineer.email = text.isEmpty ? null : text;
                    },
                    onSaved: (text) {
                      engineer.email = text.isEmpty ? null : text;
                    },
                  ),
                  8.height,
                  16.height,
                  Consumer<ServiceRequestsProvider>(
                    builder: (context, snapshot, _) => AppFilledButton(
                      label: context.translation.save,
                      loading: snapshot.isLoading ?? false,
                      onPressed: () async {
                        _formKey.currentState!.save();

                        if (engineer.personName?.isEmpty ?? true) {
                          "Engineer supplier name is empty".showToast;
                          return;
                        }

                        engineer.personRoleId = suppEngRole?.id;

                        SuppEngineerWorkOrders? suppEngineer = await snapshot.addSupplierEngineer(engineer);
                        if (suppEngineer == null) {
                          Fluttertoast.showToast(msg: context.translation.failedToCompleteRequest);
                          return;
                        }
                        Fluttertoast.showToast(msg: context.translation.successfulRequestMessage);
                        Navigator.pop(context, suppEngineer);
                      },
                    ),
                  ),
                  16.height,
                ],
              ),
            ),
          ),
        )
      ],
    );
  }
}
