<?php $__env->startSection('title', 'ইস্যুকৃত বইয়ের তালিকা'); ?>

<?php $__env->startSection('content'); ?>
<div class="container-fluid">
    <!-- Header -->
    <div class="row mb-3">
        <div class="col-12">
            <div class="card card-primary">
                <div class="card-header bg-primary text-white">
                    <h5 class="mb-0"><i class="fa fa-hand-holding"></i> ইস্যুকৃত বইয়ের তালিকা</h5>
                </div>
                <div class="card-body">
                    <a href="<?php echo e(route('library.index')); ?>" class="btn btn-secondary">
                        <i class="fa fa-arrow-left"></i> ড্যাশবোর্ডে ফিরে যান
                    </a>
                    <a href="<?php echo e(route('library.issues', ['status' => 'issued'])); ?>" class="btn btn-info">
                        <i class="fa fa-book"></i> ইস্যুকৃত
                    </a>
                    <a href="<?php echo e(route('library.issues', ['status' => 'returned'])); ?>" class="btn btn-success">
                        <i class="fa fa-check"></i> ফেরতকৃত
                    </a>
                    <a href="<?php echo e(route('library.issues', ['status' => 'overdue'])); ?>" class="btn btn-danger">
                        <i class="fa fa-exclamation-triangle"></i> মেয়াদোত্তীর্ণ
                    </a>
                    <a href="#" class="btn btn-warning" onclick="window.print()">
                        <i class="fa fa-print"></i> প্রিন্ট
                    </a>
                </div>
            </div>
        </div>
    </div>

    <!-- Search Box -->
    <div class="row mb-3">
        <div class="col-12">
            <div class="card">
                <div class="card-body">
                    <form action="<?php echo e(route('library.issues')); ?>" method="GET" class="form-inline justify-content-center">
                        <div class="form-group mx-2">
                            <label class="mr-2">স্ট্যাটাস:</label>
                            <select name="status" class="form-control">
                                <option value="">সবগুলি</option>
                                <option value="issued" <?php echo e(request('status') == 'issued' ? 'selected' : ''); ?>>ইস্যুকৃত</option>
                                <option value="returned" <?php echo e(request('status') == 'returned' ? 'selected' : ''); ?>>ফেরতকৃত</option>
                                <option value="overdue" <?php echo e(request('status') == 'overdue' ? 'selected' : ''); ?>>মেয়াদোত্তীর্ণ</option>
                            </select>
                        </div>
                        <div class="form-group mx-2">
                            <label class="mr-2">ছাত্র আইডি:</label>
                            <input type="text" name="student_id" class="form-control" value="<?php echo e(request('student_id')); ?>" placeholder="ছাত্র আইডি">
                        </div>
                        <button type="submit" class="btn btn-primary">
                            <i class="fa fa-search"></i> খুঁজুন
                        </button>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <!-- Issues List -->
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header bg-info text-white">
                    <h5 class="mb-0"><i class="fa fa-list"></i> ইস্যুকৃত বইয়ের তালিকা (মোট: <?php echo e($issues->total()); ?>টি)</h5>
                </div>
                <div class="card-body">
                    <?php if($issues->isEmpty()): ?>
                        <div class="alert alert-info text-center">
                            <i class="fa fa-info-circle fa-2x"></i>
                            <h5>কোনো ইস্যু পাওয়া যায়নি</h5>
                        </div>
                    <?php else: ?>
                        <div class="table-responsive">
                            <table class="table table-bordered table-striped table-hover">
                                <thead class="bg-secondary text-white">
                                    <tr>
                                        <th>ক্রমিক</th>
                                        <th>বই</th>
                                        <th>ইস্যুকৃত</th>
                                        <th>ইস্যুর তারিখ</th>
                                        <th>ফেরতের তারিখ</th>
                                        <th>অবশিষ্ট দিন</th>
                                        <th>স্ট্যাটাস</th>
                                        <th>জরিমানা</th>
                                        <th width="150">একশন</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php $__currentLoopData = $issues; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $index => $issue): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <?php
                                        $daysLeft = now()->diffInDays($issue->due_date, false);
                                        $isOverdue = $daysLeft < 0 && $issue->status == 'issued';
                                    ?>
                                    <tr class="<?php echo e($isOverdue ? 'bg-light-danger' : ''); ?>">
                                        <td class="text-center"><?php echo e($issues->firstItem() + $index); ?></td>
                                        <td>
                                            <strong><?php echo e($issue->book->title_bn ?? $issue->book->title); ?></strong>
                                            <br><small>ID: <?php echo e($issue->book->book_id); ?></small>
                                        </td>
                                        <td>
                                            <?php if($issue->student): ?>
                                                <?php echo e($issue->student->name_bn); ?>

                                                <br><small><?php echo e($issue->student->student_id); ?></small>
                                            <?php elseif($issue->teacher): ?>
                                                <?php echo e($issue->teacher->name_bn); ?>

                                                <br><small><?php echo e($issue->teacher->teacher_id); ?></small>
                                            <?php endif; ?>
                                        </td>
                                        <td><?php echo e($issue->issue_date->format('d-m-Y')); ?></td>
                                        <td><?php echo e($issue->due_date->format('d-m-Y')); ?></td>
                                        <td class="text-center">
                                            <?php if($issue->status == 'returned'): ?>
                                                <span class="badge badge-success">ফেরত দেওয়া হয়েছে</span>
                                            <?php elseif($daysLeft > 0): ?>
                                                <span class="badge badge-info"><?php echo e($daysLeft); ?> দিন</span>
                                            <?php elseif($daysLeft == 0): ?>
                                                <span class="badge badge-warning">আজকে ফেরত দিন</span>
                                            <?php else: ?>
                                                <span class="badge badge-danger"><?php echo e(abs($daysLeft)); ?> দিন দেরি</span>
                                            <?php endif; ?>
                                        </td>
                                        <td>
                                            <?php if($issue->status == 'returned'): ?>
                                                <span class="badge badge-success">ফেরতকৃত</span>
                                            <?php elseif($issue->status == 'issued'): ?>
                                                <span class="badge badge-primary">ইস্যুকৃত</span>
                                            <?php elseif($issue->status == 'overdue'): ?>
                                                <span class="badge badge-danger">মেয়াদোত্তীর্ণ</span>
                                            <?php elseif($issue->status == 'lost'): ?>
                                                <span class="badge badge-dark">হারিয়ে গেছে</span>
                                            <?php endif; ?>
                                        </td>
                                        <td class="text-right">
                                            <?php if($issue->fine_amount > 0): ?>
                                                ৳ <?php echo e(number_format($issue->fine_amount, 2)); ?>

                                                <?php if($issue->fine_paid): ?>
                                                    <br><span class="badge badge-success">পরিশোধিত</span>
                                                <?php else: ?>
                                                    <br><span class="badge badge-warning">বকেয়া</span>
                                                <?php endif; ?>
                                            <?php else: ?>
                                                -
                                            <?php endif; ?>
                                        </td>
                                        <td>
                                            <?php if($issue->status != 'returned'): ?>
                                                <button class="btn btn-sm btn-success return-book" 
                                                        data-id="<?php echo e($issue->id); ?>"
                                                        data-book="<?php echo e($issue->book->title_bn ?? $issue->book->title); ?>"
                                                        data-name="<?php echo e($issue->student->name_bn ?? $issue->teacher->name_bn); ?>">
                                                    <i class="fa fa-undo"></i> ফেরত
                                                </button>
                                            <?php endif; ?>
                                            <?php if($issue->fine_amount > 0 && !$issue->fine_paid): ?>
                                                <button class="btn btn-sm btn-warning pay-fine" data-id="<?php echo e($issue->id); ?>">
                                                    <i class="fa fa-money-bill"></i> জরিমানা
                                                </button>
                                            <?php endif; ?>
                                        </td>
                                    </tr>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </tbody>
                            </table>
                        </div>
                        
                        <!-- Pagination -->
                        <div class="mt-3">
                            <?php echo e($issues->appends(request()->query())->links()); ?>

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

