@extends('layouts.admin')

@section('title', 'Detail Transaksi')

@section('page-title')
    <div class="d-flex align-items-center justify-content-between">
        <div>
            <div class="d-flex align-items-center gap-1 mb-1">
                <a href="{{ route('admin.monitoring') }}">Daftar Transaksi </a>
                <p class="mb-0">/ Detail Pesanan</p>
            </div>
            <p class="text-muted font-bold mb-0">Invoice: #{{ $transaksi->kode_invoice }}</p>
        </div>
        <a href="{{ route('petani.pesanan.index') }}" class="btn btn-light">
            <i class="bi bi-arrow-left"></i> Kembali
        </a>
    </div>
@endsection

@section('content')
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-8">
                <div class="card mb-4 border-0 shadow-sm" style="border-radius: 12px; overflow: hidden;">

                    {{-- Header Card yang Bersih --}}
                    <div class="card-header bg-white py-3 border-bottom">
                        <h5 class="mb-0 fw-bold text-dark" style="font-size: 1.1rem;">
                            <i class="bi bi-cart3 me-2 text-primary"></i>Daftar Produk
                        </h5>
                    </div>

                    <div class="card-body p-0">
                        <div class="table-responsive">
                            <table class="table align-middle mb-0">
                                <thead style="background-color: #f8f9fa;">
                                    <tr>
                                        <th scope="col" class="ps-4 py-3 text-secondary small text-uppercase fw-bold"
                                            style="width: 45%;">Produk</th>
                                        <th scope="col" class="text-end py-3 text-secondary small text-uppercase fw-bold">
                                            Harga</th>
                                        <th scope="col"
                                            class="text-center py-3 text-secondary small text-uppercase fw-bold">Qty</th>
                                        <th scope="col"
                                            class="text-end pe-4 py-3 text-secondary small text-uppercase fw-bold">Subtotal
                                        </th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach($transaksi->detailTransaksis as $detail)
                                        <tr>
                                            <td class="ps-4 py-3">
                                                <div class="d-flex align-items-center">
                                                    <div class="me-3">
                                                        <img src="{{ $detail->produk->foto_produk ? asset('storage/' . $detail->produk->foto_produk) : 'https://images.unsplash.com/photo-1586201375761-83865001e31c?q=80&w=200&auto=format&fit=crop' }}"
                                                            alt="{{ $detail->produk->nama_produk }}" class="rounded-3 shadow-sm"
                                                            style="width: 55px; height: 55px; object-fit: cover;">
                                                    </div>

                                                    {{-- Detail Nama & Badge --}}
                                                    <div>
                                                        <div class="fw-bold text-dark mb-1">{{ $detail->produk->nama_produk }}
                                                        </div>

                                                        {{-- Badge Kategori --}}
                                                        @php
                                                            $kategori = $detail->produk->kategori->nama_kategori ?? 'Umum';
                                                            $badgeColor = match ($kategori) {
                                                                default => 'bg-primary bg-opacity-10 text-primary'
                                                            };
                                                        @endphp
                                                        <span
                                                            class="badge {{ $badgeColor }} border border-opacity-10 rounded-pill px-2">
                                                            {{ $kategori }}
                                                        </span>
                                                    </div>
                                                </div>
                                            </td>

                                            {{-- Harga Satuan --}}
                                            <td class="text-end text-muted">
                                                <small>Rp {{ number_format($detail->harga_satuan, 0, ',', '.') }}</small>
                                            </td>

                                            {{-- Qty --}}
                                            <td class="text-center">
                                                <span class="badge bg-light text-dark border px-2 py-1 rounded-pill">
                                                    x{{ $detail->jumlah }}
                                                </span>
                                            </td>

                                            {{-- Subtotal --}}
                                            <td class="text-end fw-bold pe-4 text-dark">
                                                Rp {{ number_format($detail->subtotal, 0, ',', '.') }}
                                            </td>
                                        </tr>
                                    @endforeach
                                </tbody>

                                {{-- Footer Total --}}
                                <tfoot class="bg-white">
                                    <tr>
                                        <td colspan="4" class="p-0">
                                            <div class="d-flex justify-content-end align-items-center p-4"
                                                style="background: linear-gradient(to right, #fff, #f8f9fa);">
                                                <div class="text-end">
                                                    <span class="text-muted d-block small mb-1">Total Transaksi</span>
                                                    <h4 class="fw-bolder text-primary mb-0">
                                                        Rp {{ number_format($transaksi->total_harga, 0, ',', '.') }}
                                                    </h4>
                                                </div>
                                            </div>
                                        </td>
                                    </tr>
                                </tfoot>
                            </table>
                        </div>
                    </div>
                </div>
            </div>

            {{-- Info Pihak Terkait --}}
            <div class="col-md-4">
                {{-- Info Status --}}
                <div class="card mb-3">
                    <div class="card-body">
                        <h6 class="card-title text-muted">Status Pesanan</h6>
                        @php
                            $badgeClass = match ($transaksi->status) {
                                'selesai' => 'bg-success',
                                'batal' => 'bg-danger',
                                'dikirim' => 'bg-primary',
                                'diproses' => 'bg-info',
                                'menunggu konfirmasi' => 'bg-warning text-dark',
                                default => 'bg-secondary',
                            };
                        @endphp
                        <span class="badge {{ $badgeClass }} fs-6 w-100 py-2">
                            {{ ucwords($transaksi->status) }}
                        </span>
                        <small class="text-muted d-block mt-2 text-center">
                            Tanggal: {{ $transaksi->created_at->format('d M Y H:i') }}
                        </small>
                    </div>
                </div>

                {{-- Info Pembeli --}}
                <div class="card mb-3">
                    <div class="card-header bg-white">
                        <i class="bi bi-person-circle me-1"></i> Data Pembeli
                    </div>
                    <div class="card-body">
                        <h6 class="fw-bold">{{ $transaksi->pembeli->nama_lengkap ?? 'Guest' }}</h6>
                        <p class="mb-1 small text-muted"><i class="bi bi-telephone"></i>
                            {{ $transaksi->pembeli->no_hp ?? '-' }}</p>
                        <hr>
                        <small class="text-muted">Alamat Pengiriman:</small>
                        <p class="mb-0 small">{{ $transaksi->alamat_pengiriman }}</p>
                    </div>
                </div>

                {{-- Info Petani --}}
                <div class="card">
                    <div class="card-header bg-white">
                        <i class="bi bi-shop me-1"></i> Data Penjual (Petani)
                    </div>
                    <div class="card-body">
                        <h6 class="fw-bold">{{ $transaksi->petani->nama_lengkap ?? 'Tidak Diketahui' }}</h6>
                        <p class="mb-0 small text-muted">{{ $transaksi->petani->nama_usaha ?? '-' }}</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection