import 'package:flutter/material.dart';

import 'dart:convert';
import 'package:path_provider/path_provider.dart';
import 'package:intl/intl.dart';
import 'package:dio/dio.dart';
import 'package:qhd_prevention/pages/home/work/laws_list_picker.dart';
import 'package:qhd_prevention/pages/home/work/laws_regulations_two_page.dart';
import 'package:qhd_prevention/pages/home/work/read_file_page.dart';

import 'package:qhd_prevention/pages/home/work/risk_list_picker.dart';
import 'package:qhd_prevention/pages/http/ApiService.dart';
import 'package:qhd_prevention/pages/my_appbar.dart';


import '../../customWidget/search_bar_widget.dart';

import '../../../tools/tools.dart';

import 'package:url_launcher/url_launcher.dart';



class LawsRegulationsPage extends StatefulWidget {
  const LawsRegulationsPage({super.key});

  @override
  State<LawsRegulationsPage> createState() => _LawsRegulationsPage();
}

class _LawsRegulationsPage extends State<LawsRegulationsPage> {
  final TextEditingController _searchController = TextEditingController();
  List<LawsListBean> _accordionList = [];
  List<dynamic> _fileList = [];
  String _keywords = '';
  String treeJson = "";

  @override
  void initState() {
    super.initState();
    _getLowList("");
  }

  Future<void> _getLowList(String keyWord) async {
    try {
      _accordionList.clear();
      _fileList.clear();
      final result = await ApiService.getLowList(keyWord);
      if (result['result'] == 'success') {
        final List<dynamic> newList = result['varList'] ?? [];
        setState(() {
          if(keyWord.isEmpty) {
            treeJson = result['zTreeNodes']??"";
            _accordionList = parseDepartments(treeJson);
          }else {
            _fileList.addAll(newList);
          }
        });
      } else {
        _showMessage('加载数据失败');
      }
    } catch (e) {
      // 出错时可以 Toast 或者在页面上显示错误状态
      print('加载数据失败：$e');
    }
  }

  List<LawsListBean> parseDepartments(String jsonString) {
    // 1. 解码 JSON 字符串
    final List<dynamic> jsonList = json.decode(jsonString);

    // 2. 转换为部门对象列表
    return jsonList
        .map((jsonItem) => LawsListBean.fromJson(jsonItem))
        .toList();
  }

  void _goToDetail(String id) {
    Navigator.push(
      context,
      MaterialPageRoute(builder: (context) => StudyDetailPage(parentId: id)),
    );
  }

  Future<void> _downloadFile(String filePath) async {
    //下载
    try {
      final Uri url = Uri.parse(ApiService.baseImgPath + filePath);
      if (!await launchUrl(url,mode:LaunchMode.externalApplication)) {
        throw Exception('Could not launch $url');
      }
    } catch (e) {
      print(e.toString());
    }
  }

  Future<void> _showFile(dynamic file) async {
    //预览
    try {
      final url =ApiService.baseImgPath + file["FILEPATH"];

      pushPage(
        ReadFilePage(
          fileUrl: url,
        ),
        context,
      );
    } catch (e) {
      print(e.toString());
    }
  }




  String _formatDate(String dateString) {
    try {
      final date = DateTime.parse(dateString);
      return DateFormat('yyyy-MM-dd HH:mm:ss').format(date);
    } catch (e) {
      return dateString;
    }
  }

  String _getFileType(String extension) {
    switch (extension) {
      case '.docx':
      case '.doc':
        return '文件属性：文档';
      case '.xls':
      case '.xlsx':
        return '文件属性：表格';
      case '.ppt':
      case '.pptx':
        return '文件属性：幻灯片';
      case '.pdf':
        return '文件属性：PDF';
      default:
        return '文件属性：';
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppbar(title: "法律法规"),
      body: Column(
        children: [
          // 搜索栏
          Container(
            color: Colors.white,
            child:Padding(
              padding: EdgeInsets.all(10),
              child: SearchBarWidget(
                controller: _searchController,
                onSearch: (keyboard) {
                  // 输入请求接口
                  _getLowList(keyboard);
                },
              ),
            ),
          ),
          Expanded(child: _buildContent()),
        ],
      ),
    );
  }

