import 'package:enaklo_pos/core/components/components.dart';
import 'package:enaklo_pos/core/components/custom_modal_dialog.dart';
import 'package:enaklo_pos/core/constants/colors.dart';
import 'package:enaklo_pos/presentation/customer/bloc/customer_form/customer_form_bloc.dart';
import 'package:enaklo_pos/presentation/customer/bloc/customer_loader/customer_loader_bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

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

  @override
  State<FormCustomerDialog> createState() => _FormCustomerDialogState();
}

class _FormCustomerDialogState extends State<FormCustomerDialog> {
  TextEditingController nameController = TextEditingController();
  TextEditingController phoneController = TextEditingController();
  TextEditingController emailController = TextEditingController();
  TextEditingController addressController = TextEditingController();

  final _formKey = GlobalKey<FormState>();

  void onSave() async {
    if (_formKey.currentState!.validate()) {
      context.read<CustomerFormBloc>().add(
            CustomerFormEvent.create(
              name: nameController.text,
              phoneNumber: phoneController.text,
              email: emailController.text,
              address: addressController.text,
              isActive: true,
            ),
          );
    }
  }

  @override
  Widget build(BuildContext context) {
    return CustomModalDialog(
      title: 'Tambah Pelanggan',
      subtitle: 'Tambahkan pelanggan baru',
      contentPadding:
          const EdgeInsets.symmetric(horizontal: 16.0, vertical: 24.0),
      child: Form(
        key: _formKey,
        child: Column(
          children: [
            CustomTextField(
              controller: nameController,
              label: 'Name *',
              validator: (value) {
                if (value == null || value.isEmpty) {
                  return 'Nama harus diisi';
                }
                return null;
              },
            ),
            SpaceHeight(16),
            CustomTextField(
              controller: emailController,
              label: 'Email (Opsional)',
            ),
            SpaceHeight(16),
            CustomTextField(
              controller: phoneController,
              keyboardType: TextInputType.phone,
              label: 'No. Handphone (Opsional)',
            ),
            SpaceHeight(16),
            CustomTextField(
              controller: addressController,
              label: 'Alamat (Opsional)',
              maxLines: 5,
            ),
            SpaceHeight(24),
            BlocListener<CustomerFormBloc, CustomerFormState>(
              listener: (context, state) {
                state.maybeWhen(
                  orElse: () {},
                  success: () {
                    Navigator.pop(context);
                    ScaffoldMessenger.of(context).showSnackBar(
                      const SnackBar(
                        content: Text('Pelanggan berhasil disimpan'),
                        backgroundColor: AppColors.green,
                      ),
                    );
                    context
                        .read<CustomerLoaderBloc>()
                        .add(const CustomerLoaderEvent.getCustomer());
                  },
                  error: (message) =>
                      ScaffoldMessenger.of(context).showSnackBar(
                    SnackBar(
                      content: Text(message),
                      backgroundColor: AppColors.red,
                    ),
                  ),
                );
              },
              child: BlocBuilder<CustomerFormBloc, CustomerFormState>(
                builder: (context, state) {
                  return state.maybeWhen(
                    orElse: () => Button.filled(
                      onPressed: onSave,
                      label: 'Simpan',
                    ),
                    loading: () => Center(
                      child: const CircularProgressIndicator(),
                    ),
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}
