@extends('layouts.app')

@section('page-title', 'Add Expense')

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-md-8 offset-md-2">
            <div class="card">
                <div class="card-header">
                    <h4>Add New Expense</h4>
                </div>
                <div class="card-body">
                    <form action="{{ route('expenses.store') }}" method="POST">
                        @csrf

                        <div class="row">
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label for="date" class="form-label">Date *</label>
                                    <input type="date" class="form-control" id="date" name="date" value="{{ old('date', now()->toDateString()) }}" required>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label for="expense_type_id" class="form-label">Expense Type *</label>
                                    <select class="form-select" id="expense_type_id" name="expense_type_id" required>
                                        <option value="">Select Type</option>
                                        @foreach($expenseTypes as $type)
                                            <option value="{{ $type->id }}" {{ old('expense_type_id') == $type->id ? 'selected' : '' }}>{{ $type->name }}</option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>
                        </div>

                        <div class="row">
                           <!-- <div class="col-md-6">
                                <div class="mb-3">
                                    <label for="segment_id" class="form-label">Segment</label>
                                    <select class="form-select" id="segment_id" name="segment_id">
                                        <option value="">Select Segment</option>
                                        @foreach($segments as $segment)
                                            <option value="{{ $segment->id }}">{{ $segment->name }}</option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>-->
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label for="amount" class="form-label">Total Amount *</label>
                                    <input type="number" class="form-control" id="amount" name="amount" step="0.01" min="0.01" value="{{ old('amount') }}" required>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label for="paid_amount" class="form-label">Paid Amount</label>
                                    <input type="number" class="form-control" id="paid_amount" name="paid_amount" step="0.01" min="0" value="{{ old('paid_amount', old('amount')) }}" placeholder="Leave blank if paying full">
                                    <small class="text-muted">Enter amount paid now. Leave blank or equal to total for full payment.</small>
                                </div>
                            </div>
                        </div>

                        <div class="mb-3">
                            <label for="payment_method" class="form-label">Payment Method *</label>
                            <select class="form-select" id="payment_method" name="payment_method" required>
                                <option value="" {{ old('payment_method') == '' ? 'selected' : '' }}>Select Method</option>
                                <option value="cash" {{ old('payment_method') == 'cash' ? 'selected' : '' }}>Cash</option>
                                <option value="bank" {{ old('payment_method') == 'bank' ? 'selected' : '' }}>Bank ({{ count($bankAccounts) }} available)</option>
                                <option value="digital" {{ old('payment_method') == 'digital' ? 'selected' : '' }}>Digital ({{ count($digitalPayments) }} available)</option>
                            </select>
                        </div>

                        <div class="mb-3" id="bank_account_field" style="display: none;">
                            <label for="bank_account_id" class="form-label">Bank Account</label>
                            <select class="form-select" id="bank_account_id" name="bank_account_id">
                                <option value="">Select Account</option>
                                @foreach($bankAccounts as $account)
                                    <option value="{{ $account->id }}" {{ old('bank_account_id') == $account->id ? 'selected' : '' }}>{{ $account->bank_name }} - {{ $account->account_name }}</option>
                                @endforeach
                            </select>
                        </div>

                        <div class="mb-3" id="digital_payment_field" style="display: none;">
                            <label for="digital_payment_id" class="form-label">Digital Payment</label>
                            <select class="form-select" id="digital_payment_id" name="digital_payment_id">
                                <option value="">Select Account</option>
                                @foreach($digitalPayments as $payment)
                                    <option value="{{ $payment->id }}" {{ old('digital_payment_id') == $payment->id ? 'selected' : '' }}>{{ $payment->provider }} - {{ $payment->account_number }}</option>
                                @endforeach
                            </select>
                        </div>

                        <div class="mb-3">
                            <label for="description" class="form-label">Description</label>
                            <textarea class="form-control" id="description" name="description" rows="3">{{ old('description') }}</textarea>
                        </div>

                        <div class="d-flex justify-content-between">
                            <a href="{{ route('expenses.index') }}" class="btn btn-secondary">Cancel</a>
                            <button type="submit" class="btn btn-primary">Create Expense</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    function togglePaymentFields() {
        var method = document.getElementById('payment_method').value;
        var bankField = document.getElementById('bank_account_field');
        var digitalField = document.getElementById('digital_payment_field');

        if (method === 'bank') {
            bankField.style.display = 'block';
            digitalField.style.display = 'none';
        } else if (method === 'digital') {
            bankField.style.display = 'none';
            digitalField.style.display = 'block';
        } else {
            bankField.style.display = 'none';
            digitalField.style.display = 'none';
        }
    }

    // Initialize on page load
    document.addEventListener('DOMContentLoaded', function() {
        var paymentMethodSelect = document.getElementById('payment_method');
        if (paymentMethodSelect) {
            paymentMethodSelect.addEventListener('change', togglePaymentFields);
            // Call immediately to show fields if already selected (form resubmission case)
            togglePaymentFields();
        }
    });
</script>
@endsection
