@php $isConnected = $integration && $integration->isConnected(); $tabs = [ 'overview' => 'Overview', 'orders' => 'Orders', 'products' => 'Products', 'offers' => 'Offers', 'customers' => 'Customers', 'events' => 'Automations', 'analytics' => 'Analytics', 'logs' => 'Activity', 'settings' => 'Settings', ]; $activeTab = array_key_exists($activeTab ?? 'overview', $tabs) ? $activeTab : 'overview'; $currency = $currency ?? ($integration?->store_currency ?? 'USD'); // Friendly, grouped automation map — each key is a saveEvents event_type. $AUTO = [ 'Order lifecycle' => [ 'order.created' => ['Order placed', 'Sent the moment a new order comes in.'], 'order.processing' => ['Order confirmed', 'Payment received — the order is being prepared.'], 'order.completed' => ['Order delivered', 'Order marked complete in WooCommerce.'], 'order.on-hold' => ['Order on hold', 'Awaiting payment or stock — keep the buyer informed.'], 'order.cancelled' => ['Order cancelled', 'The order was cancelled.'], 'order.refunded' => ['Order refunded', 'A refund was issued.'], ], 'Revenue recovery' => [ 'cod/confirm' => [ 'COD double-confirmation', 'Ask cash-on-delivery buyers to confirm Yes / No before you ship — the #1 way to cut RTO.', ], 'cod/prepaid' => [ 'COD → Prepaid nudge', 'Offer COD buyers a pay-online link (template can carry {{ order_url }} + an incentive).', ], 'checkout.created' => [ 'Abandoned cart · step 1', 'First nudge when checkout starts but no order lands. Needs the ' . brand_name() . ' WooCommerce plugin.', ], 'cart/step2' => ['Abandoned cart · step 2', 'Scheduled follow-up after the delay below.'], 'cart/step3' => ['Abandoned cart · step 3', 'Final reminder — add a discount in the template.'], 'stock/back' => [ 'Back in stock', 'Message everyone on the waitlist when a sold-out product is restocked.', ], 'subscription.payment_failed' => [ 'Subscription dunning', 'When a subscription renewal payment fails, send a one-tap "update payment" link. Needs the ' . brand_name() . ' WooCommerce plugin.', ], ], 'Lifecycle marketing' => [ 'customer.created' => [ 'Welcome new customer', 'Greet a customer the first time they register on the store.', ], 'review/reward' => [ 'Review-gated coupon', 'When a recent buyer replies with a positive rating (4–5 / "great"), auto-send this reward template — put a coupon in it. Pair it with an "Order delivered" template that asks them to rate.', ], ], ]; // Variable names available to order templates (for the advanced var-map hint). $varHint = 'name, first_name, order_number, order_name, total, currency, order_url, tracking_number, tracking_url'; @endphp @if (!$isConnected) {{-- ════════════════════════════════════════════════════════════ NOT CONNECTED — /connect-style sidebar + form. Matches the Shopify connect screen visual pattern. ════════════════════════════════════════════════════════════ --}}
{{ __('Integrations') }} / {{ __('WooCommerce') }}

{{ __('Connect') }} {{ __('WooCommerce') }}

{{ __('Pull WooCommerce orders, refunds, products, and customer events into chat threads. Confirm orders, ship updates, and recover carts on WhatsApp without leaving :app.', ['app' => \App\Models\SystemSetting::get('app_name', config('app.name', 'WaDesk'))]) }}

@if (session('error'))
{{ session('error') }}
@endif @if (!$appEnabled)
{{ __("WooCommerce isn't enabled yet") }}

{{ __('An administrator must enable the WooCommerce integration before any workspace can connect a store.') }}

@else
{{ __('Step 2') }}

{{ __('Store credentials') }}

@csrf

{{ __("HTTPS required for Basic Auth. We'll detect WP REST automatically.") }}

{{ __("Stored encrypted at rest. Only used to call your store's REST API.") }}

{{ __('Webhook URL · auto-configured after connect') }}
{{ url('/woocommerce/webhook/{secret}') }}
@endif
@else {{-- ════════════════════════════════════════════════════════════ CONNECTED — preserve the existing sidebar+main+tabs layout. The visual chrome is identical to the previous static page; only the numbers and tab content are wired to real data now. ════════════════════════════════════════════════════════════ --}} @if (session('success'))
{{ session('success') }}
@endif
{{ __('Integrations') }} / {{ __('WooCommerce') }} / {{ $tabs[$activeTab] }}

{{ $integration->store_name ?: parse_url($integration->store_url, PHP_URL_HOST) }} {{ strtolower($tabs[$activeTab]) }}

@switch($activeTab) @case('overview') Live snapshot pulled from your WooCommerce REST API. @break @case('orders') Last 10 orders fetched from the store. @break @case('products') Most recent products in the catalog. @break @case('offers') Broadcast a product offer or win back lapsed customers. @break @case('customers') Customer accounts ordered by recency. @break @case('events') Turn order, COD, cart & stock automations on or off. @break @case('analytics') Revenue, message volume & COD impact for this store. @break @case('logs') Webhook delivery log for this store. @break @case('settings') Connection details — re-verify or disconnect. @break @endswitch