  Widget _buildContent() {
    if (_accordionList.isNotEmpty) {
      return  LawsListPicker(
        data: _accordionList,
        onSelected: (item) {
          pushPage(LawsRegulationsTwoPage(item!.id), context);
          // setState(() {
          // riskId = item?.id;
          // itemNameTwo=item!.name;
          // setResult();

          // });

        },
      );

      // return ListView.builder(
      //   itemCount: _accordionList.length,
      //   itemBuilder:
      //       (context, index) =>
      //           CustomCollapse(item: _accordionList[index], onTap: _goToDetail),
      // );
    } else if (_fileList.isNotEmpty) {
      return ListView.builder(
        itemCount: _fileList.length,
        itemBuilder:
            (context, index) => FileCard(
          file: _fileList[index],
          onDownload: _downloadFile,
          onPreview: _showFile,
          formatDate: _formatDate,
          getFileType: _getFileType,
        ),
      );
    } else {
      return NoDataWidget.show();
    }
  }

  void _showMessage(String msg) {
    ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(msg)));
  }
}

class CustomCollapse extends StatefulWidget {
  final dynamic item;
  final Function(String) onTap;

  const CustomCollapse({super.key, required this.item, required this.onTap});

  @override
  State<CustomCollapse> createState() => _CustomCollapseState();
}

class _CustomCollapseState extends State<CustomCollapse> {
  bool _isExpanded = false;

  @override
  Widget build(BuildContext context) {
    final hasChildren =
        widget.item['children'] != null &&
            (widget.item['children'] as List).isNotEmpty;

    return Card(
      margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
      child: Column(
        children: [
          ListTile(
            title: Text(
              widget.item['name'] ?? '未命名',
              style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
            ),
            trailing:
            hasChildren
                ? Icon(
              _isExpanded ? Icons.expand_less : Icons.expand_more,
              color: Colors.blue,
            )
                : null,
            onTap:
            hasChildren
                ? () => setState(() => _isExpanded = !_isExpanded)
                : () => widget.onTap(widget.item['id'] ?? ''),
          ),
          if (_isExpanded && hasChildren)
            ...(widget.item['children'] as List).map<Widget>((child) {
              return Padding(
                padding: const EdgeInsets.only(left: 16.0),
                child: ListTile(
                  title: Text(child['name']),
                  leading: const Icon(Icons.description, color: Colors.blue),
                  onTap: () => widget.onTap(child['id'] ?? ''),
                ),
              );
            }).toList(),
        ],
      ),
    );
  }
}

class FileCard extends StatelessWidget {
  final dynamic file;
  final Function(String) onDownload;
  final Function(dynamic) onPreview;
  final String Function(String) formatDate;
  final String Function(String) getFileType;

  const FileCard({
    super.key,
    required this.file,
    required this.onDownload,
    required this.onPreview,
    required this.formatDate,
    required this.getFileType,
  });

  @override
  Widget build(BuildContext context) {
    return Card(
      margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
      child: Padding(
        padding: const EdgeInsets.all(12.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              file['NAME'] ?? '未命名文件',
              style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
            const SizedBox(height: 10),
            Text(
              getFileType(file['extension_name'] ?? ''),
              style: TextStyle(color: Colors.grey[600]),
            ),
            const SizedBox(height: 5),
            Text(
              '上传日期：${formatDate(file['CTIME'] ?? '')}',
              style: TextStyle(color: Colors.grey[600]),
            ),
            const SizedBox(height: 15),
            Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                OutlinedButton.icon(
                  icon: const Icon(Icons.download, size: 18),
                  label: const Text('下载'),
                  onPressed: () => onDownload(file['FILEPATH'] ?? ''),
                  style: OutlinedButton.styleFrom(
                    foregroundColor: Colors.blue,
                    side: const BorderSide(color: Colors.blue),
                  ),
                ),
                const SizedBox(width: 10),
                ElevatedButton.icon(
                  icon: const Icon(Icons.visibility, size: 18),
                  label: const Text('预览'),
                  onPressed: () => onPreview(file),
                  style: ElevatedButton.styleFrom(
                    backgroundColor: Colors.cyan,
                    foregroundColor: Colors.white,
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

//详情页
class StudyDetailPage extends StatelessWidget {
  final String parentId;

  const StudyDetailPage({super.key, required this.parentId});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('详情页面')),
      body: Center(child: Text('父级ID: $parentId')),
    );
  }
}
