@extends('layouts.app')
@section('title', 'Gross Profit — ' . $job->job_number)
@section('breadcrumb')<a href="{{ route('reports.gross-profit') }}" class="hover:text-white">Gross Profit</a> <i class="fas fa-chevron-right text-xs mx-1 text-slate-600"></i> <span class="text-white">{{ $job->job_number }}</span>@endsection

@section('content')

<div class="flex items-center justify-between mb-6">
    <div>
        <h1 class="mb-1">Gross Profit Report</h1>
        <p>{{ $job->job_number }} · {{ $job->contact?->full_name }} · {{ $job->stageLabel() }}</p>
    </div>
    <div class="flex gap-2">
        <a href="{{ route('reports.gross-profit.pdf', $job) }}" class="btn" style="background:#7c3aed;color:#ede9fe;">
            <i class="fas fa-file-pdf mr-1"></i> Export PDF
        </a>
        <a href="{{ route('reports.gross-profit') }}" class="btn btn-secondary">
            <i class="fas fa-arrow-left mr-1"></i> Back to Report
        </a>
    </div>
</div>

{{-- KPI Summary --}}
<div class="grid grid-cols-2 lg:grid-cols-4 gap-4 mb-6">
    <div class="card p-4 text-center">
        <div class="text-xs text-slate-400 mb-1">Revenue</div>
        <div class="text-2xl font-bold text-white">${{ number_format($revenue) }}</div>
        <div class="text-xs text-slate-500 mt-1">
            Ex. GST
            @if($variantRevenue > 0)
            · <span class="text-indigo-400">incl. ${{ number_format($variantRevenue) }} variants</span>
            @endif
        </div>
    </div>
    <div class="card p-4 text-center">
        <div class="text-xs text-slate-400 mb-1">Total Costs</div>
        <div class="text-2xl font-bold text-orange-400">${{ number_format($totalCost) }}</div>
        <div class="text-xs text-slate-500 mt-1">Mat + Labour + PO</div>
    </div>
    <div class="card p-4 text-center border {{ $grossProfit >= 0 ? 'border-green-700 bg-green-900/10' : 'border-red-700 bg-red-900/10' }}">
        <div class="text-xs text-slate-400 mb-1">Gross Profit</div>
        <div class="text-2xl font-bold {{ $grossProfit >= 0 ? 'text-green-400' : 'text-red-400' }}">${{ number_format($grossProfit) }}</div>
    </div>
    <div class="card p-4 text-center">
        <div class="text-xs text-slate-400 mb-1">Gross Margin</div>
        <div class="text-2xl font-bold {{ $margin >= 30 ? 'text-green-400' : ($margin >= 15 ? 'text-yellow-400' : 'text-red-400') }}">{{ $margin }}%</div>
        <div class="w-full h-2 bg-slate-700 rounded-full overflow-hidden mt-2">
            <div class="h-full {{ $margin >= 30 ? 'bg-green-500' : ($margin >= 15 ? 'bg-yellow-400' : 'bg-red-500') }} rounded-full" style="width:{{ min(100, max(0, $margin)) }}%"></div>
        </div>
    </div>
</div>

