@extends('layouts.admin-simple')

@section('title', 'Stock on Project Report')

@section('breadcrumb')
    <li class="breadcrumb-item">Warehouse</li>
    <li class="breadcrumb-item"><a href="{{ route('warehouse.reports.index') }}">Reports</a></li>
    <li class="breadcrumb-item active">Stock on Project</li>
@endsection

@section('page-title')
    <div class="page-title-left">
        <h3 class="mb-1">Stock on Project Report</h3>
        <p class="text-muted mb-0" style="font-size: 13px;">View stock levels and inventory distribution across projects</p>
    </div>
@endsection

@section('content')
    <!-- Statistics Cards -->
    <div class="row mb-4 warehouse-stats-row">
        <div class="col-xl-3 col-md-6 mb-3">
            <x-warehouse.stats-card
                title="Total Projects"
                :value="$stats['total_projects']"
                icon="business_center"
                color="primary"
                subtitle="Projects with stock"
            />
        </div>

        <div class="col-xl-3 col-md-6 mb-3">
            <x-warehouse.stats-card
                title="Unique Items"
                :value="number_format($stats['total_unique_items'])"
                icon="inventory_2"
                color="success"
                subtitle="Different items across projects"
            />
        </div>

        <div class="col-xl-3 col-md-6 mb-3">
            <x-warehouse.stats-card
                title="Total Stock Qty"
                :value="number_format($stats['total_stock'], 2)"
                icon="inventory"
                color="info"
                subtitle="Total quantity on projects"
            />
        </div>

        <div class="col-xl-3 col-md-6 mb-3">
            <x-warehouse.stats-card
                title="Total Value"
                :value="'AED ' . number_format($stats['total_value'], 2)"
                icon="attach_money"
                color="warning"
                subtitle="Total inventory value"
                :showDirham="true"
            />
        </div>
    </div>

    <!-- Filters Panel -->
    <div class="card mb-4 warehouse-filters-panel">
        <div class="card-header">
            <h5 class="mb-0">
                <i class="material-icons align-middle mr-2">filter_list</i>
                Filters
            </h5>
        </div>
        <div class="card-body">
            <form id="filterForm" class="warehouse-filter-form" method="GET" action="{{ route('warehouse.reports.stock-on-project') }}">
                <div class="row">
                    <div class="col-md-3 mb-3">
                        <label class="form-label">Search</label>
                        <input type="text" name="search" value="{{ request('search') }}" class="form-control"
                               placeholder="Project name, number">
                    </div>

                    <div class="col-md-3 mb-3">
                        <label class="form-label">Project</label>
                        <select name="project_id" class="form-control warehouse-select2" data-placeholder="All Projects">
                            <option value="">All Projects</option>
                            @foreach($allProjects as $project)
                                <option value="{{ $project->id }}" {{ request('project_id') == $project->id ? 'selected' : '' }}>
                                    {{ $project->project_name }} @if($project->project_number) ({{ $project->project_number }}) @endif
                                </option>
                            @endforeach
                        </select>
                    </div>

                    <div class="col-md-2 mb-3">
                        <label class="form-label">Project Status</label>
                        <select name="project_status" class="form-control warehouse-select2" data-placeholder="All Statuses">
                            <option value="">All Statuses</option>
                            @foreach($projectStatuses as $status)
                                <option value="{{ $status }}" {{ request('project_status') == $status ? 'selected' : '' }}>
                                    {{ ucfirst(str_replace('_', ' ', $status)) }}
                                </option>
                            @endforeach
                        </select>
                    </div>

                    <div class="col-md-2 mb-3">
                        <label class="form-label">Division</label>
                        <select name="division_id" class="form-control warehouse-select2" data-placeholder="All Divisions">
                            <option value="">All Divisions</option>
                            @foreach($divisions as $division)
                                <option value="{{ $division->id }}" {{ request('division_id') == $division->id ? 'selected' : '' }}>
                                    {{ $division->division_name }}
                                </option>
                            @endforeach
                        </select>
                    </div>

                    <div class="col-md-2 mb-3">
                        <label class="form-label">Date From</label>
                        <input type="date" name="date_from" value="{{ request('date_from') }}" class="form-control warehouse-datepicker">
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-2 mb-3">
                        <label class="form-label">Date To</label>
                        <input type="date" name="date_to" value="{{ request('date_to') }}" class="form-control warehouse-datepicker">
                    </div>

                    <div class="col-md-2 mb-3">
                        <label class="form-label">&nbsp;</label>
                        <button type="submit" class="btn btn-primary btn-block">
                            <i class="material-icons" style="font-size: 18px; vertical-align: middle;">search</i>
                            Apply
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <!-- Export Buttons -->
    <div class="row warehouse-export-buttons">
        <div class="col-12 text-right">
            <button class="btn btn-success warehouse-export-btn" data-export="excel">
                <i class="material-icons" style="font-size: 18px; vertical-align: middle;">download</i>
                Export Excel
            </button>
            <button class="btn btn-info warehouse-export-btn" data-export="pdf">
                <i class="material-icons" style="font-size: 18px; vertical-align: middle;">picture_as_pdf</i>
                Export PDF
            </button>
            <button class="btn btn-secondary warehouse-export-btn" data-action="print">
                <i class="material-icons" style="font-size: 18px; vertical-align: middle;">print</i>
                Print
            </button>
        </div>
    </div>

    <!-- Stock on Project Report Table -->
    <div class="card">
        <div class="card-header warehouse-report-header">
            <h5 class="mb-0 warehouse-report-title">
                <i class="material-icons align-middle mr-2">business_center</i>
                Stock on Project Report ({{ $projects->total() }} total projects)
            </h5>
        </div>
        <div class="card-body p-0">
            <div class="warehouse-table-container">
                <!-- Loading Overlay -->
                <div class="warehouse-loading-overlay" id="tableLoadingOverlay">
                    <div class="warehouse-loading-content">
                        <div class="text-center">
                            <div class="spinner-border text-primary" role="status">
                                <span class="sr-only">Loading...</span>
                            </div>
                            <div class="mt-2">Loading project data...</div>
                        </div>
                    </div>
                </div>

                <!-- Table -->
                <div class="warehouse-table-wrapper">
                    <table class="table table-hover mb-0 warehouse-table">
                        <thead class="thead-light">
                            <tr>
                                <th style="min-width: 200px;">Project Name</th>
                                <th style="min-width: 120px;">Project Number</th>
                                <th style="min-width: 100px;" class="text-center">Status</th>
                                <th style="min-width: 100px;" class="text-right">Unique Items</th>
                                <!-- <th style="min-width: 120px;" class="text-right">Total Stock Qty</th> -->
                                <th style="min-width: 120px;" class="text-right">Effective Stock</th>
                                <th style="min-width: 120px;" class="text-right">Total Value</th>
                                <th style="min-width: 80px;" class="text-center">Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($projects as $project)
                                <tr>
                                    <td>
                                        <div class="d-flex align-items-center">
                                            <i class="material-icons mr-2" style="color: {{ $project->status === 'active' ? '#28a745' : ($project->status === 'completed' ? '#007bff' : '#6c757d') }}; font-size: 20px;">work</i>
                                            <div>
                                                <div class="font-weight-medium">{{ $project->project_name }}</div>
                                                @if($project->project_number)
                                                    <small class="text-muted">{{ $project->project_number }}</small>
                                                @endif
                                            </div>
                                        </div>
                                    </td>
                                    <td class="warehouse-cell-nowrap">
                                        <span class="badge warehouse-badge warehouse-status-secondary">{{ $project->project_number ?: 'N/A' }}</span>
                                    </td>
                                    <td class="warehouse-cell-status">
                                        @php
                                            $statusColors = [
                                                'active' => 'success',
                                                'completed' => 'info',
                                                'on_hold' => 'warning',
                                                'cancelled' => 'danger'
                                            ];
                                            $statusColor = $statusColors[$project->status] ?? 'secondary';
                                        @endphp
                                        <span class="badge warehouse-badge warehouse-status-{{ $statusColor }}">
                                            {{ ucfirst(str_replace('_', ' ', $project->status)) }}
                                        </span>
                                    </td>
                                    <td class="text-right warehouse-cell-number">
                                        <strong>{{ number_format($project->unique_items) }}</strong>
                                    </td>
                                    <!-- <td class="text-right warehouse-cell-number">
                                        <strong>{{ number_format($project->total_stock, 2) }}</strong>
                                    </td> -->
                                    <td class="text-right warehouse-cell-number">
                                        <strong class="text-{{ ($project->effective_stock ?? 0) > 0 ? 'success' : (($project->effective_stock ?? 0) < 0 ? 'danger' : 'muted') }}">
                                            {{ number_format($project->effective_stock ?? 0, 2) }}
                                        </strong>
                                    </td>
                                    <td class="text-right warehouse-cell-currency">
                                        <strong class="text-success">AED {{ number_format($project->total_value, 2) }}</strong>
                                    </td>
                                    <td class="text-center">
                                        <a href="{{ route('warehouse.reports.stock-on-project.details', $project->id) }}"
                                           class="btn btn-sm btn-outline-primary warehouse-action-btn">
                                            <i class="material-icons">visibility</i>
                                        </a>
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="8" class="warehouse-empty-state">
                                        <i class="material-icons">business_center</i>
                                        <h5>No Projects Found</h5>
                                        <p class="text-muted">No projects match your current filters or have stock available.</p>
                                    </td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        
        @if($projects->hasPages())
            <div class="card-footer">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        Showing {{ $projects->firstItem() }} to {{ $projects->lastItem() }} 
                        of {{ $projects->total() }} projects
                    </div>
                    <div>
                        <nav class="warehouse-pagination">
                            {{ $projects->links('pagination::bootstrap-4') }}
                        </nav>
                    </div>
                </div>
            </div>
        @endif
    </div>
