import 'package:absenkuv3/Bloc/Shift/ShiftKaryawanBloc.dart';
import 'package:absenkuv3/Repository/Model/PageModel/mTukarShiftPage.dart';
import 'package:absenkuv3/Repository/Model/mFilter.dart';
import 'package:absenkuv3/Setting/value_image.dart';
import 'package:absenkuv3/UI/Modul/TukarShift/Template/KomponenShift.dart';
import 'package:absenkuv3/UI/Template/DateRangePicker/Bloc/DateRangePickerBloc.dart';
import 'package:absenkuv3/UI/Template/DateRangePicker/UI/templateDateRangePicker.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class ListTukarShiftKaryawan extends StatefulWidget {
  final PageTukarShiftModel redaksional;
  ListTukarShiftKaryawan(this.redaksional);

  @override
  _ListTukarShiftKaryawanState createState() => _ListTukarShiftKaryawanState();
}

class _ListTukarShiftKaryawanState extends State<ListTukarShiftKaryawan> {
  final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
  ScrollController _scrollController = new ScrollController();
    final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    BlocProvider.of<DateRangePickerBloc>(context).add(SetDate([DateTime(DateTime.now().year,DateTime.now().month, 1),DateTime(DateTime.now().year, DateTime.now().month, DateTime.now().day)]));BlocProvider.of<DateRangePickerBloc>(context).add(SetDate([DateTime(DateTime.now().year,DateTime.now().month, 1),DateTime(DateTime.now().year, DateTime.now().month+1,0)]));
    _scrollController.addListener(()=>BlocProvider.of<ShiftKaryawanBloc>(context).add(LoadMoreEvent(context, scaffoldKey, _scrollController)));
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      key: scaffoldKey,
      body: Stack(
        children: [
          BlocBuilder<ShiftKaryawanBloc, ShiftKaryawanState>(
              builder: (BuildContext context, ShiftKaryawanState state){
                if(state is GetListShiftKaryawanSuccessState || state is LoadMoreProgressState)
                  return Stack(
                    children: <Widget>[
                      ListView.builder(
                          physics: AlwaysScrollableScrollPhysics(),
                          controller: _scrollController,
                          itemCount: state.data!.length,
                          itemBuilder: (BuildContext context, index) {
                            return Column(
                              children: [
                                CardTukarShift(state.data![index],'shiftKaryawan', scaffoldKey,formKey, widget.redaksional),
                                index == state.data!.length - 1 ? SizedBox(height: 75) : Container()
                              ],
                            );
                          }
                      ),
                      state is LoadMoreProgressState? Positioned(
                          bottom : 75,
                          child: Container(
                              width: MediaQuery.of(context).size.width,
                              child: Center(child: CircularProgressIndicator(strokeWidth: 8,)))):Container(),
                    ],
                  );
                else if(state is ShiftKaryawanProgressState) return Center(child: CircularProgressIndicator(strokeWidth: 8,),);
                else return Center(child: Image.asset(ValueImage.noData, height: 250,),);
              }),
          Positioned(
            bottom: 16,
            right: 14,
            child: BlocListener<DateRangePickerBloc, DateRangePickerState>(
              listener: (BuildContext context, DateRangePickerState dateRangePickerState){
                if(dateRangePickerState is DateAvailable) BlocProvider.of<ShiftKaryawanBloc>(context).add(GetListShiftKaryawanEvent(context, ModelFilter(dateRange: dateRangePickerState.date)));
                },
              child: TemplateDateRangePicker(
                firstDate: DateTime(DateTime.now().year, DateTime.now().month - 3, 1),
                lastDate: DateTime(DateTime.now().year, DateTime.now().month + 3, 0),
              ),
            ),
          ),
        ],
      ),
    );
  }
}


