{% extends 'base.html' %}
{% load static %}

{% block title %}Edit Receipt - {{ receipt.receipt_number }}{% endblock %}

{% block content %}
<div class="min-h-screen bg-gray-50">
    <!-- Header -->
    <div class="bg-gradient-to-r from-blue-600 to-blue-800 text-white py-8">
        <div class="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex items-center space-x-4">
                <div class="bg-white bg-opacity-20 p-3 rounded-full">
                    <span>✏️</span>
                </div>
                <div>
                    <h1 class="text-3xl font-bold">Edit Receipt</h1>
                    <p class="text-blue-100 mt-1">Receipt Number: {{ receipt.receipt_number }}</p>
                </div>
            </div>
        </div>
    </div>

    <div class="max-w-4xl mx-auto py-8 px-4 sm:px-6 lg:px-8">
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-8">
            <form method="post" class="space-y-6">
                {% csrf_token %}
                
                <!-- Receipt Information -->
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">Receipt Number</label>
                        <input type="text" value="{{ receipt.receipt_number }}" disabled 
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg bg-gray-50 text-gray-500">
                    </div>
                    
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">Borrower</label>
                        <input type="text" value="{{ receipt.borrower.get_full_name }}" disabled 
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg bg-gray-50 text-gray-500">
                    </div>
                    
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">Loan Number</label>
                        <input type="text" value="{{ receipt.loan.loan_number }}" disabled 
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg bg-gray-50 text-gray-500">
                    </div>
                    
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">Payment Date</label>
                        <input type="text" value="{{ receipt.payment_date|date:'M d, Y H:i' }}" disabled 
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg bg-gray-50 text-gray-500">
                    </div>
                </div>

                <!-- Editable Fields -->
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <label for="amount_paid" class="block text-sm font-medium text-gray-700 mb-2">Amount Paid *</label>
                        <input type="number" step="0.01" name="amount_paid" id="amount_paid" 
                               value="{{ receipt.amount_paid }}" required
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                    </div>
                    
                    <div>
                        <label for="payment_method" class="block text-sm font-medium text-gray-700 mb-2">Payment Method *</label>
                        <select name="payment_method" id="payment_method" required
                                class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
                            {% for method_value, method_label in payment_methods %}
                                <option value="{{ method_value }}" {% if receipt.payment_method == method_value %}selected{% endif %}>
                                    {{ method_label }}
                                </option>
                            {% endfor %}
                        </select>
                    </div>
                </div>

                <!-- Balance Information (Read-only) -->
                <div class="bg-gray-50 rounded-lg p-6">
                    <h3 class="text-lg font-semibold text-gray-900 mb-4">Balance Information</h3>
                    <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Previous Balance</label>
                            <div class="text-lg font-semibold text-gray-900">KES {{ receipt.previous_balance|floatformat:2 }}</div>
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Amount Paid</label>
                            <div class="text-lg font-semibold text-green-600">KES {{ receipt.amount_paid|floatformat:2 }}</div>
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">New Balance</label>
                            <div class="text-lg font-semibold {% if receipt.new_balance > 0 %}text-red-600{% else %}text-green-600{% endif %}">
                                KES {{ receipt.new_balance|floatformat:2 }}
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Action Buttons -->
                <div class="flex justify-between items-center pt-6 border-t border-gray-200">
                    <a href="{% url 'utils:receipts_list' %}" 
                       class="px-6 py-2 border border-gray-300 rounded-lg text-gray-700 hover:bg-gray-50 transition-colors">
                        <span>←</span>Back to Receipts
                    </a>
                    
                    <div class="flex space-x-3">
                        <a href="{% url 'utils:view_receipt' receipt.id %}" target="_blank"
                           class="px-6 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 transition-colors">
                            <i class="fas fa-eye mr-2"></i>Preview
                        </a>
                        
                        <button type="submit" 
                                class="px-6 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors">
                            <span>💾</span>Save Changes
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
{% endblock %}