@extends('layouts.app')

@section('content')
    <x-common.page-breadcrumb pageTitle="{{ $title ?? 'Detail Pembelian' }}" />

    @include('partials.alerts')

    <div class="grid grid-cols-1 gap-6 lg:grid-cols-3">
        {{-- Main Info --}}
        <div class="lg:col-span-2 space-y-6">
            {{-- Header Card --}}
            <div class="rounded-2xl border border-gray-200 bg-white dark:border-gray-800 dark:bg-white/[0.03]">
                <div class="border-b border-gray-200 px-5 py-4 dark:border-gray-800">
                    <div class="flex items-center justify-between">
                        <h3 class="text-lg font-semibold text-gray-800 dark:text-white/90">
                            {{ $purchase->material->name ?? 'Pembelian' }}
                        </h3>
                        <div class="flex items-center gap-2">
                            @if($purchase->payment_status === 'paid')
                                <span class="inline-flex rounded-full px-3 py-1 text-xs font-medium bg-success-100 text-success-700 dark:bg-success-500/20 dark:text-success-400">
                                    Lunas
                                </span>
                            @elseif($purchase->payment_status === 'partial')
                                <span class="inline-flex rounded-full px-3 py-1 text-xs font-medium bg-warning-100 text-warning-700 dark:bg-warning-500/20 dark:text-warning-400">
                                    Sebagian Dibayar
                                </span>
                            @else
                                <span class="inline-flex rounded-full px-3 py-1 text-xs font-medium bg-error-100 text-error-700 dark:bg-error-500/20 dark:text-error-400">
                                    Belum Dibayar
                                </span>
                            @endif
                        </div>
                    </div>
                </div>

                <div class="p-5">
                    <div class="grid grid-cols-1 gap-6 md:grid-cols-2">
                        <div>
                            <h4 class="text-sm font-semibold text-gray-500 dark:text-gray-400 mb-3">Detail Pembelian</h4>
                            <dl class="space-y-2">
                                <div>
                                    <dt class="text-xs text-gray-500">Tanggal</dt>
                                    <dd class="text-sm font-medium text-gray-800 dark:text-white/90">
                                        {{ $purchase->purchase_date->translatedFormat('d F Y') }}
                                    </dd>
                                </div>
                                <div>
                                    <dt class="text-xs text-gray-500">Bahan</dt>
                                    <dd class="text-sm font-medium text-gray-800 dark:text-white/90">
                                        {{ $purchase->material->name ?? '-' }}
                                    </dd>
                                </div>
                                <div>
                                    <dt class="text-xs text-gray-500">Tipe</dt>
                                    <dd class="text-sm font-medium text-gray-800 dark:text-white/90">
                                        {{ $purchase->type_label }}
                                    </dd>
                                </div>
                            </dl>
                        </div>

                        <div>
                            <h4 class="text-sm font-semibold text-gray-500 dark:text-gray-400 mb-3">Supplier & Qty</h4>
                            <dl class="space-y-2">
                                <div>
                                    <dt class="text-xs text-gray-500">Supplier</dt>
                                    <dd class="text-sm font-medium text-gray-800 dark:text-white/90">
                                        {{ $purchase->supplierRelation->name ?? $purchase->supplier ?? '-' }}
                                    </dd>
                                </div>
                                <div>
                                    <dt class="text-xs text-gray-500">Jumlah</dt>
                                    <dd class="text-sm font-medium text-gray-800 dark:text-white/90">
                                        {{ number_format($purchase->quantity, 2, ',', '.') }} {{ $purchase->material->unit ?? '' }}
                                    </dd>
                                </div>
                                <div>
                                    <dt class="text-xs text-gray-500">Harga Satuan</dt>
                                    <dd class="text-sm font-medium text-gray-800 dark:text-white/90">
                                        Rp {{ number_format($purchase->unit_price, 0, ',', '.') }}
                                    </dd>
                                </div>
                            </dl>
                        </div>
                    </div>

                    @if($purchase->notes)
                        <div class="mt-4 rounded-lg bg-gray-50 dark:bg-gray-800 p-3">
                            <dt class="text-xs text-gray-500 mb-1">Catatan</dt>
                            <dd class="text-sm text-gray-700 dark:text-gray-300">{{ $purchase->notes }}</dd>
                        </div>
                    @endif
                </div>
            </div>

            {{-- Payment Summary --}}
            <div class="rounded-2xl border border-gray-200 bg-white dark:border-gray-800 dark:bg-white/[0.03]">
                <div class="border-b border-gray-200 px-5 py-4 dark:border-gray-800">
                    <h3 class="text-lg font-semibold text-gray-800 dark:text-white/90">Ringkasan Pembayaran</h3>
                </div>
                <div class="p-5">
                    @php
                        $progressPercent = $purchase->total_price > 0
                            ? min(100, ($purchase->paid_amount / $purchase->total_price) * 100)
                            : 0;
                    @endphp

                    {{-- Progress Bar --}}
                    <div class="mb-4">
                        <div class="flex items-center justify-between mb-2">
                            <span class="text-sm text-gray-500 dark:text-gray-400">Progress</span>
                            <span class="text-sm font-semibold text-brand-600 dark:text-brand-400">
                                {{ number_format($progressPercent, 0) }}%
                            </span>
                        </div>
                        <div class="h-2.5 w-full overflow-hidden rounded-full bg-gray-200 dark:bg-gray-700">
                            <div
                                class="h-full rounded-full bg-brand-500 transition-all duration-500"
                                style="width: {{ $progressPercent }}%"
                            ></div>
                        </div>
                    </div>

                    {{-- Totals --}}
                    <div class="space-y-2">
                        <div class="flex items-center justify-between">
                            <span class="text-sm text-gray-500 dark:text-gray-400">Total Tagihan</span>
                            <span class="text-sm font-medium text-gray-800 dark:text-white/90">
                                {{ $purchase->formatted_total }}
                            </span>
                        </div>
                        <div class="flex items-center justify-between">
                            <span class="text-sm text-gray-500 dark:text-gray-400">Sudah Dibayar</span>
                            <span class="text-sm font-medium text-success-600 dark:text-success-400">
                                {{ $purchase->formatted_paid }}
                            </span>
                        </div>
                        <div class="flex items-center justify-between pt-2 border-t border-gray-100 dark:border-gray-700">
                            <span class="text-sm font-medium text-gray-700 dark:text-gray-300">Sisa Hutang</span>
                            <span class="text-lg font-bold {{ $purchase->remaining > 0 ? 'text-error-600 dark:text-error-400' : 'text-success-600 dark:text-success-400' }}">
                                {{ $purchase->formatted_remaining }}
                            </span>
                        </div>
                    </div>
                </div>
            </div>

            {{-- Payment History --}}
            <div class="rounded-2xl border border-gray-200 bg-white dark:border-gray-800 dark:bg-white/[0.03]">
                <div class="border-b border-gray-200 px-5 py-4 dark:border-gray-800">
                    <div class="flex items-center justify-between">
                        <h3 class="text-lg font-semibold text-gray-800 dark:text-white/90">Riwayat Pembayaran</h3>
                        <span class="text-xs text-gray-500 dark:text-gray-400">
                            {{ $purchase->payments->count() }} pembayaran
                        </span>
                    </div>
                </div>

                <div class="p-5">
                    @if($purchase->payments->count())
                        <div class="space-y-4">
                            @foreach($purchase->payments as $index => $payment)
                                <div class="rounded-xl border border-gray-200 dark:border-gray-700 p-4">
                                    <div class="flex items-start justify-between mb-3">
                                        <div class="flex items-center gap-3">
                                            <span class="inline-flex items-center justify-center h-6 w-6 rounded-full bg-success-100 dark:bg-success-500/20 text-success-700 dark:text-success-400 text-xs font-bold">
                                                {{ $index + 1 }}
                                            </span>
                                            <div>
                                                <h5 class="text-sm font-semibold text-gray-800 dark:text-white/90">
                                                    {{ $payment->formatted_amount }}
                                                </h5>
                                                <p class="text-xs text-gray-500">
                                                    {{ $payment->payment_date->translatedFormat('d F Y') }}
                                                </p>
                                            </div>
                                        </div>

                                        <div class="flex items-center gap-2">
                                            @if($payment->proof)
                                                <a
                                                    href="{{ $payment->proof_url }}"
                                                    target="_blank"
                                                    class="inline-flex items-center gap-1 rounded-lg bg-brand-50 px-2 py-1 text-xs font-medium text-brand-600 hover:bg-brand-100 dark:bg-brand-500/10 dark:text-brand-400"
                                                >
                                                    <svg class="h-3.5 w-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"/>
                                                    </svg>
                                                    Bukti
                                                </a>
                                            @endif

                                            <form action="{{ route('purchases.payments.destroy', [$purchase, $payment]) }}" method="POST" onsubmit="return confirm('Hapus pembayaran ini?')">
                                                @csrf
                                                @method('DELETE')
                                                <button type="submit" class="text-xs text-error-500 hover:text-error-600">
                                                    Hapus
                                                </button>
                                            </form>
                                        </div>
                                    </div>

                                    @if($payment->notes)
                                        <div class="rounded-lg bg-gray-50 dark:bg-gray-800 p-2">
                                            <span class="text-xs text-gray-500">Catatan:</span>
                                            <span class="text-sm text-gray-700 dark:text-gray-300">{{ $payment->notes }}</span>
                                        </div>
                                    @endif
                                </div>
                            @endforeach
                        </div>
                    @else
                        <div class="text-center py-8">
                            <svg class="mx-auto h-12 w-12 text-gray-300 dark:text-gray-600" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 10h18M7 15h1m4 0h1m-7 4h12a3 3 0 003-3V8a3 3 0 00-3-3H6a3 3 0 00-3 3v8a3 3 0 003 3z"/>
                            </svg>
                            <p class="mt-2 text-sm text-gray-500 dark:text-gray-400">Belum ada pembayaran</p>
                        </div>
                    @endif
                </div>
            </div>
        </div>

        {{-- Sidebar --}}
        <div class="space-y-6">
            {{-- Actions --}}
            <div class="rounded-2xl border border-gray-200 bg-white dark:border-gray-800 dark:bg-white/[0.03]">
                <div class="border-b border-gray-200 px-5 py-4 dark:border-gray-800">
                    <h3 class="text-lg font-semibold text-gray-800 dark:text-white/90">Aksi</h3>
                </div>
                <div class="p-5 space-y-3">
                    <a
                        href="{{ route('purchases.index') }}"
                        class="flex w-full items-center justify-center gap-2 rounded-lg border border-gray-300 bg-white px-4 py-2.5 text-sm font-medium text-gray-700 hover:bg-gray-50 dark:border-gray-700 dark:bg-gray-800 dark:text-gray-300 dark:hover:bg-gray-700"
                    >
                        <svg class="size-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18" />
                        </svg>
                        Kembali
                    </a>
                </div>
            </div>

            {{-- Add Payment Form --}}
            @if($purchase->hasDebt())
                <div class="rounded-2xl border border-gray-200 bg-white dark:border-gray-800 dark:bg-white/[0.03]" x-data="addPaymentForm({{ (int) $purchase->remaining }})">
                    <div class="border-b border-gray-200 px-5 py-4 dark:border-gray-800">
                        <h3 class="text-lg font-semibold text-gray-800 dark:text-white/90">Tambah Pembayaran</h3>
                    </div>

                    <div class="p-5">
                        <form action="{{ route('purchases.payments.store', $purchase) }}" method="POST" enctype="multipart/form-data" class="space-y-4">
                            @csrf

                            {{-- Quick Amount --}}
                            <div class="flex gap-2">
                                <button
                                    type="button"
                                    @click="setAmount({{ (int) $purchase->remaining }})"
                                    class="flex-1 rounded-lg border border-brand-200 bg-brand-50 px-3 py-2 text-xs font-medium text-brand-700 hover:bg-brand-100 dark:border-brand-500/30 dark:bg-brand-500/10 dark:text-brand-400"
                                >
                                    Lunasi
                                </button>

                                @if($purchase->remaining > 100000)
                                    <button
                                        type="button"
                                        @click="setAmount({{ (int) floor($purchase->remaining / 2) }})"
                                        class="flex-1 rounded-lg border border-gray-200 bg-gray-50 px-3 py-2 text-xs font-medium text-gray-700 hover:bg-gray-100 dark:border-gray-700 dark:bg-gray-800 dark:text-gray-300"
                                    >
                                        50%
                                    </button>
                                @endif
                            </div>

                            <div>
                                <label class="mb-1.5 block text-sm font-medium text-gray-700 dark:text-gray-400">
                                    Jumlah <span class="text-error-500">*</span>
                                </label>
                                <div class="relative flex">
                                    <span class="inline-flex items-center px-3 text-sm text-gray-500 bg-gray-100 border border-r-0 border-gray-300 rounded-l-lg dark:bg-gray-800 dark:border-gray-700 dark:text-gray-400">
                                        Rp
                                    </span>
                                    <input
                                        type="text"
                                        x-model="amount_display"
                                        @input="formatCurrency($event)"
                                        required
                                        class="h-10 w-full rounded-r-lg border border-gray-300 bg-transparent px-3 py-2 text-sm text-gray-800 focus:border-brand-300 focus:outline-none focus:ring focus:ring-brand-200 focus:ring-opacity-40 dark:border-gray-700 dark:bg-gray-900 dark:text-white/90 dark:focus:border-brand-800"
                                        placeholder="0"
                                    >
                                </div>
                                <input type="hidden" name="amount" :value="amount">
                                <p class="mt-1 text-xs text-gray-500 dark:text-gray-400">
                                    Maks: {{ $purchase->formatted_remaining }}
                                </p>
                            </div>

                            <div>
                                <x-form.date-picker name="payment_date" label="Tanggal Bayar" :defaultDate="date('Y-m-d')" required />
                            </div>

                            <div>
                                <label class="mb-1.5 block text-sm font-medium text-gray-700 dark:text-gray-400">
                                    Metode Pembayaran <span class="text-error-500">*</span>
                                </label>
                                <select
                                    name="payment_method"
                                    required
                                    class="h-10 w-full rounded-lg border border-gray-300 bg-transparent px-3 py-2 text-sm text-gray-800 focus:border-brand-300 focus:outline-none focus:ring focus:ring-brand-200 focus:ring-opacity-40 dark:border-gray-700 dark:bg-gray-900 dark:text-white/90 dark:focus:border-brand-800"
                                >
                                    <option value="cash" {{ old('payment_method', 'cash') === 'cash' ? 'selected' : '' }}>
                                        Cash
                                    </option>
                                    <option value="transfer" {{ old('payment_method') === 'transfer' ? 'selected' : '' }}>
                                        Bank Transfer
                                    </option>
                                </select>
                                <p class="mt-1 text-xs text-gray-500 dark:text-gray-400">
                                    Transfer akan memotong saldo Kas Bank gabungan.
                                </p>
                            </div>

                            <div>
                                <label class="mb-1.5 block text-sm font-medium text-gray-700 dark:text-gray-400">
                                    Bukti Pembayaran
                                </label>
                                <input
                                    type="file"
                                    name="proof"
                                    accept="image/*"
                                    class="block w-full text-sm text-gray-500 file:mr-3 file:py-1.5 file:px-3 file:rounded-lg file:border-0 file:text-xs file:font-medium file:bg-brand-50 file:text-brand-700 hover:file:bg-brand-100 dark:text-gray-400 dark:file:bg-brand-500/20 dark:file:text-brand-400"
                                >
                            </div>

                            <div>
                                <x-form.textarea name="notes" label="Catatan" rows="2" placeholder="Catatan (opsional)" />
                            </div>

                            <button
                                type="submit"
                                class="flex w-full items-center justify-center gap-2 rounded-lg bg-brand-500 px-4 py-2.5 text-sm font-medium text-white hover:bg-brand-600"
                            >
                                <svg class="size-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" />
                                </svg>
                                Simpan Pembayaran
                            </button>
                        </form>
                    </div>
                </div>
            @else
                {{-- Lunas Badge --}}
                <div class="rounded-2xl border border-success-200 bg-success-50 dark:border-success-500/30 dark:bg-success-500/10 p-6 text-center">
                    <div class="mx-auto mb-3 flex h-12 w-12 items-center justify-center rounded-full bg-success-100 dark:bg-success-500/20">
                        <svg class="h-6 w-6 text-success-600 dark:text-success-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
                        </svg>
                    </div>
                    <h4 class="font-semibold text-success-700 dark:text-success-400">Lunas</h4>
                    <p class="mt-1 text-xs text-success-600/80 dark:text-success-400/70">Semua pembayaran selesai</p>
                </div>
            @endif

            {{-- Timeline --}}
            <div class="rounded-2xl border border-gray-200 bg-white dark:border-gray-800 dark:bg-white/[0.03]">
                <div class="border-b border-gray-200 px-5 py-4 dark:border-gray-800">
                    <h3 class="text-lg font-semibold text-gray-800 dark:text-white/90">Riwayat</h3>
                </div>
                <div class="p-5">
                    <div class="space-y-4">
                        <div class="flex gap-3">
                            <div class="flex h-8 w-8 items-center justify-center rounded-full bg-brand-100 text-brand-500 dark:bg-brand-500/20">
                                <svg class="size-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4" />
                                </svg>
                            </div>
                            <div>
                                <p class="text-sm font-medium text-gray-800 dark:text-white/90">Pembelian Dibuat</p>
                                <p class="text-xs text-gray-500">{{ $purchase->created_at->format('d M Y H:i') }}</p>
                            </div>
                        </div>

                        @foreach($purchase->payments as $payment)
                            <div class="flex gap-3">
                                <div class="flex h-8 w-8 items-center justify-center rounded-full bg-success-100 text-success-500 dark:bg-success-500/20">
                                    <svg class="size-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" />
                                    </svg>
                                </div>
                                <div>
                                    <p class="text-sm font-medium text-gray-800 dark:text-white/90">
                                        Pembayaran {{ $payment->formatted_amount }}
                                    </p>
                                    <p class="text-xs text-gray-500">{{ $payment->payment_date->format('d M Y') }}</p>
                                </div>
                            </div>
                        @endforeach

                        @if($purchase->payment_status === 'paid')
                            <div class="flex gap-3">
                                <div class="flex h-8 w-8 items-center justify-center rounded-full bg-success-100 text-success-500 dark:bg-success-500/20">
                                    <svg class="size-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
                                    </svg>
                                </div>
                                <div>
                                    <p class="text-sm font-medium text-gray-800 dark:text-white/90">Lunas</p>
                                    <p class="text-xs text-gray-500">Pembayaran selesai</p>
                                </div>
                            </div>
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        function addPaymentForm(maxRemaining) {
            return {
                max: parseInt(maxRemaining) || 0,

                amount: 0,
                amount_display: '',

                formatCurrency(event) {
                    let raw = (event.target.value || '').replace(/[^0-9]/g, '');
                    let num = parseInt(raw) || 0;

                    if (this.max > 0 && num > this.max) num = this.max;

                    this.amount = num;
                    this.amount_display = num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, '.');
                    event.target.value = this.amount_display;
                },

                setAmount(value) {
                    let num = parseInt(value) || 0;
                    if (this.max > 0 && num > this.max) num = this.max;

                    this.amount = num;
                    this.amount_display = num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, '.');
                }
            }
        }
    </script>
@endsection