<?php
if (Auth('admin')->User()->dashboard_style == 'light') {
    $text = 'dark';
    $bg = 'light';
} else {
    $bg = 'dark';
    $text = 'light';
}
?>


<?php $__env->startSection('title', $title ?? 'Contact Messages Management'); ?>

<?php $__env->startSection('content'); ?>
    <?php echo $__env->make('admin.topmenu', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
    <?php echo $__env->make('admin.sidebar', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
    <div class="main-panel">
        <div class="content">
            <div class="page-inner">
                <div class="mt-2 mb-4">
                    <h1 class="title1 text-<?php echo e($text); ?>"><?php echo e($title ?? 'Contact Messages Management'); ?></h1>
                    <p class="text-muted mt-1">Manage and respond to contact messages from website visitors</p>
                </div>

                <?php if (isset($component)) { $__componentOriginalc254754b9d5db91d5165876f9d051922ca0066f4 = $component; } ?>
<?php $component = $__env->getContainer()->make(Illuminate\View\AnonymousComponent::class, ['view' => 'components.danger-alert','data' => []]); ?>
<?php $component->withName('danger-alert'); ?>
<?php if ($component->shouldRender()): ?>
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>
<?php $component->withAttributes([]); ?>
<?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__componentOriginalc254754b9d5db91d5165876f9d051922ca0066f4)): ?>
<?php $component = $__componentOriginalc254754b9d5db91d5165876f9d051922ca0066f4; ?>
<?php unset($__componentOriginalc254754b9d5db91d5165876f9d051922ca0066f4); ?>
<?php endif; ?>
                <?php if (isset($component)) { $__componentOriginalc254754b9d5db91d5165876f9d051922ca0066f4 = $component; } ?>