{{-- Accepted Variants Revenue (item-wise breakdown) --}}
@if($acceptedVariants->count())
<div class="card overflow-hidden mb-6">
    <div class="p-4 border-b border-slate-700 flex items-center justify-between">
        <h3><i class="fas fa-code-branch text-indigo-400 mr-2"></i>Accepted Variant Profit</h3>
        <span class="text-sm font-semibold text-indigo-400">+${{ number_format($variantRevenue, 2) }}</span>
    </div>
    <table class="w-full text-sm">
        <thead>
            <tr class="text-xs text-slate-400 border-b border-slate-700">
                <th class="px-4 py-2 text-left">Variant</th>
                <th class="px-4 py-2 text-left">Item Description</th>
                <th class="px-4 py-2 text-left">Item Code</th>
                <th class="px-4 py-2 text-center">Unit</th>
                <th class="px-4 py-2 text-right">Qty</th>
                <th class="px-4 py-2 text-right">Retail Price</th>
                <th class="px-4 py-2 text-right">W/Sale Price</th>
                <th class="px-4 py-2 text-right">Retail Total</th>
                <th class="px-4 py-2 text-right">W/Sale Total</th>
                <th class="px-4 py-2 text-right">Profit</th>
            </tr>
        </thead>
        <tbody>
            @foreach($variantItemLines as $line)
            @php $isFirst = $loop->first || $variantItemLines[$loop->index - 1]['variant_id'] !== $line['variant_id']; @endphp
            <tr class="table-row border-b border-slate-800 {{ $loop->even ? 'bg-slate-900/30' : '' }}">
                <td class="px-4 py-2 text-white font-medium">
                    @if($isFirst){{ $line['variant_name'] }}@endif
                </td>
                <td class="px-4 py-2 text-slate-300">{{ $line['description'] }}</td>
                <td class="px-4 py-2 text-slate-400 text-xs font-mono">{{ $line['item_code'] ?? '—' }}</td>
                <td class="px-4 py-2 text-center text-slate-400">{{ $line['unit'] ?? '—' }}</td>
                <td class="px-4 py-2 text-right text-slate-300">{{ number_format($line['qty'], 2) }}</td>
                <td class="px-4 py-2 text-right text-slate-300">${{ number_format($line['retail_price'], 2) }}</td>
                <td class="px-4 py-2 text-right text-slate-500">${{ number_format($line['wsale_price'], 2) }}</td>
                <td class="px-4 py-2 text-right text-indigo-400 font-medium">${{ number_format($line['retail_total'], 2) }}</td>
                <td class="px-4 py-2 text-right text-indigo-400 font-medium">${{ number_format($line['wsale_total'], 2) }}</td>
                <td class="px-4 py-2 text-right {{ $line['profit'] >= 0 ? 'text-green-400' : 'text-red-400' }} font-medium">
                    {{ $line['profit'] >= 0 ? '+' : '' }}${{ number_format($line['profit'], 2) }}
                </td>
            </tr>
            @endforeach
        </tbody>
        <tfoot>
            <tr class="font-semibold border-t-2 border-slate-600">
                <td></td>
                <td colspan="7" class="px-4 py-2 text-right text-slate-300">Total Variant Profit</td>
                <td class="px-4 py-2 text-right text-indigo-400">+${{ number_format($variantRevenue, 2) }}</td>
                <td class="px-4 py-2 text-right {{ $variantProfit >= 0 ? 'text-green-400' : 'text-red-400' }}">
                    {{ $variantProfit >= 0 ? '+' : '' }}${{ number_format($variantProfit, 2) }}
                </td>
            </tr>
        </tfoot>
    </table>
</div>
@endif

{{-- Quote Materials --}}
@if($allItems->count())
<div class="card overflow-hidden mb-6">
    <div class="p-4 border-b border-slate-700 flex items-center justify-between">
        <h3><i class="fas fa-boxes text-orange-400 mr-2"></i>Quote Materials</h3>
        <span class="text-sm font-semibold text-orange-400">-${{ number_format($matCost, 2) }}</span>
    </div>
    <table class="w-full text-sm">
        <thead>
            <tr class="text-xs text-slate-400 border-b border-slate-700">
                <th class="px-4 py-2 text-left">Description</th>
                <th class="px-4 py-2 text-left">Section</th>
                <th class="px-4 py-2 text-right">Qty</th>
                <th class="px-4 py-2 text-right">Retail Total</th>
                <th class="px-4 py-2 text-right">W/Sale Cost</th>
            </tr>
        </thead>
        <tbody>
            @foreach($allItems as $item)
            <tr class="table-row border-b border-slate-800">
                <td class="px-4 py-2 text-white">{{ $item->description }}</td>
                <td class="px-4 py-2 text-slate-400">{{ $item->section?->name ?? '—' }}</td>
                <td class="px-4 py-2 text-right text-slate-400">{{ $item->qty ?? 1 }}</td>
                <td class="px-4 py-2 text-right text-slate-300">${{ number_format($item->total_retail ?? $item->total ?? 0, 2) }}</td>
                <td class="px-4 py-2 text-right text-orange-400">${{ number_format($item->total_wsale ?? 0, 2) }}</td>
            </tr>
            @endforeach
        </tbody>
        <tfoot>
            <tr class="font-semibold border-t-2 border-slate-600">
                <td colspan="4" class="px-4 py-2 text-right text-slate-300">Total Material Cost</td>
                <td class="px-4 py-2 text-right text-orange-400">-${{ number_format($matCost, 2) }}</td>
            </tr>
        </tfoot>
    </table>
