<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<style>
*{margin:0;padding:0;box-sizing:border-box}
body{font-family:DejaVu Sans,sans-serif;font-size:11px;color:#1E293B;line-height:1.5}
.header{background:#0B1437;color:white;padding:28px 32px;margin-bottom:20px}
.app{font-size:9px;font-weight:bold;letter-spacing:1.5px;opacity:.5;text-transform:uppercase;margin-bottom:6px}
.header h1{font-size:20px;font-weight:bold;margin-bottom:2px}
.header .sub{font-size:10px;opacity:.7}
.header .meta{text-align:right;font-size:10px;opacity:.7}
.row{display:table;width:100%}
.left{display:table-cell;vertical-align:top}
.right{display:table-cell;vertical-align:top;text-align:right}
.zscore-box{margin:0 32px 20px;padding:16px;border-radius:6px;text-align:center}
.zval{font-size:30px;font-weight:bold}
.zbadge{display:inline-block;padding:4px 16px;border-radius:20px;font-size:10px;font-weight:bold;letter-spacing:1px;color:white;margin-top:6px}
.grid{display:table;width:calc(100% - 64px);margin:0 32px 18px;border-collapse:separate;border-spacing:6px}
.grid td{background:#F8FAFF;border-radius:5px;padding:8px;vertical-align:top}
.glabel{font-size:8.5px;color:#64748B;font-weight:bold;text-transform:uppercase;letter-spacing:.5px}
.gval{font-size:11px;font-weight:bold;color:#1E293B;margin-top:2px}
.sec{margin:0 32px 6px}
.sec-title{font-size:8.5px;font-weight:bold;color:#64748B;text-transform:uppercase;letter-spacing:.8px;border-bottom:1px solid #E2E8F0;padding-bottom:5px;margin-bottom:8px}
.comps{display:table;width:calc(100% - 64px);margin:0 32px 6px;border-collapse:separate;border-spacing:5px}
.comps td{background:#F8FAFF;border-radius:5px;padding:7px;text-align:center}
.cn{font-size:9px;font-weight:bold;color:#1A56DB}
.cv{font-size:12px;font-weight:bold;font-family:monospace}
.cw{font-size:8px;color:#94A3B8;margin-top:1px}
.cd{font-size:8px;color:#64748B;margin-top:2px}
.formula{background:#F1F5F9;padding:7px 12px;border-radius:5px;margin:0 32px 18px;font-size:9.5px;color:#475569;text-align:center}
.tables{display:table;width:calc(100% - 64px);margin:0 32px 18px;border-collapse:collapse}
.tcol{display:table-cell;vertical-align:top;width:50%;padding-right:12px}
.tcol:last-child{padding-right:0;padding-left:12px}
.ttitle{font-size:10px;font-weight:bold;color:#1E293B;margin-bottom:5px}
.fin{width:100%;border-collapse:collapse}
.fin td{padding:5px 6px;font-size:9.5px;border-bottom:1px solid #F1F5F9}
.fin .bold td{font-weight:bold;background:#F8FAFF}
.tr{text-align:right}
.cblue{color:#1A56DB}
.cpurple{color:#7C3AED}
.cred{color:#DC2626}
.rekom{margin:0 32px 18px;padding:10px 14px;border-radius:0 6px 6px 0;background:#F8FAFF}
.rekom ul{padding-left:14px;margin-top:4px}
.rekom li{font-size:9.5px;margin-bottom:3px}
.footer{margin:0 32px;padding-top:8px;border-top:1px solid #E2E8F0;font-size:8.5px;color:#94A3B8}
.footer-row{display:table;width:100%}
.fl{display:table-cell}
.fr{display:table-cell;text-align:right}
</style>
</head>
<body>
@php
    $cat = $analysis->z_category;
    $colors = [
        'safe'     => ['bg'=>'#D1FAE5','text'=>'#065F46','badge'=>'#059669','border'=>'#6EE7B7','label'=>'ZONA AMAN',    'rekom_border'=>'#10B981'],
        'grey'     => ['bg'=>'#FEF3C7','text'=>'#92400E','badge'=>'#D97706','border'=>'#FCD34D','label'=>'ZONA WASPADA', 'rekom_border'=>'#F59E0B'],
        'distress' => ['bg'=>'#FEE2E2','text'=>'#991B1B','badge'=>'#DC2626','border'=>'#FCA5A5','label'=>'ZONA BERBAHAYA','rekom_border'=>'#EF4444'],
    ];
    $c   = $colors[$cat] ?? $colors['grey'];
    $cur = $analysis->currency;
    $fd  = $financial;
    // Helper closure — valid di Blade PHP block
    $fmt = function($v) use ($cur) {
        $n = (float)($v ?? 0);
        $sign = $n < 0 ? '-' : '';
        return $cur . ' ' . $sign . number_format(abs($n), 0, ',', '.');
    };
@endphp

{{-- Header --}}
<div class="header">
    <div class="row">
        <div class="left">
            <div class="app">FIN-ANALYSIS PRO</div>
            <div style="font-size:9px;opacity:.55;margin-bottom:5px;letter-spacing:.5px;text-transform:uppercase">Laporan Analisis Keuangan</div>
            <h1>{{ $analysis->company_name }}</h1>
            <div class="sub">{{ $analysis->business_field }}</div>
        </div>
        <div class="right" style="color:white">
            <div style="font-size:10px;font-weight:bold">{{ $analysis->period }}</div>
            <div style="font-size:9px;opacity:.65;margin-top:3px">{{ $analysis->created_at->format('d F Y') }}</div>
            <div style="font-size:9px;opacity:.65;margin-top:2px">{{ $generated_at }}</div>
        </div>
    </div>
</div>

{{-- Z-Score --}}
<div class="zscore-box" style="background:{{ $c['bg'] }};border:1.5px solid {{ $c['border'] }}">
    <div style="font-size:9px;font-weight:bold;color:{{ $c['text'] }};letter-spacing:.8px;text-transform:uppercase;margin-bottom:4px">Altman Z'-Score</div>
    <div class="zval" style="color:{{ $c['text'] }}">{{ number_format($analysis->z_score, 4) }}</div>
    <div class="zbadge" style="background:{{ $c['badge'] }}">{{ $c['label'] }}</div>
</div>

{{-- Info Grid --}}
<table class="grid">
    <tr>
        @foreach([['Perusahaan',$analysis->company_name],['Bidang Usaha',$analysis->business_field],['Periode',$analysis->period],['Mata Uang',$analysis->currency]] as [$l,$v])
        <td><div class="glabel">{{ $l }}</div><div class="gval">{{ $v }}</div></td>
        @endforeach
    </tr>
</table>

{{-- Komponen --}}
<div class="sec"><div class="sec-title">Komponen Z'-Score</div></div>
<table class="comps">
    <tr>
        @foreach([
            ['X1',$zScoreComponents['x1'],'×0.717','Modal Kerja/Total Aset'],
            ['X2',$zScoreComponents['x2'],'×0.847','Laba Ditahan/Total Aset'],
            ['X3',$zScoreComponents['x3'],'×3.107','EBIT/Total Aset'],
            ['X4',$zScoreComponents['x4'],'×0.420','Ekuitas/Total Utang'],
            ['X5',$zScoreComponents['x5'],'×0.998','Pendapatan/Total Aset'],
        ] as [$n,$v,$w,$d])
        <td>
            <div class="cn">{{ $n }}</div>
            <div class="cv">{{ number_format($v,4) }}</div>
            <div class="cw">{{ $w }}</div>
            <div class="cd">{{ $d }}</div>
        </td>
        @endforeach
    </tr>
</table>

<div class="formula">
    Z' = 0.717({{ number_format($zScoreComponents['x1'],4) }}) + 0.847({{ number_format($zScoreComponents['x2'],4) }}) + 3.107({{ number_format($zScoreComponents['x3'],4) }}) + 0.420({{ number_format($zScoreComponents['x4'],4) }}) + 0.998({{ number_format($zScoreComponents['x5'],4) }})
    = <strong style="color:#1A56DB">{{ number_format($analysis->z_score,4) }}</strong>
</div>

{{-- Detail Keuangan --}}
<div class="sec"><div class="sec-title">Detail Laporan Keuangan</div></div>
<table class="tables">
    <tr>
        <td class="tcol">
            <div class="ttitle">Neraca</div>
            <table class="fin">
                @foreach([
                    ['Aset Lancar',$fd['aset_lancar']??0,false,''],
                    ['Aset Tetap',$fd['aset_tetap']??0,false,''],
                    ['Total Aset',$fd['total_aset']??0,true,'cblue'],
                    ['Utang Lancar',$fd['utang_lancar']??0,false,''],
                    ['Utang Jangka Panjang',$fd['utang_jangka_panjang']??0,false,''],
                    ['Total Utang',$fd['total_utang']??0,true,'cblue'],
                    ['Nilai Buku Ekuitas',$fd['nilai_buku_ekuitas']??0,true,($fd['nilai_buku_ekuitas']??0)<0?'cred':'cblue'],
                ] as [$l,$v,$b,$cls])
                <tr class="{{ $b?'bold':'' }}">
                    <td>{{ $l }}</td>
                    <td class="tr {{ $v<0?'cred':$cls }}">
                        {{ $analysis->currency }} {{ number_format(abs($v ?? 0), 0, ',', '.') }}
                    </td>
                </tr>
                @endforeach
            </table>
        </td>
        <td class="tcol">
            <div class="ttitle">Laba Rugi</div>
            <table class="fin">
                @foreach([
                    ['Pendapatan',$fd['pendapatan']??0,false,''],
                    ['Biaya Operasional',$fd['biaya_operasional']??0,false,''],
                    ['Laba Sebelum Pajak',$fd['laba_sebelum_pajak']??0,true,($fd['laba_sebelum_pajak']??0)<0?'cred':'cpurple'],
                    ['Prive',$fd['prive']??0,false,''],
                    ['Laba Ditahan',$fd['laba_ditahan']??0,true,($fd['laba_ditahan']??0)<0?'cred':'cpurple'],
                ] as [$l,$v,$b,$cls])
                <tr class="{{ $b?'bold':'' }}">
                    <td>{{ $l }}</td>
                    <td class="tr {{ $v<0?'cred':$cls }}">
                        {{ $analysis->currency }} {{ number_format(abs($v ?? 0), 0, ',', '.') }}
                    </td>
                </tr>
                @endforeach
            </table>
        </td>
    </tr>
</table>

{{-- Rekomendasi --}}
<div class="sec"><div class="sec-title">Interpretasi & Rekomendasi</div></div>
<div class="rekom" style="border-left:3.5px solid {{ $c['rekom_border'] }}">
    <div style="font-size:10px;font-weight:bold;color:{{ $c['text'] }};margin-bottom:4px">
        @if($cat==='safe') Perusahaan berada di <strong>Zona Aman</strong> — kondisi keuangan sehat.
        @elseif($cat==='grey') Perusahaan berada di <strong>Zona Waspada</strong> — perlu perhatian khusus.
        @else Perusahaan berada di <strong>Zona Berbahaya</strong> — diperlukan tindakan segera. @endif
    </div>
    <ul>
        @foreach($interpretation['recommendations'] as $r)
        <li>{{ $r }}</li>
        @endforeach
    </ul>
</div>

{{-- Kesimpulan --}}
<div class="sec" style="margin-bottom:14px">
    <div class="sec-title">Kesimpulan</div>
    <div style="font-size:10px;color:#1E293B;line-height:1.6">
        {{ $interpretation['description'] }}
        Nilai Z-Score <strong>{{ number_format($analysis->z_score,4) }}</strong> ini mengacu pada model
        Altman Z'-Score (modified) untuk perusahaan non-publik dengan batas Zona Aman Z' &gt; 2.9,
        Zona Waspada 1.23 &lt; Z' &le; 2.9, dan Zona Berbahaya Z' &le; 1.23.
    </div>
</div>

{{-- Tanda Tangan --}}
<div style="margin:0 32px 18px;text-align:right">
    <div style="font-size:9.5px;color:#64748B">Dianalisis oleh,</div>
    <div style="margin-top:32px;border-top:1px solid #1E293B;width:160px;display:inline-block"></div>
    <div style="font-size:10px;font-weight:bold;color:#1E293B">{{ $generated_by }}</div>
    <div style="font-size:9px;color:#64748B">{{ $generated_at }}</div>
</div>

{{-- Footer --}}
<div class="footer">
    <div class="footer-row">
        <div class="fl">FIN-ANALYSIS PRO &mdash; Sistem Analisis Altman Z-Score</div>
        <div class="fr">&copy; {{ date('Y') }} FIN-ANALYSIS PRO. All rights reserved.</div>
    </div>
</div>
</body>
</html>