<?php $component = $__env->getContainer()->make(Illuminate\View\AnonymousComponent::class, ['view' => 'components.success-alert','data' => []]); ?>
<?php $component->withName('success-alert'); ?>
<?php if ($component->shouldRender()): ?>
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>
<?php $component->withAttributes([]); ?>
<?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__componentOriginalc254754b9d5db91d5165876f9d051922ca0066f4)): ?>
<?php $component = $__componentOriginalc254754b9d5db91d5165876f9d051922ca0066f4; ?>
<?php unset($__componentOriginalc254754b9d5db91d5165876f9d051922ca0066f4); ?>
<?php endif; ?>

                <!-- Statistics Cards -->
                <div class="row mb-4">
                    <div class="col-xl-2 col-md-4 mb-4">
                        <div class="card border-left-primary shadow h-100 py-2">
                            <div class="card-body">
                                <div class="row no-gutters align-items-center">
                                    <div class="col mr-2">
                                        <div class="text-xs font-weight-bold text-primary text-uppercase mb-1">Total Messages</div>
                                        <div class="h5 mb-0 font-weight-bold text-<?php echo e($text); ?>"><?php echo e($stats['total']); ?></div>
                                    </div>
                                    <div class="col-auto">
                                        <i class="fas fa-envelope fa-2x text-gray-300"></i>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="col-xl-2 col-md-4 mb-4">
                        <div class="card border-left-warning shadow h-100 py-2">
                            <div class="card-body">
                                <div class="row no-gutters align-items-center">
                                    <div class="col mr-2">
                                        <div class="text-xs font-weight-bold text-warning text-uppercase mb-1">Unread</div>
                                        <div class="h5 mb-0 font-weight-bold text-<?php echo e($text); ?>"><?php echo e($stats['unread']); ?></div>
                                    </div>
                                    <div class="col-auto">
                                        <i class="fas fa-envelope-open fa-2x text-gray-300"></i>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="col-xl-2 col-md-4 mb-4">
                        <div class="card border-left-success shadow h-100 py-2">
                            <div class="card-body">
                                <div class="row no-gutters align-items-center">
                                    <div class="col mr-2">
                                        <div class="text-xs font-weight-bold text-success text-uppercase mb-1">Read</div>
                                        <div class="h5 mb-0 font-weight-bold text-<?php echo e($text); ?>"><?php echo e($stats['read']); ?></div>
                                    </div>
                                    <div class="col-auto">
                                        <i class="fas fa-check-circle fa-2x text-gray-300"></i>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="col-xl-2 col-md-4 mb-4">
                        <div class="card border-left-info shadow h-100 py-2">
                            <div class="card-body">
                                <div class="row no-gutters align-items-center">
                                    <div class="col mr-2">
                                        <div class="text-xs font-weight-bold text-info text-uppercase mb-1">Replied</div>
                                        <div class="h5 mb-0 font-weight-bold text-<?php echo e($text); ?>"><?php echo e($stats['replied']); ?></div>
                                    </div>
                                    <div class="col-auto">
                                        <i class="fas fa-reply fa-2x text-gray-300"></i>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="col-xl-2 col-md-4 mb-4">
                        <div class="card border-left-secondary shadow h-100 py-2">
                            <div class="card-body">
                                <div class="row no-gutters align-items-center">
                                    <div class="col mr-2">
                                        <div class="text-xs font-weight-bold text-secondary text-uppercase mb-1">Archived</div>
                                        <div class="h5 mb-0 font-weight-bold text-<?php echo e($text); ?>"><?php echo e($stats['archived']); ?></div>
                                    </div>
                                    <div class="col-auto">
                                        <i class="fas fa-archive fa-2x text-gray-300"></i>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="col-xl-2 col-md-4 mb-4">
                        <div class="card border-left-dark shadow h-100 py-2">
                            <div class="card-body">
                                <div class="row no-gutters align-items-center">
                                    <div class="col mr-2">
                                        <div class="text-xs font-weight-bold text-dark text-uppercase mb-1">This Week</div>
                                        <div class="h5 mb-0 font-weight-bold text-<?php echo e($text); ?>"><?php echo e($stats['recent']); ?></div>
                                    </div>
                                    <div class="col-auto">
                                        <i class="fas fa-calendar-week fa-2x text-gray-300"></i>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Search and Filter Section -->
                <div class="mb-5 row">
                    <div class="col-md-12">
                        <div class="card p-md-4 p-2">
                            <div class="card-header py-3">
                                <h6 class="m-0 font-weight-bold text-primary">
                                    <i class="fas fa-search mr-2"></i>Search & Filter Messages
                                </h6>
                            </div>
                            <div class="card-body">
                                <form method="GET" action="<?php echo e(route('admin.contact-messages.index')); ?>" class="row">
                                    <div class="col-md-4 mb-3">
                                        <label for="search" class="form-label text-<?php echo e($text); ?> font-weight-bold">Search Messages</label>
                                        <input type="text" class="form-control text-<?php echo e($text); ?>" id="search" name="search"
                                               value="<?php echo e(request('search')); ?>"
                                               placeholder="Search by name, email, subject, or phone...">
                                    </div>
                                    <div class="col-md-3 mb-3">
                                        <label for="status" class="form-label text-<?php echo e($text); ?> font-weight-bold">Status Filter</label>
                                        <select class="form-control text-<?php echo e($text); ?>" id="status" name="status">
                                            <option value="">All Status</option>
                                            <option value="unread" <?php echo e(request('status') === 'unread' ? 'selected' : ''); ?>>Unread</option>
                                            <option value="read" <?php echo e(request('status') === 'read' ? 'selected' : ''); ?>>Read</option>
                                            <option value="replied" <?php echo e(request('status') === 'replied' ? 'selected' : ''); ?>>Replied</option>
                                            <option value="archived" <?php echo e(request('status') === 'archived' ? 'selected' : ''); ?>>Archived</option>
                                        </select>
                                    </div>
                                    <div class="col-md-3 d-flex align-items-end mb-3">
                                        <button type="submit" class="btn btn-primary mr-2">
                                            <i class="fas fa-search mr-1"></i>Search
                                        </button>
                                        <a href="<?php echo e(route('admin.contact-messages.index')); ?>" class="btn btn-secondary">
                                            <i class="fas fa-undo mr-1"></i>Reset
                                        </a>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Messages Table -->
                <div class="mb-5 row">
                    <div class="col-md-12">
                        <div class="card p-md-4 p-2">
                            <div class="card-header py-3 d-flex justify-content-between align-items-center">
                                <h6 class="m-0 font-weight-bold text-primary">
                                    <i class="fas fa-list mr-2"></i>Contact Messages (<?php echo e($messages->total()); ?> total)
                                </h6>
                                <div class="btn-group">
                                    <button type="button" class="btn btn-sm btn-success" onclick="markSelectedAsRead()">
                                        <i class="fas fa-check mr-1"></i>Mark as Read
                                    </button>
                                    <button type="button" class="btn btn-sm btn-secondary" onclick="archiveSelected()">
                                        <i class="fas fa-archive mr-1"></i>Archive
                                    </button>
                                    <button type="button" class="btn btn-sm btn-danger" onclick="deleteSelected()">
                                        <i class="fas fa-trash mr-1"></i>Delete
                                    </button>
                                </div>
                            </div>
                            <div class="card-body">
                                <?php if($messages->count() > 0): ?>
                                    <form id="bulkActionForm" method="POST">
                                        <?php echo csrf_field(); ?>
                                        <input type="hidden" name="_method" value="POST" id="methodInput">
                                        <div class="table-responsive">
                                            <table class="table table-bordered" width="100%" cellspacing="0">
                                                <thead class="thead-light">
                                                    <tr>
                                                        <th class="text-<?php echo e($text); ?>">
                                                            <input type="checkbox" id="selectAll" onchange="toggleAll(this)">
                                                        </th>
                                                        <th class="text-<?php echo e($text); ?>">From</th>
                                                        <th class="text-<?php echo e($text); ?>">Subject</th>
                                                        <th class="text-<?php echo e($text); ?>">Service Info</th>
                                                        <th class="text-<?php echo e($text); ?>">Received</th>
                                                        <th class="text-<?php echo e($text); ?>">Status</th>
                                                        <th class="text-<?php echo e($text); ?>">Actions</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php $__currentLoopData = $messages; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $message): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                        <tr class="<?php echo e($message->status === 'unread' ? 'font-weight-bold' : ''); ?>">
                                                            <td>
                                                                <input type="checkbox" name="message_ids[]" value="<?php echo e($message->id); ?>" class="message-checkbox">
                                                            </td>
                                                            <td class="text-<?php echo e($text); ?>">
                                                                <div class="font-weight-bold"><?php echo e($message->fname); ?></div>
                                                                <small class="text-muted"><?php echo e($message->email); ?></small>
                                                                <?php if($message->phone): ?>
                                                                    <br><small class="text-muted"><?php echo e($message->phone); ?></small>
                                                                <?php endif; ?>
                                                            </td>
                                                            <td class="text-<?php echo e($text); ?>">
                                                                <a href="<?php echo e(route('admin.contact-messages.show', $message)); ?>" class="text-decoration-none">
                                                                    <?php echo e(Str::limit($message->subject, 50)); ?>

                                                                </a>
                                                                <br><small class="text-muted"><?php echo e(Str::limit($message->message, 80)); ?></small>
                                                            </td>
                                                            <td class="text-<?php echo e($text); ?>">
                                                                <?php if($message->service_branch): ?>
                                                                    <span class="badge badge-primary"><?php echo e($message->service_branch); ?></span>
                                                                    <?php if($message->rank): ?>
                                                                        <br><span class="badge badge-secondary mt-1"><?php echo e($message->rank); ?></span>
                                                                    <?php endif; ?>
                                                                <?php else: ?>
                                                                    <span class="text-muted">Civilian</span>
                                                                <?php endif; ?>
                                                            </td>
                                                            <td class="text-<?php echo e($text); ?>"><?php echo e($message->formatted_created_at); ?></td>
                                                            <td>
                                                                <span class="badge badge-<?php echo e($message->status_color); ?>">
                                                                    <?php if($message->status === 'unread'): ?>
                                                                        <i class="fas fa-envelope mr-1"></i>
                                                                    <?php elseif($message->status === 'read'): ?>
                                                                        <i class="fas fa-envelope-open mr-1"></i>
                                                                    <?php elseif($message->status === 'replied'): ?>
                                                                        <i class="fas fa-reply mr-1"></i>
                                                                    <?php elseif($message->status === 'archived'): ?>
                                                                        <i class="fas fa-archive mr-1"></i>
                                                                    <?php endif; ?>
                                                                    <?php echo e($message->status_text); ?>

                                                                </span>
                                                            </td>
                                                            <td>
                                                                <div class="btn-group" role="group">
                                                                    <a href="<?php echo e(route('admin.contact-messages.show', $message)); ?>"
                                                                       class="btn btn-sm btn-info" title="View Details">
                                                                        <i class="fas fa-eye"></i>
                                                                    </a>

                                                                    <?php if($message->status === 'unread'): ?>
                                                                        <form method="POST" action="<?php echo e(route('admin.contact-messages.update-status', $message)); ?>" class="d-inline">
                                                                            <?php echo csrf_field(); ?>
                                                                            <?php echo method_field('PATCH'); ?>
                                                                            <input type="hidden" name="status" value="read">
                                                                            <button type="submit" class="btn btn-sm btn-success" title="Mark as Read">
                                                                                <i class="fas fa-check"></i>
                                                                            </button>
                                                                        </form>
                                                                    <?php endif; ?>

                                                                    <?php if($message->status !== 'replied'): ?>
                                                                        <form method="POST" action="<?php echo e(route('admin.contact-messages.update-status', $message)); ?>" class="d-inline">
                                                                            <?php echo csrf_field(); ?>
                                                                            <?php echo method_field('PATCH'); ?>
                                                                            <input type="hidden" name="status" value="replied">
                                                                            <button type="submit" class="btn btn-sm btn-primary" title="Mark as Replied">
                                                                                <i class="fas fa-reply"></i>
                                                                            </button>
                                                                        </form>
                                                                    <?php endif; ?>

                                                                    <?php if($message->status !== 'archived'): ?>
                                                                        <form method="POST" action="<?php echo e(route('admin.contact-messages.update-status', $message)); ?>" class="d-inline">
                                                                            <?php echo csrf_field(); ?>
                                                                            <?php echo method_field('PATCH'); ?>
                                                                            <input type="hidden" name="status" value="archived">
                                                                            <button type="submit" class="btn btn-sm btn-secondary" title="Archive">
                                                                                <i class="fas fa-archive"></i>
                                                                            </button>
                                                                        </form>
                                                                    <?php endif; ?>

                                                                    <form method="POST" action="<?php echo e(route('admin.contact-messages.destroy', $message)); ?>" class="d-inline">
                                                                        <?php echo csrf_field(); ?>
                                                                        <?php echo method_field('DELETE'); ?>
                                                                        <button type="submit" class="btn btn-sm btn-danger" title="Delete"
                                                                                onclick="return confirm('Are you sure you want to delete this message? This action cannot be undone.')">
                                                                            <i class="fas fa-trash"></i>
                                                                        </button>
                                                                    </form>
                                                                </div>
                                                            </td>
                                                        </tr>
                                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                                </tbody>
                                            </table>
                                        </div>
                                    </form>

                                    <!-- Pagination -->
                                    <div class="d-flex justify-content-center">
                                        <?php echo e($messages->appends(request()->query())->links()); ?>

                                    </div>
                                <?php else: ?>
                                    <div class="text-center py-5">
                                        <i class="fas fa-inbox fa-3x text-gray-300 mb-3"></i>
                                        <h5 class="text-<?php echo e($text); ?>">No messages found</h5>
                                        <p class="text-muted">
                                            <?php if(request('search') || request('status')): ?>
                                                Try adjusting your search criteria or filters.
                                            <?php else: ?>
                                                No contact messages have been received yet.
                                            <?php endif; ?>
                                        </p>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

