@extends('layouts.admin', ['title' => 'ملف العميل 360'])

@php
    $fmt = fn ($minor) => number_format(($minor ?? 0) / 100, 2).' EGP';
    $name = $customer->display_name ?: $customer->name;
@endphp

@section('content')
    <section class="ac-section">
        <div class="ac-panel ac-profile-hero">
            <div class="ac-customer-head">
                <div style="display:flex;align-items:center;gap:14px">
                    <span class="ac-avatar" style="width:64px;height:64px;font-size:26px">{{ mb_strtoupper(mb_substr($name, 0, 1)) }}</span>
                    <div>
                        <h2>{{ $name }}</h2>
                        <p>{{ $customer->email }} · {{ $customer->phone ?: 'لا يوجد هاتف' }} · رقم العميل {{ $customer->public_id }}</p>
                    </div>
                </div>
                <div class="ac-actions">
                    <span class="ac-badge {{ $customer->status === 'active' ? 'green' : 'red' }}">{{ $customer->status === 'active' ? 'نشط' : 'موقوف' }}</span>
                    <span class="ac-badge {{ $customer->risk_score >= 70 ? 'red' : ($customer->risk_score >= 35 ? 'orange' : 'green') }}">مخاطر {{ $customer->risk_score }}/100</span>
                </div>
            </div>

            <div class="ac-kpi-grid" style="margin-top:18px">
                <div class="ac-kpi"><span>إجمالي الإنفاق</span><strong>{{ $fmt($customer->total_spent_minor) }}</strong></div>
                <div class="ac-kpi" style="--accent:#16a34a"><span>الطلبات</span><strong>{{ number_format($customer->orders_count) }}</strong></div>
                <div class="ac-kpi" style="--accent:#7c3aed"><span>رصيد المحفظة</span><strong>{{ $fmt($customer->wallet?->balance_minor) }}</strong></div>
                <div class="ac-kpi" style="--accent:#f59e0b"><span>نقاط الولاء</span><strong>{{ number_format($customer->loyalty_points) }}</strong></div>
            </div>
        </div>

        <div class="ac-grid-3">
            <div class="ac-panel">
                <div class="ac-panel-head"><h2>تحكم العميل</h2></div>
                <form method="POST" action="{{ route('admin.customers.update', $customer) }}" class="ac-form-grid">
                    @csrf
                    @method('PATCH')
                    <label>اسم العرض<input name="display_name" value="{{ old('display_name', $customer->display_name) }}"></label>
                    <label>الهاتف<input name="phone" value="{{ old('phone', $customer->phone) }}"></label>
                    <label>الحالة
                        <select name="status">
                            <option value="active" @selected($customer->status === 'active')>نشط</option>
                            <option value="suspended" @selected($customer->status === 'suspended')>موقوف</option>
                        </select>
                    </label>
                    <label>درجة المخاطر<input type="number" min="0" max="100" name="risk_score" value="{{ old('risk_score', $customer->risk_score) }}"></label>
                    <button class="ac-btn" style="grid-column:1/-1">حفظ بيانات العميل</button>
                </form>
            </div>

            <div class="ac-panel">
                <div class="ac-panel-head"><h2>تعديل المحفظة</h2></div>
                <form method="POST" action="{{ route('admin.customers.wallet-adjustment', $customer) }}" class="ac-form-grid">
                    @csrf
                    <label>نوع الحركة
                        <select name="direction">
                            <option value="credit">إضافة رصيد</option>
                            <option value="debit">خصم رصيد</option>
                        </select>
                    </label>
                    <label>المبلغ<input type="number" step="0.01" min="0.01" name="amount" required></label>
                    <label style="grid-column:1/-1">السبب<input name="description" required placeholder="تعويض يدوي، استرداد، تصحيح عملية..."></label>
                    <button class="ac-btn" style="grid-column:1/-1">تطبيق حركة المحفظة</button>
                </form>
            </div>

            <div class="ac-panel">
                <div class="ac-panel-head"><h2>ملاحظة إدارية</h2></div>
                <form method="POST" action="{{ route('admin.customers.notes.store', $customer) }}" class="ac-form-grid">
                    @csrf
                    <label style="grid-column:1/-1">النوع
                        <select name="type">
                            <option value="general">عام</option>
                            <option value="risk">مخاطر</option>
                            <option value="payment">دفع</option>
                            <option value="support">دعم</option>
                            <option value="vip">عميل مميز</option>
                        </select>
                    </label>
                    <label style="grid-column:1/-1">الملاحظة
                        <textarea name="body" required rows="4" placeholder="ملاحظة داخلية لفريق الإدارة..."></textarea>
                    </label>
                    <button class="ac-btn" style="grid-column:1/-1">إضافة ملاحظة</button>
                </form>
            </div>
        </div>

        <div class="ac-grid-2">
            <div class="ac-panel">
                <div class="ac-panel-head"><h2>الطلبات</h2><span class="ac-badge blue">آخر {{ $orders->count() }}</span></div>
                <div class="ac-table-wrap">
                    <table class="ac-table">
                        <thead><tr><th>الطلب</th><th>الإجمالي</th><th>الدفع</th><th>التنفيذ</th><th>التاريخ</th></tr></thead>
                        <tbody>
                            @forelse($orders as $order)
                                <tr>
                                    <td><a href="{{ route('admin.orders.show', $order) }}">{{ $order->order_number }}</a></td>
                                    <td>{{ $fmt($order->total_minor) }}</td>
                                    <td><span class="ac-badge {{ $order->payment_status === 'paid' ? 'green' : 'orange' }}">{{ $order->payment_status }}</span></td>
                                    <td><span class="ac-badge blue">{{ $order->fulfillment_status }}</span></td>
                                    <td>{{ $order->created_at->format('Y-m-d H:i') }}</td>
                                </tr>
                            @empty
                                <tr><td colspan="5"><div class="ac-empty">لا توجد طلبات بعد.</div></td></tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>

            <div class="ac-panel">
                <div class="ac-panel-head"><h2>سجل المحفظة</h2></div>
                <div class="ac-list">
                    @forelse($walletEntries as $entry)
                        <div class="ac-list-row"><span>{{ $entry->description ?: $entry->type }}</span><strong>{{ $fmt($entry->amount_minor) }}</strong><em>{{ $entry->created_at->diffForHumans() }}</em></div>
                    @empty
                        <div class="ac-empty">لا توجد حركات محفظة.</div>
                    @endforelse
                </div>
            </div>
        </div>

        <div class="ac-grid-3">
            <div class="ac-panel">
                <div class="ac-panel-head"><h2>المدفوعات اليدوية</h2></div>
                <div class="ac-list">
                    @forelse($manualPayments as $payment)
                        <div class="ac-list-row"><span>{{ $payment->gateway?->name_ar ?: 'بوابة دفع' }}</span><strong>{{ $fmt($payment->amount_minor) }}</strong><em>{{ $payment->status }}</em></div>
                    @empty
                        <div class="ac-empty">لا توجد مدفوعات يدوية.</div>
                    @endforelse
                </div>
            </div>

            <div class="ac-panel">
                <div class="ac-panel-head"><h2>تذاكر الدعم</h2></div>
                <div class="ac-list">
                    @forelse($tickets as $ticket)
                        <a class="ac-list-row" href="{{ route('admin.support-tickets.show', $ticket) }}"><span>{{ $ticket->subject }}</span><strong>{{ $ticket->status }}</strong><em>{{ $ticket->created_at->diffForHumans() }}</em></a>
                    @empty
                        <div class="ac-empty">لا توجد تذاكر دعم.</div>
                    @endforelse
                </div>
            </div>

            <div class="ac-panel">
                <div class="ac-panel-head"><h2>سلوك العميل</h2></div>
                <div class="ac-list">
                    @forelse($events as $event)
                        <div class="ac-list-row"><span>{{ str_replace('_', ' ', $event->event_name) }}</span><strong>{{ $event->entity_type ?: 'المتجر' }}</strong><em>{{ $event->occurred_at->diffForHumans() }}</em></div>
                    @empty
                        <div class="ac-empty">لا يوجد نشاط متتبع بعد.</div>
                    @endforelse
                </div>
            </div>
        </div>

        <div class="ac-panel">
            <div class="ac-panel-head"><h2>الملاحظات الداخلية</h2></div>
            <div class="ac-list">
                @forelse($notes as $note)
                    <div class="ac-list-row"><span>{{ $note->body }}</span><strong>{{ $note->type }}</strong><em>{{ $note->admin?->name }} · {{ $note->created_at->diffForHumans() }}</em></div>
                @empty
                    <div class="ac-empty">لا توجد ملاحظات داخلية بعد.</div>
                @endforelse
            </div>
        </div>
    </section>
@endsection
