import 'package:flutter/material.dart';

// 自定义弹窗组件
class WorkAlertDialog extends StatefulWidget {
  final String alertTitle; // 弹窗标题
  final String inputHint; // 输入框提示文本
  final String contentHint; // 内容框提示文本
  final String initialTitle; // 标题初始值
  final String initialContent; // 内容初始值
  final Function(String, String) onConfirm; // 确定回调
  final VoidCallback? onCancel; // 取消回调

  const WorkAlertDialog({
    super.key,
    required this.alertTitle,
    this.inputHint = "请输入标题",
    this.contentHint = "请输入内容",
    this.initialTitle = "",
    this.initialContent = "",
    required this.onConfirm,
    this.onCancel,
  });

  @override
  State<WorkAlertDialog> createState() => _WorkAlertDialogState();
}

class _WorkAlertDialogState extends State<WorkAlertDialog> {
  late TextEditingController _titleController;
  late TextEditingController _contentController;

  @override
  void initState() {
    super.initState();
    _titleController = TextEditingController(text: widget.initialTitle);
    _contentController = TextEditingController(text: widget.initialContent);
  }

  @override
  Widget build(BuildContext context) {
    return Dialog(
      backgroundColor: Colors.transparent,
      child: Container(
        width: MediaQuery.of(context).size.width * 0.6,
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(5),
        ),
        padding: const EdgeInsets.all(15),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 弹窗标题
            Text(
              widget.alertTitle,
              style: const TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.bold,
              ),
            ),
            const SizedBox(height: 5),

            // 标题输入框
            TextField(
              controller: _titleController,
              decoration: InputDecoration(
                border: const OutlineInputBorder(),
                hintText: widget.inputHint,
                contentPadding: const EdgeInsets.symmetric(horizontal: 10, vertical: 8),
              ),
            ),
            const SizedBox(height: 20),

            // 内容标题
            const Text(
              '内容：',
              style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
            ),
            const SizedBox(height: 5),

            // 内容输入框
            TextField(
              controller: _contentController,
              maxLines: 4,
              decoration: InputDecoration(
                border: const OutlineInputBorder(),
                hintText: widget.contentHint,
                contentPadding: const EdgeInsets.symmetric(horizontal: 10, vertical: 12),
              ),
            ),
            const SizedBox(height: 10),
            Padding(
              padding: EdgeInsets.only(left: 40, right: 40),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  ElevatedButton(
                    child: const Text('确定', style: TextStyle(color: Colors.white, fontSize: 14),),
                    style: ElevatedButton.styleFrom(
                      // maximumSize: Size(50, 30),
                      minimumSize: Size(50, 30),
                      backgroundColor: Colors.blue,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(5),
                      ),
                    ),
                    onPressed: () {
                      Navigator.of(context).pop(); // 关闭弹窗
                      widget.onConfirm(
                          _titleController.text,
                          _contentController.text
                      );
                    },
                  ),
                  ElevatedButton(

                    onPressed: () {
                      Navigator.of(context).pop(); // 关闭弹窗
                      widget.onCancel?.call();
                    },
                    style: ElevatedButton.styleFrom(
                      // maximumSize: Size(50, 30),
                      minimumSize: Size(50, 30),

                      backgroundColor: Colors.red,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(5),
                      ),
                    ),
                    child: const Text('取消', style: TextStyle(color: Colors.white, fontSize: 14),),
                  )
                ],
              ),
            )
            // 按钮行

          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    _titleController.dispose();
    _contentController.dispose();
    super.dispose();
  }
}

// 在其他页面调用弹窗的方法
void showWorkAlert({
  required BuildContext context,
  required String alertTitle,
  String inputHint = "请输入标题",
  String contentHint = "请输入内容",
  String initialTitle = "",
  String initialContent = "",
  required Function(String title, String content) onConfirm,
  VoidCallback? onCancel,
}) {
  showDialog(
    context: context,
    barrierColor: Colors.black.withOpacity(0.5), // 黑色半透明背景
    builder: (context) => WorkAlertDialog(
      alertTitle: alertTitle,
      inputHint: inputHint,
      contentHint: contentHint,
      initialTitle: initialTitle,
      initialContent: initialContent,
      onConfirm: onConfirm,
      onCancel: onCancel,
    ),
  );
}