import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/providers/api/ppm_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/lookup.dart';
import 'package:test_sa/models/plan_preventive_visit/plan_preventive_visit_model.dart';
import 'package:test_sa/models/ppm/ppm_check_list.dart';
import 'package:test_sa/new_views/app_style/app_color.dart';
import 'package:test_sa/new_views/common_widgets/app_text_form_field.dart';
import 'package:test_sa/new_views/common_widgets/single_item_drop_down_menu.dart';
import 'package:test_sa/providers/ppm_checklist_status_provider.dart';
import 'package:test_sa/views/widgets/item_views/info_header_widget.dart';
import 'package:test_sa/views/widgets/loaders/no_data_found.dart';

class PpmPmChecklistForm extends StatefulWidget {
  final List<PpmChecklists>? models;
  final List<PreventiveVisitChecklists>? checkList;

  const PpmPmChecklistForm({Key? key, this.models, this.checkList}) : super(key: key);

  @override
  State<PpmPmChecklistForm> createState() => _PpmPmChecklistFormState();
}

class _PpmPmChecklistFormState extends State<PpmPmChecklistForm> {
  PpmChecklists item = PpmChecklists();
  List<int> showLabel = [];

  @override
  Widget build(BuildContext context) {
    int totalTabs = Provider.of<PpmProvider>(context, listen: false).totalTabs;
    final ppmProvider = Provider.of<PpmProvider>(context, listen: false);
    int currentIndex = totalTabs == 4 ? 4 : 3;
    List<PreventiveVisitChecklists>? list = widget.checkList ?? [];
    return (list.isEmpty)
        ? const NoDataFound().center
        : ListView.separated(
            padding: const EdgeInsets.only(left: 16, right: 16, top: 8, bottom: 16),
            itemCount: list.length,
            shrinkWrap: true,
            separatorBuilder: (cxt, index) => 12.height,
            itemBuilder: (context, index) {
              return IgnorePointer(
                ignoring: ppmProvider.isReadOnly,
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    if (index == 0) ...[

                      // "$currentIndex /$totalTabs . PM Checklist".heading5(context).custom(color: AppColor.textStyleColor(context)),
                      InfoHeader16Widget("$currentIndex /$totalTabs . PM Checklist"),
                      16.height,
                    ],
                    AppTextFormField(
                      labelText: "Task".addTranslation,

                      labelStyle: AppTextStyles.tinyFont.copyWith(color: context.isDark ? AppColor.neutral30 : AppColor.neutral120, fontWeight: FontWeight.w500),
                      initialValue: list[index].instructionText?.text ?? "",
                      enable: false,
                      showShadow: false,
                      makeMultiLinesNull: true,
                      enableColor: AppColor.neutral100,
                      backgroundColor: context.isDark ? AppColor.neutral20 : AppColor.neutral100,
                      // backgroundColor: context.isDark ? AppColor.neutral50 : null,
                      onChange: (text) {
                        list[index].taskComment = text;
                      },
                    ),
                    8.height,
                    SingleItemDropDownMenu<Lookup, PpmChecklistStatusProvider>(
                      context: context,
                      showShadow: false,
                      backgroundColor: context.isDark ? AppColor.neutral20 : AppColor.neutral100,
                      initialValue: list[index].taskStatus?.id == null ? null : Lookup(id: list[index].taskStatus?.id?.toInt(), name: list[index].taskStatus?.name),
                      title: context.translation.taskStatus,
                      onSelect: (value) {
                        if (value != null) {
                          list[index].taskStatus = Lookup(id: value.id, name: value.name, value: value.value);
                        }
                      },
                    ),
                    8.height,
                    AppTextFormField(
                      labelText: context.translation.measuredValue,
                      labelStyle: AppTextStyles.tinyFont.copyWith(color: context.isDark ? AppColor.neutral30 : AppColor.neutral120, fontWeight: FontWeight.w500),
                      initialValue: list[index].measuredValue ?? "",
                      // enable: false,
                      backgroundColor: context.isDark ? AppColor.neutral20 : AppColor.neutral100,
                      showShadow: false,
                      onChange: (text) {
                        list[index].measuredValue = text;
                      },
                    ),
                    8.height,
                    AppTextFormField(
                      labelText: "Task Comment".addTranslation,
                      labelStyle: AppTextStyles.tinyFont.copyWith(color: context.isDark ? AppColor.neutral30 : AppColor.neutral120, fontWeight: FontWeight.w500),
                      initialValue: list[index].taskComment ?? "",
                      textInputType: TextInputType.multiline,
                      makeMultiLinesNull: true,
                      backgroundColor: context.isDark ? AppColor.neutral20 : AppColor.neutral100,
                      showShadow: false,
                      onChange: (text) {
                        list[index].taskComment = text;
                      },
                    ),
                  ],
                ).toShadowContainer(context, showShadow: false, borderRadius: 20),
              );
            },
          );
  }
}
