<?php $__env->startSection('title', 'User Details'); ?>
<?php $__env->startSection('page-title', 'User Details: ' . $user->name); ?>

<?php $__env->startSection('content'); ?>

<?php
$canViewCalls = auth()->guard('admin')->user()->hasPermission('logs.call.view');
$canViewSms = auth()->guard('admin')->user()->hasPermission('logs.sms.view');
$canViewPromos = auth()->guard('admin')->user()->hasPermission('logs.promotion.view');
$hasLogsPermission = $canViewCalls || $canViewSms;

$canViewWhatsApp = $canViewSms; // WhatsApp visibility follows SMS permission
$visibleStatsCount = ($canViewCalls ? 1 : 0) + ($canViewSms ? 1 : 0) + ($canViewWhatsApp ? 1 : 0) + ($canViewPromos ? 1 : 0);
$colSize = $visibleStatsCount > 0 ? max(3, floor(12 / $visibleStatsCount)) : 12;
$settings = $user->settings;
?>

<!-- Action Bar -->
<div class="d-flex justify-content-between align-items-center mb-4">
    <a href="<?php echo e(route('dashboard.users')); ?>" class="btn btn-outline-secondary">
        <i class="bi bi-arrow-left"></i> Back to Users
    </a>
    <div class="btn-group">
        <?php if(auth()->guard('admin')->user()->hasPermission('users.edit')): ?>
        <a href="<?php echo e(route('dashboard.users.edit', $user->id)); ?>" class="btn btn-primary">
            <i class="bi bi-pencil"></i> Edit User
        </a>
        <?php endif; ?>

        <?php if(auth()->guard('admin')->user()->hasPermission('users.toggle_status')): ?>
        <form action="<?php echo e(route('dashboard.users.toggle-status', $user->id)); ?>" method="POST" class="d-inline">
            <?php echo csrf_field(); ?>
            <button type="submit" class="btn btn-<?php echo e($user->is_active ? 'warning' : 'success'); ?>">
                <i class="bi bi-<?php echo e($user->is_active ? 'pause' : 'play'); ?>"></i>
                <?php echo e($user->is_active ? 'Deactivate' : 'Activate'); ?>

            </button>
        </form>
        <?php endif; ?>

        <?php if(auth()->guard('admin')->user()->hasPermission('users.toggle_sms')): ?>
        <form action="<?php echo e(route('dashboard.users.toggle-sms', $user->id)); ?>" method="POST" class="d-inline">
            <?php echo csrf_field(); ?>
            <button type="submit" class="btn btn-outline-<?php echo e($user->sms_service_enabled ? 'secondary' : 'primary'); ?>" title="<?php echo e($user->sms_service_enabled ? 'Disable SMS' : 'Enable SMS'); ?>">
                <i class="bi bi-chat-dots"></i> <?php echo e($user->sms_service_enabled ? 'SMS On' : 'SMS Off'); ?>

            </button>
        </form>
        <form action="<?php echo e(route('dashboard.users.toggle-whatsapp', $user->id)); ?>" method="POST" class="d-inline">
            <?php echo csrf_field(); ?>
            <button type="submit" class="btn btn-outline-<?php echo e(($user->settings && $user->settings->whatsapp_enabled) ? 'secondary' : 'success'); ?>" title="<?php echo e(($user->settings && $user->settings->whatsapp_enabled) ? 'Disable WhatsApp' : 'Enable WhatsApp'); ?>">
                <i class="bi bi-whatsapp"></i> <?php echo e(($user->settings && $user->settings->whatsapp_enabled) ? 'WA On' : 'WA Off'); ?>

            </button>
        </form>
        <?php endif; ?>

        <?php if(auth()->guard('admin')->user()->hasPermission('users.force_logout')): ?>
        <form action="<?php echo e(route('dashboard.users.force-logout', $user->id)); ?>" method="POST" class="d-inline">
            <?php echo csrf_field(); ?>
            <button type="submit" class="btn btn-outline-danger"
                data-confirm="Force logout this user from their device?"
                data-confirm-title="Force Logout"
                data-confirm-btn="Force Logout"
                data-confirm-class="btn-warning">
                <i class="bi bi-box-arrow-right"></i> Force Logout
            </button>
        </form>
        <?php endif; ?>
    </div>