@switch($activeTab) @case('overview')
{{ __('Revenue · 30d') }}
{{ $currency }} {{ number_format($revenue30d, 2) }}
{{ __('from WC sales report') }}
{{ __('Orders · 30d') }}
{{ number_format($orders30d) }}
{{ number_format($counts['orders'] ?? 0) }} {{ __('total all-time') }}
{{ __('Customers') }}
{{ number_format($counts['customers'] ?? 0) }}
{{ number_format($customers30d) }} new in 30d
{{ __('Items sold · 30d') }}
{{ number_format($itemsSold30d) }}
{{ $activeEvents }} {{ __('active automations') }}

{{ __('Recent orders') }}

{{ __('View all →') }}
@forelse (collect($orders)->take(5) as $o) @php $b = $o['billing'] ?? []; $name = trim(($b['first_name'] ?? '') . ' ' . ($b['last_name'] ?? '')) ?: $b['email'] ?? '—'; @endphp
#{{ $o['number'] ?? ($o['id'] ?? '?') }}
{{ $name }}
{{ $o['currency'] ?? $currency }} {{ number_format((float) ($o['total'] ?? 0), 2) }}
{{ ucfirst((string) ($o['status'] ?? '—')) }}
@empty
{{ __('No orders fetched yet. Hit') }} {{ __('Sync now') }}.
@endforelse

{{ __('Recent webhook activity') }}

{{ __('View all →') }}
@forelse ($recentLogs->take(6) as $log) @php $css = match ($log->status) { 'processed', 'sent' => 'bg-wa-green/15 text-wa-deep', 'failed' => 'bg-accent-coral/15 text-accent-coral', 'skipped' => 'bg-paper-100 text-ink-700', default => 'bg-accent-amber/15 text-[#8B5A14]', }; @endphp
{{ $log->event_type }}
{{ $log->created_at?->diffForHumans() }}
{{ $log->status }}
@empty
{{ __('No webhook events received yet.') }}
@endforelse
@break @case('orders')
@forelse ($orders as $o) @php $b = $o['billing'] ?? []; $name = trim(($b['first_name'] ?? '') . ' ' . ($b['last_name'] ?? '')) ?: $b['email'] ?? '—'; @endphp @empty @endforelse
{{ __('Order') }} {{ __('Customer') }} {{ __('Status') }} {{ __('Total') }} {{ __('Placed') }}
#{{ $o['number'] ?? ($o['id'] ?? '?') }} {{ $name }} {{ ucfirst((string) ($o['status'] ?? '—')) }} {{ $o['currency'] ?? $currency }} {{ number_format((float) ($o['total'] ?? 0), 2) }} {{ \Carbon\Carbon::parse($o['date_created'] ?? null)->diffForHumans() }}
{{ __('No orders found.') }}
@break @case('products')
@forelse ($products as $p) @php $img = $p['images'][0]['src'] ?? null; @endphp
@if ($img) @else
{{ __('no image') }}
@endif
{{ $p['name'] ?? '—' }}
{{ ucfirst((string) ($p['status'] ?? '—')) }} @if (isset($p['price']) && $p['price'] !== '') {{ $currency }} {{ number_format((float) $p['price'], 2) }} @endif
@empty
{{ __('No products found.') }}
@endforelse
@break @case('customers')
@forelse ($customers as $c) @php $b = $c['billing'] ?? []; @endphp @empty @endforelse
{{ __('Name') }} {{ __('Email') }} {{ __('Phone') }} {{ __('Orders') }} {{ __('Joined') }}
{{ trim(($c['first_name'] ?? '') . ' ' . ($c['last_name'] ?? '')) ?: '—' }} {{ $c['email'] ?? '—' }} {{ $b['phone'] ?? '—' }} {{ number_format((int) ($c['orders_count'] ?? 0)) }} {{ \Carbon\Carbon::parse($c['date_created'] ?? null)->diffForHumans() }}
{{ __('No customers yet.') }}
@break @case('events') @if ($templates->isEmpty())
{{ __('You have no approved WhatsApp templates yet. Create one in') }} {{ __('Templates') }} {{ __('to switch an automation on.') }}
@endif
@csrf @foreach ($AUTO as $groupLabel => $cards)
{{ $groupLabel }}
@foreach ($cards as $key => $meta) @php $ev = $eventsByType[$key] ?? null; $active = (bool) $ev?->is_active; $tplId = $ev?->template_id; $sendTo = $ev?->send_to ?? 'customer'; $delay = $ev?->delay_seconds ?? 0; $vmap = is_array($ev?->var_map) ? implode(', ', $ev->var_map) : ''; $sentCount = (int) ($logsByEvent[$key] ?? 0); $isScheduled = in_array( $key, ['cart/step2', 'cart/step3', 'checkout.created'], true, ); @endphp
{{ $meta[0] }}
{{ $meta[1] }}
{{ $key }} · {{ $sentCount }} {{ __('logged') }}
{{ __('Advanced · variable order') }}

