<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>مشتريات العضو <?= e($member['full_name_ar'] ?? '—') ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/sales" class="btn btn-outline"><i data-lucide="arrow-right" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> العودة للمبيعات</a>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>

<?php
$statusColors = [
    'completed'          => ['bg' => '#ECFDF5', 'color' => '#059669'],
    'partially_refunded' => ['bg' => '#FFF7ED', 'color' => '#D97706'],
    'fully_refunded'     => ['bg' => '#FEE2E2', 'color' => '#DC2626'],
    'voided'             => ['bg' => '#FEE2E2', 'color' => '#DC2626'],
];
$statusLabels = [
    'completed'          => 'مكتملة',
    'partially_refunded' => 'مسترد جزئي',
    'fully_refunded'     => 'مسترد كلي',
    'voided'             => 'ملغاة',
];
?>

<!-- Member Info -->
<?php if ($member): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:20px;display:flex;align-items:center;gap:12px;">
        <div style="width:48px;height:48px;border-radius:50%;background:#0D737720;display:flex;align-items:center;justify-content:center;">
            <i data-lucide="user" style="width:22px;height:22px;color:#0D7377;"></i>
        </div>
        <div>
            <div style="font-weight:700;font-size:16px;color:#1F2937;"><?= e($member['full_name_ar']) ?></div>
            <div style="font-size:13px;color:#6B7280;">سجل المشتريات</div>
        </div>
        <div style="margin-right:auto;padding:6px 16px;background:#F3F4F6;border-radius:8px;">
            <div style="font-size:11px;color:#6B7280;">عدد المبيعات</div>
            <div style="font-weight:700;font-size:18px;color:#0D7377;"><?= count($sales) ?></div>
        </div>
    </div>
</div>
<?php endif; ?>

<!-- Sales List -->
<div class="card">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
        <i data-lucide="receipt" style="width:18px;height:18px;color:#0D7377;"></i>
        <h3 style="margin:0;color:#0D7377;font-size:15px;">المبيعات</h3>
    </div>
    <?php if (!empty($sales)): ?>
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>رقم الفاتورة</th>
                    <th>التاريخ</th>
                    <th>الإجمالي</th>
                    <th>طريقة الدفع</th>
                    <th>الحالة</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($sales as $sale): ?>
                <?php
                $st = $sale['status'] ?? 'completed';
                $stColors = $statusColors[$st] ?? ['bg' => '#F3F4F6', 'color' => '#6B7280'];
                $stLabel = $statusLabels[$st] ?? $st;
                ?>
                <tr>
                    <td style="font-weight:600;">
                        <a href="/sales/<?= (int) $sale['id'] ?>" style="color:#0D7377;text-decoration:none;">
                            <code style="font-size:12px;background:#F3F4F6;padding:2px 8px;border-radius:4px;"><?= e($sale['invoice_number'] ?? '') ?></code>
                        </a>
                    </td>
                    <td style="font-size:13px;"><?= e(substr($sale['sale_date'] ?? '', 0, 10)) ?></td>
                    <td style="font-weight:700;direction:ltr;text-align:left;"><?= money($sale['total_amount'] ?? 0) ?></td>
                    <td style="font-size:13px;"><?= e($sale['payment_method'] ?? '—') ?></td>
                    <td>
                        <span style="display:inline-block;padding:3px 10px;border-radius:10px;font-size:12px;font-weight:600;background:<?= $stColors['bg'] ?>;color:<?= $stColors['color'] ?>;">
                            <?= e($stLabel) ?>
                        </span>
                    </td>
                    <td>
                        <a href="/sales/<?= (int) $sale['id'] ?>" class="btn btn-sm btn-outline" style="font-size:12px;padding:4px 10px;">
                            <i data-lucide="eye" style="width:13px;height:13px;vertical-align:middle;"></i> عرض
                        </a>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php else: ?>
    <div style="padding:60px 20px;text-align:center;color:#9CA3AF;">
        <i data-lucide="shopping-bag" style="width:40px;height:40px;color:#D1D5DB;margin-bottom:12px;"></i>
        <p style="margin:0;font-size:15px;">لا توجد مبيعات لهذا العضو</p>
    </div>
    <?php endif; ?>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    if (typeof lucide !== 'undefined') {
        lucide.createIcons();
    }
});
</script>
<?php $__template->endSection(); ?>
