@extends('layouts.app')
@section('title',$config['title'])
@section('page_title',$config['title'])
@section('content')
<div class="page-head"><div><h1>{{ $config['title'] }}</h1><p>Manage {{ strtolower($config['title']) }} used throughout the system.</p></div>@if(auth()->user()->hasPermission($config['permission'].'.create'))<a class="btn btn-primary" href="{{ route('masters.create',$module) }}">+ Add {{ $config['singular'] }}</a>@endif</div>
<div class="card">
    <div class="toolbar"><form class="search"><input class="control" name="search" value="{{ request('search') }}" placeholder="Search {{ strtolower($config['title']) }}..."><button class="btn btn-secondary">Search</button>@if(request('search'))<a class="btn btn-secondary" href="{{ route('masters.index',$module) }}">Clear</a>@endif</form><span class="muted">{{ $records->total() }} records</span></div>
    <div class="table-wrap"><table class="table"><thead><tr>@foreach($config['columns'] as $label)<th>{{ $label }}</th>@endforeach<th>Actions</th></tr></thead><tbody>
    @forelse($records as $record)<tr>@foreach($config['columns'] as $field=>$label)<td>
        @php($value=$record->{$field})
        @if(array_key_exists($field,$displayOptions)){{ $displayOptions[$field][$value] ?? $value }}
        @elseif(is_bool($value) || in_array($field,['is_active','is_control_account']))<span class="badge {{ $value ? 'badge-green':'badge-red' }}">{{ $value ? 'Yes':'No' }}</span>
        @elseif(str_contains($field,'status'))<span class="badge status-{{ $value }}">{{ ucwords(str_replace('_',' ',$value)) }}</span>
        @elseif($value instanceof \Carbon\CarbonInterface){{ $value->format('d M Y') }}
        @elseif(is_numeric($value) && (str_contains($field,'amount') || in_array($field,['rate','credit_limit','opening_balance','total_amount']))){{ number_format((float)$value,2) }}
        @else{{ \Illuminate\Support\Str::limit((string)$value,70) }}@endif
    </td>@endforeach<td><div class="actions"><a class="btn btn-secondary btn-sm" href="{{ route('masters.show',[$module,$record->id]) }}">View</a>@if(auth()->user()->hasPermission($config['permission'].'.edit_draft'))<a class="btn btn-secondary btn-sm" href="{{ route('masters.edit',[$module,$record->id]) }}">Edit</a>@endif @if(auth()->user()->hasPermission($config['permission'].'.delete_draft'))<form method="post" action="{{ route('masters.destroy',[$module,$record->id]) }}">@csrf @method('delete')<button class="btn btn-danger btn-sm" data-confirm="Delete this record?">Delete</button></form>@endif</div></td></tr>
    @empty<tr><td colspan="{{ count($config['columns'])+1 }}" class="empty">No records found.</td></tr>@endforelse
    </tbody></table></div>
    @if($records->hasPages())<div class="pagination">{{ $records->links() }}</div>@endif
</div>
@endsection
