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

<div class="space-y-6">
    <div class="bg-white p-8 rounded-3xl shadow-sm border border-gray-100">
        <div class="flex items-center gap-3 mb-6">
            <div class="w-10 h-10 bg-indigo-50 text-indigo-600 rounded-xl flex items-center justify-center">
                <i class="fas fa-file-invoice-dollar text-lg"></i>
            </div>
            <h3 class="font-black text-xl text-gray-800">বাল্ক ফি জেনারেশন</h3>
        </div>

        <form id="genForm" class="grid grid-cols-1 md:grid-cols-4 gap-4 items-end">
			<?php echo csrf_field(); ?>
			<div>
				<label class="block text-sm font-bold text-gray-700 mb-1">ক্লাস সিলেক্ট করুন</label>
				<select name="class_id" class="w-full p-3 bg-gray-50 border border-gray-200 rounded-xl" required>
					<option value="">ক্লাস বেছে নিন</option>
					<?php $__currentLoopData = $classes; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $class): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
						<option value="<?php echo e($class->id); ?>"><?php echo e($class->class_name); ?></option>
					<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
				</select>
			</div>

			<div>
				<label class="block text-sm font-bold text-gray-700 mb-1">ফি-র ধরন</label>
				<select name="fee_type_id" class="w-full p-3 bg-gray-50 border border-gray-200 rounded-xl" required>
					<option value="">ফি টাইপ বেছে নিন</option>
					<?php $__currentLoopData = $feeTypes; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $ft): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
						<option value="<?php echo e($ft->id); ?>"><?php echo e($ft->type_name); ?></option>
					<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
				</select>
			</div>

			<div class="grid grid-cols-2 gap-2">
				<div>
					<label class="block text-sm font-bold text-gray-700 mb-1">মাস</label>
					<select name="month" class="w-full p-3 bg-gray-50 border border-gray-200 rounded-xl">
						<?php $__currentLoopData = range(1,12); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $m): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
							<option value="<?php echo e($m); ?>" <?php echo e(date('n') == $m ? 'selected' : ''); ?>>
								<?php echo e(date('F', mktime(0,0,0,$m,1))); ?>

							</option>
						<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
					</select>
				</div>
				<div>
					<label class="block text-sm font-bold text-gray-700 mb-1">বছর</label>
					<input type="number" name="year" value="<?php echo e(date('Y')); ?>" class="w-full p-3 bg-gray-50 border border-gray-200 rounded-xl">
				</div>
			</div>

			<button type="submit" id="btnGen" class="bg-indigo-600 text-white px-6 py-3 rounded-xl font-bold hover:bg-indigo-700 shadow-lg">
				জেনারেট
			</button>
		</form>
    </div>

    <div class="bg-white rounded-3xl shadow-sm border border-gray-100 overflow-hidden">
        <div class="p-8 border-b border-gray-50 flex justify-between items-center">
            <h3 class="font-black text-xl text-gray-800">সাম্প্রতিক ইনভয়েসসমূহ</h3>
            <span class="text-xs font-bold text-gray-400 uppercase tracking-widest">সর্বশেষ ১০০টি রেকর্ড</span>
        </div>
        
        <div class="overflow-x-auto">
            <table id="invoiceTable" class="w-full text-left border-collapse">
                <thead>
                    <tr class="bg-gray-50/50 text-gray-400 text-[11px] uppercase tracking-wider border-b border-gray-100">
                        <th class="px-8 py-4">ইনভয়েস নং</th>
                        <th class="py-4">ছাত্রের নাম</th>
                        <th class="py-4">মাস/বছর</th>
                        <th class="py-4">মোট টাকা</th>
                        <th class="py-4">বকেয়া</th>
                        <th class="py-4">স্ট্যাটাস</th>
                        <th class="px-8 py-4 text-right">অ্যাকশন</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-50">
                    <?php $__currentLoopData = $invoices; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $inv): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <tr class="hover:bg-gray-50/80 transition-all group">
                        <td class="px-8 py-4 font-mono text-[11px] text-gray-500"><?php echo e($inv->invoice_no); ?></td>
                        <td class="py-4">
                            <div class="font-bold text-gray-800"><?php echo e($inv->student->student_name_en ?? 'N/A'); ?></div>
                            <div class="text-[10px] text-gray-400">ID: <?php echo e($inv->student->admission_no ?? 'N/A'); ?></div>
                        </td>
                        <td class="py-4">
                            <span class="text-sm font-medium text-gray-600"><?php echo e(date('F', mktime(0,0,0,$inv->month, 1))); ?>, <?php echo e($inv->year); ?></span>
                        </td>
                        <td class="py-4">
                            <span class="font-black text-indigo-600"><?php echo e(number_format($inv->total_amount, 0)); ?> ৳</span>
                        </td>
                        <td class="py-4">
                            <span class="font-black <?php echo e($inv->due_amount > 0 ? 'text-red-500' : 'text-emerald-500'); ?>">
                                <?php echo e(number_format($inv->due_amount, 0)); ?> ৳
                            </span>
                        </td>
                        <td class="py-4">
                            <span class="px-3 py-1 rounded-full text-[10px] font-black uppercase tracking-tighter <?php echo e($inv->status == 'Paid' ? 'bg-emerald-100 text-emerald-700' : ($inv->status == 'Partial' ? 'bg-amber-100 text-amber-700' : 'bg-red-100 text-red-700')); ?>">
                                <?php echo e($inv->status); ?>

                            </span>
                        </td>
                        <td class="px-8 py-4 text-right">
							<div class="flex justify-end items-center gap-2">
								<button onclick="showInvoiceDetails(<?php echo e($inv->id); ?>)" 
										class="w-9 h-9 flex items-center justify-center rounded-xl bg-blue-50 text-blue-600 hover:bg-blue-600 hover:text-white transition-all shadow-sm" 
										title="বিস্তারিত দেখুন">
									<i class="fas fa-list-ul text-sm"></i>
								</button>

								<a href="<?php echo e(url('admin/invoices/print/'.$inv->id)); ?>" target="_blank" 
								   class="w-9 h-9 flex items-center justify-center rounded-xl bg-indigo-50 text-indigo-600 hover:bg-indigo-600 hover:text-white transition-all shadow-sm" 
								   title="ইনভয়েস প্রিন্ট">
									<i class="fas fa-file-invoice text-sm"></i>
								</a>

								<?php if($inv->status == 'Unpaid'): ?>
									<button onclick="deleteInvoice(<?php echo e($inv->id); ?>)" 
											class="w-9 h-9 flex items-center justify-center rounded-xl bg-red-50 text-red-500 hover:bg-red-600 hover:text-white transition-all shadow-sm" 
											title="ডিলিট করুন">
										<i class="fas fa-trash-alt text-sm"></i>
									</button>
								<?php endif; ?>
							</div>
						</td>
                    </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<div id="detailsModal" class="fixed inset-0 bg-gray-900/50 backdrop-blur-sm hidden z-50 flex items-center justify-center">
    <div class="bg-white rounded-3xl shadow-2xl w-full max-w-lg overflow-hidden animate-in fade-in zoom-in duration-300">
        <div class="p-6 border-b border-gray-50 flex justify-between items-center bg-indigo-600 text-white">
            <h3 class="font-black text-lg">ইনভয়েস ব্রেকডাউন</h3>
            <button onclick="closeModal()" class="hover:rotate-90 transition-all duration-300">
                <i class="fas fa-times"></i>
            </button>
        </div>
        <div class="p-6">
            <div id="modalContent" class="space-y-4">
                <div class="flex justify-center py-10">
                    <i class="fas fa-circle-notch fa-spin text-3xl text-indigo-600"></i>
                </div>
            </div>
        </div>
        <div class="p-4 bg-gray-50 text-right">
            <button onclick="closeModal()" class="px-6 py-2 bg-gray-200 text-gray-700 rounded-xl font-bold hover:bg-gray-300 transition-all">বন্ধ করুন</button>
        </div>
    </div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<script>
    $(document).ready(function() {
        // ১. ডাইনামিক সেকশন ফিল্টারিং লজিক
        $('#class_id').on('change', function() {
            const classId = $(this).val();
            if(classId) {
                $('#section_id').val(''); // রিসেট
                $('.section-option').hide(); // সব হাইড
                $(`.section-option[data-class="${classId}"]`).show(); // শুধু ওই ক্লাসেরগুলো শো
            } else {
                $('.section-option').show();
            }
        });

        // ২. ফর্ম সাবমিট (AJAX)
        $('#genForm').on('submit', function(e) {
            e.preventDefault();
            
            Swal.fire({
                title: 'আপনি কি নিশ্চিত?',
                text: "এই ক্লাস ও সেকশনের ছাত্রদের জন্য ইনভয়েস জেনারেট হবে!",
                icon: 'question',
                showCancelButton: true,
                confirmButtonColor: '#4f46e5',
                confirmButtonText: 'হ্যাঁ, শুরু করুন',
                cancelButtonText: 'বাতিল'
            }).then((result) => {
                if (result.isConfirmed) {
                    $('#btnGen').prop('disabled', true).html('<i class="fas fa-spinner fa-spin"></i> প্রসেসিং...');

                    $.ajax({
                        url: "<?php echo e(route('fee.generate.process')); ?>",
                        method: "POST",
                        data: $(this).serialize(),
                        success: function(res) {
                            if(res.status == 'success') {
                                Swal.fire('সফল!', res.message, 'success').then(() => location.reload());
                            } else {
                                Swal.fire('এরর!', res.message, 'error');
                                $('#btnGen').prop('disabled', false).html('<i class="fas fa-magic"></i> জেনারেট ইনভয়েস');
                            }
                        },
                        error: function() {
                            Swal.fire('এরর!', 'সার্ভারে সমস্যা হয়েছে', 'error');
                            $('#btnGen').prop('disabled', false).html('<i class="fas fa-magic"></i> জেনারেট ইনভয়েস');
                        }
                    });
                }
            });
        });
    });

    function collectPayment(id) {
        // পেমেন্ট কালেকশন মডাল ফাংশনালিটি এখানে হবে
        Swal.fire('তথ্য', 'পেমেন্ট কালেকশন মডিউলটি পরবর্তী ধাপে যুক্ত করা হবে।', 'info');
    }
	
	// ১. মডাল ওপেন এবং ডাটা লোড
	function showInvoiceDetails(id) {
		$('#detailsModal').removeClass('hidden');
		$('#modalContent').html('<div class="flex justify-center py-10"><i class="fas fa-circle-notch fa-spin text-3xl text-indigo-600"></i></div>');

		$.ajax({
			url: "<?php echo e(url('admin/invoices/details')); ?>/" + id,
			method: "GET",
			success: function(res) {
				let html = `
					<div class="bg-indigo-50 p-4 rounded-2xl mb-4">
						<p class="text-xs text-indigo-600 font-bold uppercase">ইনভয়েস নং: ${res.invoice.invoice_no}</p>
						<p class="text-sm text-gray-700">ছাত্র: <b>${res.invoice.student.student_name_en}</b></p>
					</div>
					<table class="w-full text-sm">
						<thead>
							<tr class="text-left text-gray-400 border-b border-gray-100">
								<th class="pb-2">ফি-র ধরন</th>
								<th class="pb-2 text-right">পরিমাণ</th>
							</tr>
						</thead>
						<tbody class="divide-y divide-gray-50">`;
				
				res.items.forEach(item => {
					html += `
						<tr>
							<td class="py-3 font-medium text-gray-700">${item.fee_type.type_name}</td>
							<td class="py-3 text-right font-bold text-gray-900">${parseFloat(item.net_amount).toLocaleString()} ৳</td>
						</tr>`;
				});

				html += `
						</tbody>
						<tfoot>
							<tr class="border-t-2 border-indigo-100">
								<td class="pt-3 font-black text-indigo-600">সর্বমোট</td>
								<td class="pt-3 text-right font-black text-indigo-600 text-lg">${parseFloat(res.invoice.total_amount).toLocaleString()} ৳</td>
							</tr>
						</tfoot>
					</table>`;
				
				$('#modalContent').html(html);
			},
			error: function() {
				$('#modalContent').html('<p class="text-red-500 text-center py-10">তথ্য লোড করতে সমস্যা হয়েছে।</p>');
			}
		});
	}

	// ২. মডাল বন্ধ করা
	function closeModal() {
		$('#detailsModal').addClass('hidden');
	}

	// ৩. ডিলিট ফাংশন (আগের মতোই)
	function deleteInvoice(id) {
		Swal.fire({
			title: 'আপনি কি নিশ্চিত?',
			text: "ডিলিট করলে লেজার এন্ট্রি সহ সব মুছে যাবে!",
			icon: 'warning',
			showCancelButton: true,
			confirmButtonColor: '#ef4444',
			confirmButtonText: 'হ্যাঁ, ডিলিট করুন'
		}).then((result) => {
			if (result.isConfirmed) {
				$.ajax({
					url: "<?php echo e(url('admin/invoices/delete')); ?>/" + id,
					method: "DELETE",
					data: { _token: "<?php echo e(csrf_token()); ?>" },
					success: function(res) {
						Swal.fire('সফল!', res.message, 'success').then(() => location.reload());
					}
				});
			}
		});
	}
</script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.admin', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\xampp\htdocs\kgschool\resources\views/admin/invoices/index.blade.php ENDPATH**/ ?>