<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>{{ $member->name }} - একাউন্ট লেজার</title>
    <style>
        body {
            font-family: 'SutonnyMJ', 'Helvetica', sans-serif;
            font-size: 12px;
            line-height: 1.4;
        }
        .header {
            text-align: center;
            margin-bottom: 20px;
            border-bottom: 2px solid #333;
            padding-bottom: 10px;
        }
        .header h2 {
            margin: 0;
            font-size: 18px;
        }
        .header h3 {
            margin: 5px 0;
            font-size: 14px;
        }
        .member-info {
            margin-bottom: 15px;
        }
        .member-info table {
            width: 100%;
        }
        .member-info td {
            padding: 3px;
        }
        .summary-box {
            margin-bottom: 15px;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        .summary-box .row {
            margin-bottom: 5px;
        }
        .table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 15px;
        }
        .table th, .table td {
            border: 1px solid #ddd;
            padding: 5px;
            text-align: left;
        }
        .table th {
            background-color: #f5f5f5;
            font-weight: bold;
        }
        .table tfoot th {
            background-color: #e9ecef;
        }
        .text-right {
            text-align: right;
        }
        .text-center {
            text-align: center;
        }
        .font-weight-bold {
            font-weight: bold;
        }
        .page-break {
            page-break-after: always;
        }
        .footer {
            margin-top: 30px;
            padding-top: 10px;
            border-top: 1px solid #ddd;
            font-size: 10px;
            color: #666;
        }
        .signature {
            margin-top: 30px;
        }
        .signature table {
            width: 100%;
        }
        .signature td {
            padding: 10px;
            text-align: center;
        }
        .badge {
            display: inline-block;
            padding: 2px 5px;
            font-size: 10px;
            border-radius: 3px;
        }
        .badge-success { background-color: #d4edda; color: #155724; }
        .badge-danger { background-color: #f8d7da; color: #721c24; }
        .badge-info { background-color: #d1ecf1; color: #0c5460; }
    </style>
</head>
<body>
    <div class="header">
        <h2>{{ config('app.name', 'সহকারী সমিতি') }}</h2>
        <h3>মেম্বার একাউন্ট লেজার</h3>
        <p>মেম্বার: {{ $member->name }} (আইডি: {{ $member->member_id }})</p>
        <p>সময়কাল: {{ date('d/m/Y', strtotime($startDate)) }} থেকে {{ date('d/m/Y', strtotime($endDate)) }}</p>
    </div>

    <div class="member-info">
        <table>
            <tr>
                <td width="25%"><strong>মেম্বার আইডি:</strong> {{ $member->member_id }}</td>
                <td width="25%"><strong>নাম:</strong> {{ $member->name }}</td>
                <td width="25%"><strong>ফোন:</strong> {{ $member->phone }}</td>
                <td width="25%"><strong>ঠিকানা:</strong> {{ $member->present_address }}</td>
            </tr>
        </table>
    </div>

    <div class="summary-box">
        <div class="row">
            <div class="col" style="float: left; width: 25%;">
                <strong>ওপেনিং ব্যালেন্স:</strong><br>
                {{ number_format($summary['opening_balance'], 2) }} টাকা
            </div>
            <div class="col" style="float: left; width: 25%;">
                <strong>মোট আমানত:</strong><br>
                {{ number_format($summary['total_savings'] + $summary['total_loan_paid'] + $summary['total_other_income'], 2) }} টাকা
            </div>
            <div class="col" style="float: left; width: 25%;">
                <strong>মোট উত্তোলন:</strong><br>
                {{ number_format($summary['total_loan_disbursed'] + $summary['total_other_expense'], 2) }} টাকা
            </div>
            <div class="col" style="float: left; width: 25%;">
                <strong>ক্লোজিং ব্যালেন্স:</strong><br>
                {{ number_format($summary['closing_balance'], 2) }} টাকা
            </div>
        </div>
        <div style="clear: both;"></div>
    </div>

    <table class="table">
        <thead>
            <tr>
                <th width="5%">#</th>
                <th width="10%">তারিখ</th>
                <th width="15%">লেনদেন</th>
                <th width="25%">বিবরণ</th>
                <th width="10%">ভাউচার</th>
                <th width="10%">ডেবিট</th>
                <th width="10%">ক্রেডিট</th>
                <th width="15%">ব্যালেন্স</th>
            </tr>
        </thead>
        <tbody>
            <!-- Opening Balance -->
            <tr>
                <td>0</td>
                <td>{{ date('d/m/Y', strtotime($startDate)) }}</td>
                <td>ওপেনিং ব্যালেন্স</td>
                <td>পূর্ববর্তী ব্যালেন্স</td>
                <td>-</td>
                <td class="text-right">
                    @if($summary['opening_balance'] < 0)
                        {{ number_format(abs($summary['opening_balance']), 2) }}
                    @else
                        0.00
                    @endif
                </td>
                <td class="text-right">
                    @if($summary['opening_balance'] > 0)
                        {{ number_format($summary['opening_balance'], 2) }}
                    @else
                        0.00
                    @endif
                </td>
                <td class="text-right font-weight-bold">
                    {{ number_format($summary['opening_balance'], 2) }}
                </td>
            </tr>
            
            @foreach($transactions as $transaction)
            <tr>
                <td>{{ $loop->iteration }}</td>
                <td>{{ date('d/m/Y', strtotime($transaction['date'])) }}</td>
                <td>
                    <span class="badge badge-{{ $transaction['type'] == 'সঞ্চয়' ? 'success' : ($transaction['type'] == 'ঋণ গ্রহণ' ? 'danger' : 'info') }}">
                        {{ $transaction['type'] }}
                    </span>
                </td>
                <td>{{ $transaction['description'] }}</td>
                <td>{{ $transaction['voucher_no'] }}</td>
                <td class="text-right">
                    @if($transaction['debit'] > 0)
                        {{ number_format($transaction['debit'], 2) }}
                    @else
                        0.00
                    @endif
                </td>
                <td class="text-right">
                    @if($transaction['credit'] > 0)
                        {{ number_format($transaction['credit'], 2) }}
                    @else
                        0.00
                    @endif
                </td>
                <td class="text-right font-weight-bold">
                    {{ number_format($transaction['balance'], 2) }}
                </td>
            </tr>
            @endforeach
        </tbody>
        <tfoot>
            <tr>
                <th colspan="5" class="text-right">মোট:</th>
                <th class="text-right">{{ number_format(collect($transactions)->sum('debit'), 2) }}</th>
                <th class="text-right">{{ number_format(collect($transactions)->sum('credit'), 2) }}</th>
                <th class="text-right font-weight-bold">{{ number_format($summary['closing_balance'], 2) }}</th>
            </tr>
        </tfoot>
    </table>

    <div class="row" style="display: flex;">
        <div style="width: 50%; padding: 10px;">
            <h4>আমানতের বিবরণ</h4>
            <table style="width: 100%;">
                <tr>
                    <td>মাসিক সঞ্চয়:</td>
                    <td class="text-right">{{ number_format($summary['total_savings'], 2) }} টাকা</td>
                </tr>
                <tr>
                    <td>ঋণ কিস্তি:</td>
                    <td class="text-right">{{ number_format($summary['total_loan_paid'], 2) }} টাকা</td>
                </tr>
                <tr>
                    <td>অন্যান্য আয়:</td>
                    <td class="text-right">{{ number_format($summary['total_other_income'], 2) }} টাকা</td>
                </tr>
                <tr style="background-color: #f8f9fa;">
                    <td><strong>মোট আমানত:</strong></td>
                    <td class="text-right">
                        <strong>{{ number_format($summary['total_savings'] + $summary['total_loan_paid'] + $summary['total_other_income'], 2) }} টাকা</strong>
                    </td>
                </tr>
            </table>
        </div>
        <div style="width: 50%; padding: 10px;">
            <h4>উত্তোলনের বিবরণ</h4>
            <table style="width: 100%;">
                <tr>
                    <td>ঋণ গ্রহণ:</td>
                    <td class="text-right">{{ number_format($summary['total_loan_disbursed'], 2) }} টাকা</td>
                </tr>
                <tr>
                    <td>অন্যান্য ব্যয়:</td>
                    <td class="text-right">{{ number_format($summary['total_other_expense'], 2) }} টাকা</td>
                </tr>
                <tr style="background-color: #f8f9fa;">
                    <td><strong>মোট উত্তোলন:</strong></td>
                    <td class="text-right">
                        <strong>{{ number_format($summary['total_loan_disbursed'] + $summary['total_other_expense'], 2) }} টাকা</strong>
                    </td>
                </tr>
            </table>
        </div>
    </div>

    <div class="signature">
        <table>
            <tr>
                <td width="33%">
                    <br><br>
                    ______________________<br>
                    মেম্বারের স্বাক্ষর
                </td>
                <td width="33%">
                    <br><br>
                    ______________________<br>
                    ট্রেজারারের স্বাক্ষর
                </td>
                <td width="33%">
                    <br><br>
                    ______________________<br>
                    সভাপতির স্বাক্ষর
                </td>
            </tr>
        </table>
    </div>

    <div class="footer">
        <div class="text-center">
            রিপোর্ট তৈরি তারিখ: {{ $reportDate }}<br>
            সময়: {{ date('h:i A') }}<br>
            সিস্টেম তৈরি করেছেন: {{ config('app.name', 'সহকারী সমিতি') }}<br>
            পৃষ্ঠা <span class="page-number"></span>
        </div>
    </div>

    <script type="text/php">
        if (isset($pdf)) {
            $text = "পৃষ্ঠা {PAGE_NUM} / {PAGE_COUNT}";
            $size = 8;
            $font = $fontMetrics->getFont("SutonnyMJ");
            $width = $fontMetrics->get_text_width($text, $font, $size) / 2;
            $x = ($pdf->get_width() - $width) / 2;
            $y = $pdf->get_height() - 20;
            $pdf->page_text($x, $y, $text, $font, $size);
        }
    </script>
</body>
</html>