import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class EditListItems {
  static Widget createColumnTextItem({
    required String title,
    required String text,
}) {
    return Column(
      spacing: 5,
      children: [
        _leftLitle(false, title, Colors.black),
        Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            Expanded(child: Text(text, style: TextStyle(fontSize: 13, color: Colors.black54),))
          ],
        )
      ],
    );
  }
  static Widget createRowSpaceBetweenItem({
    required String leftText,
    required String rightText,
    double verticalPadding = 10,
    double horizontalPadding = 0,
    Color textColor = Colors.black,
    bool isRight = false,
    bool isImportent = false,
    void Function()? onTap,

  }) {
    return Padding(
      padding: EdgeInsets.symmetric(
        vertical: verticalPadding,
        horizontal: horizontalPadding,
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          _leftLitle(isImportent, leftText, textColor),
          if (isRight)
            GestureDetector(
              onTap: onTap,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(
                    rightText,
                    style: TextStyle(fontSize: 15, color: Colors.grey),
                  ),
                  SizedBox(width: 2),
                  Icon(
                    Icons.arrow_forward_ios_rounded,
                    color: Colors.black45,
                    size: 15,
                  ),
                ],
              ),
            )
          else
            Text(rightText, style: TextStyle(fontSize: 15, color: Colors.black87)),
        ],
      ),
    );
  }
  /// 提示
  static Widget bottomTipWidget() {
    return Container(
      padding: const EdgeInsets.all(15),
      color: Colors.white,
      child: Text(
        '        严禁在本互联网非涉密平台处理、传输国家秘密和工作秘密，请确认扫描、传输的文件资料不涉及国家秘密和工作秘密',
        style: TextStyle(fontSize: 14, color: Colors.red),
      ),
    );
  }
  /// 标题加输入框
  static Widget createBuildMultilineInput({
    required String label,
    required String hint,
    required TextEditingController controller,
    bool isImportent = false,

    /// 是否纵向布局（多行）
    bool isVal = false,

    /// 是否可以编辑
    bool isEdit = true,
  }) {
    const cellPadding = EdgeInsets.symmetric(vertical: 0);
    final labelWidget = _leftLitle(isImportent, label, Colors.black);

    if (isVal) {
      return Padding(
        padding: cellPadding,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            labelWidget,
            TextField(
              readOnly: !isEdit,
              controller: controller,
              keyboardType: TextInputType.multiline,
              minLines: 1,     // 最小 3 行
              maxLines: 1,     // 最多 6 行，可根据需要调整
              style: const TextStyle(fontSize: 15),
              decoration: InputDecoration(
                hintText: hint,
                hintStyle: const TextStyle(color: Colors.red),
                border: InputBorder.none,
              ),
            ),
          ],
        ),
      );
    } else {
      return Padding(
        padding: cellPadding,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            labelWidget,
            const SizedBox(width: 8),
            Expanded(
              child: TextField(
                readOnly: !isEdit,
                controller: controller,
                textAlign: TextAlign.right,
                decoration: InputDecoration(
                  hintText: hint,
                  hintStyle: const TextStyle(color: Colors.red),
                  border: InputBorder.none,
                  isDense: true,  // 减小上下内边距
                  contentPadding: const EdgeInsets.symmetric(vertical: 8),
                ),
                style: const TextStyle(fontSize: 15),
                maxLines: 1,
              ),
            ),
          ],
        ),
      );
    }
  }
  /// 类型3：文本和图片上下布局
  static Widget createTextImageItem({
    required String text,
    required List<String> imageUrls,
    double imageHeight = 90,
    double verticalPadding = 10,
    double horizontalPadding = 0,
    // 点击图片时回调，index 为被点击图片的下标
    void Function(int index)? onImageTapped,
  }) {
    return Padding(
      padding: EdgeInsets.symmetric(
        vertical: verticalPadding,
        horizontal: horizontalPadding,
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _leftLitle(false, text, Colors.black),
          const SizedBox(height: 10),
          Wrap(
            spacing: 8, // 水平间距
            runSpacing: 8, // 垂直间距
            children: List.generate(imageUrls.length, (i) {
              final url = imageUrls[i];
              Widget img;
              if (url.startsWith('http')) {
                img = Image.network(
                  url,
                  height: imageHeight,
                  width: imageHeight ,
                  fit: BoxFit.cover,
                );
              } else {
                img = Image.asset(
                  url,
                  height: imageHeight,
                  width: imageHeight * 3 / 2,
                  fit: BoxFit.cover,
                );
              }
              return GestureDetector(
                onTap: () {
                  if (onImageTapped != null) onImageTapped(i);
                },
                child: ClipRRect(
                  borderRadius: BorderRadius.circular(4),
                  child: img,
                ),
              );
            }),
          ),
        ],
      ),
    );
  }
  /// 带标题的单选项行，支持自动换行和统一标题样式
  static Widget createRadioRow({
    required String label,
    required List<String> options,
    required int selectedValue,
    required ValueChanged<int> onChanged,
    bool isImportent = false,
  }) {
    const cellPadding = EdgeInsets.symmetric(vertical: 10, horizontal: 0);
    final labelWidget = _leftLitle(isImportent, label, Colors.black);

    return Padding(
      padding: cellPadding,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          labelWidget,
          const SizedBox(height: 6),
          Wrap(
            spacing: 5,
            runSpacing: 5,
            children: List.generate(options.length, (i) {
              return _buildRadioOption(
                isSelected: selectedValue == i,
                label: options[i],
                value: i,
                onChanged: (v) => onChanged(v!),
              );
            }),
          ),
        ],
      ),
    );
  }
  ///  分类头部
  static Widget createYesNoSection({
    required String title,
    required String yesLabel,
    required String noLabel,
    required bool groupValue,
    required ValueChanged<bool> onChanged,
    double verticalPadding = 15,
    double horizontalPadding = 10,
    bool isImportant = true,
  }) {
    return Padding(
      padding: EdgeInsets.only(top: 0, right: horizontalPadding, left: horizontalPadding, bottom: verticalPadding),
      child: Container(
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(5),
        ),
        child: Row(
          children: [
            Expanded(
              child: Row(
                spacing: 3,
                children: [
                  if (isImportant)
                    Text("*", style: TextStyle(color: Colors.red, fontSize: 20)),
                  Text(
                    title,
                    style: TextStyle(
                      fontSize: 15,
                      color: Colors.black,
                    ),
                  ),
                ],
              )
            ),
            Row(
              children: [
                Row(
                  children: [
                    Radio<bool>(
                      activeColor: Colors.blue,
                      value: true,
                      groupValue: groupValue,
                      onChanged: (val) => onChanged(val!),
                    ),
                    Text(yesLabel),
                  ],
                ),
                const SizedBox(width: 16),
                Row(
                  children: [
                    Radio<bool>(
                      activeColor: Colors.blue,
                      value: false,
                      groupValue: groupValue,
                      onChanged: (val) => onChanged(val!),
                    ),
                    Text(noLabel),
                  ],
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
  // 单个自定义 Radio 选项
  static Widget _buildRadioOption({
    required bool isSelected,
    required String label,
    required int value,
    required ValueChanged<int?> onChanged,
  }) {
    return InkWell(
      onTap: () => onChanged(value),
      splashColor: Colors.blue.withAlpha(50),
      borderRadius: BorderRadius.circular(20),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Icon(
            isSelected ? Icons.check_circle : Icons.circle_outlined,
            size: 22,
            color: isSelected ? Colors.blue : Colors.grey,
          ),
          const SizedBox(width: 8),
          Text(
            label,
            style: TextStyle(
              fontSize: 15,
              color: isSelected ? Colors.blue : Colors.grey[700],
              fontWeight: isSelected ? FontWeight.bold : FontWeight.normal,
            ),
          ),
          const SizedBox(width: 8),
        ],
      ),
    );
  }

  static Widget _leftLitle(bool isImportant, String title, Color textColor) {
    return Row(
      spacing: 3,

      children: [
        if (isImportant)
          Text("*", style: TextStyle(color: Colors.red, fontSize: 20)),
        Text(title, style: TextStyle(fontSize: 15, color: textColor)),
      ],
    );
  }
}
