@extends('layouts.admin')

@section('title', 'Detail Pesanan')
@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('petani.pesanan.index', $pesanan->id) }}">Daftar Pesanan  </a>
                <p class="mb-0">/ Detail Pesanan</p>
            </div>
            <p class="text-muted font-bold mb-0">Invoice: #{{ $pesanan->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="row">
        {{-- KOLOM KIRI: Daftar Produk --}}
        <div class="col-lg-8">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">Daftar Produk yang Dipesan</h5>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-hover mb-0">
                            <thead>
                                <tr>
                                    <th>Produk</th>
                                    <th class="text-center">Qty</th>
                                    <th class="text-end">Harga</th>
                                    <th class="text-end">Subtotal</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($pesanan->detailTransaksis as $detail)
                                    @if($detail->produk->petani_id == Auth::guard('petani')->id())
                                        <tr>
                                            <td>
                                                <div class="d-flex align-items-center">
                                                    <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' }}"
                                                        class="rounded me-3" width="50" height="50" style="object-fit: cover;">
                                                    <div>
                                                        <p class="mb-1 fw-bold">{{ $detail->produk->nama_produk }}</p>
                                                        <span
                                                            class="badge bg-primary-green">{{ $detail->produk->kategori->nama_kategori ?? 'Umum' }}</span>
                                                    </div>
                                                </div>
                                            </td>
                                            <td class="text-center">
                                                <span class="badge bg-primary-green">{{ $detail->jumlah }}</span>
                                            </td>
                                            <td class="text-end">Rp {{ number_format($detail->harga_satuan, 0, ',', '.') }}</td>
                                            <td class="text-end fw-bold">Rp {{ number_format($detail->subtotal, 0, ',', '.') }}</td>
                                        </tr>
                                    @endif
                                @endforeach
                            </tbody>
                        </table>
                    </div>

                    <div class="mt-4 pt-3 border-top">
                        <div class="d-flex justify-content-between align-items-center">
                            <h5 class="mb-0">Total</h5>
                            @php
                                $totalPetani = $pesanan->detailTransaksis->filter(function ($detail) {
                                    return $detail->produk->petani_id == Auth::guard('petani')->id();
                                })->sum('subtotal');
                            @endphp
                            <h4 class="mb-0 fw-bold">Rp {{ number_format($totalPetani, 0, ',', '.') }}</h4>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        {{-- Panel Aksi & Info --}}
        <div class="col-lg-4">
            @if($pesanan->status == 'menunggu_konfirmasi' || $pesanan->status == 'menunggu konfirmasi')
                <div class="card mb-3">
                    <div class="card-body">
                        <div class="mb-3">
                            <h6 class="alert-heading mb-2">Perlu Konfirmasi</h6>
                            <p class="mb-0 small">Pastikan stok tersedia sebelum menerima pesanan ini.</p>
                        </div>

                        <form action="{{ route('petani.pesanan.update', $pesanan->id) }}" method="POST" class="mb-2">
                            @csrf @method('PATCH')
                            <input type="hidden" name="status" value="diproses">
                            <button class="btn btn-primary w-100" onclick="return confirm('Stok aman? Terima pesanan ini?')">
                                <i class="bi bi-check-circle"></i> Terima Pesanan
                            </button>
                        </form>

                        <form action="{{ route('petani.pesanan.update', $pesanan->id) }}" method="POST">
                            @csrf @method('PATCH')
                            <input type="hidden" name="status" value="batal">
                            <button class="btn btn-danger w-100" onclick="return confirm('Yakin ingin menolak?')">
                                <i class="bi bi-x-circle"></i> Tolak
                            </button>
                        </form>
                    </div>
                </div>
            @elseif($pesanan->status == 'diproses')
                <div class="card mb-3">
                    <div class="card-body">
                        <div class="mb-3">
                            <h6 class="alert-heading">Sedang Diproses</h6>
                            <p class="mb-0 small">Klik tombol di bawah jika barang sudah diserahkan ke kurir.</p>
                        </div>
                        <form action="{{ route('petani.pesanan.update', $pesanan->id) }}" method="POST">
                            @csrf @method('PATCH')
                            <input type="hidden" name="status" value="dikirim">
                            <button class="btn btn-info w-100">
                                <i class="bi bi-truck"></i> Update Pengiriman
                            </button>
                        </form>
                    </div>
                </div>
            @endif

            {{-- STATUS BADGE --}}
            <div class="card mb-3">
                <div class="card-body">
                    <h6 class="card-title mb-3">Status Pesanan</h6>
                    @php
                        $statusBadge = match ($pesanan->status) {
                            'menunggu_konfirmasi', 'menunggu konfirmasi' => 'bg-warning',
                            'diproses' => 'bg-info',
                            'dikirim' => 'bg-primary',
                            'selesai' => 'bg-success',
                            'batal' => 'bg-danger',
                            default => 'bg-secondary'
                        };
                    @endphp
                    <span class="badge {{ $statusBadge }} fs-6">
                        {{ str_replace('_', ' ', ucwords($pesanan->status)) }}
                    </span>
                </div>
            </div>

            {{-- INFO PEMBELI --}}
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">Data Pembeli</h5>
                </div>
                <div class="card-body">
                    <div class="d-flex align-items-center mb-3">
                        <div class="d-flex justify-content-center align-items-center me-2">
                            <div class="stats-icon blue"><i class="bi bi-people-fill"></i></div>
                        </div>
                        <div>
                            <h6 class="mb-0">{{ $pesanan->pembeli->nama_lengkap }}</h6>
                            <small class="text-muted">{{ $pesanan->pembeli->username }}</small>
                        </div>
                    </div>

                    <div class="mb-3">
                        <small class="text-muted d-block mb-1">Kontak</small>
                        <a href="https://wa.me/{{ $pesanan->pembeli->no_hp }}" target="_blank"
                            class="text-success text-decoration-none">
                            <i class="bi bi-whatsapp"></i> {{ $pesanan->pembeli->no_hp }}
                        </a>
                    </div>

                    <div>
                        <small class="text-muted d-block mb-1">Alamat Pengiriman</small>
                        <p class="mb-0">{{ $pesanan->alamat_pengiriman }}</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection