<?php $__env->startSection('title', 'সদস্য তালিকা'); ?>

<?php $__env->startSection('content'); ?>
<div class="container-fluid">
    <!-- ব্রেডক্রাম্ব -->
    <nav aria-label="breadcrumb">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="<?php echo e(route('dashboard')); ?>">ড্যাশবোর্ড</a></li>
            <li class="breadcrumb-item active" aria-current="page">সদস্য তালিকা</li>
        </ol>
    </nav>
    
    <div class="card shadow">
        <div class="card-header py-3 d-flex justify-content-between align-items-center">
            <h6 class="m-0 font-weight-bold">সদস্য তালিকা</h6>
            <div>
                <a href="<?php echo e(route('members.create')); ?>" class="btn btn-primary">
                    <i class="fas fa-user-plus me-1"></i>নতুন সদস্য
                </a>
                <a href="<?php echo e(route('members.report')); ?>" class="btn btn-info ms-1">
                    <i class="fas fa-chart-bar me-1"></i>রিপোর্ট
                </a>
                <?php if(auth()->user()->user_type == 'admin' || auth()->user()->user_type == 'committee'): ?>
                <a href="<?php echo e(route('members.export.excel')); ?>" class="btn btn-success ms-1">
                    <i class="fas fa-download me-1"></i>এক্সপোর্ট
                </a>
                <?php endif; ?>
            </div>
        </div>
        <div class="card-body">
            <!-- ফিল্টার ফর্ম -->
            <form method="GET" action="<?php echo e(route('members.index')); ?>" class="row g-3 mb-4">
                <div class="col-md-3">
                    <input type="text" name="search" class="form-control" placeholder="সদস্য নং, নাম বা ফোন" 
                           value="<?php echo e(request('search')); ?>">
                </div>
                <div class="col-md-2">
                    <select name="status" class="form-select">
                        <option value="">সব স্ট্যাটাস</option>
                        <option value="active" <?php echo e(request('status') == 'active' ? 'selected' : ''); ?>>সক্রিয়</option>
                        <option value="inactive" <?php echo e(request('status') == 'inactive' ? 'selected' : ''); ?>>নিষ্ক্রিয়</option>
                        <option value="pending" <?php echo e(request('status') == 'pending' ? 'selected' : ''); ?>>পেন্ডিং</option>
                        <option value="terminated" <?php echo e(request('status') == 'terminated' ? 'selected' : ''); ?>>বাতিল</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <select name="approval_status" class="form-select">
                        <option value="">সব অনুমোদন</option>
                        <option value="pending" <?php echo e(request('approval_status') == 'pending' ? 'selected' : ''); ?>>অপেক্ষমান</option>
                        <option value="approved" <?php echo e(request('approval_status') == 'approved' ? 'selected' : ''); ?>>অনুমোদিত</option>
                        <option value="rejected" <?php echo e(request('approval_status') == 'rejected' ? 'selected' : ''); ?>>প্রত্যাখ্যান</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <select name="form_fee_paid" class="form-select">
                        <option value="">সব ফর্ম ফি</option>
                        <option value="paid" <?php echo e(request('form_fee_paid') == 'paid' ? 'selected' : ''); ?>>পরিশোধিত</option>
                        <option value="unpaid" <?php echo e(request('form_fee_paid') == 'unpaid' ? 'selected' : ''); ?>>বকেয়া</option>
                    </select>
                </div>
                <div class="col-md-1">
                    <button type="submit" class="btn btn-primary w-100">
                        <i class="fas fa-search me-1"></i>খুঁজুন
                    </button>
                </div>
                <div class="col-md-1">
                    <a href="<?php echo e(route('members.index')); ?>" class="btn btn-secondary w-100">
                        <i class="fas fa-redo me-1"></i>রিসেট
                    </a>
                </div>
            </form>
            
            <!-- সারাংশ -->
            <div class="row mb-4">
                <div class="col-md-3 col-sm-6 mb-3">
                    <div class="card border-primary">
                        <div class="card-body text-center">
                            <h5 class="card-title text-primary"><?php echo e($members->total()); ?></h5>
                            <p class="card-text">মোট সদস্য</p>
                        </div>
                    </div>
                </div>
                <div class="col-md-3 col-sm-6 mb-3">
                    <div class="card border-success">
                        <div class="card-body text-center">
                            <h5 class="card-title text-success"><?php echo e($activeCount); ?></h5>
                            <p class="card-text">সক্রিয় সদস্য</p>
                        </div>
                    </div>
                </div>
                <div class="col-md-3 col-sm-6 mb-3">
                    <div class="card border-warning">
                        <div class="card-body text-center">
                            <h5 class="card-title text-warning"><?php echo e($pendingCount); ?></h5>
                            <p class="card-text">অপেক্ষমান অনুমোদন</p>
                        </div>
                    </div>
                </div>
                <div class="col-md-3 col-sm-6 mb-3">
                    <div class="card border-danger">
                        <div class="card-body text-center">
                            <h5 class="card-title text-danger"><?php echo e($formFeePendingCount); ?></h5>
                            <p class="card-text">ফর্ম ফি বকেয়া</p>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- সদস্য টেবিল -->
            <div class="table-responsive">
                <table class="table table-bordered table-hover" id="membersTable">
                    <thead class="table-dark">
                        <tr>
                            <th width="50">#</th>
                            <th>সদস্য নং</th>
                            <th>নাম</th>
                            <th>ফোন</th>
                            <th>এনআইডি</th>
                            <th>নিবন্ধন তারিখ</th>
                            <th>অবস্থা</th>
                            <th>অনুমোদন</th>
                            <th>ফর্ম ফি</th>
                            <th>কার্যক্রম</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php $__empty_1 = true; $__currentLoopData = $members; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $member): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                        <tr>
                            <td><?php echo e($loop->iteration + ($members->currentPage() - 1) * $members->perPage()); ?></td>
                            <td>
                                <strong><?php echo e($member->member_id ?? 'N/A'); ?></strong>
                                <?php if($member->form_fee_paid == false): ?>
                                <span class="badge bg-danger ms-1" title="ফর্ম ফি বকেয়া">!</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <div class="d-flex align-items-center">
                                    <?php if($member->photo): ?>
                                    <img src="<?php echo e(asset('storage/' . $member->photo)); ?>" 
                                         class="rounded-circle me-2" 
                                         style="width: 35px; height: 35px; object-fit: cover;"
                                         onerror="this.src='<?php echo e(asset('images/default-user.png')); ?>'">
                                    <?php else: ?>
                                    <div class="rounded-circle bg-secondary d-flex align-items-center justify-content-center me-2" 
                                         style="width: 35px; height: 35px;">
                                        <i class="fas fa-user text-white"></i>
                                    </div>
                                    <?php endif; ?>
                                    <div>
                                        <strong><?php echo e($member->name ?? 'N/A'); ?></strong><br>
                                        <small class="text-muted"><?php echo e($member->father_name ?? 'N/A'); ?></small>
                                    </div>
                                </div>
                            </td>
                            <td>
                                <?php if($member->phone): ?>
                                <a href="tel:<?php echo e($member->phone); ?>" class="text-decoration-none">
                                    <i class="fas fa-phone me-1"></i><?php echo e($member->phone); ?>

                                </a>
                                <?php else: ?>
                                <span class="text-muted">নাই</span>
                                <?php endif; ?>
                            </td>
                            <td><?php echo e($member->nid ?? 'N/A'); ?></td>
                            <td>
                                <?php if($member->registration_date): ?>
                                    <?php echo e(\Carbon\Carbon::parse($member->registration_date)->format('d/m/Y')); ?>

                                <?php else: ?>
                                    <?php echo e(\Carbon\Carbon::parse($member->created_at)->format('d/m/Y')); ?>

                                <?php endif; ?>
                            </td>
                            <td>
                                <?php
                                    $statusClass = [
                                        'active' => 'success',
                                        'inactive' => 'secondary',
                                        'pending' => 'warning',
                                        'terminated' => 'danger'
                                    ][$member->status ?? 'pending'] ?? 'secondary';
                                    
                                    $statusText = [
                                        'active' => 'সক্রিয়',
                                        'inactive' => 'নিষ্ক্রিয়',
                                        'pending' => 'পেন্ডিং',
                                        'terminated' => 'বাতিল'
                                    ][$member->status ?? 'pending'] ?? 'পেন্ডিং';
                                ?>
                                <span class="badge bg-<?php echo e($statusClass); ?>">
                                    <?php echo e($statusText); ?>

                                </span>
                            </td>
                            <td>
                                <?php
                                    $approvalClass = [
                                        'approved' => 'success',
                                        'pending' => 'warning',
                                        'rejected' => 'danger'
                                    ][$member->approval_status ?? 'pending'] ?? 'warning';
                                    
                                    $approvalText = [
                                        'approved' => 'অনুমোদিত',
                                        'pending' => 'অপেক্ষমান',
                                        'rejected' => 'প্রত্যাখ্যান'
                                    ][$member->approval_status ?? 'pending'] ?? 'অপেক্ষমান';
                                ?>
                                <span class="badge bg-<?php echo e($approvalClass); ?>">
                                    <?php echo e($approvalText); ?>

                                </span>
                            </td>
                            <td>
                                <?php
                                    $formFeeClass = $member->form_fee_paid ? 'success' : 'danger';
                                    $formFeeText = $member->form_fee_paid ? 'পরিশোধিত' : 'বকেয়া';
                                ?>
                                <span class="badge bg-<?php echo e($formFeeClass); ?>">
                                    <?php echo e($formFeeText); ?>

                                </span>
                            </td>
                            <td>
                                <div class="btn-group btn-group-sm" role="group">
                                    <a href="<?php echo e(route('members.show', $member->id)); ?>" class="btn btn-info" title="দেখুন">
                                        <i class="fas fa-eye"></i>
                                    </a>
                                    <a href="<?php echo e(route('members.edit', $member->id)); ?>" class="btn btn-warning" title="সম্পাদনা">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    <a href="<?php echo e(route('members.print-card', $member->id)); ?>" class="btn btn-secondary" title="কার্ড প্রিন্ট" target="_blank">
                                        <i class="fas fa-print"></i>
                                    </a>
                                    <?php if($member->approval_status != 'approved' && auth()->user()->user_type != 'member'): ?>
                                    <button type="button" class="btn btn-danger delete-btn" 
                                            data-id="<?php echo e($member->id); ?>" 
                                            data-name="<?php echo e($member->name); ?>" 
                                            title="ডিলিট">
                                        <i class="fas fa-trash"></i>
                                    </button>
                                    <?php elseif(auth()->user()->user_type == 'member'): ?>
                                    <button type="button" class="btn btn-secondary disabled" title="অধিকার নেই">
                                        <i class="fas fa-ban"></i>
                                    </button>
                                    <?php else: ?>
                                    <button type="button" class="btn btn-secondary disabled" title="অনুমোদিত সদস্য ডিলিট করা যাবে না">
                                        <i class="fas fa-ban"></i>
                                    </button>
                                    <?php endif; ?>
                                </div>
                            </td>
                        </tr>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                        <tr>
                            <td colspan="10" class="text-center py-4">
                                <i class="fas fa-users fa-3x text-muted mb-3"></i>
                                <h5 class="text-muted">কোন সদস্য পাওয়া যায়নি</h5>
                                <p class="text-muted">নতুন সদস্য নিবন্ধন করুন</p>
                                <a href="<?php echo e(route('members.create')); ?>" class="btn btn-primary">
                                    <i class="fas fa-user-plus me-1"></i>নতুন সদস্য নিবন্ধন
                                </a>
                            </td>
                        </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
            
            <!-- পেজিনেশন -->
            <?php if($members->hasPages()): ?>
            <div class="d-flex justify-content-between align-items-center mt-3">
                <div class="text-muted">
                    <?php echo e($members->firstItem()); ?>-<?php echo e($members->lastItem()); ?> / <?php echo e($members->total()); ?> রেকর্ড
                </div>
                <div>
                    <?php echo e($members->withQueryString()->links('pagination::bootstrap-5')); ?>

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

