@extends('admin.layouts.app')
@section('title','EMI / Installment')
@section('content')
<div class="page-head"><div><h1 class="page-title">EMI / Installment</h1><div class="page-subtitle">Installment sale, schedule and collection</div></div></div>

<form method="POST" action="{{ route('admin.installments.store') }}" class="card panel-tight grid-4">@csrf<div><label>Invoice No</label><input name="sale_invoice_no"></div><div><label>Customer</label><select name="customer_id" required>@foreach($customers as $customer)<option value="{{ $customer->id }}">{{ $customer->name }}</option>@endforeach</select></div><div><label>Branch</label><select name="branch_id"><option value="">Select</option>@foreach($branches as $branch)<option value="{{ $branch->id }}">{{ $branch->name }}</option>@endforeach</select></div><div><label>Status</label><select name="status"><option>Active</option><option>Closed</option><option>Defaulted</option></select></div><div><label>Principal</label><input type="number" step="0.01" name="principal_amount" required></div><div><label>Down Payment</label><input type="number" step="0.01" name="down_payment"></div><div><label>Installment Amount</label><input type="number" step="0.01" name="installment_amount" required></div><div><label>Installment Count</label><input type="number" name="installment_count" value="6"></div><div><label>Interval Days</label><input type="number" name="interval_days" value="30"></div><div><label>Start Date</label><input type="date" name="start_date" value="{{ date('Y-m-d') }}"></div><div><label>Penalty / Late Day</label><input type="number" step="0.01" name="penalty_per_late_day" value="0"></div><div><label>Notes</label><input name="notes"></div><div class="form-actions" style="grid-column:1/-1"><button class="btn primary">Create EMI Plan</button><a class="btn" href="{{ route('admin.installments.defaulters') }}">Defaulters</a></div></form>
<div class="card panel-tight table-wrap"><table class="table"><thead><tr><th>ID</th><th>Invoice</th><th>Customer</th><th>Principal</th><th>Inst.</th><th>Status</th><th>Action</th></tr></thead><tbody>@forelse($plans as $plan)<tr><td>{{ $plan->id }}</td><td>{{ $plan->sale_invoice_no ?: '—' }}</td><td>{{ $plan->customer_id }}</td><td>{{ $plan->principal_amount }}</td><td>{{ $plan->installment_count }} x {{ $plan->installment_amount }}</td><td>{{ $plan->status }}</td><td><details><summary class="btn">Collect</summary><form method="POST" action="{{ route('admin.installments.collect',$plan) }}" class="grid-2" style="margin-top:10px">@csrf<div><label>Payment ID</label><select name="payment_id">@foreach(\App\Models\InstallmentPayment::where('installment_sale_id',$plan->id)->where('status','!=','Paid')->get() as $payment)<option value="{{ $payment->id }}">#{{ $payment->id }} - {{ $payment->scheduled_date }} - {{ $payment->amount_due }}</option>@endforeach</select></div><div><label>Amount Paid</label><input type="number" step="0.01" name="amount_paid" required></div><div><label>Paid Date</label><input type="date" name="paid_date" value="{{ date('Y-m-d') }}"></div><div><label>Late Fee</label><input type="number" step="0.01" name="late_fee"></div><div style="grid-column:1/-1"><label>Notes</label><input name="notes"></div><div class="form-actions" style="grid-column:1/-1"><button class="btn primary">Save Collection</button></div></form></details></td></tr>@empty<tr><td colspan="7">No EMI plans found.</td></tr>@endforelse</tbody></table>{{ $plans->links() }}</div>

@endsection
