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/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_calibration_tools.dart';
import 'package:test_sa/new_views/common_widgets/app_filled_button.dart';
import 'package:test_sa/views/widgets/equipment/pick_asset.dart';
import 'package:test_sa/views/widgets/pentry/calibration_tool_asset_picker.dart';

import '../../../new_views/app_style/app_color.dart';
import '../date_and_time/date_picker.dart';
import 'auto_complete_fields/auto_complete_devices_field.dart';

class PentryCalibrationToolForm extends StatefulWidget {
  final List<PpmCalibrationTools> models;

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

  @override
  State<PentryCalibrationToolForm> createState() => _PentryCalibrationToolFormState();
}

class _PentryCalibrationToolFormState extends State<PentryCalibrationToolForm> {
  @override
  Widget build(BuildContext context) {
    final userProvider = Provider.of<UserProvider>(context);
    return ListView.builder(
      itemCount: widget.models.length + 1,
      padding: EdgeInsets.only(top: 16.toScreenHeight),
      itemBuilder: (context, index) {
        if (index == widget.models.length) {
          return AppFilledButton(
            label: context.translation.addAsset,
            maxWidth: true,
            textColor: Colors.white,
            buttonColor: context.isDark ? AppColor.neutral60 : AppColor.neutral50,
            onPressed: () async {
              if (widget.models?.isNotEmpty ?? false) {
                if (widget.models.last.assetId == null) {
                  await Fluttertoast.showToast(msg: "${context.translation.youHaveToSelect} ${context.translation.assetNumber}");
                  return;
                }
                if (widget.models.last.calibrationDateOfTesters == null) {
                  await Fluttertoast.showToast(msg: "${context.translation.youHaveToSelect} ${context.translation.date}");
                  return;
                }
              }
              widget.models.add(PpmCalibrationTools(id: 0));
              setState(() {});
            },
          );
        }
        final model = widget.models[index];
        return Container(
          padding: const EdgeInsets.all(16),
          margin: EdgeInsets.only(bottom: 16.toScreenHeight),
          decoration: BoxDecoration(
            color: AppColor.background(context),
            borderRadius: BorderRadius.circular(20),
            boxShadow: [BoxShadow(color: Colors.black.withOpacity(0.03), blurRadius: 14)],
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  "${context.translation.asset} ${index + 1}".heading5(context),
                  "trash".toSvgAsset(height: 20, width: 15).onPress(() {
                    widget.models.remove(model);
                    setState(() {});
                  }),
                ],
              ),
              16.height,
              CalibrationToolAssetPicker(
                initialValue: model.assetId == null ? null : Lookup(id: model.assetId, name: model.assetNumber ?? ""),
                hospitalId: userProvider.user.clientId,
                onPick: (number) {
                  model.assetNumber = number.name;
                  model.assetId = number.id;
                  setState(() {});
                },
              ),
              8.height,
              ADatePicker(
                label: context.translation.calibrationDate,
                date: DateTime.tryParse(model.calibrationDateOfTesters ?? ""),
                from: DateTime.now().subtract(const Duration(days: 90)),
                backgroundColor: context.isDark ? AppColor.neutral50 : AppColor.background(context),
                withBorder: false,
                onDatePicker: (date) {
                  model.calibrationDateOfTesters = date?.toIso8601String();
                  setState(() {});
                },
              ),
            ],
          ),
        );
      },
    );
  }
}