<!-- ডিলিট মডাল -->
<div class="modal fade" id="deleteModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">সদস্য ডিলিট করুন</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <p>আপনি কি নিশ্চিত যে আপনি <strong id="deleteMemberName"></strong> সদস্যকে ডিলিট করতে চান?</p>
                <p class="text-danger"><small>এই কাজটি বাতিল করা যাবে না।</small></p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">বাতিল</button>
                <form id="deleteForm" method="POST" style="display: inline;">
                    <?php echo csrf_field(); ?>
                    <?php echo method_field('DELETE'); ?>
                    <button type="submit" class="btn btn-danger">ডিলিট করুন</button>
                </form>
            </div>
        </div>
    </div>
</div>

<!-- স্ট্যাটাস পরিবর্তন মডাল -->
<div class="modal fade" id="statusModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">সদস্য স্ট্যাটাস পরিবর্তন</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form id="statusForm" method="POST">
                <?php echo csrf_field(); ?>
                <?php echo method_field('POST'); ?>
                <div class="modal-body">
                    <div class="mb-3">
                        <label for="status" class="form-label">স্ট্যাটাস</label>
                        <select class="form-select" id="status" name="status" required>
                            <option value="active">সক্রিয়</option>
                            <option value="inactive">নিষ্ক্রিয়</option>
                            <option value="pending">পেন্ডিং</option>
                            <option value="terminated">বাতিল</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label for="reason" class="form-label">কারণ (ঐচ্ছিক)</label>
                        <textarea class="form-control" id="reason" name="reason" rows="3"></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">বাতিল</button>
                    <button type="submit" class="btn btn-primary">পরিবর্তন করুন</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- অনুমোদন স্ট্যাটাস পরিবর্তন মডাল -->