{{ __('Comma-separated fields mapped in order to your numbered template placeholders. Leave blank for the smart default: name, order, total.') }}

@endforeach
@endforeach
@break @case('offers')
{{-- Offer broadcast --}}
@csrf

{{ __('Product offer broadcast') }}

{{ __('Send an approved template to every opted-in contact in a segment. Inject a product + coupon.') }}

{{-- Smart-segment win-back --}}
@csrf

{{ __('Smart-segment broadcast') }}

{{ __('Target by order history — win back lapsed buyers or reward your best customers.') }}

{{ __('Days = 0 targets any time (pure segment). Spend is in store currency. Opted-out contacts are always skipped.') }}

@break @case('analytics') @php $A = $analytics ?? []; @endphp
{{ __('Revenue · mirrored') }}
{{ $currency }} {{ number_format((float) ($A['revenue_total'] ?? 0), 2) }}
{{ number_format((int) ($A['orders_total'] ?? 0)) }} {{ __('orders synced') }}
{{ __('Avg order value') }}
{{ $currency }} {{ number_format((float) ($A['aov'] ?? 0), 2) }}
{{ __('per order') }}
{{ __('Messages sent') }}
{{ number_format((int) ($A['messages_sent'] ?? 0)) }}
{{ number_format((int) ($A['offers_sent'] ?? 0)) }} {{ __('offer broadcasts') }}
{{ __('Recovery sends') }}
{{ number_format((int) ($A['recovery_sends'] ?? 0)) }}
{{ __('COD / cart / stock / offers') }}
{{ __('COD revenue protected') }}
{{ $currency }} {{ number_format((float) ($A['cod_protected'] ?? 0), 2) }}
{{ number_format((int) ($A['cod_confirmed'] ?? 0)) }} {{ __('COD orders confirmed') }}
{{ __('RTO avoided') }}
{{ $currency }} {{ number_format((float) ($A['rto_avoided'] ?? 0), 2) }}
{{ number_format((int) ($A['cod_cancelled'] ?? 0)) }} {{ __('would-be RTOs cancelled early') }}
{{ __('Active automations') }}
{{ number_format((int) $activeEvents) }}
{{ __('firing on store events') }}

{{ __('Revenue · last 14 days') }}

@foreach ($A['trend'] ?? [] as $pt) @php $h = (int) round((($pt['value'] ?? 0) / ($A['trend_max'] ?? 1)) * 100); @endphp
{{ $pt['label'] ?? '' }}
@endforeach
@break @case('logs')
@foreach (['processed' => 'Processed', 'sent' => 'Sent', 'skipped' => 'Skipped', 'failed' => 'Failed'] as $k => $label) @php $n = (int) ($logsByStatus[$k] ?? 0); @endphp
{{ $label }}
{{ number_format($n) }}
@endforeach
@forelse ($recentLogs as $log) @php $css = match ($log->status) { 'processed', 'sent' => 'bg-wa-green/15 text-wa-deep', 'failed' => 'bg-accent-coral/15 text-accent-coral', 'skipped' => 'bg-paper-100 text-ink-700', default => 'bg-accent-amber/15 text-[#8B5A14]', }; @endphp @empty @endforelse
{{ __('Event') }} {{ __('Recipient') }} {{ __('Status') }} {{ __('When') }}
{{ $log->event_type }} {{ $log->recipient ?? '—' }} {{ $log->status }} {{ $log->created_at?->diffForHumans() }}
{{ __('No webhook events received yet.') }}
@break @case('settings')
{{ __('Store URL') }}
{{ $integration->store_url }}
{{ __('Connected at') }}
{{ $integration->connected_at?->format('M d, Y H:i') ?? '—' }}
{{ __('Last verified') }}
{{ $integration->last_verified_at?->diffForHumans() ?? '—' }}
{{ __('WooCommerce version') }}
{{ $integration->store_version ?: '—' }}
{{ __('Webhook URL') }}
{{ url('/woocommerce/webhook/' . $integration->webhook_secret) }}
@php $webhookCount = is_array($integration->metadata['webhook_ids'] ?? null) ? count(array_filter($integration->metadata['webhook_ids'])) : 0; @endphp
{{ __('Registered webhooks') }}
{{ $webhookCount }} active subscription{{ $webhookCount === 1 ? '' : 's' }}
{{-- Companion plugin — unlocks the events the REST API can't emit --}}

{{ __(':brand companion plugin', ['brand' => brand_name()]) }} {{ __('optional · unlocks more') }}

{{ __("WooCommerce's REST webhooks can't see a few high-value moments. Install this tiny plugin on your store to unlock:") }}

  • {{ __('Abandoned-cart recovery (captures the phone at checkout)') }}
  • {{ __('Subscription dunning (failed renewal → pay-now link)') }}
{{ __('Download plugin (.zip)') }} {{ __('Your store URL + secret are pre-filled. Upload via Plugins → Add New → Upload.') }}
@break @endswitch
@endif