@extends('layouts.app')

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

    <!-- Stats -->
    <div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-6">
        <div class="rounded-2xl border border-gray-200 bg-white dark:border-gray-800 dark:bg-white/[0.03] p-5">
            <p class="text-sm text-gray-500 dark:text-gray-400">Total Bahan</p>
            <p class="text-2xl font-semibold text-gray-800 dark:text-white/90">{{ $stats['total_materials'] }}</p>
        </div>
        <div class="rounded-2xl border border-warning-200 bg-warning-50 dark:border-warning-800 dark:bg-warning-500/10 p-5">
            <p class="text-sm text-warning-700 dark:text-warning-400">Stok Rendah</p>
            <p class="text-2xl font-semibold text-warning-800 dark:text-warning-400">{{ $stats['low_stock'] }}</p>
        </div>
        <div class="rounded-2xl border border-error-200 bg-error-50 dark:border-error-800 dark:bg-error-500/10 p-5">
            <p class="text-sm text-error-700 dark:text-error-400">Stok Habis</p>
            <p class="text-2xl font-semibold text-error-800 dark:text-error-400">{{ $stats['out_of_stock'] }}</p>
        </div>
    </div>

    <div class="rounded-2xl border border-gray-200 bg-white dark:border-gray-800 dark:bg-white/[0.03] p-5 lg:p-6">
        <div class="flex flex-wrap items-center justify-between gap-4 mb-6">
            <div>
                <h2 class="text-xl font-semibold text-gray-800 dark:text-white/90">{{ $title }}</h2>
            </div>
            <div class="flex gap-2">
                <a href="{{ route('stock.incoming.create') }}"
                    class="inline-flex items-center justify-center gap-2 rounded-lg bg-success-500 px-4 py-2.5 text-sm font-medium text-white transition hover:bg-success-600">
                    <svg xmlns="http://www.w3.org/2000/svg" class="w-5 h-5" viewBox="0 0 20 20" fill="currentColor">
                        <path fill-rule="evenodd" d="M10 5a1 1 0 011 1v3h3a1 1 0 110 2h-3v3a1 1 0 11-2 0v-3H6a1 1 0 110-2h3V6a1 1 0 011-1z" clip-rule="evenodd" />
                    </svg>
                    Tambah Stok
                </a>
            </div>
        </div>

        <!-- Filter -->
        <form method="GET" class="flex flex-wrap gap-3 mb-6 items-end">
            <div class="w-48">
                <x-form.select 
                    name="status" 
                    label="Status Stok"
                    placeholder="Semua Status"
                    :value="request('status')"
                    :options="[
                        'available' => 'Tersedia',
                        'low' => 'Stok Rendah',
                        'out' => 'Stok Habis',
                        'custom' => 'Barang Custom'
                    ]"
                />
            </div>
            <button type="submit" class="h-11 rounded-lg bg-gray-100 px-4 text-sm font-medium text-gray-700 hover:bg-gray-200 dark:bg-gray-800 dark:text-gray-300">
                Filter
            </button>
        </form>

        <div class="overflow-hidden rounded-xl border border-gray-200 dark:border-gray-800">
            <table class="min-w-full divide-y divide-gray-200 dark:divide-gray-800">
                <thead class="bg-gray-50 dark:bg-gray-800/50">
                    <tr>
                        <th class="px-5 py-3 text-left text-xs font-medium uppercase tracking-wider text-gray-500 dark:text-gray-400">Bahan</th>
                        <th class="px-5 py-3 text-left text-xs font-medium uppercase tracking-wider text-gray-500 dark:text-gray-400">Kategori</th>
                        <th class="px-5 py-3 text-right text-xs font-medium uppercase tracking-wider text-gray-500 dark:text-gray-400">Stok</th>
                        <th class="px-5 py-3 text-right text-xs font-medium uppercase tracking-wider text-gray-500 dark:text-gray-400">Min. Stok</th>
                        <th class="px-5 py-3 text-center text-xs font-medium uppercase tracking-wider text-gray-500 dark:text-gray-400">Status</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-200 bg-white dark:divide-gray-800 dark:bg-white/[0.03]">
                    @forelse ($materials as $material)
                        @php
                            $isCustom = (bool) ($material->is_custom ?? false);
                            $isOut = !$isCustom && (float) $material->stock <= 0;
                            $isLow = !$isCustom && (bool) ($material->is_low_stock ?? false);
                        @endphp

                        <tr>
                            <td class="whitespace-nowrap px-5 py-4 text-sm font-medium text-gray-800 dark:text-white/90">
                                <div class="flex items-center gap-2">
                                    <span>{{ $material->name }}</span>
                                    @if($isCustom)
                                        <span class="inline-flex items-center rounded-full px-2.5 py-0.5 text-xs font-medium bg-brand-50 text-brand-700 dark:bg-brand-500/15 dark:text-brand-400">
                                            Custom
                                        </span>
                                    @endif
                                </div>
                            </td>
                            <td class="px-5 py-4 text-sm text-gray-500 dark:text-gray-400">
                                {{ $material->getCategoryName() }}
                            </td>
                            <td class="whitespace-nowrap px-5 py-4 text-right text-sm font-medium">
                                @if($isCustom)
                                    <div class="text-brand-600 dark:text-brand-400 font-semibold">
                                        Mengikuti komponen
                                    </div>
                                    <div class="text-xs text-gray-400 dark:text-gray-500">
                                        Stok bahan custom tidak dipakai
                                    </div>
                                @else
                                    <span class="{{ $isOut ? 'text-error-500' : ($isLow ? 'text-warning-500' : 'text-gray-800 dark:text-white/90') }}">
                                        {{ number_format((float) $material->stock, 2, ',', '.') }} {{ $material->unit }}
                                    </span>
                                @endif
                            </td>
                            <td class="whitespace-nowrap px-5 py-4 text-right text-sm text-gray-500 dark:text-gray-400">
                                @if($isCustom)
                                    -
                                @else
                                    {{ number_format((float) $material->min_stock, 2, ',', '.') }} {{ $material->unit }}
                                @endif
                            </td>
                            <td class="whitespace-nowrap px-5 py-4 text-center">
                                @if ($isCustom)
                                    <span class="inline-flex items-center rounded-full px-2.5 py-0.5 text-xs font-medium bg-brand-50 text-brand-700 dark:bg-brand-500/15 dark:text-brand-400">
                                        Custom
                                    </span>
                                @elseif ($isOut)
                                    <span class="inline-flex items-center rounded-full px-2.5 py-0.5 text-xs font-medium bg-error-50 text-error-700 dark:bg-error-500/15 dark:text-error-500">
                                        Habis
                                    </span>
                                @elseif ($isLow)
                                    <span class="inline-flex items-center rounded-full px-2.5 py-0.5 text-xs font-medium bg-warning-50 text-warning-700 dark:bg-warning-500/15 dark:text-warning-500">
                                        Rendah
                                    </span>
                                @else
                                    <span class="inline-flex items-center rounded-full px-2.5 py-0.5 text-xs font-medium bg-success-50 text-success-700 dark:bg-success-500/15 dark:text-success-500">
                                        OK
                                    </span>
                                @endif
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="5" class="px-5 py-8 text-center text-sm text-gray-500 dark:text-gray-400">
                                Tidak ada data stok
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        <div class="mt-4">
            {{ $materials->links() }}
        </div>
    </div>
@endsection
