<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>كشف حساب العميل</title>
    <style>
        body { font-family: Arial, sans-serif; direction: rtl; line-height: 1.6; margin: 0; padding: 10px; }
        .header { text-align: center; margin-bottom: 30px; border-bottom: 2px solid #333; padding-bottom: 10px; }
        .header h1 { margin: 0; font-size: 24px; }
        .header p { margin: 5px 0; }
        .info-section { margin-bottom: 20px; }
        .info-section h3 { background: #f0f0f0; padding: 10px; margin: 0; font-size: 14px; }
        .info-row { display: flex; justify-content: space-between; padding: 8px 10px; border-bottom: 1px solid #eee; }
        .info-row .label { font-weight: bold; color: #333; }
        .info-row .value { color: #666; }
        .table { width: 100%; border-collapse: collapse; margin: 20px 0; }
        .table th { background: #333; color: white; padding: 10px; text-align: right; font-weight: bold; font-size: 12px; }
        .table td { padding: 8px 10px; border-bottom: 1px solid #ddd; text-align: right; font-size: 12px; }
        .table tr:nth-child(even) { background: #f9f9f9; }
        .summary { margin: 20px 0; }
        .summary-row { display: flex; justify-content: space-between; padding: 8px; border-bottom: 1px solid #eee; }
        .summary-row.section { background: #f0f0f0; font-weight: bold; }
        .page-break { page-break-after: always; }
    </style>
</head>
<body>
    <div class="header">
        <h1>كشف حساب العميل</h1>
        <p>{{ $statement['customer_name'] ?? '-' }}</p>
    </div>

    <div class="info-section">
        <h3>معلومات العميل</h3>
        <div class="info-row">
            <span class="label">اسم العميل:</span>
            <span class="value">{{ $statement['customer_name'] ?? '-' }}</span>
        </div>
        <div class="info-row">
            <span class="label">رقم العميل:</span>
            <span class="value">{{ $statement['customer_code'] ?? '-' }}</span>
        </div>
        <div class="info-row">
            <span class="label">الفترة:</span>
            <span class="value">من {{ \Carbon\Carbon::parse($fromDate)->format('d-m-Y') }} إلى {{ \Carbon\Carbon::parse($toDate)->format('d-m-Y') }}</span>
        </div>
        <div class="info-row">
            <span class="label">الرصيد الحالي:</span>
            <span class="value" style="font-weight: bold; color: {{ $statement['current_balance'] >= 0 ? 'green' : 'red' }}">
                {{ number_format($statement['current_balance'] ?? 0, 2) }} ريال
            </span>
        </div>
    </div>

    <div class="summary">
        <div class="summary-row section">
            <span class="label">ملخص الفترة</span>
        </div>
        <div class="summary-row">
            <span class="label">إجمالي الفواتير:</span>
            <span>{{ number_format($statement['total_invoiced'] ?? 0, 2) }} ريال</span>
        </div>
        <div class="summary-row">
            <span class="label">المدفوع:</span>
            <span>{{ number_format($statement['total_paid'] ?? 0, 2) }} ريال</span>
        </div>
        <div class="summary-row">
            <span class="label">المعلق:</span>
            <span>{{ number_format($statement['total_pending'] ?? 0, 2) }} ريال</span>
        </div>
        <div class="summary-row">
            <span class="label">معدل التحصيل:</span>
            <span>{{ $statement['collection_rate'] ?? 0 }}%</span>
        </div>
    </div>

    <table class="table">
        <thead>
            <tr>
                <th>التاريخ</th>
                <th>النوع</th>
                <th>الوصف</th>
                <th>المرجع</th>
                <th>المبلغ</th>
                <th>الرصيد</th>
            </tr>
        </thead>
        <tbody>
            @forelse($statement['transactions'] ?? [] as $transaction)
                <tr>
                    <td>{{ \Carbon\Carbon::parse($transaction['date'])->format('d-m-Y') }}</td>
                    <td>{{ $transaction['type'] == 'invoice' ? 'فاتورة' : 'دفعة' }}</td>
                    <td>{{ $transaction['description'] ?? '-' }}</td>
                    <td>{{ $transaction['reference_number'] ?? '-' }}</td>
                    <td style="color: {{ $transaction['type'] == 'invoice' ? 'red' : 'green' }}; font-weight: bold;">
                        {{ $transaction['type'] == 'invoice' ? '+' : '-' }}{{ number_format(abs($transaction['amount']), 2) }}
                    </td>
                    <td style="font-weight: bold; color: {{ $transaction['running_balance'] >= 0 ? 'green' : 'red' }}">
                        {{ number_format($transaction['running_balance'], 2) }}
                    </td>
                </tr>
            @empty
                <tr>
                    <td colspan="6" style="text-align: center;">لا توجد حركات</td>
                </tr>
            @endforelse
        </tbody>
    </table>

    <div style="margin-top: 50px; text-align: center; color: #999; font-size: 12px;">
        <p>تم إنشاء هذا التقرير بواسطة نظام إدارة المشاريع</p>
        <p>{{ date('d-m-Y H:i') }}</p>
    </div>
</body>
</html>