</div>
@endif

{{-- Labour --}}
@if($labourLines->count())
<div class="card overflow-hidden mb-6">
    <div class="p-4 border-b border-slate-700 flex items-center justify-between">
        <h3><i class="fas fa-user-clock text-blue-400 mr-2"></i>Labour Costs</h3>
        <span class="text-sm font-semibold text-blue-400">-${{ number_format($labourCost, 2) }}</span>
    </div>
    <table class="w-full text-sm">
        <thead>
            <tr class="text-xs text-slate-400 border-b border-slate-700">
                <th class="px-4 py-2 text-left">Staff</th>
                <th class="px-4 py-2 text-left">Role</th>
                <th class="px-4 py-2 text-left">Date</th>
                <th class="px-4 py-2 text-right">Hours</th>
                <th class="px-4 py-2 text-right">Rate</th>
                <th class="px-4 py-2 text-right">Cost</th>
            </tr>
        </thead>
        <tbody>
            @foreach($labourLines as $line)
            <tr class="table-row border-b border-slate-800">
                <td class="px-4 py-2 text-white">{{ $line['staff'] }}</td>
                <td class="px-4 py-2 text-slate-400">{{ $line['role'] }}</td>
                <td class="px-4 py-2 text-slate-400">{{ $line['date'] }}</td>
                <td class="px-4 py-2 text-right text-slate-300">{{ $line['hours'] > 0 ? $line['hours'].'h' : '—' }}</td>
                <td class="px-4 py-2 text-right text-slate-400">{{ $line['rate'] > 0 ? '$'.$line['rate'].'/hr' : '—' }}</td>
                <td class="px-4 py-2 text-right text-blue-400 font-medium">${{ number_format($line['cost'], 2) }}</td>
            </tr>
            @endforeach
        </tbody>
        <tfoot>
            <tr class="font-semibold border-t-2 border-slate-600">
                <td colspan="5" class="px-4 py-2 text-right text-slate-300">Total Labour Cost</td>
                <td class="px-4 py-2 text-right text-blue-400">-${{ number_format($labourCost, 2) }}</td>
            </tr>
        </tfoot>
    </table>
</div>
@endif

{{-- Invoice Deductions --}}
@if($invoiceLines->count())
<div class="card overflow-hidden mb-6">
    <div class="p-4 border-b border-slate-700 flex items-center justify-between">
        <h3><i class="fas fa-file-invoice text-amber-400 mr-2"></i>Invoice Deductions <span class="text-xs text-slate-400 font-normal">(reduces gross profit)</span></h3>
        <span class="text-sm font-semibold text-amber-400">-${{ number_format($invoiceDeductions, 2) }}</span>
    </div>
    <table class="w-full text-sm">
        <thead>
            <tr class="text-xs text-slate-400 border-b border-slate-700">
                <th class="px-4 py-2 text-left">Invoice #</th>
                <th class="px-4 py-2 text-left">Date</th>
                <th class="px-4 py-2 text-left">Note</th>
                <th class="px-4 py-2 text-left">Added By</th>
                <th class="px-4 py-2 text-right">Amount (incl GST)</th>
            </tr>
        </thead>
        <tbody>
            @foreach($invoiceLines as $line)
            <tr class="table-row border-b border-slate-800">
                <td class="px-4 py-2 font-mono text-white">{{ $line['invoice_number'] }}</td>
                <td class="px-4 py-2 text-slate-400">{{ $line['invoice_date'] }}</td>
                <td class="px-4 py-2 text-slate-400">{{ $line['note'] ?: '—' }}</td>
                <td class="px-4 py-2 text-slate-400">{{ $line['added_by'] }}</td>
                <td class="px-4 py-2 text-right text-amber-400 font-medium">-${{ number_format($line['amount'], 2) }}</td>
            </tr>
            @endforeach
        </tbody>
        <tfoot>
            <tr class="font-semibold border-t-2 border-slate-600">
                <td colspan="4" class="px-4 py-2 text-right text-slate-300">Total Invoice Deductions</td>
                <td class="px-4 py-2 text-right text-amber-400">-${{ number_format($invoiceDeductions, 2) }}</td>
            </tr>
        </tfoot>
    </table>