<script>
function toggleAll(source) {
    const checkboxes = document.querySelectorAll('.message-checkbox');
    checkboxes.forEach(checkbox => {
        checkbox.checked = source.checked;
    });
}

function getSelectedIds() {
    const checkedBoxes = document.querySelectorAll('.message-checkbox:checked');
    return Array.from(checkedBoxes).map(cb => cb.value);
}

function markSelectedAsRead() {
    const selectedIds = getSelectedIds();
    if (selectedIds.length === 0) {
        alert('Please select at least one message.');
        return;
    }

    const form = document.getElementById('bulkActionForm');
    const methodInput = document.getElementById('methodInput');
    methodInput.value = 'POST';
    form.action = "<?php echo e(route('admin.contact-messages.bulk.mark-read')); ?>";
    form.submit();
}

function archiveSelected() {
    const selectedIds = getSelectedIds();
    if (selectedIds.length === 0) {
        alert('Please select at least one message.');
        return;
    }

    if (confirm('Are you sure you want to archive the selected messages?')) {
        const form = document.getElementById('bulkActionForm');
        const methodInput = document.getElementById('methodInput');
        methodInput.value = 'POST';
        form.action = "<?php echo e(route('admin.contact-messages.bulk.archive')); ?>";
        form.submit();
    }
}

function deleteSelected() {
    const selectedIds = getSelectedIds();
    if (selectedIds.length === 0) {
        alert('Please select at least one message.');
        return;
    }

    if (confirm('Are you sure you want to delete the selected messages? This action cannot be undone.')) {
        const form = document.getElementById('bulkActionForm');
        const methodInput = document.getElementById('methodInput');
        methodInput.value = 'DELETE';
        form.action = "<?php echo e(route('admin.contact-messages.bulk.delete')); ?>";
        form.submit();
    }
}
</script>

<?php if(session('success')): ?>
    <script>
        Swal.fire({
            icon: 'success',
            title: 'Success!',
            text: '<?php echo e(session('success')); ?>',
            confirmButtonColor: '#28a745'
        });
    </script>
<?php endif; ?>

<?php if(session('error')): ?>
    <script>
        Swal.fire({
            icon: 'error',
            title: 'Error!',
            text: '<?php echo e(session('error')); ?>',
            confirmButtonColor: '#dc3545'
        });
    </script>
<?php endif; ?>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.app', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/elitemaxpro/milita.elitemaxpro.click/resources/views/admin/contact-messages/index.blade.php ENDPATH**/ ?>