@extends('layouts.admin')

@section('title', 'Detail Pesanan')
@section('page-title', 'Detail Pesanan #' . $pesanan->kode_invoice)

@section('content')
<div class="row">
    <div class="col-md-8">
        <div class="card">
            <div class="card-header">
                <h4>Informasi Pesanan</h4>
            </div>
            <div class="card-body">
                <table class="table table-borderless">
                    <tr>
                        <th width="30%">Nomor Invoice</th>
                        <td>: <span class="text-primary font-bold">#{{ $pesanan->kode_invoice }}</span></td>
                    </tr>
                    <tr>
                        <th>Tanggal Pesanan</th>
                        <td>: {{ \Carbon\Carbon::parse($pesanan->tanggal_transaksi)->format('d F Y H:i') }}</td>
                    </tr>
                    <tr>
                        <th>Status</th>
                        <td>: 
                            @if($pesanan->status == 'menunggu_konfirmasi')
                                <span class="badge bg-warning">Perlu Konfirmasi</span>
                            @elseif($pesanan->status == 'diproses')
                                <span class="badge bg-info">Diproses</span>
                            @elseif($pesanan->status == 'dikirim')
                                <span class="badge bg-primary">Dikirim</span>
                            @elseif($pesanan->status == 'selesai')
                                <span class="badge bg-success">Selesai</span>
                            @else
                                <span class="badge bg-danger">Batal</span>
                            @endif
                        </td>
                    </tr>
                </table>

                <hr>

                <h5>Daftar Produk</h5>
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>Produk</th>
                                <th class="text-center">Jumlah</th>
                                <th class="text-end">Harga Satuan</th>
                                <th class="text-end">Subtotal</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($pesanan->details 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) : asset('template/frontend/img/fruite-item-5.jpg') }}" 
                                                     alt="{{ $detail->produk->nama_produk }}" 
                                                     class="rounded me-2" 
                                                     style="width: 50px; height: 50px; object-fit: cover;">
                                                <div>
                                                    <h6 class="mb-0">{{ $detail->produk->nama_produk }}</h6>
                                                </div>
                                            </div>
                                        </td>
                                        <td class="text-center">{{ $detail->jumlah }}</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>
                        <tfoot>
                            <tr>
                                <td colspan="3" class="text-end fw-bold">Total Pendapatan:</td>
                                <td class="text-end fw-bold text-success">
                                    @php
                                        $totalPetani = $pesanan->details->filter(function($detail) {
                                            return $detail->produk->petani_id == Auth::guard('petani')->id();
                                        })->sum('subtotal');
                                    @endphp
                                    Rp {{ number_format($totalPetani, 0, ',', '.') }}
                                </td>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <div class="col-md-4">
        <div class="card">
            <div class="card-header">
                <h4>Informasi Pembeli</h4>
            </div>
            <div class="card-body">
                <div class="d-flex align-items-center mb-3">
                    <div class="avatar avatar-lg me-3">
                        <img src="{{ asset('template/admin/static/images/faces/2.jpg') }}" alt="Pembeli">
                    </div>
                    <div>
                        <h5 class="mb-0">{{ $pesanan->pembeli->nama_lengkap }}</h5>
                        <small class="text-muted">{{ $pesanan->pembeli->username }}</small>
                    </div>
                </div>
                
                <hr>
                
                <p><strong><i class="bi bi-telephone me-2"></i> No. HP / WA:</strong><br>
                <a href="https://wa.me/{{ $pesanan->pembeli->no_hp }}" target="_blank" class="text-success">{{ $pesanan->pembeli->no_hp }}</a></p>
                
                <p><strong><i class="bi bi-geo-alt me-2"></i> Alamat Pengiriman:</strong><br>
                {{ $pesanan->alamat_pengiriman }}</p>
            </div>
        </div>

        <div class="card">
            <div class="card-header">
                <h4>Aksi Pesanan</h4>
            </div>
            <div class="card-body">
                @if($pesanan->status == 'menunggu_konfirmasi')
                    <p class="text-muted mb-3">Pesanan baru masuk. Silakan terima jika stok tersedia atau tolak jika tidak bisa dipenuhi.</p>
                    <div class="d-grid gap-2">
                        <form action="{{ route('petani.pesanan.update', $pesanan->id) }}" method="POST">
                            @csrf @method('PATCH')
                            <input type="hidden" name="status" value="diproses">
                            <button class="btn btn-success w-100" onclick="return confirm('Terima pesanan ini?')">
                                <i class="bi bi-check-circle me-2"></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('Tolak pesanan ini?')">
                                <i class="bi bi-x-circle me-2"></i> Tolak Pesanan
                            </button>
                        </form>
                    </div>
                @elseif($pesanan->status == 'diproses')
                    <p class="text-muted mb-3">Pesanan sedang diproses. Jika barang sudah dikirim atau siap diambil, update statusnya.</p>
                    <form action="{{ route('petani.pesanan.update', $pesanan->id) }}" method="POST">
                        @csrf @method('PATCH')
                        <input type="hidden" name="status" value="dikirim">
                        <button class="btn btn-primary w-100">
                            <i class="bi bi-truck me-2"></i> Kirim Barang
                        </button>
                    </form>
                @elseif($pesanan->status == 'dikirim')
                    <div class="alert alert-info">
                        <i class="bi bi-info-circle me-2"></i> Barang sedang dalam pengiriman. Menunggu konfirmasi selesai dari pembeli atau sistem.
                    </div>
                @elseif($pesanan->status == 'selesai')
                    <div class="alert alert-success">
                        <i class="bi bi-check-circle-fill me-2"></i> Pesanan Selesai.
                    </div>
                @elseif($pesanan->status == 'batal')
                    <div class="alert alert-danger">
                        <i class="bi bi-x-circle-fill me-2"></i> Pesanan Dibatalkan.
                    </div>
                @endif

                <div class="mt-3 text-center">
                    <a href="{{ route('petani.pesanan.index') }}" class="btn btn-link text-secondary">Kembali ke Daftar</a>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection