import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:qhd_prevention/customWidget/photo_picker_row.dart';
import 'package:qhd_prevention/customWidget/toast_util.dart';
import 'package:qhd_prevention/http/ApiService.dart';
import 'package:qhd_prevention/pages/home/NFC/home_nfc_check_danger_page.dart';
import 'package:qhd_prevention/pages/my_appbar.dart';
import 'package:qhd_prevention/tools/tools.dart';

// feedback_type.dart
enum NfcFeedbackType {
  readError('读取失败', 0),
  nfcBld('标签损坏', 1),
  nfcLose('标签丢失', 2);

  final String typeName;
  final int type;

  const NfcFeedbackType(this.typeName, this.type);
}

class NfcQuestionFecebook extends StatefulWidget {
  const NfcQuestionFecebook({super.key, required this.info,required this.taskInfo});

  final Map taskInfo;
  final Map info;

  @override
  State<NfcQuestionFecebook> createState() => _NfcQuestionFecebookState();
}

class _NfcQuestionFecebookState extends State<NfcQuestionFecebook> {
  final _formKey = GlobalKey<FormState>();
  final TextEditingController _descriptionController = TextEditingController();
  final FocusNode _descriptionFocus = FocusNode(); // <- 新增

  // 反馈类型
  NfcFeedbackType? _selectedType = NfcFeedbackType.readError;

  // 上传的图片
  List<String> _images = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getFacebookDetail();
  }

  Future<void> _getFacebookDetail() async {
    final result = await ApiService.getNfcFeedBackDetail({});
    try{
      if (result['result'] == 'success') {

      }
    }catch(e) {}
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppbar(title: "NFC异常上报"),
      body: Container(
        color: Colors.white,
        child: Form(
          key: _formKey,
          child: SingleChildScrollView(
            padding: const EdgeInsets.all(16),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                // 问题描述
                const Text(
                  '详细问题',
                  style: TextStyle(fontWeight: FontWeight.bold),
                ),
                const SizedBox(height: 8),
                TextFormField(
                  controller: _descriptionController,
                  focusNode: _descriptionFocus,
                  autofocus: false,
                  maxLines: 5,
                  decoration: const InputDecoration(
                    hintText: '请补充详细问题...',
                    border: OutlineInputBorder(),
                    contentPadding: EdgeInsets.all(10),
                  ),
                  validator: (value) {
                    if (value == null || value.isEmpty) {
                      return '请补充详细问题';
                    }
                    return null;
                  },
                ),
                const SizedBox(height: 24),

                // 反馈类型
                const Text(
                  '反馈类型',
                  style: TextStyle(fontWeight: FontWeight.bold),
                ),
                const SizedBox(height: 8),
                Wrap(
                  spacing: 16,
                  children:
                      NfcFeedbackType.values.map((type) {
                        return ChoiceChip(
                          label: Text(type.typeName),
                          selected: _selectedType == type,
                          onSelected: (selected) {
                            setState(() {
                              if (selected) {
                                _selectedType = type;
                              }
                            });
                          },
                        );
                      }).toList(),
                ),

                // 图片上传
                const SizedBox(height: 16),

                RepairedPhotoSection(
                  horizontalPadding: 0,
                  title: "请提供相关问题照片",
                  maxCount: 4,
                  mediaType: MediaType.image,
                  isCamera: true,
                  onChanged: (files) {
                    // 上传 files 到服务器
                    _images.clear();
                    for (int i = 0; i < files.length; i++) {
                      _images.add(files[i].path);
                    }
                  },
                  onAiIdentify: () {},
                ),
                const SizedBox(height: 40),

                // 提交按钮
                SizedBox(
                  width: double.infinity,
                  child: ElevatedButton(
                    onPressed: _submitFeedback,
                    style: ElevatedButton.styleFrom(
                      backgroundColor: Colors.blue,
                      padding: const EdgeInsets.symmetric(vertical: 16),
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(8),
                      ),
                    ),
                    child: const Text(
                      '下一步',
                      style: TextStyle(fontSize: 18, color: Colors.white),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  // 提交反馈
  Future<void> _submitFeedback() async {
    final text = _descriptionController.text.trim();

    if (text.isEmpty) {
      _showMessage('请填写问题');
      return;
    }

    if (_images.isEmpty) {
      _showMessage('请上传图片');
      return;
    }
    Map data = {
      ...widget.info,
      ...widget.taskInfo,
      'EXCEPTION_TYPE': _selectedType?.type,
      'MANUAL_CONFIRMATION': '1',
      // 'PHOTO_URL': imagePaths,
      'DESCRIPTION': text,
    };

    pushPage(HomeNfcCheckDangerPage(info: data, facebookImages: _images, isNfcError: true,isEdit: true,), context);

    // String imagePaths = "";
    // for (int i = 0; i < _images.length; i++) {
    //   String imagePath = await _reloadFeedBack(_images[i]);
    //
    //   if (0 == i) {
    //     imagePaths = imagePath;
    //   } else {
    //     imagePaths = "$imagePaths,$imagePath";
    //   }
    // }
    //
    // _setFeedBack(text, imagePaths);
  }
  @override
  void dispose() {
    _descriptionController.dispose();
    super.dispose();
  }


  Future<void> _setFeedBack(String text, String imagePaths) async {
    try {
      Map data = {
        "PATROL_TASK_ID":widget.taskInfo['PATROL_TASK_ID']?? '',
        "PIPELINE_AREA_ID" :widget.taskInfo['PIPELINE_AREA_ID']?? '',
        "EQUIPMENT_PIPELINE_ID" :widget.info['EQUIPMENT_PIPELINE_ID']?? '',
        "PATROL_RECORD_ID" :widget.info['EQUIPMENT_PIPELINE_ID'] ?? '',
        "PATROL_RECORD_DETAIL_ID" :widget.info['PATROL_RECORD_DETAIL_ID']?? '',

        "NFC_CODE" :widget.info['PIPELINE_AREA_ID'],
        'EXCEPTION_TYPE': _selectedType?.type,
        'PHOTO_URL': imagePaths,
        'DESCRIPTION': text,
      };

      final raw = await ApiService.nfcFeedBack(data);

      if (raw['result'] == 'success') {
        _showMessage('提交成功');
      } else {
        _showMessage('提交失败');
      }
    } catch (e) {
      // 出错时可以 Toast 或者在页面上显示错误状态
      print('加载首页数据失败：$e');
    }
  }

  void _showMessage(String msg) {
    ToastUtil.showNormal(context, msg);
  }
}
