import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../../../const/colors.dart';
import '../bloc/comment_bloc.dart';
import '../model/comment_model.dart';

class CommentWidget extends StatelessWidget {
  //CommentWidget({Key? key}) : super(key: key);

  CommentModel? commentModel;
  Function(CommentModel commentModel)? onData;
  CommentWidget(this.onData, {super.key});
  @override
  Widget build(BuildContext context) {
    BlocProvider.of<CommentBloc>(context).add(
        RequestComment(context: context));
    return
      BlocConsumer<CommentBloc, CommentState>(
          listener: (context, state) async {},
          builder: (context, state) {
            return
              Column(
              children: [
                Padding(
                  padding: const EdgeInsets.fromLTRB(10, 10, 0, 0),
                  child: Row(
                    children: [
                      const Text('Nhan xet cua phu huynh',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 15),),
                      const SizedBox(width: 100),
                      TextButton(
                          child: Text('Xem Them  >>'),
                          onPressed: () {
                            print('Pressed');
                          }
                      )
                    ],
                  ),
                ),
                BlocBuilder<CommentBloc, CommentState>(
                    builder: (context, state) {
                      if(state is CommentInitial){
                        return const Text('CommentInitial');
                      }
                      if(state is CommentFailed){
                        //const CircularProgressIndicator();
                        return Text('CommentFailed');
                      }
                      if(state is CommentLoading){
                        return const Text('CommentLoading');
                      }
                      if(state is CommentSuccess){
                        commentModel = state.commentModel;
                        onData!(commentModel!);
                        return
                          Container(
                            decoration: const BoxDecoration(
                              borderRadius: BorderRadius.all(Radius.circular(10)),
                              color: AppColors.background,
                              boxShadow: [
                                BoxShadow(
                                  color: AppColors.primary_light,
                                  spreadRadius: 1,
                                ),
                              ],
                            ),
                            height: 250,
                            width: double.maxFinite,
                            child:
                            PageView.builder(
                                itemCount: state.commentModel!.data!.length,
                                itemBuilder: (BuildContext context, int index) {
                                  return
                                    Column(
                                      children: [
                                        Text(state.commentModel!.data![index].courseId!),
                                        Text(state.commentModel!.data![index].courseName!),
                                      ],
                                    );
                                },
                                onPageChanged: (int index) {

                                }),
                          );
                      }
                      return const Text('NewFailed');
                    }
                ),
                const SizedBox(height: 100,),
              ],
            );
          }
      );
  }
}
