import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_pos_ecommerce/core/components/custom_text_field.dart';
import 'package:flutter_pos_ecommerce/core/extensions/build_context_ext.dart';
import 'package:flutter_pos_ecommerce/core/extensions/string_ext.dart';

import '../../../../../core/components/buttons.dart';
import '../../../../../core/components/spaces.dart';
import '../../../../ecommerce/data/models/responses/discount_response_model.dart';
import '../bloc/discount/discount_bloc.dart';

class FormUpdateDiscountDialog extends StatefulWidget {
  final Discount? data;
  const FormUpdateDiscountDialog({
    super.key,
    this.data,
  });

  @override
  State<FormUpdateDiscountDialog> createState() =>
      _FormUpdateDiscountDialogState();
}

class _FormUpdateDiscountDialogState extends State<FormUpdateDiscountDialog> {
  final nameController = TextEditingController();
  final descriptionController = TextEditingController();
  final discountController = TextEditingController();

  @override
  void initState() {
    super.initState();
    if (widget.data != null) {
      nameController.text = widget.data!.name.toString();
      descriptionController.text = widget.data!.description ?? '';
      discountController.text = widget.data!.value.toString();
    }
  }

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          IconButton(
            onPressed: () => context.pop(),
            icon: const Icon(Icons.close),
          ),
          Text(widget.data == null ? 'Tambah Diskon' : 'Edit Diskon'),
          const Spacer(),
        ],
      ),
      content: SingleChildScrollView(
        child: SizedBox(
          width: context.deviceWidth / 1,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              CustomTextField(
                controller: nameController,
                label: 'Nama Diskon',
                onChanged: (value) {},
              ),
              const SpaceHeight(24.0),
              CustomTextField(
                controller: descriptionController,
                label: 'Deskripsi (Opsional)',
                onChanged: (value) {},
              ),
              const SpaceHeight(24.0),
              CustomTextField(
                controller: discountController,
                label: 'Percent',
                prefixIcon: const Icon(Icons.percent),
                onChanged: (value) {},
                keyboardType: TextInputType.number,
              ),
              const SpaceHeight(24.0),
              BlocConsumer<DiscountBloc, DiscountState>(
                listener: (context, state) {
                  state.maybeWhen(
                    orElse: () {},
                    success: (message) {
                      Navigator.of(context).pop();
                      context
                          .read<DiscountBloc>()
                          .add(const DiscountEvent.getDiscounts());
                    },
                  );
                },
                builder: (context, state) {
                  return state.maybeWhen(
                    orElse: () {
                      return Button.filled(
                        onPressed: () {
                          if (widget.data != null) {
                            context.read<DiscountBloc>().add(
                                  DiscountEvent.updateDiscount(
                                    id: widget.data!.id!,
                                    name: nameController.text,
                                    description: descriptionController.text,
                                    value:
                                        double.parse(discountController.text),
                                  ),
                                );
                          } else {}
                        },
                        label: 'Ubah Diskon',
                      );
                    },
                    loading: () {
                      return const Center(
                        child: CircularProgressIndicator(),
                      );
                    },
                  );
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}
