import 'package:enaklo_pos/core/components/buttons.dart';
import 'package:enaklo_pos/core/components/custom_modal_dialog.dart';
import 'package:enaklo_pos/core/components/custom_text_field.dart';
import 'package:enaklo_pos/core/components/flushbar.dart';
import 'package:enaklo_pos/core/components/spaces.dart';
import 'package:enaklo_pos/core/extensions/build_context_ext.dart';
import 'package:enaklo_pos/presentation/table/blocs/create_table/create_table_bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

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

  @override
  State<FormTableNewDialog> createState() => _FormTableNewDialogState();
}

class _FormTableNewDialogState extends State<FormTableNewDialog> {
  TextEditingController tableNameController = TextEditingController();
  TextEditingController capacityController = TextEditingController();
  TextEditingController locationController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return CustomModalDialog(
      title: 'Tambah Meja',
      subtitle: 'Silahkan isi data meja',
      contentPadding:
          const EdgeInsets.symmetric(horizontal: 16.0, vertical: 24.0),
      child: Column(
        children: [
          CustomTextField(
            controller: tableNameController,
            label: 'Nama Meja',
          ),
          SpaceHeight(16),
          CustomTextField(
            controller: capacityController,
            label: 'Kapasitas',
            keyboardType: TextInputType.number,
          ),
          SpaceHeight(16),
          CustomTextField(
            controller: locationController,
            label: 'Lokasi',
          ),
          SpaceHeight(24),
          BlocListener<CreateTableBloc, CreateTableState>(
            listener: (context, state) {
              state.maybeWhen(
                orElse: () {},
                success: (message) {
                  context.pop();
                  AppFlushbar.showSuccess(context, message);
                },
                error: (message) {
                  AppFlushbar.showError(context, message);
                },
              );
            },
            child: BlocBuilder<CreateTableBloc, CreateTableState>(
              builder: (context, state) {
                return state.maybeWhen(
                  orElse: () => Button.filled(
                    onPressed: () {
                      context.read<CreateTableBloc>().add(
                            CreateTableEvent.createTable(
                              capacity: int.parse(capacityController.text),
                              location: locationController.text,
                              tableName: tableNameController.text,
                            ),
                          );
                    },
                    label: 'Simpan',
                  ),
                  loading: () =>
                      Center(child: const CircularProgressIndicator()),
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}
