import 'package:flutter/material.dart';
import 'package:seedina/utils/style/gcolor.dart';

class EditorParam extends StatefulWidget {
  final String unit;
  final TextEditingController textController;
  final String title;
  final String dialogTitle;
  final int maxlength;
  final String helper;

  const EditorParam({
    super.key,
    required this.title,
    required this.dialogTitle,
    required this.unit,
    required this.textController,
    required this.maxlength,
    required this.helper,
  });

  @override
  _EditorParamState createState() => _EditorParamState();
}

class _EditorParamState extends State<EditorParam> {
  void _showEditDialog() {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text(
            widget.dialogTitle,
            style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600),
          ),
          content: Container(
            height: 100,
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                SizedBox(
                  width: 124,
                  child: TextFormField(
                    controller: widget.textController,
                    keyboardType:
                        TextInputType.numberWithOptions(decimal: true),
                    maxLength: widget.maxlength,
                    decoration: InputDecoration(
                      border: OutlineInputBorder(),
                      suffixText: widget.unit,
                      counterText: '',
                      helperText: widget.helper,
                      helperStyle: TextStyle(color: Colors.red),
                    ),
                  ),
                ),
              ],
            ),
          ),
          actions: [
            TextButton(
              onPressed: () => Navigator.pop(context),
              child: Text('Batal'),
              style: ButtonStyle(
                foregroundColor: MaterialStateProperty.all(Colors.red),
              ),
            ),
            ElevatedButton(
              onPressed: () {
                setState(() {});
                Navigator.pop(context);
              },
              child: Text('Simpan'),
              style: ElevatedButton.styleFrom(
                foregroundColor: Colors.white,
                backgroundColor: GColors.myBiru,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(16),
                ),
              ),
            ),
          ],
          backgroundColor: Colors.white,
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    // Langsung gunakan widget.textController.text untuk menampilkan nilai terbaru
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(widget.title, style: TextStyle(fontSize: 12)),
        SizedBox(height: 2),
        Row(
          children: [
            Text(
              '${widget.textController.text} ${widget.unit}',
              style: TextStyle(fontSize: 16),
            ),
            IconButton(
              onPressed: _showEditDialog,
              icon: Icon(Icons.edit, color: GColors.myBiru),
            ),
          ],
        ),
      ],
    );
  }
}
