import 'package:enaklo_pos/core/components/custom_modal_dialog.dart';
import 'package:flutter/material.dart';
import 'package:enaklo_pos/core/extensions/build_context_ext.dart';

import '../../../core/components/buttons.dart';
import '../../../core/components/custom_text_field.dart';
import '../../../core/components/spaces.dart';

class FormTaxDialog extends StatefulWidget {
  final int taxValue;
  final int serviceChargeValue;
  final Function(int taxValue, int serviceChargeValue)? onSave;

  const FormTaxDialog({
    super.key,
    required this.taxValue,
    required this.serviceChargeValue,
    this.onSave,
  });

  @override
  State<FormTaxDialog> createState() => _FormTaxDialogState();
}

class _FormTaxDialogState extends State<FormTaxDialog> {
  late final TextEditingController serviceFeeController;
  late final TextEditingController taxFeeController;

  @override
  void initState() {
    super.initState();
    serviceFeeController =
        TextEditingController(text: widget.serviceChargeValue.toString());
    taxFeeController = TextEditingController(text: widget.taxValue.toString());
  }

  @override
  void dispose() {
    serviceFeeController.dispose();
    taxFeeController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return CustomModalDialog(
      title: 'Edit Perhitungan Biaya',
      contentPadding: const EdgeInsets.all(16.0),
      child: SingleChildScrollView(
        child: SizedBox(
          width: context.deviceWidth / 3,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              CustomTextField(
                controller: serviceFeeController,
                label: 'Biaya Layanan (%)',
                onChanged: (value) {},
                keyboardType: TextInputType.number,
                suffixIcon: const Icon(Icons.percent),
              ),
              const SpaceHeight(24.0),
              CustomTextField(
                controller: taxFeeController,
                label: 'Pajak PB1 (%)',
                onChanged: (value) {},
                keyboardType: TextInputType.number,
                suffixIcon: const Icon(Icons.percent),
              ),
              const SpaceHeight(24.0),
              Button.filled(
                onPressed: () {
                  final taxValue = int.tryParse(taxFeeController.text) ?? 0;
                  final serviceChargeValue =
                      int.tryParse(serviceFeeController.text) ?? 0;

                  if (widget.onSave != null) {
                    widget.onSave!(taxValue, serviceChargeValue);
                  }

                  context.pop();
                },
                label: 'Simpan',
              )
            ],
          ),
        ),
      ),
    );
  }
}
