@extends('layouts.main')

@section('title', 'Expiry Management')

@section('content')
<div class="d-flex justify-content-between align-items-center mb-4">
    <h4><i class="bi bi-calendar-x me-2"></i>Item Expiry Management</h4>
</div>

@if(session('success'))
    <div class="alert alert-success">{{ session('success') }}</div>
@endif

<div class="row mb-4">
    <div class="col-md-4">
        <div class="card border-danger">
            <div class="card-body text-center">
                <h5 class="text-danger">Expired Items</h5>
                <h2 class="mb-0">{{ $expired->count() }}</h2>
                <small class="text-muted">Items past expiry date</small>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card border-warning">
            <div class="card-body text-center">
                <h5 class="text-warning">Expiring Soon (30 days)</h5>
                <h2 class="mb-0">{{ $expiringSoon->count() }}</h2>
                <small class="text-muted">Items expiring within 30 days</small>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card border-success">
            <div class="card-body text-center">
                <h5 class="text-success">Total Tracked</h5>
                <h2 class="mb-0">{{ $expired->count() + $expiringSoon->count() }}</h2>
                <small class="text-muted">Items with expiry date</small>
            </div>
        </div>
    </div>
</div>

<div class="card">
    <div class="card-header">
        <div class="d-flex justify-content-between align-items-center">
            <div>
                <ul class="nav nav-pills">
                    <li class="nav-item">
                        <a class="nav-link {{ $filter === 'all' ? 'active' : '' }}" href="{{ route('expiry.index', ['filter' => 'all']) }}">All</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link {{ $filter === 'expired' ? 'active' : '' }}" href="{{ route('expiry.index', ['filter' => 'expired']) }}">Expired</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link {{ $filter === 'expiring_7' ? 'active' : '' }}" href="{{ route('expiry.index', ['filter' => 'expiring_7']) }}">Expiring 7 Days</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link {{ $filter === 'expiring_30' ? 'active' : '' }}" href="{{ route('expiry.index', ['filter' => 'expiring_30']) }}">Expiring 30 Days</a>
                    </li>
                </ul>
            </div>
        </div>
    </div>
    <div class="card-body">
        @if($products->isEmpty())
            <div class="text-center text-muted py-4">
                <i class="bi bi-check-circle fs-1"></i>
                <p class="mt-2">No items with expiry dates found.</p>
            </div>
        @else
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>Product</th>
                            <th>Category</th>
                            <th>Stock</th>
                            <th>Expiry Date</th>
                            <th>Days Left</th>
                            <th>Status</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($products as $product)
                        @php
                            $isExpired = $product->expiry_date && \Carbon\Carbon::parse($product->expiry_date)->isPast();
                            $daysLeft = $product->expiry_date ? floor(\Carbon\Carbon::parse($product->expiry_date)->diffInDays(now())) : null;
                        @endphp
                        <tr class="{{ $isExpired ? 'table-danger' : ($daysLeft !== null && $daysLeft <= 30 ? 'table-warning' : '') }}">
                            <td>
                                <strong>{{ $product->name }}</strong><br>
                                <small class="text-muted">{{ $product->sku }}</small>
                            </td>
                            <td>{{ $product->category->name ?? '-' }}</td>
                            <td>{{ $product->stock_quantity }}</td>
                            <td>{{ $product->expiry_date ? $product->expiry_date->format('d M Y') : '-' }}</td>
                            <td>
                                @if($daysLeft !== null)
                                    @if($isExpired)
                                        <span class="badge bg-danger">{{ abs($daysLeft) }} days ago</span>
                                    @else
                                        <span class="badge bg-{{ $daysLeft <= 7 ? 'danger' : ($daysLeft <= 30 ? 'warning' : 'success') }}">{{ $daysLeft }} days</span>
                                    @endif
                                @else
                                    -
                                @endif
                            </td>
                            <td>
                                @if($isExpired)
                                    <span class="badge bg-danger">Expired</span>
                                @elseif($daysLeft !== null && $daysLeft <= 7)
                                    <span class="badge bg-danger">Critical</span>
                                @elseif($daysLeft !== null && $daysLeft <= 30)
                                    <span class="badge bg-warning">Warning</span>
                                @else
                                    <span class="badge bg-success">Good</span>
                                @endif
                            </td>
                            <td>
                                <button class="btn btn-sm btn-outline-primary" data-bs-toggle="modal" data-bs-target="#updateModal{{ $product->id }}">
                                    <i class="bi bi-pencil"></i>
                                </button>
                                @if(!$isExpired)
                                <button class="btn btn-sm btn-outline-warning" data-bs-toggle="modal" data-bs-target="#extendModal{{ $product->id }}">
                                    <i class="bi bi-plus-circle"></i> Extend
                                </button>
                                @endif
                                <button class="btn btn-sm btn-outline-danger" data-bs-toggle="modal" data-bs-target="#disposeModal{{ $product->id }}">
                                    <i class="bi bi-trash"></i>
                                </button>
                            </td>
                        </tr>

                        <div class="modal fade" id="updateModal{{ $product->id }}" tabindex="-1">
                            <div class="modal-dialog">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <h5 class="modal-title">Update Expiry Date - {{ $product->name }}</h5>
                                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                    </div>
                                    <form action="{{ route('expiry.update', $product->id) }}" method="POST">
                                        @csrf
                                        @method('PUT')
                                        <div class="modal-body">
                                            <p class="text-muted mb-2">Current expiry: {{ $product->expiry_date ? $product->expiry_date->format('d M Y') : 'Not set' }}</p>
                                            <div class="mb-3">
                                                <label class="form-label">New Expiry Date</label>
                                                <input type="date" name="expiry_date" class="form-control" value="{{ $product->expiry_date?->format('Y-m-d') }}">
                                            </div>
                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                                            <button type="submit" class="btn btn-primary">Update</button>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>

                        <div class="modal fade" id="extendModal{{ $product->id }}" tabindex="-1">
                            <div class="modal-dialog">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <h5 class="modal-title">Extend Expiry - {{ $product->name }}</h5>
                                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                    </div>
                                    <form action="{{ route('expiry.extend', $product->id) }}" method="POST">
                                        @csrf
                                        <div class="modal-body">
                                            <div class="mb-3">
                                                <label class="form-label">Extend by (days)</label>
                                                <input type="number" name="days" class="form-control" min="1" max="365" value="30" required>
                                            </div>
                                            <p class="text-muted">Current expiry: {{ $product->expiry_date?->format('d M Y') }}</p>
                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                                            <button type="submit" class="btn btn-warning">Extend</button>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>

                        <div class="modal fade" id="disposeModal{{ $product->id }}" tabindex="-1">
                            <div class="modal-dialog">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <h5 class="modal-title">Dispose Stock - {{ $product->name }}</h5>
                                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                    </div>
                                    <form action="{{ route('expiry.dispose', $product->id) }}" method="POST">
                                        @csrf
                                        <div class="modal-body">
                                            <div class="mb-3">
                                                <label class="form-label">Quantity to Dispose</label>
                                                <input type="number" name="quantity" class="form-control" min="1" max="{{ $product->stock_quantity }}" value="1" required>
                                                <small class="text-muted">Available: {{ $product->stock_quantity }}</small>
                                            </div>
                                            <div class="mb-3">
                                                <label class="form-label">Reason</label>
                                                <input type="text" name="reason" class="form-control" placeholder="e.g., Expired, Damaged" required>
                                            </div>
                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                                            <button type="submit" class="btn btn-danger">Dispose</button>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                        @endforeach
                    </tbody>
                </table>
            </div>
        @endif
    </div>
</div>
@endsection