<div class="modal fade" id="approvalModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">অনুমোদন স্ট্যাটাস পরিবর্তন</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form id="approvalForm" method="POST">
                <?php echo csrf_field(); ?>
                <?php echo method_field('POST'); ?>
                <div class="modal-body">
                    <div class="mb-3">
                        <label for="approval_status" class="form-label">অনুমোদন স্ট্যাটাস</label>
                        <select class="form-select" id="approval_status" name="approval_status" required>
                            <option value="pending">অপেক্ষমান</option>
                            <option value="approved">অনুমোদিত</option>
                            <option value="rejected">প্রত্যাখ্যান</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label for="remarks" class="form-label">মন্তব্য (ঐচ্ছিক)</label>
                        <textarea class="form-control" id="remarks" name="remarks" rows="3"></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">বাতিল</button>
                    <button type="submit" class="btn btn-primary">পরিবর্তন করুন</button>
                </div>
            </form>
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('scripts'); ?>
<script>
    document.addEventListener('DOMContentLoaded', function() {
        // ডিলিট বাটন হ্যান্ডলিং
        document.querySelectorAll('.delete-btn').forEach(button => {
            button.addEventListener('click', function() {
                const memberId = this.getAttribute('data-id');
                const memberName = this.getAttribute('data-name');
                
                document.getElementById('deleteMemberName').textContent = memberName;
                document.getElementById('deleteForm').action = `/members/${memberId}`;
                
                const deleteModal = new bootstrap.Modal(document.getElementById('deleteModal'));
                deleteModal.show();
            });
        });
        
        // ফর্ম ফি পেমেন্ট এজ্যাক্স
        document.querySelectorAll('.pay-form-fee-btn').forEach(button => {
            button.addEventListener('click', function() {
                const memberId = this.getAttribute('data-id');
                
                if (confirm('আপনি কি ফর্ম ফি পেমেন্ট নিশ্চিত করতে চান?')) {
                    fetch(`/members/${memberId}/pay-form-fee`, {
                        method: 'POST',
                        headers: {
                            'X-CSRF-TOKEN': '<?php echo e(csrf_token()); ?>',
                            'Content-Type': 'application/json',
                            'Accept': 'application/json'
                        }
                    })
                    .then(response => response.json())
                    .then(data => {
                        if (data.success) {
                            location.reload();
                        } else {
                            alert(data.message || 'পেমেন্ট করতে সমস্যা হয়েছে');
                        }
                    })
                    .catch(error => {
                        console.error('Error:', error);
                        alert('পেমেন্ট করতে সমস্যা হয়েছে');
                    });
                }
            });
        });
        
        // স্ট্যাটাস পরিবর্তন মডাল
        document.querySelectorAll('.status-btn').forEach(button => {
            button.addEventListener('click', function() {
                const memberId = this.getAttribute('data-id');
                const currentStatus = this.getAttribute('data-status');
                
                document.getElementById('statusForm').action = `/members/${memberId}/change-status`;
                document.getElementById('status').value = currentStatus;
                
                const statusModal = new bootstrap.Modal(document.getElementById('statusModal'));
                statusModal.show();
            });
        });
        
        // অনুমোদন স্ট্যাটাস পরিবর্তন মডাল
        document.querySelectorAll('.approval-btn').forEach(button => {
            button.addEventListener('click', function() {
                const memberId = this.getAttribute('data-id');
                const currentStatus = this.getAttribute('data-approval-status');
                
                document.getElementById('approvalForm').action = `/members/${memberId}/change-approval-status`;
                document.getElementById('approval_status').value = currentStatus;
                
                const approvalModal = new bootstrap.Modal(document.getElementById('approvalModal'));
                approvalModal.show();
            });
        });
        
        // Simple sorting
        const table = document.getElementById('membersTable');
        if (table) {
            const headers = table.querySelectorAll('thead th');
            headers.forEach((header, index) => {
                header.style.cursor = 'pointer';
                header.addEventListener('click', () => {
                    sortTable(index);
                });
            });
        }
        
        function sortTable(column) {
            const table = document.getElementById('membersTable');
            const tbody = table.querySelector('tbody');
            const rows = Array.from(tbody.querySelectorAll('tr'));
            
            const isAsc = table.getAttribute('data-sort-dir') !== 'asc';
            
            rows.sort((a, b) => {
                const aText = a.children[column].textContent.trim();
                const bText = b.children[column].textContent.trim();
                
                if (!isNaN(aText) && !isNaN(bText)) {
                    return isAsc ? aText - bText : bText - aText;
                }
                
                if (aText.match(/\d{2}\/\d{2}\/\d{4}/) && bText.match(/\d{2}\/\d{2}\/\d{4}/)) {
                    const aDate = aText.split('/').reverse().join('-');
                    const bDate = bText.split('/').reverse().join('-');
                    return isAsc ? new Date(aDate) - new Date(bDate) : new Date(bDate) - new Date(aDate);
                }
                
                return isAsc ? aText.localeCompare(bText) : bText.localeCompare(aText);
            });
            
            rows.forEach(row => tbody.appendChild(row));
            table.setAttribute('data-sort-dir', isAsc ? 'asc' : 'desc');
        }
        
        // Quick search
        const searchInput = document.querySelector('input[name="search"]');
        if (searchInput) {
            let timeout;
            searchInput.addEventListener('input', function() {
                clearTimeout(timeout);
                timeout = setTimeout(() => {
                    this.form.submit();
                }, 500);
            });
        }
    });
</script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /home/naowarin/somoti.naowarinternational.my.id/resources/views/members/index.blade.php ENDPATH**/ ?>