</div>
@endif

{{-- PO Costs --}}
@if($poLines->count())
<div class="card overflow-hidden mb-6">
    <div class="p-4 border-b border-slate-700 flex items-center justify-between">
        <h3><i class="fas fa-shopping-cart text-purple-400 mr-2"></i>Inventory &amp; PO Costs</h3>
        <span class="text-sm font-semibold text-purple-400">-${{ number_format($poCost, 2) }}</span>
    </div>
    <table class="w-full text-sm">
        <thead>
            <tr class="text-xs text-slate-400 border-b border-slate-700">
                <th class="px-4 py-2 text-left">Description</th>
                <th class="px-4 py-2 text-left">Source</th>
                <th class="px-4 py-2 text-right">Qty</th>
                <th class="px-4 py-2 text-right">Unit Cost</th>
                <th class="px-4 py-2 text-right">Total</th>
            </tr>
        </thead>
        <tbody>
            @foreach($poLines as $line)
            <tr class="table-row border-b border-slate-800">
                <td class="px-4 py-2 text-white">{{ $line['description'] }}</td>
                <td class="px-4 py-2 font-mono text-blue-300 text-xs">{{ $line['source'] ?? '—' }}</td>
                <td class="px-4 py-2 text-right text-slate-400">{{ number_format($line['qty'], 2) }}</td>
                <td class="px-4 py-2 text-right text-slate-400">${{ number_format($line['unit_cost'], 2) }}</td>
                <td class="px-4 py-2 text-right text-purple-400 font-medium">${{ number_format($line['total'], 2) }}</td>
            </tr>
            @endforeach
        </tbody>
        <tfoot>
            <tr class="font-semibold border-t-2 border-slate-600">
                <td colspan="4" class="px-4 py-2 text-right text-slate-300">Total PO Costs</td>
                <td class="px-4 py-2 text-right text-purple-400">-${{ number_format($poCost, 2) }}</td>
            </tr>
        </tfoot>
    </table>
</div>
@endif

{{-- Works Order Fault Deductions --}}
@if($faultLines->count())
<div class="card overflow-hidden mb-6">
    <div class="p-4 border-b border-slate-700 flex items-center justify-between">
        <h3><i class="fas fa-exclamation-triangle text-orange-400 mr-2"></i>Works Order Fault Costs <span class="text-xs text-slate-400 font-normal">(reduces gross profit)</span></h3>
        <span class="text-sm font-semibold text-orange-400">-${{ number_format($faultDeductions, 2) }}</span>
    </div>
    <table class="w-full text-sm">
        <thead>
            <tr class="text-xs text-slate-400 border-b border-slate-700">
                <th class="px-4 py-2 text-left">Works Order</th>
                <th class="px-4 py-2 text-left">#</th>
                <th class="px-4 py-2 text-left">Fault Type</th>
                <th class="px-4 py-2 text-left">Approved</th>
                <th class="px-4 py-2 text-right">Cost</th>
            </tr>
        </thead>
        <tbody>
            @foreach($faultLines as $line)
            <tr class="table-row border-b border-slate-800">
                <td class="px-4 py-2 text-blue-300 font-mono">{{ $line['wo_number'] }}</td>
                <td class="px-4 py-2 text-slate-400">{{ $line['fault_num'] }}</td>
                <td class="px-4 py-2 text-white">{{ $line['fault_type'] }}</td>
                <td class="px-4 py-2">
                    @if($line['approved'] === 'approved')
                    <span class="text-green-400 text-xs"><i class="fas fa-check"></i> Approved</span>
                    @else
                    <span class="text-slate-500 text-xs">—</span>
                    @endif
                </td>
                <td class="px-4 py-2 text-right text-orange-400 font-medium">-${{ number_format($line['cost'], 2) }}</td>
            </tr>
            @endforeach
        </tbody>
        <tfoot>
            <tr class="font-semibold border-t-2 border-slate-600">
                <td colspan="4" class="px-4 py-2 text-right text-slate-300">Total Fault Deductions</td>
                <td class="px-4 py-2 text-right text-orange-400">-${{ number_format($faultDeductions, 2) }}</td>
            </tr>
        </tfoot>
    </table>
