@foreach($data as $policy)
    <table class="table" style="table-layout: fixed; width: 100%;">
        @php
            $collapseId = 'collapse-policy-' . preg_replace('/[^a-zA-Z0-9_]/', '-', $policy['policy']);
            $backColor  = $policy['policy_status'] === 'Approved' ? 'background-color:green;' : 'background-color:red;';
        @endphp
        <tr style="color:blue; cursor: pointer;" onclick="togglePolicyCollapse('{{ $collapseId }}')">
            <td style="width: 15%;">Policy Name</td>
            <td colspan="5" style="position: relative; width: 85%;">
                <span style="margin-right: 5px;" id="icon-{{ $collapseId }}">&#9654;</span>
                <a href="/admin/grade/edit/{{ $policy['grade_id'] }}" target="_blank" onclick="event.stopPropagation();">{{ $policy['policy_name'] }}</a>
                <span style="position: absolute; top: 0; right: 0; display: flex; align-items: center;">
                    @if($policy['policy_status'] === 'Approved' || $policy['eligible_net_loan_amount'] > 0)
                        <span style="margin-right: 10px; color: #333;">Eligible Net Loan Amount: <strong>&#8377;{{ number_format($policy['eligible_net_loan_amount']) }}</strong></span>
                    @endif
                    <span style="color: white;{{ $backColor }}padding: 10px;">{{ $policy['policy_status'] }}</span>
                </span>
            </td>
        </tr>
        <tbody id="{{ $collapseId }}" style="display: none;">
            <tr>
                <th style="width: 15%;">Rule Name</th>
                <th style="width: 20%;">Reason</th>
                <th style="width: 20%;">Against Value</th>
                <th style="width: 15%;">Source Value</th>
                <th style="width: 10%;">Passed</th>
                <th style="width: 20%;">Logs</th>
            </tr>
            @foreach($policy['rules'] as $rule)
                <tr style="{{ $rule['result'] === 'No' ? 'color:red' : '' }}">
                    <td style="width: 15%;">{{ $rule['key'] }}</td>
                    <td style="width: 20%;">{{ $rule['reason'] }}</td>
                    <td style="width: 20%;">{!! $rule['against_condition'] !!}</td>
                    <td style="width: 15%;">{{ $rule['source_value'] }}</td>
                    <td style="width: 10%;">{{ $rule['result'] }}</td>
                    <td style="width: 20%;">{!! $rule['logs'] !!}</td>
                </tr>
            @endforeach
        </tbody>
    </table>
@endforeach
