import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:loopin/api/video_api.dart';
import 'package:loopin/behavior/custom_scroll_behavior.dart';
import 'package:loopin/components/my_toast.dart';
import 'package:loopin/service/http.dart';
import 'package:loopin/utils/getCommonDictionary.dart';

class ReportChat extends StatefulWidget {
  final String convsationID;
  const ReportChat({super.key, required this.convsationID});

  @override
  State<ReportChat> createState() => _ReportChatState();
}

class _ReportChatState extends State<ReportChat> with SingleTickerProviderStateMixin {
  int? _selectedIndex;
  late dynamic args;
  final TextEditingController _reportController = TextEditingController();
  final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey();
  List<dynamic> reasonTypeData = [];

  @override
  void initState() {
    super.initState();
    args = Get.arguments ?? {};
    getReportReasons('ums_chat_report'); // 获取投诉枚举
  }

  @override
  void dispose() {
    _reportController.dispose();
    super.dispose();
  }

  // 加载数据的方法
  Future<void> getReportReasons(String key) async {
    try {
      final data = await Commondictionary.getCommonDictionary(key);
      setState(() {
        reasonTypeData = data;
      });
    } catch (e) {
      print('加载失败: $e');
    }
  }

  // 举报
  Future<void> doReport(String dictValue, String reasonText) async {
    try {
      final res = await Http.post(VideoApi.reportVideoApi, data: {
        'type': '1', // 类型 1 举报 2 投诉 3 建议
        'content': reasonText,
        'aimId': widget.convsationID,
        'aimType': '5', // 1 会员 2 群组 3 评论 4 视频 5 聊天
        'reasonType': dictValue
      });
      if (res['code'] == 200) {
        // 投诉成功，返回视频页面
        MyToast().tip(
          title: '投诉成功',
          position: 'center',
          type: 'success',
        );
        Get.back(result: {'returnTo': '/'});
      } else {
        MyToast().tip(
          title: '投诉失败',
          position: 'center',
          type: 'error',
        );
      }
    } catch (e) {
      print('投诉失败: $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: scaffoldKey,
      backgroundColor: Colors.white,
      appBar: AppBar(
        centerTitle: true,
        backgroundColor: Colors.white,
        foregroundColor: Colors.black,
        elevation: 0,
        title: const Text(
          '举报',
          style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
        ),
      ),
      body: ScrollConfiguration(
        behavior: CustomScrollBehavior().copyWith(scrollbars: false),
        child: SingleChildScrollView(
          padding: const EdgeInsets.all(16),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              const Text(
                '您的举报我们将尽快受理，核实后我们将第一时间告知受理结果，请尽量提交完整的举报描述',
                style: TextStyle(fontSize: 13, color: Colors.grey, height: 1.4),
              ),
              const SizedBox(height: 20),
              // 两列布局的投诉选项
              reasonTypeData.isEmpty
                  ? const Center(child: CircularProgressIndicator())
                  : GridView.builder(
                      shrinkWrap: true,
                      physics: const NeverScrollableScrollPhysics(),
                      gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: 2,
                        crossAxisSpacing: 8,
                        mainAxisSpacing: 2,
                        childAspectRatio: 4,
                      ),
                      itemCount: reasonTypeData.length,
                      itemBuilder: (context, index) {
                        return _buildRadioItem(index);
                      },
                    ),
              const SizedBox(height: 24),
              const Text(
                '举报描述（选填）',
                style: TextStyle(fontSize: 14, color: Colors.black54, fontWeight: FontWeight.w500),
              ),
              const SizedBox(height: 8),
              Container(
                decoration: BoxDecoration(
                  border: Border.all(color: Colors.grey.shade300),
                  borderRadius: BorderRadius.circular(4),
                ),
                child: TextField(
                  controller: _reportController,
                  maxLines: 5,
                  maxLength: 32,
                  cursorColor: Colors.black54,
                  style: const TextStyle(
                    color: Colors.black54,
                    fontSize: 14,
                  ),
                  decoration: const InputDecoration(
                    contentPadding: EdgeInsets.all(12),
                    border: InputBorder.none,
                    hintText: '请输入内容',
                    hintStyle: TextStyle(color: Colors.grey, fontSize: 13),
                  ),
                ),
              ),
              Align(
                alignment: Alignment.centerRight,
                child: Text(
                  '${_reportController.text.length}/32',
                  style: const TextStyle(fontSize: 12, color: Colors.grey),
                ),
              ),
              const SizedBox(height: 30),
              SizedBox(
                width: double.infinity,
                child: ElevatedButton(
                  onPressed: _selectedIndex != null ? _submitReport : null,
                  style: ElevatedButton.styleFrom(
                    backgroundColor: Colors.red,
                    foregroundColor: Colors.white,
                    padding: const EdgeInsets.symmetric(vertical: 14),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(6),
                    ),
                  ),
                  child: const Text('提交', style: TextStyle(fontSize: 15)),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildRadioItem(int index) {
    final item = reasonTypeData[index];
    final isSelected = _selectedIndex == index;

    return GestureDetector(
      onTap: () {
        setState(() {
          _selectedIndex = index;
        });
      },
      child: Container(
        padding: const EdgeInsets.symmetric(vertical: 8),
        child: Row(
          children: [
            Container(
              width: 20,
              height: 20,
              decoration: BoxDecoration(
                shape: BoxShape.circle,
                border: Border.all(
                  color: isSelected ? Colors.green : Colors.grey,
                  width: 1.5,
                ),
              ),
              child: isSelected
                  ? Center(
                      child: Container(
                        width: 12,
                        height: 12,
                        decoration: const BoxDecoration(
                          shape: BoxShape.circle,
                          color: Colors.green,
                        ),
                      ),
                    )
                  : null,
            ),
            const SizedBox(width: 8),
            Expanded(
              child: Text(
                item['dictLabel'] ?? '未知原因',
                style: TextStyle(
                  fontSize: 13,
                  color: isSelected ? Colors.green : Colors.black54,
                ),
                overflow: TextOverflow.ellipsis,
              ),
            ),
          ],
        ),
      ),
    );
  }

  void _submitReport() {
    if (_selectedIndex == null) {
      MyToast().tip(
        title: '请选择举报原因！',
        position: 'center',
        type: 'error',
      );
      return;
    }
    // 获取选中的原因
    final selectedReason = reasonTypeData[_selectedIndex!];
    final reasonText = _reportController.text;
    doReport(selectedReason['dictValue'], reasonText);
  }
}
