import 'package:fis_i18n/i18n.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/router/router.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_theme/theme.dart';
import 'package:fis_ui/index.dart';

class ItemSelected extends FStatefulWidget implements FPage {
  final List<String> items;
  final String title;
  final void Function(String)? onPressed;
  final String? selectedItem;
  @override
  FState<FStatefulWidget> createState() {
    return ItemSelectedState();
  }

  ItemSelected({
    required this.items,
    required this.title,
    this.selectedItem,
    this.onPressed,
  });

  @override
  String get pageName => "ItemSelected";
}

class ItemSelectedState extends FState<ItemSelected> {
  String _selectedItem = '';

  @override
  void initState() {
    _selectedItem = widget.selectedItem ?? '';
    super.initState();
  }

  @override
  void didUpdateWidget(ItemSelected oldWidget) {
    if (_selectedItem != widget.selectedItem) super.didUpdateWidget(oldWidget);
  }

  @override
  FWidget build(BuildContext context) {
    return FScaffold(
      appBar: FAppBar(
        title: FText(
          widget.title,
          style: TextStyle(color: Colors.white),
        ),
        centerTitle: true,
        leading: FIconButton(
          businessParent: widget,
          name: 'back',
          icon: FIcon(
            Icons.arrow_back_ios_new,
            color: Colors.white,
          ),
          onPressed: router.back,
        ),
        actions: [
          FElevatedButton(
            businessParent: widget,
            name: 'submit',
            style: ButtonStyle(
              elevation: MaterialStateProperty.all<double>(0), // 设置按钮的阴影为 0
            ),
            onPressed: () {
              if (_selectedItem.isNotEmpty) {
                widget.onPressed?.call(_selectedItem);
                router.back();
              }
            },
            child: FText(
              i18nBook.common.submit.t,
              style: TextStyle(color: Colors.white),
            ),
          ),
        ],
      ),
      backgroundColor: Colors.white,
      body: FListView(
        shrinkWrap: true,
        children: widget.items.map((e) => _buildItemSelected(e)).toList(),
      ),
    );
  }

  FWidget _buildItemSelected(String e) {
    return FGestureDetector(
      businessParent: widget,
      onTap: () {
        setState(() {
          _selectedItem = e;
        });
      },
      name: e,
      child: FContainer(
        height: 50,
        child: FRow(
          children: [
            const FSizedBox(width: 15),
            FText(e),
            FExpanded(
              child: FContainer(
                height: 50,
                width: double.infinity,
                color: Colors.white,
              ),
            ),
            FIcon(
              e == _selectedItem
                  ? Icons.radio_button_on
                  : Icons.circle_outlined,
              color: e == _selectedItem
                  ? FTheme.ins.colorScheme.primary
                  : Colors.grey,
            ),
            const FSizedBox(width: 15),
          ],
        ),
      ),
    );
  }
}
