import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:supplier_new/common/settings.dart';

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

  @override
  State<AddCreditTransactionPage> createState() =>
      _AddCreditTransactionPageState();
}

class _AddCreditTransactionPageState extends State<AddCreditTransactionPage> {
  final _formKey = GlobalKey<FormState>();

  String? _transactionType;
  String? _creditAccount;
  String? _paymentMode;
  String? _paymentStatus;
  DateTime? _selectedDate;

  final TextEditingController _amountController =
  TextEditingController(text: "₹500");
  final TextEditingController _transactionIdController =
  TextEditingController(text: "₹500");
  final TextEditingController _detailsController = TextEditingController();

  Future<void> _pickDate() async {
    DateTime now = DateTime.now();
    final picked = await showDatePicker(
      context: context,
      initialDate: now,
      firstDate: DateTime(2020),
      lastDate: DateTime(2100),
    );
    if (picked != null) {
      setState(() {
        _selectedDate = picked;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        backgroundColor: Colors.white,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back, color: Colors.black),
          onPressed: () => Navigator.pop(context),
        ),
        title:  Text(
          "Add Credit Transaction",
          style:  fontTextStyle(14,Color(0XFF2A2A2A),FontWeight.w400),
        ),
        actions: [
          TextButton(
            onPressed: () {},
            child: const Text(
              "HELP",
              style: TextStyle(color: Colors.blue),
            ),
          ),
        ],
      ),
      body: SafeArea(
        child: SingleChildScrollView(
          padding: const EdgeInsets.all(16),
          child: Form(
            key: _formKey,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                _buildDropdown(
                  label: "Transaction Type *",
                  value: _transactionType,
                  items: ["Credit", "Debit"],
                  onChanged: (val) => setState(() => _transactionType = val),
                ),
                const SizedBox(height: 12),
                _buildDropdown(
                  label: "Credit Account Name *",
                  value: _creditAccount,
                  items: ["Credit Account", "Other Account"],
                  onChanged: (val) => setState(() => _creditAccount = val),
                ),
                const SizedBox(height: 12),
                _buildTextField(
                  controller: _amountController,
                  label: "Amount (in ₹) *",
                  keyboardType: TextInputType.number,
                ),
                const SizedBox(height: 12),
                Row(
                  children: [
                    Expanded(
                      child: _buildDropdown(
                        label: "Payment Mode *",
                        value: _paymentMode,
                        items: ["Cash", "UPI", "Bank Transfer"],
                        onChanged: (val) => setState(() => _paymentMode = val),
                      ),
                    ),
                    const SizedBox(width: 12),
                    Expanded(
                      child: GestureDetector(
                        onTap: _pickDate,
                        child: AbsorbPointer(
                          child: TextFormField(
                            decoration: InputDecoration(
                              labelText: "Date *",
                              hintText: "DD-MM-YYYY",
                              suffixIcon: const Icon(Icons.calendar_today),
                              border: OutlineInputBorder(
                                borderRadius: BorderRadius.circular(8),
                              ),
                            ),
                            controller: TextEditingController(
                              text: _selectedDate == null
                                  ? ""
                                  : DateFormat("dd-MM-yyyy")
                                  .format(_selectedDate!),
                            ),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 12),
                _buildTextField(
                  controller: _transactionIdController,
                  label: "Transaction ID *",
                ),
                const SizedBox(height: 12),
                _buildDropdown(
                  label: "Payment Status *",
                  value: _paymentStatus,
                  items: ["Pending", "Completed", "Failed"],
                  onChanged: (val) => setState(() => _paymentStatus = val),
                ),
                const SizedBox(height: 12),
                _buildTextField(
                  controller: _detailsController,
                  label: "Additional Details",
                ),
                const SizedBox(height: 24),
                ElevatedButton(
                  onPressed: () {
                    if (_formKey.currentState!.validate()) {
                      // Handle save
                      ScaffoldMessenger.of(context).showSnackBar(
                        const SnackBar(content: Text("Transaction Saved")),
                      );
                    }
                  },
                  style: ElevatedButton.styleFrom(
                    backgroundColor: Colors.blue,
                    padding: const EdgeInsets.symmetric(vertical: 14),
                    minimumSize: const Size(double.infinity, 50),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(12),
                    ),
                  ),
                  child: const Text("Save Transaction"),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildDropdown({
    required String label,
    required String? value,
    required List<String> items,
    required Function(String?) onChanged,
  }) {
    return DropdownButtonFormField<String>(
      value: value,
      isExpanded: true,
      decoration: InputDecoration(
        labelText: label,
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(8),
        ),
      ),
      items: items
          .map((e) => DropdownMenuItem(
        value: e,
        child: Text(e),
      ))
          .toList(),
      onChanged: onChanged,
    );
  }

  Widget _buildTextField({
    required TextEditingController controller,
    required String label,
    TextInputType keyboardType = TextInputType.text,
  }) {
    return TextFormField(
      controller: controller,
      keyboardType: keyboardType,
      decoration: InputDecoration(
        labelText: label,
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(8),
        ),
      ),
    );
  }
}
