@extends('layouts.app')

@section('title', 'Detail Penjualan - Kei\'s Store')

@section('content')
    <div class="container-fluid px-4">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h3 class="fw-bold text-pink-gradient">
                <i class="fas fa-receipt me-2"></i>Detail Penjualan #{{ $penjualan->penjualan_id }}
            </h3>
        </div>

        {{-- Pesan Sukses/Error --}}
        @if(session('success'))
            <div class="alert alert-pink alert-dismissible fade show shadow-sm mb-4" role="alert">
                <i class="fas fa-check-circle me-2"></i>
                {{ session('success') }}
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
        @endif

        @if(session('error'))
            <div class="alert alert-danger alert-dismissible fade show shadow-sm mb-4" role="alert">
                <i class="fas fa-exclamation-circle me-2"></i>
                {{ session('error') }}
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
        @endif

        <div class="row">
            {{-- Informasi Penjualan --}}
            <div class="col-md-6 mb-4">
                <div class="card shadow-pink border-0 h-100">
                    <div class="card-header bg-pink-gradient text-white py-3">
                        <h5 class="mb-0"><i class="fas fa-info-circle me-2"></i>Informasi Order</h5>
                    </div>
                    <div class="card-body p-4">
                        <table class="table table-borderless">
                            <tr>
                                <td width="40%" class="text-pink-dark fw-semibold">Invoice</td>
                                <td width="60%" class="fw-bold">
                                    <span class="badge bg-pink-light text-pink-dark fs-6">
                                        <i class="bi bi-receipt me-1"></i>
                                        INV-{{ $penjualan->penjualan_id }}{{ date('Ymd', strtotime($penjualan->tanggal_penjualan)) }}
                                    </span>
                                </td>
                            </tr>
                            <tr>
                                <td class="text-pink-dark fw-semibold">ID Penjualan</td>
                                <td class="fw-bold">#{{ $penjualan->penjualan_id }}</td>
                            </tr>
                           
                            <tr>
                                <td class="text-pink-dark fw-semibold">Nama Pembeli</td>
                                <td>
                                    <span class="d-flex align-items-center">
                                        <i class="bi bi-person-fill me-2 text-pink"></i>
                                        {{ $penjualan->user->username ?? '-' }}
                                        @if(Auth::user()->user_id == $penjualan->user_id)
                                            <span class="badge bg-success ms-2">Anda</span>
                                        @endif
                                    </span>
                                </td>
                            </tr>
                            <tr>
                                <td class="text-pink-dark fw-semibold">Total Item</td>
                                <td>
                                    <span class="badge bg-pink-light text-pink-dark fs-6">
                                        {{ $penjualan->detailPenjualan->sum('jumlah') }} item
                                    </span>
                                    <span class="text-muted ms-2">({{ $penjualan->detailPenjualan->count() }} jenis produk)</span>
                                </td>
                            </tr>
                            <tr>
                                <td class="text-pink-dark fw-semibold">Kurir</td>
                                <td>
                                    <span class="d-flex align-items-center">
                                        <i class="bi bi-truck me-2 text-pink"></i>
                                        {{ $penjualan->kurir ?? '-' }}
                                    </span>
                                </td>
                            </tr>
                            <tr>
                                <td class="text-pink-dark fw-semibold">Ongkos Kirim</td>
                                <td class="fw-bold text-pink-dark">
                                    <i class="bi bi-cash-stack me-1"></i>
                                    Rp {{ number_format($penjualan->ongkir ?? 0, 0, ',', '.') }}
                                </td>
                            </tr>
                            
                            {{-- Tampilkan No Resi jika ada --}}
                            @if($penjualan->no_resi)
                                <tr>
                                    <td class="text-pink-dark fw-semibold">No. Resi</td>
                                    <td class="fw-bold">
                                        <span class="badge bg-primary text-white">
                                            <i class="bi bi-upc-scan me-1"></i>
                                            {{ $penjualan->no_resi }}
                                        </span>
                                    </td>
                                </tr>
                            @endif
                            
                            {{-- Tampilkan Estimasi Tiba jika ada --}}
                            @if($penjualan->estimasi_tiba)
                                <tr>
                                    <td class="text-pink-dark fw-semibold">Estimasi Tiba</td>
                                    <td class="fw-bold">
                                        <i class="bi bi-calendar-check me-1 text-pink"></i>
                                        {{ \Carbon\Carbon::parse($penjualan->estimasi_tiba)->translatedFormat('d F Y') }}
                                    </td>
                                </tr>
                            @endif
                            
                            {{-- Tampilkan Tanggal Kirim jika ada --}}
                            @if($penjualan->tgl_Kirim)
                                <tr>
                                    <td class="text-pink-dark fw-semibold">Tanggal Kirim</td>
                                    <td class="fw-bold">
                                        <i class="bi bi-send me-1 text-pink"></i>
                                        {{ \Carbon\Carbon::parse($penjualan->tgl_kirim)->translatedFormat('d F Y H:i') }}
                                    </td>
                                </tr>
                            @endif
                            {{-- Status Pembayaran --}}
                            <tr>
                                <td class="text-pink-dark fw-semibold">Status Pembayaran</td>
                                <td>
                                    @if(in_array($penjualan->status, ['paid', 'processing', 'shipped', 'delivered']))
                                        <span class="badge bg-success shadow-sm px-3 py-2">
                                            <i class="bi bi-check-circle-fill me-1"></i> Lunas
                                        </span>
                                    @else
                                        <span class="badge bg-warning text-dark shadow-sm px-3 py-2">
                                            <i class="bi bi-clock-fill me-1"></i> Menunggu Konfirmasi
                                        </span>
                                    @endif
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>

            {{-- Ringkasan Pembayaran --}}
            <div class="col-md-6 mb-4">
                <div class="card shadow-pink border-0 h-100">
                    <div class="card-header bg-pink-gradient text-white py-3">
                        <h5 class="mb-0"><i class="fas fa-credit-card me-2"></i>Ringkasan Pembayaran</h5>
                    </div>
                    <div class="card-body p-4">
                        <div class="d-flex justify-content-between align-items-center mb-3 pb-2 border-bottom">
                            <span class="fw-semibold">Subtotal Produk:</span>
                            <span class="fw-bold text-pink-dark">
                                Rp {{ number_format($penjualan->detailPenjualan->sum('subtotal'), 0, ',', '.') }}
                            </span>
                        </div>

                        <div class="d-flex justify-content-between align-items-center mb-3 pb-2 border-bottom">
                            <span class="fw-semibold">
                                <i class="bi bi-truck me-1"></i> Ongkos Kirim:
                            </span>
                            <span class="fw-bold text-pink-dark">
                                Rp {{ number_format($penjualan->ongkir ?? 0, 0, ',', '.') }}
                            </span>
                        </div>

                        <div class="d-flex justify-content-between align-items-center mb-3 pb-2 border-bottom">
                            <span class="fw-semibold fs-5">Total Harga:</span>
                            <span class="fw-bold text-pink-gradient fs-4">
                                Rp {{ number_format($penjualan->total_harga, 0, ',', '.') }}
                            </span>
                        </div>

                 
        {{-- Detail Produk --}}
        <div class="card shadow-pink border-0 mb-4">
            <div class="card-header bg-pink-gradient text-white py-3 d-flex justify-content-between align-items-center">
                <h5 class="mb-0"><i class="fas fa-shopping-basket me-2"></i>Detail Produk</h5>
                <span class="badge bg-white text-pink-dark px-3 py-2">
                    Total: {{ $penjualan->detailPenjualan->count() }} Produk
                </span>
            </div>
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead class="table-pink">
                            <tr>
                                <th class="py-3 px-4 border-0">No</th>
                                <th class="py-3 px-4 border-0">Produk</th>
                                <th class="py-3 px-4 border-0 text-end">Harga Satuan</th>
                                <th class="py-3 px-4 border-0 text-center">Jumlah</th>
                                <th class="py-3 px-4 border-0 text-end">Subtotal</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($penjualan->detailPenjualan as $index => $item)
                                <tr class="hover-pink">
                                    <td class="py-3 px-4 border-bottom">
                                        <span class="badge bg-pink-light text-pink-dark">{{ $index + 1 }}</span>
                                    </td>
                                    <td class="py-3 px-4 border-bottom">
                                        <div class="d-flex align-items-center">
                                            <div class="product-thumb-wrapper me-3">
                                                @php
                                                    $displayFoto = $item->foto_produk;
                                                    if (!$displayFoto && $item->produk) {
                                                        $displayFoto = $item->produk->primary_foto;
                                                        if ($displayFoto && strpos($displayFoto, 'http') !== 0) {
                                                            $displayFoto = asset('storage/' . $displayFoto);
                                                        }
                                                    }
                                                @endphp
                                                @if($displayFoto)
                                                    <img src="{{ $displayFoto }}" alt="{{ $item->produk->nama_produk ?? 'Produk' }}" class="product-thumb">
                                                @else
                                                    <div class="product-thumb-none">
                                                        <i class="fas fa-image"></i>
                                                    </div>
                                                @endif
                                            </div>
                                            <div>
                                                <span class="fw-semibold d-block text-pink-dark">{{ $item->produk->nama_produk ?? 'Produk Tidak Ditemukan' }}</span>
                                                <small class="text-muted"><i class="bi bi-tag me-1"></i>Kode: {{ $item->produk->produk_id ?? '-' }}</small>
                                            </div>
                                        </div>
                                    </td>
                                    <td class="py-3 px-4 border-bottom text-end">
                                        <span class="text-pink-dark fw-semibold">
                                            Rp {{ number_format($item->harga_satuan, 0, ',', '.') }}
                                        </span>
                                    </td>
                                    <td class="py-3 px-4 border-bottom text-center">
                                        <span class="badge bg-info text-white px-3 py-2">{{ $item->jumlah }}</span>
                                    </td>
                                    <td class="py-3 px-4 border-bottom text-end">
                                        <span class="fw-bold text-pink-gradient">
                                            Rp {{ number_format($item->subtotal, 0, ',', '.') }}
                                        </span>
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="5" class="text-center py-5">
                                        <div class="text-pink-muted">
                                            <i class="fas fa-box-open fa-3x mb-3 opacity-50"></i>
                                            <p class="mb-0 fs-5">Tidak ada detail produk</p>
                                        </div>
                                    </td>
                                </tr>
                            @endforelse
                        </tbody>
                        <tfoot class="table-pink">
                            <tr>
                                <td colspan="4" class="py-3 px-4 border-0 text-end fw-semibold">Subtotal Produk:</td>
                                <td class="py-3 px-4 border-0 fw-bold text-pink-dark text-end">
                                    Rp {{ number_format($penjualan->detailPenjualan->sum('subtotal'), 0, ',', '.') }}
                                </td>
                            </tr>
                            <tr>
                                <td colspan="4" class="py-2 px-4 border-0 text-end fw-semibold">Ongkos Kirim:</td>
                                <td class="py-2 px-4 border-0 fw-bold text-pink-dark text-end">
                                    + Rp {{ number_format($penjualan->ongkir ?? 0, 0, ',', '.') }}
                                </td>
                            </tr>
                            <tr class="bg-soft-pink">
                                <td colspan="4" class="py-3 px-4 border-0 text-end fw-bold fs-5">Grand Total:</td>
                                <td class="py-3 px-4 border-0 fw-bold fs-5 text-pink-gradient text-end">
                                    Rp {{ number_format($penjualan->total_harga, 0, ',', '.') }}
                                </td>
                            </tr>
                            <tr>
                                <td colspan="4" class="py-2 px-4 border-0 text-end text-muted">Total Item:</td>
                                <td class="py-2 px-4 border-0 text-end fw-semibold">
                                    {{ $penjualan->detailPenjualan->sum('jumlah') }} item
                                </td>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
        </div>

        {{-- Tombol Aksi --}}
        <div class="d-flex flex-wrap justify-content-between align-items-center pt-3 gap-2">
            <div>
                <a href="{{ route('penjualan.index') }}" class="btn btn-outline-pink px-4 py-2">
                    <i class="bi bi-arrow-left me-2"></i>Kembali ke Daftar
                </a>
            </div>
            <div class="d-flex gap-3">
                <a href="{{ route('penjualan.struk', $penjualan->penjualan_id) }}"
                    class="btn btn-pink-glow px-4 py-2 d-flex align-items-center" target="_blank">
                    <i class="bi bi-receipt me-2"></i>Print Invoice
                </a>

                @if(Auth::user()->role == 'pengguna' && Auth::user()->user_id == $penjualan->user_id && $penjualan->status == 'pending')
                    <form action="{{ route('penjualan.destroy', $penjualan->penjualan_id) }}" method="POST" class="d-inline">
                        @csrf
                        @method('DELETE')
                        <button type="submit" class="btn btn-pink-outline px-4 py-2 d-flex align-items-center"
                            onclick="return confirm('Yakin ingin menghapus penjualan ini? Semua data akan hilang!')">
                            <i class="fas fa-trash me-2"></i>Hapus
                        </button>
                    </form>
                @endif

                @if(Auth::user()->role == 'admin')
                    <a href="{{ route('penjualan.edit', $penjualan->penjualan_id) }}"
                        class="btn btn-warning px-4 py-2 d-flex align-items-center">
                        <i class="fas fa-edit me-2"></i>Edit
                    </a>
                @endif
            </div>
        </div>
    </div>

    <style>
        .text-pink-gradient {
            background: linear-gradient(135deg, #ec4899 0%, #db2777 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }

        .bg-pink-gradient {
            background: linear-gradient(135deg, #ec4899 0%, #db2777 100%);
        }

        .btn-pink-glow {
            background: linear-gradient(135deg, #ec4899 0%, #db2777 100%);
            border: none;
            color: white;
            font-weight: 600;
            padding: 12px 24px;
            border-radius: 10px;
            transition: all 0.3s ease;
            box-shadow: 0 4px 15px rgba(236, 72, 153, 0.4);
            text-decoration: none;
        }

        .btn-pink-glow:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(236, 72, 153, 0.5);
            color: white;
        }

        .btn-outline-pink {
            border: 2px solid #ec4899;
            color: #ec4899;
            font-weight: 500;
            padding: 12px 24px;
            border-radius: 10px;
            transition: all 0.3s ease;
            background: transparent;
            text-decoration: none;
        }

        .btn-outline-pink:hover {
            background: #ec4899;
            color: white;
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(236, 72, 153, 0.3);
        }

        .btn-pink-outline {
            background: transparent;
            border: 2px solid #fecdd3;
            color: #f43f5e;
            font-weight: 500;
            padding: 12px 24px;
            border-radius: 10px;
            transition: all 0.3s ease;
            text-decoration: none;
        }

        .btn-pink-outline:hover {
            background: #f43f5e;
            border-color: #f43f5e;
            color: white;
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(244, 63, 94, 0.3);
        }

        .shadow-pink {
            box-shadow: 0 4px 25px rgba(236, 72, 153, 0.1);
            border: 1px solid #fdf2f8;
        }

        .border-pink {
            border-color: #fbcfe8 !important;
        }

        .bg-soft-pink {
            background: linear-gradient(135deg, #fdf2f8 0%, #fce7f3 100%);
        }

        .text-pink-dark {
            color: #be185d;
        }

        .table-pink {
            background: linear-gradient(135deg, #fdf2f8 0%, #fce7f3 100%);
        }

        .table-pink th {
            border: none !important;
            color: #9d174d;
            font-weight: 600;
            font-size: 0.9rem;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .hover-pink:hover {
            background-color: #fdf2f8 !important;
            transition: all 0.3s ease;
        }

        .badge.bg-pink-light {
            background: linear-gradient(135deg, #fce7f3 0%, #fbcfe8 100%);
            color: #be185d;
            padding: 8px 16px;
            border-radius: 20px;
            font-weight: 600;
            font-size: 0.9rem;
        }

        .card {
            border-radius: 16px;
            overflow: hidden;
            border: none;
        }

        .table-hover tbody tr {
            border-bottom: 1px solid #fdf2f8;
        }

        .table-hover tbody tr:last-child {
            border-bottom: none;
        }

        .alert-pink {
            background: linear-gradient(135deg, #fdf2f8 0%, #fce7f3 100%);
            border: 1px solid #fbcfe8;
            color: #be185d;
            border-radius: 10px;
        }

        /* Product Thumbnails */
        .product-thumb-wrapper {
            width: 50px;
            height: 50px;
            border-radius: 12px;
            overflow: hidden;
            border: 2px solid #fce7f3;
            background: white;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.05);
            flex-shrink: 0;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .product-thumb {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }

        .product-thumb-none {
            color: #fbcfe8;
            font-size: 1.2rem;
        }
    </style>
@endsection