import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.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/helper/utils.dart';
import 'package:test_sa/models/lookup.dart';
import 'package:test_sa/models/site_contact_info_model.dart';
import 'package:test_sa/modules/demo_module/provider/demo_period_lookup_provider.dart';
import 'package:test_sa/modules/demo_module/provider/demo_provider.dart';
import 'package:test_sa/new_views/common_widgets/single_item_drop_down_menu.dart';
import 'package:test_sa/views/widgets/loaders/no_data_found.dart';
import 'package:url_launcher/url_launcher_string.dart';

import '../../new_views/app_style/app_color.dart';
import '../../new_views/common_widgets/app_filled_button.dart';
import '../cm_module/views/components/action_button/footer_action_button.dart';
import '../loan_module/provider/loan_period_provider.dart';

class DemoExtensionBottomSheet extends StatefulWidget {
  int requestId;

  DemoExtensionBottomSheet({Key? key, required this.requestId}) : super(key: key);

  @override
  State<DemoExtensionBottomSheet> createState() => _DemoExtensionBottomSheetState();
}

class _DemoExtensionBottomSheetState extends State<DemoExtensionBottomSheet> {
  Lookup? demoPeriod;
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return Container(
      clipBehavior: Clip.antiAlias,
      // height: MediaQuery.sizeOf(context).height * .5,
      decoration: const BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.only(
          topRight: Radius.circular(30),
          topLeft: Radius.circular(30),
        ),
      ),
      padding: EdgeInsets.only(
        left: 16.toScreenWidth,
        right: 16.toScreenWidth,
        top: 8.toScreenHeight,
        bottom: MediaQuery.of(context).viewInsets.bottom + MediaQuery.of(context).padding.bottom,
      ),
      child: SafeArea(
        child: Form(
          key: _formKey,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Container(
                width: 40.toScreenWidth,
                height: 5.toScreenHeight,
                decoration: BoxDecoration(color: AppColor.neutral40, borderRadius: BorderRadius.circular(30)),
              ),
              16.height,
              Align(
                alignment: AlignmentDirectional.centerStart,
                child: Text(
                  "Select Demo Period",
                  style: AppTextStyles.heading5.copyWith(fontWeight: FontWeight.w600, color: context.isDark ? AppColor.neutral30 : AppColor.neutral50),
                ),
              ),
              16.height,
              SingleItemDropDownMenu<Lookup, DemoPeriodLookupProvider>(
                context: context,
                height: 56.toScreenHeight,
                title: "Demo Period",
                initialValue: demoPeriod,
                showShadow: false,
                validator: (value) {
                  if (value == null) return "Mandatory";
                  return null;
                },
                backgroundColor: AppColor.fieldBgColor(context),
                showAsBottomSheet: true,
                onSelect: (value) {
                  demoPeriod = value;
                  setState(() {});
                },
              ),
              36.height,
              Row(
                children: [
                  AppFilledButton(
                    label: context.translation.cancel,
                    buttonColor: AppColor.white60,
                    textColor: AppColor.black10,
                    loading: false,
                    onPressed: () async {
                      Navigator.pop(context, false);
                    },
                  ).expanded,
                  16.width,
                  AppFilledButton(
                    buttonColor: AppColor.primary10,
                    label: context.translation.submit,
                    onPressed: () async {
                      if (_formKey.currentState!.validate()) {
                        Utils.showLoading(context);
                        DemoProvider demoProvider = Provider.of<DemoProvider>(context, listen: false);
                        bool isSuccess = await demoProvider.updateDemoPeriod(requestId: widget.requestId, demoPeriodId: demoPeriod!.id!, isUpdateRequired: true);
                        Utils.hideLoading(context);
                        if (isSuccess) {
                          Navigator.pop(context, true);
                        }
                      }
                    },
                  ).expanded,
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
