@extends('layouts.app')
@section('title', 'Detail Tanda Terima')
@section('page-title', 'Tanda Terima')

@section('content')
<div class="row mb-3">
    <div class="col-sm-8 d-flex align-items-center">
        <a href="{{ route('receipts.index') }}" class="btn btn-sm btn-outline-secondary mr-2"><i class="fas fa-arrow-left"></i></a>
        <h5 class="mb-0 font-weight-bold">Tanda Terima #{{ $receipt->receipt_number }}</h5>
    </div>
    <div class="col-sm-4 text-right">
        <div class="btn-group btn-group-sm">
            <a href="{{ route('receipts.print', $receipt) }}" class="btn btn-outline-secondary" target="_blank">
                <i class="fas fa-print"></i> Cetak
            </a>
            <form method="POST" action="{{ route('receipts.destroy', $receipt) }}" onsubmit="return confirm('Hapus tanda terima ini? Status invoice akan dikembalikan ke Terkirim.')" class="d-inline">
                @csrf @method('DELETE')
                <button type="submit" class="btn btn-outline-danger"><i class="fas fa-trash"></i> Hapus</button>
            </form>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-md-8">
        <div class="card card-primary card-outline mb-3">
            <div class="card-body">
                <div class="row">
                    <div class="col-md-6">
                        <div class="text-muted small mb-1">DITERIMA DARI</div>
                        <div class="font-weight-bold h5">{{ $receipt->customer->name }}</div>
                        <div class="text-muted">{{ $receipt->customer->address }}</div>
                    </div>
                    <div class="col-md-6 text-md-right">
                        <table class="table table-sm table-borderless mb-0 ml-auto" style="width:auto">
                            <tr>
                                <td class="text-muted pr-3">No. Tanda Terima</td>
                                <td class="font-weight-bold">{{ $receipt->receipt_number }}</td>
                            </tr>
                            <tr>
                                <td class="text-muted pr-3">Tanggal</td>
                                <td class="font-weight-bold">{{ $receipt->receipt_date->format('d F Y') }}</td>
                            </tr>
                            <tr>
                                <td class="text-muted pr-3">Ref. Invoice</td>
                                <td><a href="{{ route('invoices.show', $receipt->invoice) }}" class="font-weight-bold">{{ $receipt->invoice->invoice_number }}</a></td>
                            </tr>
                            <tr>
                                <td class="text-muted pr-3">Metode</td>
                                <td><span class="badge badge-secondary">{{ $receipt->payment_method ?? '-' }}</span></td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        {{-- Ringkasan jumlah --}}
        <div class="card card-success card-outline mb-3">
            <div class="card-body text-center py-4">
                <div class="text-muted mb-1">Jumlah yang Diterima</div>
                <div class="display-4 font-weight-bold text-success">
                    Rp {{ number_format($receipt->amount_received, 0, ',', '.') }}
                </div>
            </div>
        </div>

        {{-- Detail item dari invoice --}}
        <div class="card mb-3">
            <div class="card-header">
                <h3 class="card-title">Item Invoice Terkait</h3>
            </div>
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-striped align-middle mb-0">
                        <thead>
                            <tr>
                                <th class="pl-3">No</th>
                                <th>Deskripsi</th>
                                <th class="text-center">Jumlah</th>
                                <th class="text-right">Harga Satuan</th>
                                <th class="text-right pr-3">Total</th>
                            </tr>
                        </thead>
                        <tbody>
                        @foreach($receipt->invoice->items as $i => $item)
                        <tr>
                            <td class="pl-3 text-muted">{{ $i + 1 }}</td>
                            <td>{{ $item->description }}</td>
                            <td class="text-center">{{ number_format($item->quantity, 2, ',', '.') }} {{ $item->unit }}</td>
                            <td class="text-right">Rp {{ number_format($item->unit_price, 0, ',', '.') }}</td>
                            <td class="text-right pr-3 font-weight-bold">Rp {{ number_format($item->total_price, 0, ',', '.') }}</td>
                        </tr>
                        @endforeach
                        </tbody>
                        <tfoot>
                            <tr class="bg-light">
                                <td colspan="4" class="text-right font-weight-bold pl-3">TOTAL INVOICE</td>
                                <td class="text-right pr-3 font-weight-bold">Rp {{ number_format($receipt->invoice->total, 0, ',', '.') }}</td>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
        </div>

        @if($receipt->notes)
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">Catatan</h3>
            </div>
            <div class="card-body text-muted small">{{ $receipt->notes }}</div>
        </div>
        @endif
    </div>

    <div class="col-md-4">
        <div class="card card-secondary card-outline">
            <div class="card-header">
                <h3 class="card-title">TTD & Stempel Tanda Terima Ini</h3>
            </div>
            <div class="card-body">
                <form method="POST" action="{{ route('receipts.upload-assets', $receipt) }}" enctype="multipart/form-data">
                    @csrf
                    <div class="form-group">
                        <label class="small font-weight-bold">Tanda Tangan</label>
                        @if($receipt->signature_path)
                        <div class="mb-2 p-2 border rounded text-center bg-light">
                            <img src="{{ asset('storage/'.$receipt->signature_path) }}" alt="TTD" style="max-height:80px; max-width:100%">
                        </div>
                        @else
                        <div class="mb-2 text-muted small"><em>Belum ada TTD khusus.</em></div>
                        @endif
                        <input type="file" name="signature" class="form-control-file border rounded p-1" accept="image/*">
                    </div>
                    <div class="form-group">
                        <label class="small font-weight-bold">Stempel Usaha</label>
                        @if($receipt->stamp_path)
                        <div class="mb-2 p-2 border rounded text-center bg-light">
                            <img src="{{ asset('storage/'.$receipt->stamp_path) }}" alt="Stempel" style="max-height:80px; max-width:100%">
                        </div>
                        @else
                        <div class="mb-2 text-muted small"><em>Belum ada stempel khusus.</em></div>
                        @endif
                        <input type="file" name="stamp" class="form-control-file border rounded p-1" accept="image/*">
                    </div>
                    <button type="submit" class="btn btn-sm btn-primary btn-block">
                        <i class="fas fa-upload"></i> Upload TTD/Stempel
                    </button>
                    <small class="form-text text-muted mt-2">
                        Gunakan TTD/Stempel dari <a href="{{ route('settings.index') }}">Pengaturan</a> jika kosong di sini.
                    </small>
                </form>
            </div>
        </div>
    </div>
</div>
@endsection