</div>
@endif

{{-- Summary footer --}}
<div class="card p-5">
    <h3 class="mb-4">Summary</h3>
    <div class="max-w-xs ml-auto space-y-2">
        <div class="flex justify-between text-sm"><span class="text-slate-400">Quote Revenue (ex. GST)</span><span class="text-white">${{ number_format($quoteRevenue, 2) }}</span></div>
        @if($variantRevenue > 0)
        <div class="flex justify-between text-sm"><span class="text-slate-400">Accepted Variant Profit</span><span class="text-indigo-400">+${{ number_format($variantRevenue, 2) }}</span></div>
        <!-- <div class="flex justify-between text-xs text-slate-500 pl-4"><span>Variant W/Sale Cost</span><span>-${{ number_format($variantWsale, 2) }}</span></div> -->
        <!-- <div class="flex justify-between text-xs text-slate-500 pl-4"><span>Variant Net Profit</span><span class="{{ $variantProfit >= 0 ? 'text-green-400' : 'text-red-400' }}">{{ $variantProfit >= 0 ? '+' : '' }}${{ number_format($variantProfit, 2) }}</span></div> -->
        @endif
        <div class="flex justify-between text-sm"><span class="text-slate-400">Material Cost</span><span class="text-orange-400">-${{ number_format($matCost, 2) }}</span></div>
        <div class="flex justify-between text-sm"><span class="text-slate-400">Labour Cost</span><span class="text-blue-400">-${{ number_format($labourCost, 2) }}</span></div>
        @if($poCost > 0)
        <div class="flex justify-between text-sm"><span class="text-slate-400">Inventory & PO Costs</span><span class="text-purple-400">-${{ number_format($poCost, 2) }}</span></div>
        @endif
        @if($invoiceDeductions > 0)
        <div class="flex justify-between text-sm"><span class="text-slate-400">Invoice Deductions</span><span class="text-amber-400">-${{ number_format($invoiceDeductions, 2) }}</span></div>
        @endif
        @if($faultDeductions > 0)
        <div class="flex justify-between text-sm"><span class="text-slate-400">WO Fault Costs</span><span class="text-orange-400">-${{ number_format($faultDeductions, 2) }}</span></div>
        @endif
        <div class="flex justify-between text-base font-bold border-t border-slate-700 pt-2">
            <span class="{{ $grossProfit >= 0 ? 'text-green-400' : 'text-red-400' }}">Gross Profit</span>
            <span class="{{ $grossProfit >= 0 ? 'text-green-400' : 'text-red-400' }}">${{ number_format($grossProfit, 2) }}</span>
        </div>
        <div class="flex justify-between text-sm">
            <span class="text-slate-400">Gross Margin</span>
            <span class="{{ $margin >= 30 ? 'text-green-400' : ($margin >= 15 ? 'text-yellow-400' : 'text-red-400') }}">{{ $margin }}%</span>
        </div>
    </div>
</div>
@endsection