</div>

<?php if(session('success')): ?>
<div class="alert alert-success alert-dismissible fade show d-none" role="alert">
    <?php echo e(session('success')); ?>

    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>

<div class="row g-4">
    <!-- Account Info Section (Always on Left 4 cols) -->
    <div class="col-lg-4">
        <div class="table-container mb-4">
            <div class="d-flex justify-content-between align-items-center mb-3">
                <h5 class="mb-0">
                    <i class="bi bi-person-circle me-2"></i>
                    User Information
                </h5>
                <?php if($user->is_active): ?>
                <span class="badge bg-success">Active</span>
                <?php else: ?>
                <span class="badge bg-danger">Inactive</span>
                <?php endif; ?>
            </div>

            <table class="table table-borderless">
                <tr>
                    <th width="40%">ID</th>
                    <td><?php echo e($user->id); ?></td>
                </tr>
                <tr>
                    <th>Name</th>
                    <td><?php echo e($user->name); ?></td>
                </tr>
                <tr>
                    <th>Email</th>
                    <td><?php echo e($user->email); ?></td>
                </tr>
                <tr>
                    <th>Phone</th>
                    <td><?php echo e($user->phone); ?></td>
                </tr>
                <tr>
                    <th>SMS Service</th>
                    <td>
                        <?php if($user->sms_service_enabled): ?>
                        <span class="badge bg-success">Enabled</span>
                        <?php else: ?>
                        <span class="badge bg-secondary">Disabled</span>
                        <?php endif; ?>
                    </td>
                </tr>
                <tr>
                    <th>WhatsApp</th>
                    <td>
                        <?php if($settings && $settings->whatsapp_enabled): ?>
                        <span class="badge bg-success"><i class="bi bi-whatsapp"></i> Enabled</span>
                        <?php else: ?>
                        <span class="badge bg-secondary">Disabled</span>
                        <?php endif; ?>
                    </td>
                </tr>
                <tr>
                    <th>Channel</th>
                    <td>
                        <?php
                        $channel = $settings->preferred_channel ?? 'sms';
                        $channelLabel = match($channel) {
                        'whatsapp' => 'WhatsApp Only',
                        'both' => 'Both (SMS + WA)',
                        default => 'SMS Only',
                        };
                        $channelBadge = match($channel) {
                        'whatsapp' => 'bg-success',
                        'both' => 'bg-info',
                        default => 'bg-primary',
                        };
                        ?>
                        <span class="badge <?php echo e($channelBadge); ?>"><?php echo e($channelLabel); ?></span>
                    </td>
                </tr>
                <tr>
                    <th>Promotions</th>
                    <td>
                        <?php if($user->promotions_enabled): ?>
                        <span class="badge bg-success">Enabled</span>
                        <?php else: ?>
                        <span class="badge bg-secondary">Disabled</span>
                        <?php endif; ?>
                    </td>
                </tr>
                <tr>
                    <th>Last Active</th>
                    <td><?php echo e($user->last_active_at?->diffForHumans() ?? 'Never'); ?></td>
                </tr>
                <tr>
                    <th>Registered</th>
                    <td><?php echo e($user->created_at->format('M d, Y H:i')); ?></td>
                </tr>
            </table>

            <?php if($visibleStatsCount > 0): ?>
            <hr>
            <h6 class="mb-3">Statistics</h6>
            <div class="row text-center">
                <?php if($canViewCalls): ?>
                <div class="col-<?php echo e($colSize); ?>">
                    <h4 class="text-primary"><?php echo e($stats['total_calls'] ?? 0); ?></h4>
                    <small class="text-muted">Calls</small>
                </div>
                <?php endif; ?>
                <?php if($canViewSms): ?>
                <div class="col-<?php echo e($colSize); ?>">
                    <h4 class="text-success"><?php echo e($stats['total_sms'] ?? 0); ?></h4>
                    <small class="text-muted">SMS</small>
                </div>
                <?php endif; ?>
                <?php if($canViewWhatsApp): ?>
                <div class="col-<?php echo e($colSize); ?>">
                    <h4 class="text-success"><i class="bi bi-whatsapp"></i> <?php echo e($stats['total_whatsapp'] ?? 0); ?></h4>
                    <small class="text-muted">WhatsApp</small>
                </div>
                <?php endif; ?>
                <?php if($canViewPromos): ?>
                <div class="col-<?php echo e($colSize); ?>">
                    <h4 class="text-warning"><?php echo e($stats['total_promotions'] ?? 0); ?></h4>
                    <small class="text-muted">Promos</small>
                </div>
                <?php endif; ?>
            </div>
            <?php endif; ?>
        </div>

        <?php if($hasLogsPermission): ?>
        
        <?php echo $__env->make('dashboard.partials.user-settings-block', ['settings' => $settings], \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
        <?php echo $__env->make('dashboard.partials.user-templates-block', ['templates' => $user->messageTemplates, 'user_id' => $user->id], \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
        <?php endif; ?>
    </div>

    <!-- Main Content Section (Right 8 cols) -->
    <div class="col-lg-8">
        <?php if($hasLogsPermission): ?>
        <div class="table-container">
            <ul class="nav nav-tabs mb-3" id="activityTabs">
                <?php if($canViewCalls): ?>
                <li class="nav-item">
                    <a class="nav-link active" data-bs-toggle="tab" href="#callLogs">
                        <i class="bi bi-telephone me-1"></i>
                        Call Logs (<?php echo e(method_exists($callLogs, 'total') ? $callLogs->total() : $callLogs->count()); ?>)
                    </a>
                </li>
                <?php endif; ?>
                <?php if($canViewSms): ?>
                <li class="nav-item">
                    <a class="nav-link <?php echo e(!$canViewCalls ? 'active' : ''); ?>" data-bs-toggle="tab" href="#smsLogs">
                        <i class="bi bi-chat-text me-1"></i>
                        SMS / WhatsApp Logs (<?php echo e(method_exists($smsLogs, 'total') ? $smsLogs->total() : $smsLogs->count()); ?>)
                    </a>
                </li>
                <?php endif; ?>
                <?php if($canViewPromos): ?>
                <li class="nav-item">
                    <a class="nav-link <?php echo e(!$canViewCalls && !$canViewSms ? 'active' : ''); ?>" data-bs-toggle="tab" href="#promoLogs">
                        <i class="bi bi-megaphone me-1"></i>
                        Promotions (<?php echo e(method_exists($promotionLogs, 'total') ? $promotionLogs->total() : $promotionLogs->count()); ?>)
                    </a>
                </li>
                <?php endif; ?>
            </ul>

            <div class="tab-content">
                <?php if($canViewCalls): ?>
                <div class="tab-pane fade show active" id="callLogs">
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>Phone Number</th>
                                    <th>Type</th>
                                    <th>SMS Sent</th>
                                    <th>Time</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $__empty_1 = true; $__currentLoopData = $callLogs; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $call): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                <tr>
                                    <td><?php echo e($call->phone_number); ?></td>
                                    <td>
                                        <span class="badge bg-<?php echo e($call->call_type == 'incoming' ? 'success' : ($call->call_type == 'outgoing' ? 'primary' : 'danger')); ?>">
                                            <?php echo e(ucfirst($call->call_type)); ?>

                                        </span>
                                    </td>
                                    <td>
                                        <?php if($call->sms_sent): ?>
                                        <span class="badge bg-success">Yes</span>
                                        <?php else: ?>
                                        <span class="badge bg-secondary">No</span>
                                        <?php endif; ?>
                                    </td>
                                    <td><?php echo e($call->call_time->format('M d, Y H:i')); ?></td>
                                </tr>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                <tr>
                                    <td colspan="4" class="text-center text-muted">No call logs</td>
                                </tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                    <?php if(method_exists($callLogs, 'links')): ?>
                    <?php echo e($callLogs->fragment('callLogs')->links()); ?>

                    <?php endif; ?>
                </div>
                <?php endif; ?>

                <?php if($canViewSms): ?>
                <div class="tab-pane fade <?php echo e(!$canViewCalls ? 'show active' : ''); ?>" id="smsLogs">
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>Phone Number</th>
                                    <th>Channel</th>
                                    <th>Message</th>
                                    <th>Status</th>
                                    <th>Sent At</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $__empty_1 = true; $__currentLoopData = $smsLogs; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $sms): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                <tr>
                                    <td><?php echo e($sms->phone_number); ?></td>
                                    <td>
                                        <?php if(($sms->channel ?? 'sms') === 'whatsapp'): ?>
                                        <span class="badge bg-success"><i class="bi bi-whatsapp"></i> WhatsApp</span>
                                        <?php else: ?>
                                        <span class="badge bg-primary"><i class="bi bi-chat-text"></i> SMS</span>
                                        <?php endif; ?>
                                    </td>
                                    <td><?php echo e(Str::limit($sms->message, 40)); ?></td>
                                    <td>
                                        <span class="badge bg-<?php echo e($sms->status == 'sent' ? 'success' : ($sms->status == 'pending' ? 'warning' : 'danger')); ?>">
                                            <?php echo e(ucfirst($sms->status)); ?>

                                        </span>
                                    </td>
                                    <td><?php echo e($sms->sent_at?->format('M d, Y H:i') ?? '-'); ?></td>
                                </tr>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                <tr>
                                    <td colspan="5" class="text-center text-muted">No SMS / WhatsApp logs</td>
                                </tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                    <?php if(method_exists($smsLogs, 'links')): ?>
                    <?php echo e($smsLogs->fragment('smsLogs')->links()); ?>

                    <?php endif; ?>
                </div>
                <?php endif; ?>

                <?php if($canViewPromos): ?>
                <div class="tab-pane fade <?php echo e(!$canViewCalls && !$canViewSms ? 'show active' : ''); ?>" id="promoLogs">
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>Phone Number</th>
                                    <th>Message</th>
                                    <th>Trigger</th>
                                    <th>Status</th>
                                    <th>Sent At</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $__empty_1 = true; $__currentLoopData = $promotionLogs; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $promo): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                <tr>
                                    <td><?php echo e($promo->phone_number); ?></td>
                                    <td><?php echo e(Str::limit($promo->message, 40)); ?></td>
                                    <td>
                                        <span class="badge bg-<?php echo e($promo->trigger_type == 'incoming' ? 'success' : ($promo->trigger_type == 'outgoing' ? 'primary' : 'danger')); ?>">
                                            <?php echo e(ucfirst($promo->trigger_type)); ?>

                                        </span>
                                    </td>
                                    <td>
                                        <span class="badge bg-<?php echo e($promo->status == 'sent' ? 'success' : ($promo->status == 'pending' ? 'warning' : 'danger')); ?>">
                                            <?php echo e(ucfirst($promo->status)); ?>

                                        </span>
                                    </td>
                                    <td><?php echo e($promo->sent_at?->format('M d, Y H:i') ?? '-'); ?></td>
                                </tr>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                <tr>
                                    <td colspan="5" class="text-center text-muted">No promotion logs</td>
                                </tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                    <?php if(method_exists($promotionLogs, 'links')): ?>
                    <?php echo e($promotionLogs->fragment('promoLogs')->links()); ?>

                    <?php endif; ?>
                </div>
                <?php endif; ?>
            </div>
        </div>
        <?php else: ?>
        
        <div class="row g-4">
            <div class="col-md-12">
                <?php echo $__env->make('dashboard.partials.user-settings-block', ['settings' => $settings, 'wide' => true], \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
            </div>
            <div class="col-md-12">
                <?php echo $__env->make('dashboard.partials.user-templates-block', ['templates' => $user->messageTemplates, 'user_id' => $user->id, 'wide' => true], \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
            </div>
        </div>
        <?php endif; ?>
    </div>
</div>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.dashboard', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/zwfgftjb/autopromix.byteengine.in/resources/views/dashboard/user-details.blade.php ENDPATH**/ ?>