import 'package:flutter/material.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/int_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/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';

class PentryPMChecklistForm extends StatefulWidget {
  final List<PpmChecklists> models;

  const PentryPMChecklistForm({Key key, this.models}) : super(key: key);

  @override
  State<PentryPMChecklistForm> createState() => _PentryPMChecklistFormState();
}

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

  @override
  Widget build(BuildContext context) {
    final list = widget.models?.where((element) => element.task != null)?.toList();
    return (list?.isEmpty ?? true)
        ? context.translation.noDataFound.heading5(context).center
        : ListView.builder(
            padding: EdgeInsets.only(top: 16.toScreenHeight),
            itemCount: list?.length,
            shrinkWrap: true,
            itemBuilder: (context, index) {
              return Card(
                child: ExpansionTile(
                  shape: const Border(),
                  title: (list[index].task ?? "").heading5(context),
                  onExpansionChanged: (value) {
                    if (!value) {
                      showLabel.remove(index);
                    } else {
                      showLabel.add(index);
                    }
                    setState(() {});
                  },
                  subtitle: showLabel.isNotEmpty && showLabel.contains(index) ? null : "${context.translation.status} ${list[index].taskStatusName}".bodyText(context),
                  iconColor: context.isDark ? AppColor.neutral30 : AppColor.neutral50,
                  collapsedIconColor: context.isDark ? AppColor.neutral30 : AppColor.neutral50,
                  childrenPadding: EdgeInsets.symmetric(horizontal: 16.toScreenWidth, vertical: 8.toScreenHeight),
                  children: [
                    if (showLabel.isNotEmpty && showLabel.contains(index))
                      SingleItemDropDownMenu<Lookup, PpmChecklistStatusProvider>(
                        context: context,
                        backgroundColor: context.isDark ? AppColor.neutral50 : null,
                        initialValue: list[index].taskStatusId == null ? null : Lookup(id: list[index].taskStatusId, name: list[index].taskStatusName),
                        title: context.translation.status,
                        onSelect: (value) {
                          list[index].taskStatusId = value.id;
                          list[index].taskStatusName = value.name;
                        },
                      ),
                    if (showLabel.isNotEmpty && showLabel.contains(index)) 8.height,
                    AppTextFormField(
                      labelText: context.translation.measuredValue,
                      initialValue: list[index].measuredValue,
                      enable: false,
                      backgroundColor: context.isDark ? AppColor.neutral20 : null,
                      // backgroundColor: context.isDark ? AppColor.neutral50 : null,
                      // onChange: (text) {
                      //   list[index].measuredValue = text;
                      // },
                    ),
                    8.height,
                    AppTextFormField(
                      labelText: context.translation.comment,
                      initialValue: list[index].taskComment,
                      enable: false,
                      backgroundColor: context.isDark ? AppColor.neutral20 : null,
                      // backgroundColor: context.isDark ? AppColor.neutral50 : null,
                      // onChange: (text) {
                      //   list[index].taskComment = text;
                      // },
                    ),
                    8.height,
                  ],
                ),
              );
            },
          );
  }
}
