import 'package:cached_network_image/cached_network_image.dart';
import 'package:farm_tpf/models/item_dropdown.dart';
import 'package:farm_tpf/presentation/screens/codes/models/stamp_type.dart';
import 'package:farm_tpf/presentation/screens/codes/widgets/item_column.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter/src/widgets/placeholder.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:get/get.dart';
import 'package:keyboard_dismisser/keyboard_dismisser.dart';

import '../../../utils/utils.dart';
import '../../custom_widgets/app_bar_widget.dart';
import '../../custom_widgets/button_widget.dart';
import '../../custom_widgets/date_picker/date_picker_widget.dart';
import '../../custom_widgets/dropdown/dropdown_bottom_sheet.dart';
import '../../custom_widgets/textfield/text_field_normal.dart';
import 'cubit/create_task_cubit.dart';

class CreateTaskPage extends StatefulWidget {
  final int cropId;
  const CreateTaskPage({
    super.key,
    required this.cropId,
  });

  @override
  State<CreateTaskPage> createState() => _CreateTaskPageState();
}

class _CreateTaskPageState extends State<CreateTaskPage> {
  final bloc = CreateTaskCubit();

  @override
  void initState() {
    super.initState();
    bloc.preparedData(cropId: widget.cropId);
  }

  @override
  void dispose() {
    bloc.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBarWidget(),
      body: BlocListener<CreateTaskCubit, CreateTaskState>(
        bloc: bloc,
        listener: ((context, state) {
          if (state is CreateTaskLoading) {
            SchedulerBinding.instance.addPostFrameCallback((timeTask) {
              UtilWidget.showLoading();
            });
          } else if (state is CreateTaskFailure) {
            SchedulerBinding.instance.addPostFrameCallback((timeTask) {
              UtilWidget.hideLoading();
              // UtilWidget.showToastError(state.errorMessage);
            });
          } else if (state is CreateTaskPrepareDataSuccessful) {
            SchedulerBinding.instance.addPostFrameCallback((timeTask) {
              UtilWidget.hideLoading();
            });
          }
        }),
        child: KeyboardDismisser(
          child: Container(
            child: Form(
              key: bloc.formKey,
              child: Column(
                children: [
                  Expanded(
                    child: _widgetBody(),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: ButtonWidget(
                      title: 'Cập nhật',
                      onPressed: () {
                        bloc.onSubmit(widget.cropId);
                      },
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  Widget _widgetBody() {
    return Container(
      padding: const EdgeInsets.all(16),
      child: SingleChildScrollView(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            ItemColumnWidget(
              title: 'Giao việc cho',
              child: ValueListenableBuilder<String>(
                valueListenable: bloc.selectedPEmployee,
                builder: (context, selected, _) {
                  return ValueListenableBuilder<List<ItemDropDown>>(
                    valueListenable: bloc.employees,
                    builder: (context, employees, _) {
                      return DropdownBottomSheet(
                        dataSources: employees,
                        initValue: selected,
                        onSelected: (val) {
                          bloc.selectedPEmployee.value = val.key ?? '';
                        },
                        hint: 'Giao việc cho',
                      );
                    },
                  );
                },
              ),
            ),
            const SizedBox(
              height: 8,
            ),
            ItemColumnWidget(
              title: 'Tiêu đề',
              child: TextFieldNormal(
                controller: bloc.titleNameCtl,
                maxLines: 1,
                hint: 'Tiêu đề',
              ),
            ),
            const SizedBox(
              height: 8,
            ),
            ItemColumnWidget(
              title: 'Mô tả công việc',
              child: TextFieldNormal(
                controller: bloc.detailCtl,
                maxLines: 3,
                hint: 'Mô tả công việc',
              ),
            ),
            const SizedBox(
              height: 8,
            ),
            const SizedBox(height: 8),
            ValueListenableBuilder<DateTime>(
              valueListenable: bloc.deadline,
              builder: (context, dexuat, _) {
                return ItemColumnWidget(
                  title: 'Hạn chót',
                  child: DatePickerWidget(
                    initDateTime: dexuat,
                    onUpdateDateTime: (selectedDate) {
                      if (selectedDate != null) {
                        bloc.deadline.value = selectedDate;
                      }
                    },
                  ),
                );
              },
            ),
            const SizedBox(
              height: 16,
            ),
          ],
        ),
      ),
    );
  }
}
