import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/views/desktop/check_list_page_creation/widgets/FValueController.dart';

/// 周 天 特殊输入框
class CheckListPageWeekAndDay extends FStatefulWidget {
  const CheckListPageWeekAndDay({
    Key? key,
    required this.configuration,
    required this.buildKey,
    this.increaseCount,
    this.controller,
  }) : super(key: key);
  final Map<String, dynamic> configuration;
  final String buildKey;
  final FValueController? controller;

  final ValueCallback<String>? increaseCount;

  @override
  FState<CheckListPageWeekAndDay> createState() => _BuildItemWeekAndDayState();
}

class _BuildItemWeekAndDayState extends FState<CheckListPageWeekAndDay> {
  num week = 0;
  num onlyDay = 0;
  @override
  void initState() {
    widget.controller?.addListener(_updateOptionsData);
    super.initState();
  }

  void _updateOptionsData() {
    if (mounted) {
      setState(() {});
    }
  }

  @override
  FWidget build(BuildContext context) {
    bool isWeekAndDay =
        widget.controller?.value != null && widget.controller?.value != '';
    String weekValue = isWeekAndDay
        ? (num.parse(widget.controller!.value) ~/ 7).toString()
        : '';
    String dayValue = isWeekAndDay
        ? (num.parse(widget.controller!.value) % 7).toString()
        : '';
    TextEditingController weekController = TextEditingController.fromValue(
      TextEditingValue(
        text: weekValue,
        selection: TextSelection.fromPosition(
          TextPosition(
            affinity: TextAffinity.downstream,
            offset: weekValue.length,
          ),
        ),
      ),
    );

    TextEditingController dayController = TextEditingController.fromValue(
      TextEditingValue(
        text: dayValue,
        selection: TextSelection.fromPosition(
          TextPosition(
            affinity: TextAffinity.downstream,
            offset: dayValue.length,
          ),
        ),
      ),
    );

    final child = widget.configuration['name'] != 'LGA'
        ? FRow(
            children: [
              FBorderInput(
                maxLength: 20,
                borderColor: Color.fromRGBO(230, 230, 230, 1),
                height: 38,
                contentSize: 16,
                controller: weekController,
                onChanged: (value) {
                  week = num.parse(value);
                  widget.increaseCount?.call((week * 7 + onlyDay).toString());
                },
              ),
              FSizedBox(
                width: 5,
              ),
              FText(i18nBook.remedical.week.t),
              FSizedBox(
                width: 5,
              ),
              FBorderInput(
                maxLength: 20,
                borderColor: Color.fromRGBO(230, 230, 230, 1),
                height: 38,
                contentSize: 16,
                controller: dayController,
                onChanged: (value) {
                  if (value.toString().isNotEmpty) {
                    onlyDay = num.parse(value);
                  } else {
                    onlyDay = 0;
                  }
                  widget.increaseCount?.call((week * 7 + onlyDay).toString());
                },
              ),
              FSizedBox(
                width: 5,
              ),
              FText(i18nBook.remedical.day.t)
            ],
          )
        : FRow(
            children: [
              FBorderInput(
                maxLength: 20,
                borderColor: Color.fromRGBO(230, 230, 230, 1),
                height: 38,
                contentSize: 16,
                onChanged: (value) {
                  week = num.parse(value);
                  widget.increaseCount?.call((week * 7 + onlyDay).toString());
                },
              ),
              FSizedBox(
                width: 5,
              ),
              FText(i18nBook.remedical.week.t),
              FSizedBox(
                width: 5,
              ),
              FBorderInput(
                maxLength: 20,
                borderColor: Color.fromRGBO(230, 230, 230, 1),
                height: 38,
                contentSize: 16,
                onChanged: (value) {
                  if (value.toString().isNotEmpty) {
                    onlyDay = num.parse(value);
                  } else {
                    onlyDay = 0;
                  }
                  widget.increaseCount?.call((week * 7 + onlyDay).toString());
                },
              ),
              FSizedBox(
                width: 5,
              ),
              FText(i18nBook.remedical.day.t)
            ],
          );
    return FExpanded(
      child: child,
    );
  }
}
