import 'package:flutter/material.dart';
import 'package:qhd_prevention/customWidget/custom_button.dart';
import 'package:qhd_prevention/http/ApiService.dart';
import 'package:qhd_prevention/pages/home/study/study_detail_page.dart';
import 'package:qhd_prevention/pages/my_appbar.dart';
import 'package:qhd_prevention/tools/tools.dart';

class StudyClassListPage extends StatefulWidget {
  const StudyClassListPage(this.studyData, {super.key});
  final Map studyData;
  @override
  State<StudyClassListPage> createState() => _StudyClassListPageState();
}

class _StudyClassListPageState extends State<StudyClassListPage> {
  late List<dynamic> _list = [];
  late String _classId = '';
  late String _post_id = '';
  @override
  void initState() {
    super.initState();
    _classId = widget.studyData['CLASS_ID'] ?? '';
    _post_id = widget.studyData['POST_ID'] ?? '';
    WidgetsBinding.instance.addPostFrameCallback((_) {
      _getData();
    });
  }

  Future<void> _getData() async {
    LoadingDialogHelper.show();
    try {
      final result = await ApiService.getClassList(_classId, _post_id);
      if (result['result'] == 'success') {
        final List<dynamic> newList = result['varList'] ?? [];
        setState(() {
          _list.addAll(newList);
        });
      }
    } catch (e) {
      print('加载出错: $e');
    } finally {
      LoadingDialogHelper.hide();
    }
  }
  Future<void> _nowStudy(Map item) async {
    final result = await ApiService.getVideoPermissions();
    if (result['result'] == 'success') {
      SessionService.instance.setStudyToken(result['token'] ?? '');
      pushPage(StudyDetailPage(item, widget.studyData['STUDENT_ID']), context);
    }
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppbar(title: "课程列表"),
      body: SafeArea(
        child: _list.isEmpty
            ? NoDataWidget.show() : ListView.builder(
          itemCount: _list.length,
          itemBuilder: (context, index) {
            return _buildItem(_list[index]);
          },
        ),
      ),
    );
  }

  Widget _buildItem(Map item) {

    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 15, vertical: 8),
      padding: const EdgeInsets.all(12),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(10),
        boxShadow: [
          BoxShadow(
            color: Colors.grey.shade200,
            blurRadius: 5,
            offset: Offset(0, 2),
          ),
        ],
      ),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Image.network(
            ApiService.baseImgPath + item['COVERPATH'],
            width: 100,
            height: 80,
            fit: BoxFit.fill,
          ),
          const SizedBox(width: 10),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  item['CURRICULUMNAME'],
                  style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
                ),
                Row(
                  // crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Expanded(child: Text(
                      item['CURRICULUMINTRODUCE'] ?? '',
                      softWrap: true,
                      overflow: TextOverflow.visible,
                      style: const TextStyle(color: Colors.black54),
                    ),),
                  ],
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                  const SizedBox(),
                  CustomButton(
                    text: "立即学习",
                    backgroundColor: Colors.blue,
                    height: 38,
                    onPressed: () {
                      _nowStudy(item);
                    },
                  ),
                ],)
              ],
            ),
          ),
          // const SizedBox(width: 5),

        ],
      ),
    );
  }
}
