@extends('layouts.admin')
@section('title','Invoice '.$invoice->invoice_number)
@section('content')
<div class="row">
<div class="col-lg-8">
<div class="card mb-3">
  <div class="card-header d-flex justify-content-between align-items-center flex-wrap gap-2">
    <div>
      <span class="fw-bold text-primary">{{ $invoice->invoice_number }}</span>
      <span class="badge {{ ['draft'=>'bg-secondary','sent'=>'bg-primary','paid'=>'bg-success','overdue'=>'bg-danger','partial'=>'bg-warning text-dark','cancelled'=>'bg-secondary'][$invoice->status]??'bg-secondary' }} ms-2">{{ strtoupper($invoice->status) }}</span>
      @if($invoice->is_recurring)<span class="badge bg-info ms-1"><i class="fas fa-rotate me-1"></i>Recurring</span>@endif
    </div>
    <div class="d-flex gap-2">
      @if($invoice->status==='draft')
      <a href="{{ route('admin.invoices.edit',$invoice) }}" class="btn btn-sm btn-outline-secondary"><i class="fas fa-edit"></i></a>
      <form action="{{ route('admin.invoices.send',$invoice) }}" method="POST">@csrf<button class="btn btn-sm btn-primary"><i class="fas fa-paper-plane me-1"></i>Kirim</button></form>
      @if(auth()->user()->role === 'admin')
      <form action="{{ route('admin.invoices.destroy',$invoice) }}" method="POST" onsubmit="return confirm('Hapus invoice ini?')" style="display:inline">@csrf @method('DELETE')<button class="btn btn-sm btn-outline-danger"><i class="fas fa-trash me-1"></i>Hapus</button></form>
      @endif
      @endif
      <a href="{{ route('admin.invoices.pdf',$invoice) }}" class="btn btn-sm btn-danger" target="_blank"><i class="fas fa-file-pdf me-1"></i>PDF</a>
    </div>
  </div>
  <div class="card-body">
    <div class="row mb-3">
      <div class="col-md-6">
        <div class="text-muted small">Kepada:</div>
        <div class="fw-bold">{{ $invoice->client->name }}</div>
        <div class="small">{{ $invoice->client->address }}</div>
        <div class="small">{{ $invoice->client->email }}</div>
      </div>
      <div class="col-md-6 text-md-end">
        <div class="text-muted small">Tanggal: <span class="text-dark">{{ $invoice->issue_date->format('d/m/Y') }}</span></div>
        <div class="text-muted small">Jatuh Tempo: <span class="text-dark {{ $invoice->status==='overdue'?'text-danger fw-bold':'' }}">{{ $invoice->due_date->format('d/m/Y') }}</span></div>
        @if($invoice->billing_month)<div class="text-muted small">Periode: <span class="text-dark">{{ date('F',$invoice->billing_month*2629746) }} {{ $invoice->billing_year }}</span></div>@endif
      </div>
    </div>

    <table class="table table-bordered">
      <thead class="table-light"><tr><th>Deskripsi</th><th class="text-center" width="80">Qty</th><th class="text-end" width="150">Harga Satuan</th><th class="text-end" width="150">Total</th></tr></thead>
      <tbody>
      @foreach($invoice->items as $item)
      <tr>
        <td>{{ $item->description }}</td>
        <td class="text-center">{{ $item->qty }}</td>
        <td class="text-end">Rp {{ number_format($item->unit_price,0,',','.') }}</td>
        <td class="text-end fw-semibold">Rp {{ number_format($item->total,0,',','.') }}</td>
      </tr>
      @endforeach
      </tbody>
      <tfoot>
        <tr><td colspan="3" class="text-end">Subtotal</td><td class="text-end">Rp {{ number_format($invoice->subtotal,0,',','.') }}</td></tr>
        @if($invoice->tax_percent > 0)<tr><td colspan="3" class="text-end">PPN ({{ $invoice->tax_percent }}%)</td><td class="text-end">Rp {{ number_format($invoice->tax_amount,0,',','.') }}</td></tr>@endif
        <tr class="table-primary fw-bold"><td colspan="3" class="text-end">TOTAL</td><td class="text-end">Rp {{ number_format($invoice->total,0,',','.') }}</td></tr>
        <tr><td colspan="3" class="text-end">Dibayar</td><td class="text-end text-success">Rp {{ number_format($invoice->amount_paid,0,',','.') }}</td></tr>
        @if($invoice->total > $invoice->amount_paid)<tr class="fw-bold"><td colspan="3" class="text-end">Sisa</td><td class="text-end text-danger">Rp {{ number_format($invoice->total - $invoice->amount_paid,0,',','.') }}</td></tr>@endif
      </tfoot>
    </table>

    @if($invoice->notes)<div class="text-muted small mt-2"><strong>Catatan:</strong> {{ $invoice->notes }}</div>@endif
  </div>
</div>

{{-- Payments --}}
<div class="card">
  <div class="card-header">Riwayat Pembayaran</div>
  <div class="list-group list-group-flush">
    @forelse($invoice->payments as $p)
    <div class="list-group-item">
      <div class="d-flex justify-content-between">
        <span class="fw-semibold small text-success">Rp {{ number_format($p->amount,0,',','.') }}</span>
        <span class="text-muted small">{{ $p->payment_date->format('d/m/Y') }}</span>
      </div>
      <div class="text-muted" style="font-size:11px">{{ $p->payment_method }} {{ $p->reference_number ? '· '.$p->reference_number : '' }}</div>
    </div>
    @empty
    <div class="list-group-item text-center text-muted small py-3">Belum ada pembayaran</div>
    @endforelse
  </div>
</div>
</div>

<div class="col-lg-4">
  @if(!in_array($invoice->status,['paid','cancelled']))
  <div class="card">
    <div class="card-header fw-semibold">Catat Pembayaran</div>
    <div class="card-body">
      <form action="{{ route('admin.invoices.payment',$invoice) }}" method="POST">@csrf
      <div class="mb-2"><label class="form-label small fw-semibold">Jumlah (Rp) *</label><input type="number" name="amount" class="form-control form-control-sm" min="1" required value="{{ $invoice->total - $invoice->amount_paid }}"></div>
      <div class="mb-2"><label class="form-label small fw-semibold">Tanggal *</label><input type="date" name="payment_date" class="form-control form-control-sm" value="{{ now()->format('Y-m-d') }}" required></div>
      <div class="mb-2"><label class="form-label small fw-semibold">Metode</label>
        <select name="payment_method" class="form-select form-select-sm">
          @foreach(['Transfer Bank','Tunai','QRIS','Cek'] as $m)<option>{{ $m }}</option>@endforeach
        </select>
      </div>
      <div class="mb-2"><label class="form-label small fw-semibold">No. Referensi</label><input type="text" name="reference_number" class="form-control form-control-sm"></div>
      <div class="mb-3"><label class="form-label small fw-semibold">Catatan</label><textarea name="notes" class="form-control form-control-sm" rows="2"></textarea></div>
      <button type="submit" class="btn btn-success btn-sm w-100"><i class="fas fa-check me-1"></i>Catat Pembayaran</button>
      </form>
    </div>
  </div>
  @endif
</div>
</div>
@endsection
