<div class="kanban-card status-{{ $task->status }}" data-id="{{ $task->id }}" onclick="openTaskModal(this.dataset.id)">
    <div class="d-flex justify-content-between mb-2">
        <span class="badge bg-light text-dark">#{{ $task->item->order->subscription->invoice_number ?? 'N/A' }}</span>
        <small class="text-muted">{{ $task->created_at->format('M d') }}</small>
    </div>
    <h6 class="fw-bold mb-1">{{ $task->title }}</h6>
    <div class="small text-muted mb-2">
        {{ $task->item->order->customer_name ?? 'Unknown Customer' }}
    </div>

    <div class="d-flex align-items-center justify-content-between mt-2 pt-2 border-top">
        <div class="small">
            @php
                $subscription = $task->item->order->subscription;
                $currentService = $service ?? ($subscription ? $subscription->service : null);
            @endphp
            @if($subscription)
                @if($currentService && ($currentService->type === 'monthly_subscription_open' || $currentService->type === 'monthly_subscription_limited' || $currentService->type === 'one_time_service'))
                    @if($subscription->is_expired)
                        <span class="text-danger fw-bold">منتهي</span>
                    @else
                        <span class="text-success fw-bold">نشط</span>
                    @endif
                @else
                    @if(($subscription->price - $subscription->discount - $subscription->payments->sum('amount')) > 0)
                        <span class="text-danger fw-bold">المتبقي: {{ number_format(($subscription->price - $subscription->discount - $subscription->payments->sum('amount')), 0) }}</span>
                    @else
                        <span class="text-success fw-bold">مدفوع</span>
                    @endif
                @endif
            @endif
        </div>
        <div class="text-muted small">
            <span title="Comments">
                <i class="bi bi-chat-dots me-1"></i> {{ $task->comments->count() }}
            </span>
            <span title="Activity" class="ms-2">
                <i class="bi bi-clock-history me-1"></i> {{ $task->activities->count() }}
            </span>
        </div>
    </div>
    
    @php
        $shouldShowWhatsAppButton = false;
        if ($currentService && in_array($currentService->type, ['wordpress_templates', 'domains_emails', 'web_development', 'app_development', 'ads_creation'], true)) {
            $shouldShowWhatsAppButton = in_array($task->status, ['working', 'qa', 'done', 'completed'], true);
        }

        $whatsappUrl = null;
        if ($shouldShowWhatsAppButton && $subscription) {
            $phone = preg_replace('/[^0-9]/', '', $subscription->whatsapp ?? $subscription->phone ?? '');
            if ($phone) {
                if (str_starts_with($phone, '0')) {
                    $phone = '964'.substr($phone, 1);
                } elseif (!str_starts_with($phone, '964')) {
                    $phone = '964'.$phone;
                }
            }

            $stageKey = $task->status === 'done' ? 'done' : $task->status;
            $message = __('whatsapp.wp_templates.message', [
                'invoice' => $subscription->invoice_number ?? '',
                'customer' => $subscription->customer_name ?? '',
                'business' => $subscription->business_name ?? '',
                'task' => $task->title ?? '',
                'stage' => __('status.'.$stageKey),
            ]);

            if ($task->status === 'qa') {
                $total = (($subscription->price ?? 0) - ($subscription->discount ?? 0));
                $paid = $subscription->payments->sum('amount');
                $remaining = max(0, $total - $paid);
                $message .= "\n\n".__('whatsapp.wp_templates.qa_extra', [
                    'total' => number_format($total, 0),
                    'remaining' => number_format($remaining, 0),
                ]);
            }

            if ($phone) {
                $whatsappUrl = 'https://wa.me/'.$phone.'?text='.urlencode($message);
            }
        }
    @endphp
    @if($whatsappUrl)
        <div class="mt-2 whatsapp-container">
            <a href="{{ $whatsappUrl }}"
               target="_blank"
               class="btn btn-sm btn-outline-success w-100 whatsapp-btn"
               onclick="event.stopPropagation();">
                <i class="bi bi-whatsapp me-1"></i> <span class="whatsapp-label">{{ __('whatsapp.send_message') }}</span>
            </a>
        </div>
    @endif

    @php
        $isConsultationsCard = $currentService && in_array($currentService->type, ['monthly_subscription_open', 'monthly_subscription_limited', 'one_time_service'], true);
        $showCampaignManagementButton = $isConsultationsCard && ($consultationsCampaignManagementEnabled ?? true);
        $showOptimizationsButton = $isConsultationsCard;
    @endphp
    @if($showCampaignManagementButton || $showOptimizationsButton)
        <div class="mt-2 campaign-btn-container">
            @if($showCampaignManagementButton)
                <a href="{{ route('operations.campaigns.manage', $subscription->id) }}"
                   class="btn btn-sm btn-primary w-100"
                   onclick="event.stopPropagation();">
                    <i class="bi bi-megaphone me-1"></i> {{ __('campaigns.manage_campaign') }}
                </a>
            @endif
            @if($showOptimizationsButton)
                <button type="button"
                        class="btn btn-sm btn-outline-primary w-100 {{ $showCampaignManagementButton ? 'mt-2' : '' }}"
                        onclick="event.stopPropagation(); openOptimizationsModal(this.closest('.kanban-card')?.dataset?.id);">
                    <i class="bi bi-sliders me-1"></i> ادارة التحسينات
                </button>
            @endif
        </div>
    @endif
</div>
