@extends('layouts.app')
@section('title', $title)
@section('crumbs', '<a href="'.route('reports.index').'">Reports</a> · <b>'.$title.'</b>')
@section('content')
@php
$dateFilters = $filters[0];
$selectFilters = $filters[1];
$colKeys = array_keys($columns);
$filterOpts = function($ff) {
  switch($ff) {
    case 'quarry_id': return \App\Models\Quarry::orderBy('name')->pluck('name','id');
    case 'project_id': return \App\Models\Project::orderBy('code')->pluck('code','id');
    case 'customer_id': return \App\Models\Customer::orderBy('name')->pluck('name','id');
    case 'supplier_id': return \App\Models\Supplier::orderBy('name')->pluck('name','id');
    case 'product_id': return \App\Models\Product::orderBy('name')->pluck('name','id');
    case 'equipment_id': return \App\Models\Equipment::orderBy('code')->pluck('code','id');
    case 'item_id': return \App\Models\StockItem::orderBy('name')->pluck('name','id');
    case 'account_id': return \App\Models\Account::orderBy('code')->get()->pluck('display_name','id');
    case 'bank_id': return \App\Models\BankAccount::orderBy('bank_name')->get()->pluck('display_name','id');
    default: return collect();
  }
};
$cellNum = function($v) { return (float) str_replace([',','GH₵',' '], '', (string) ($v ?? 0)); };
@endphp
<div class="card"><div class="card-h"><h2>{{ $title }}</h2>
<div class="sp no-print">
<a class="btn btn-sm btn-ghost" href="{{ request()->fullUrlWithQuery(['export'=>'csv']) }}">⬇ CSV</a>
<a class="btn btn-sm btn-ghost" href="{{ request()->fullUrlWithQuery(['export'=>'pdf']) }}">📄 PDF</a>
<button class="btn btn-sm btn-ghost" data-print>🖨 Print</button>
</div></div>
<div class="card-b">
@if(count($dateFilters) || count($selectFilters))
<form method="GET" class="toolbar no-print rep-filters" style="margin-bottom:16px">
@if(in_array('from', $dateFilters))<div class="fld"><label>From</label><input type="date" name="from" value="{{ $from }}"></div>@endif
@if(in_array('to', $dateFilters))<div class="fld"><label>To</label><input type="date" name="to" value="{{ $to }}"></div>@endif
@foreach($selectFilters as $ff)
<div class="fld"><label>{{ ucwords(str_replace('_',' ',$ff)) }}</label><select name="{{ $ff }}"><option value="">— All —</option>
@foreach($filterOpts($ff) as $ov=>$ol)<option value="{{ $ov }}" @if($req->get($ff)==$ov) selected @endif>{{ $ol }}</option>@endforeach
</select></div>
@endforeach
<div class="fld"><label>&nbsp;</label><button class="btn btn-sm">Run</button></div>
</form>
@endif
<p class="muted small">{{ $__company->name ?? '' }} · @if(count($dateFilters)) Period {{ $from }} → {{ $to }} · @endif {{ count($rows) }} row(s)</p>
<div class="twrap"><table class="tbl"><thead><tr>
@foreach($columns as $cl => $al)<th class="{{ $al === 'r' ? 'r' : '' }}">{{ $cl }}</th>@endforeach
</tr></thead><tbody>
@forelse($rows as $row)<tr>
@foreach($colKeys as $i => $ck)<td class="{{ ($columns[$ck] ?? '') === 'r' ? 'r' : '' }}">{{ $row[$i] ?? '' }}</td>@endforeach
</tr>
@empty<tr><td colspan="{{ count($columns) }}" class="muted c">No data for the selected filters.</td></tr>@endforelse
</tbody>
@if(count($totals) && count($rows))
<tfoot><tr>
@foreach($colKeys as $i => $ck)
<td class="{{ ($columns[$ck] ?? '') === 'r' ? 'r' : '' }}">@if(in_array($ck, $totals)){{ money(collect($rows)->sum(fn($r) => $cellNum($r[$i] ?? 0))) }}@elseif($i === 0) Total @endif</td>
@endforeach
</tr></tfoot>
@endif
</table></div>
</div></div>
@endsection