@endsection

@push('styles')
    <link href="{{ asset('admin-assets/css/warehouse-reports.css') }}" rel="stylesheet" />
    <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
    <link href="https://cdn.jsdelivr.net/npm/select2-bootstrap-5-theme@1.3.0/dist/select2-bootstrap-5-theme.min.css" rel="stylesheet" />
    <style>
        .select2-container--bootstrap-5 .select2-selection {
            min-height: 38px;
            border: 1px solid #ced4da;
        }
        .select2-container--bootstrap-5 .select2-selection--single .select2-selection__rendered {
            line-height: 36px;
        }
        .select2-container--bootstrap-5 .select2-selection--single .select2-selection__arrow {
            height: 36px;
        }
        .select2-container {
            width: 100% !important;
        }
    </style>
@endpush

@push('scripts')
    <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
    <script src="{{ asset('admin-assets/js/warehouse-reports.js') }}"></script>
    <script>
        $(document).ready(function() {
            // Initialize Select2 for filter dropdowns
            $('.warehouse-select2').select2({
                theme: 'bootstrap-5',
                allowClear: true,
                width: '100%',
                minimumResultsForSearch: 0,
                placeholder: function() {
                    return $(this).data('placeholder') || 'Select...';
                }
            });
        });
    </script>
@endpush
