
@php
    use Illuminate\Support\Facades\Storage;
@endphp

<div class="card customer-card m-0">
    <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">
        <h6 class="mb-0">
            <i class="bi bi-person-badge"></i> بطاقة العميل
        </h6>
        <button type="button" class="btn-close btn-close-white" onclick="$(this).closest('.customer-card').remove()"></button>
    </div>

    <div class="card-body">
        <!-- معلومات العميل الأساسية -->
        <div class="row">
            <div class="col-md-4 text-center mb-3">
                @if($customer->avatar)
                    <img src="{{ Storage::disk("attachments")->url("/users-avatars/{$customer->avatar}") }}"
                         class="rounded-circle img-thumbnail"
                         width="120"
                         height="120"
                         style="object-fit: cover;"
                         alt="{{ $customer->name }}">
                @else
                    <img src="{{ Storage::disk("attachments")->url("/users-avatars/blank.svg") }}"
                         class="rounded-circle"
                         width="120"
                         height="120"
                         style="object-fit: cover;"
                         alt="{{ $customer->name }}">
                @endif
            </div>

            <div class="col-md-8">
                <h5 class="mb-1">{{ $customer->name }}</h5>
                <p class="text-muted mb-2">
                    <small><i class="bi bi-upc-scan"></i> {{ $customer->code }}</small>
                </p>

                <div class="row mt-3">
                    <div class="col-sm-6 mb-2">
                        <small class="text-muted d-block">رقم الجوال</small>
                        <span><i class="bi bi-phone"></i> {{ $customer->phone }}</span>
                    </div>

                    @if($customer->email)
                        <div class="col-sm-6 mb-2">
                            <small class="text-muted d-block">البريد الإلكتروني</small>
                            <span><i class="bi bi-envelope"></i> {{ $customer->email }}</span>
                        </div>
                    @endif

                    <div class="col-sm-6 mb-2">
                        <small class="text-muted d-block">الجنسية</small>
                        <span><i class="bi bi-flag"></i> {{ $customer->nationality->name_ar ?? 'غير محدد' }}</span>
                    </div>

                    <div class="col-sm-6 mb-2">
                        <small class="text-muted d-block">العمر</small>
                        <span><i class="bi bi-calendar"></i> {{ $customer->age }} سنة</span>
                    </div>
                </div>
            </div>
        </div>

        <hr>

        <!-- معلومات الاشتراك -->
        <div class="row">
            <div class="col-12">
                <h6 class="fw-bold">
                    <i class="bi bi-ticket-perforated"></i> الاشتراك الحالي
                </h6>
            </div>

            @if($customer->activeSubscription)
                <div class="col-md-6">
                    <div class="info-item mb-2">
                        <small class="text-muted d-block">الباقة</small>
                        <span class="fw-bold">{{ $customer->activeSubscription->plan->name }}</span>
                    </div>

                    <div class="info-item mb-2">
                        <small class="text-muted d-block">تاريخ البدء</small>
                        <span>{{ $customer->activeSubscription->start_date->format('Y-m-d') }}</span>
                    </div>

                    <div class="info-item mb-2">
                        <small class="text-muted d-block">تاريخ الانتهاء</small>
                        <span class="{{ $customer->activeSubscription->end_date->isPast() ? 'text-danger' : 'text-success' }}">
                            {{ $customer->activeSubscription->end_date->format('Y-m-d') }}
                            @if($customer->activeSubscription->end_date->isPast())
                                <span class="badge bg-danger">منتهي</span>
                            @else
                                <span class="badge bg-success">{{ $customer->activeSubscription->end_date->diffInDays(now()) }} يوم متبقي</span>
                            @endif
                        </span>
                    </div>
                </div>

                <div class="col-md-6">
                    @if($customer->activeSubscription->plan->sessions_count)
                        <div class="info-item mb-2">
                            <small class="text-muted d-block">الجلسات المتبقية</small>
                            <div class="progress" style="height: 20px;">
                                @php
                                    $usedSessions = $customer->activeSubscription->used_sessions ?? 0;
                                    $totalSessions = $customer->activeSubscription->plan->sessions_count;
                                    $remainingSessions = $totalSessions - $usedSessions;
                                    $percentage = ($usedSessions / $totalSessions) * 100;
                                @endphp
                                <div class="progress-bar bg-success"
                                     role="progressbar"
                                     style="width: {{ $percentage }}%"
                                     aria-valuenow="{{ $usedSessions }}"
                                     aria-valuemin="0"
                                     aria-valuemax="{{ $totalSessions }}">
                                    {{ $usedSessions }}/{{ $totalSessions }}
                                </div>
                            </div>
                            <small class="text-muted">{{ $remainingSessions }} جلسة متبقية</small>
                        </div>
                    @endif

                    <div class="info-item mb-2">
                        <small class="text-muted d-block">رصيد العميل</small>
                        <span class="fw-bold {{ $customer->current_balance > 0 ? 'text-success' : 'text-muted' }}">
                            {{ number_format($customer->current_balance, 2) }} ر.س
                        </span>
                    </div>

                    <div class="info-item mb-2">
                        <small class="text-muted d-block">المدرب المساعد</small>
                        <span>{{ $customer->assignedCoach->name ?? 'غير محدد' }}</span>
                    </div>
                </div>
            @else
                <div class="col-12">
                    <div class="alert alert-warning mb-0">
                        <i class="bi bi-exclamation-triangle"></i>
                        لا يوجد اشتراك نشط لهذا العميل
                    </div>
                </div>
            @endif
        </div>

        <!-- آخر 5 زيارات -->
        @if(isset($lastAttendances) && $lastAttendances->count() > 0)
            <hr>
            <div class="row">
                <div class="col-12">
                    <h6 class="fw-bold mb-3">
                        <i class="bi bi-clock-history"></i> آخر الزيارات
                    </h6>

                    <div class="table-responsive">
                        <table class="table table-sm table-hover">
                            <thead>
                            <tr>
                                <th>التاريخ</th>
                                <th>الدخول</th>
                                <th>الخروج</th>
                                <th>المدة</th>
                                <th>الحالة</th>
                            </tr>
                            </thead>
                            <tbody>
                            @foreach($lastAttendances as $attendance)
                                <tr>
                                    <td>{{ $attendance->check_in->format('Y-m-d') }}</td>
                                    <td>{{ $attendance->check_in->format('h:i A') }}</td>
                                    <td>{{ $attendance->check_out ? $attendance->check_out->format('h:i A') : '-' }}</td>
                                    <td>{{ $attendance->duration_formatted }}</td>
                                    <td>
                                    <span class="badge bg-{{ $attendance->status_color }}">
                                        {{ $attendance->status_text }}
                                    </span>
                                    </td>
                                </tr>
                            @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        @endif

        <hr>

        <!-- أزرار الإجراءات -->
        <div class="row mt-2">
            <div class="col-12">
                @if($customer->is_checked_in_today)
                    <div class="alert alert-success mb-2">
                        <i class="bi bi-check-circle-fill"></i>
                        تم تسجيل الدخول اليوم الساعة
                        {{ $customer->todayAttendance->check_in->format('h:i A') }}
                    </div>
                @else
                    <button class="btn btn-success w-100 mb-2" onclick="showQuickCheckIn({{ $customer->id }})">
                        <i class="bi bi-box-arrow-in-right"></i> تسجيل دخول
                    </button>
                @endif

                <div class="btn-group w-100">
                    <a href="{{ route('customers.show', $customer->id) }}" class="btn btn-outline-primary" target="_blank">
                        <i class="bi bi-eye"></i> عرض التفاصيل
                    </a>
                    <a href="{{ route('subscriptions.create', ['customer_id' => $customer->id]) }}" class="btn btn-outline-info">
                        <i class="bi bi-plus-circle"></i> اشتراك جديد
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>