<!-- Return Book Modal -->
<div class="modal fade" id="returnModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-success text-white">
                <h5 class="modal-title">বই ফেরত নেওয়া</h5>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>
            <form action="" method="POST" id="returnForm">
                <?php echo csrf_field(); ?>
                <div class="modal-body">
                    <p><strong>বই:</strong> <span id="bookName"></span></p>
                    <p><strong>ইস্যুকৃত:</strong> <span id="userName"></span></p>
                    
                    <div class="form-check mb-3">
                        <input type="checkbox" class="form-check-input" name="calculate_fine" id="calculateFine" value="1">
                        <label class="form-check-label" for="calculateFine">
                            জরিমানা হিসাব করুন
                        </label>
                    </div>
                    
                    <div class="form-group">
                        <label>মন্তব্য</label>
                        <textarea name="remarks" class="form-control" rows="2"></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">বাতিল</button>
                    <button type="submit" class="btn btn-success">বই ফেরত নিন</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Pay Fine Modal -->
<div class="modal fade" id="fineModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-warning">
                <h5 class="modal-title">জরিমানা পরিশোধ</h5>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>
            <form action="" method="POST" id="fineForm">
                <?php echo csrf_field(); ?>
                <div class="modal-body">
                    <div class="form-group">
                        <label>পরিশোধের মাধ্যম</label>
                        <select name="payment_method" class="form-control" required>
                            <option value="cash">নগদ</option>
                            <option value="bKash">বিকাশ</option>
                            <option value="nagad">নগদ</option>
                        </select>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">বাতিল</button>
                    <button type="submit" class="btn btn-warning">জরিমানা পরিশোধ করুন</button>
                </div>
            </form>
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('scripts'); ?>
<script>
    $(document).ready(function() {
        // Return book
        $('.return-book').click(function() {
            var id = $(this).data('id');
            var book = $(this).data('book');
            var name = $(this).data('name');
            
            $('#bookName').text(book);
            $('#userName').text(name);
            $('#returnForm').attr('action', '<?php echo e(url("library/return")); ?>/' + id);
            
            $('#returnModal').modal('show');
        });
        
        // Pay fine
        $('.pay-fine').click(function() {
            var id = $(this).data('id');
            $('#fineForm').attr('action', '<?php echo e(url("library/fine/pay")); ?>/' + id);
            $('#fineModal').modal('show');
        });
    });
</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/school.naowarinternational.my.id/resources/views/library/issues.blade.php ENDPATH**/ ?>