import 'package:flutter/material.dart';
import 'package:kmobile/data/models/user.dart';
import '../../../l10n/app_localizations.dart';

class AccountInfoScreen extends StatefulWidget {
  final List<User> users;
  final int selectedIndex;

  const AccountInfoScreen({
    super.key,
    required this.users,
    required this.selectedIndex,
  });

  @override
  State<AccountInfoScreen> createState() => _AccountInfoScreen();
}

class _AccountInfoScreen extends State<AccountInfoScreen> {
  late User selectedUser;
  @override
  void initState() {
    super.initState();
    selectedUser = widget.users[widget.selectedIndex];
  }

  @override
  Widget build(BuildContext context) {
    final users = widget.users;
    int selectedIndex = widget.selectedIndex;
    return Scaffold(
      appBar: AppBar(
        title: Text(AppLocalizations.of(context)
            .accountInfo
            .replaceFirst(RegExp('\n'), '')),
      ),
      body: ListView(
        padding: const EdgeInsets.all(16.0),
        children: [
          Text(
            AppLocalizations.of(context).accountNumber,
            style: const TextStyle(fontWeight: FontWeight.w500, fontSize: 14),
          ),

          DropdownButton<User>(
            value: selectedUser,
            onChanged: (User? newUser) {
              if (newUser != null) {
                setState(() {
                  selectedUser = newUser;
                });
              }
            },
            items: widget.users.map((user) {
              return DropdownMenuItem<User>(
                value: user,
                child: Text(user.accountNo.toString()),
              );
            }).toList(),
          ),

          InfoRow(
            title: AppLocalizations.of(context).customerNumber,
            value: selectedUser.cifNumber ?? 'N/A',
          ),
          InfoRow(
            title: AppLocalizations.of(context).productName,
            value: selectedUser.productType ?? 'N/A',
          ),
          // InfoRow(title: 'Account Opening Date', value: users[selectedIndex].accountOpeningDate ?? 'N/A'),
          InfoRow(
            title: AppLocalizations.of(context).accountStatus,
            value: 'OPEN',
          ),
          InfoRow(
            title: AppLocalizations.of(context).availableBalance,
            value: selectedUser.availableBalance ?? 'N/A',
          ),
          InfoRow(
            title: AppLocalizations.of(context).currentBalance,
            value: selectedUser.currentBalance ?? 'N/A',
          ),

          users[selectedIndex].approvedAmount != null
              ? InfoRow(
                  title: AppLocalizations.of(context).approvedAmount,
                  value: selectedUser.approvedAmount ?? 'N/A',
                )
              : const SizedBox.shrink(),
        ],
      ),
    );
  }
}

class InfoRow extends StatelessWidget {
  final String title;
  final String value;

  const InfoRow({required this.title, required this.value, super.key});

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    return Container(
      width: double.infinity,
      margin: const EdgeInsets.symmetric(vertical: 8),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            title,
            style: TextStyle(
              fontSize: 15,
              fontWeight: FontWeight.w500,
              color: theme.colorScheme.onSurfaceVariant,
            ),
          ),
          const SizedBox(height: 3),
          Text(
            value,
            style: TextStyle(fontSize: 16, color: theme.colorScheme.onSurface),
          ),
        ],
      ),
    );
  }
}
