@extends('layouts.admin')
@section('title', 'Detail Pembelian - SI Apotek')

@section('content')
<div class="mb-6 flex justify-between items-center">
    <div class="flex items-center gap-4">
        <a href="{{ route('purchases.index') }}" class="text-gray-500 hover:text-emerald-600 transition">
            <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18"></path></svg>
        </a>
        <h2 class="text-2xl font-semibold text-gray-700">Detail Pembelian</h2>
    </div>
</div>

<div class="bg-white rounded-lg shadow-sm border border-gray-100 overflow-hidden mb-6 p-6">
    <div class="grid grid-cols-2 md:grid-cols-4 gap-4 mb-6 pb-6 border-b border-gray-100">
        <div>
            <p class="text-sm text-gray-500">Tanggal Transaksi</p>
            <p class="font-semibold text-gray-800">{{ \Carbon\Carbon::parse($purchase->date)->format('d M Y') }}</p>
        </div>
        <div>
            <p class="text-sm text-gray-500">Dibuat Oleh</p>
            <p class="font-semibold text-gray-800">{{ $purchase->user->name }}</p>
        </div>
        <div>
            <p class="text-sm text-gray-500">Supplier</p>
            <p class="font-semibold text-gray-800">{{ $purchase->supplier->name }}</p>
        </div>
        <div>
            <p class="text-sm text-gray-500">Total Transaksi</p>
            <p class="font-bold text-xl text-emerald-600">Rp {{ number_format($purchase->total_amount, 0, ',', '.') }}</p>
        </div>
    </div>

    <h3 class="text-lg font-semibold text-gray-700 mb-4">Rincian Obat</h3>
    <div class="overflow-x-auto">
        <table class="w-full whitespace-no-wrap">
            <thead>
                <tr class="text-xs font-semibold tracking-wide text-left text-gray-500 uppercase border-b bg-gray-50">
                    <th class="px-4 py-3">Nama Obat</th>
                    <th class="px-4 py-3 text-center">Batch / ED</th>
                    <th class="px-4 py-3 text-center">Qty</th>
                    <th class="px-4 py-3 text-right">Harga Beli</th>
                    <th class="px-4 py-3 text-right">Subtotal</th>
                </tr>
            </thead>
            <tbody class="divide-y">
                @foreach($purchase->details as $item)
                <tr class="text-gray-700">
                    <td class="px-4 py-3">
                        <p class="font-medium text-sm">{{ $item->medicine->name }}</p>
                        <p class="text-xs text-gray-500">{{ $item->medicine->code }}</p>
                    </td>
                    <td class="px-4 py-3 text-sm text-center">
                        <p>{{ $item->batch_number ?? '-' }}</p>
                        <p class="text-xs {{ \Carbon\Carbon::parse($item->expired_date)->isPast() ? 'text-red-500 font-bold' : 'text-gray-500' }}">
                            ED: {{ \Carbon\Carbon::parse($item->expired_date)->format('d/m/Y') }}
                        </p>
                    </td>
                    <td class="px-4 py-3 text-sm text-center font-semibold">
                        {{ $item->qty }} <span class="font-normal text-gray-500">{{ $item->medicine->unit->name ?? '' }}</span>
                    </td>
                    <td class="px-4 py-3 text-sm text-right">Rp {{ number_format($item->purchase_price, 0, ',', '.') }}</td>
                    <td class="px-4 py-3 text-sm text-right font-semibold">Rp {{ number_format($item->subtotal, 0, ',', '.') }}</td>
                </tr>
                @endforeach
            </tbody>
        </table>
    </div>
</div>
@endsection
