@extends('layouts.app')

@section('title', 'ইস্যুকৃত বইয়ের তালিকা')

@section('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="{{ route('library.index') }}" class="btn btn-secondary">
                        <i class="fa fa-arrow-left"></i> ড্যাশবোর্ডে ফিরে যান
                    </a>
                    <a href="{{ route('library.issues', ['status' => 'issued']) }}" class="btn btn-info">
                        <i class="fa fa-book"></i> ইস্যুকৃত
                    </a>
                    <a href="{{ route('library.issues', ['status' => 'returned']) }}" class="btn btn-success">
                        <i class="fa fa-check"></i> ফেরতকৃত
                    </a>
                    <a href="{{ 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="{{ 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" {{ request('status') == 'issued' ? 'selected' : '' }}>ইস্যুকৃত</option>
                                <option value="returned" {{ request('status') == 'returned' ? 'selected' : '' }}>ফেরতকৃত</option>
                                <option value="overdue" {{ 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="{{ 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> ইস্যুকৃত বইয়ের তালিকা (মোট: {{ $issues->total() }}টি)</h5>
                </div>
                <div class="card-body">
                    @if($issues->isEmpty())
                        <div class="alert alert-info text-center">
                            <i class="fa fa-info-circle fa-2x"></i>
                            <h5>কোনো ইস্যু পাওয়া যায়নি</h5>
                        </div>
                    @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>
                                    @foreach($issues as $index => $issue)
                                    @php
                                        $daysLeft = now()->diffInDays($issue->due_date, false);
                                        $isOverdue = $daysLeft < 0 && $issue->status == 'issued';
                                    @endphp
                                    <tr class="{{ $isOverdue ? 'bg-light-danger' : '' }}">
                                        <td class="text-center">{{ $issues->firstItem() + $index }}</td>
                                        <td>
                                            <strong>{{ $issue->book->title_bn ?? $issue->book->title }}</strong>
                                            <br><small>ID: {{ $issue->book->book_id }}</small>
                                        </td>
                                        <td>
                                            @if($issue->student)
                                                {{ $issue->student->name_bn }}
                                                <br><small>{{ $issue->student->student_id }}</small>
                                            @elseif($issue->teacher)
                                                {{ $issue->teacher->name_bn }}
                                                <br><small>{{ $issue->teacher->teacher_id }}</small>
                                            @endif
                                        </td>
                                        <td>{{ $issue->issue_date->format('d-m-Y') }}</td>
                                        <td>{{ $issue->due_date->format('d-m-Y') }}</td>
                                        <td class="text-center">
                                            @if($issue->status == 'returned')
                                                <span class="badge badge-success">ফেরত দেওয়া হয়েছে</span>
                                            @elseif($daysLeft > 0)
                                                <span class="badge badge-info">{{ $daysLeft }} দিন</span>
                                            @elseif($daysLeft == 0)
                                                <span class="badge badge-warning">আজকে ফেরত দিন</span>
                                            @else
                                                <span class="badge badge-danger">{{ abs($daysLeft) }} দিন দেরি</span>
                                            @endif
                                        </td>
                                        <td>
                                            @if($issue->status == 'returned')
                                                <span class="badge badge-success">ফেরতকৃত</span>
                                            @elseif($issue->status == 'issued')
                                                <span class="badge badge-primary">ইস্যুকৃত</span>
                                            @elseif($issue->status == 'overdue')
                                                <span class="badge badge-danger">মেয়াদোত্তীর্ণ</span>
                                            @elseif($issue->status == 'lost')
                                                <span class="badge badge-dark">হারিয়ে গেছে</span>
                                            @endif
                                        </td>
                                        <td class="text-right">
                                            @if($issue->fine_amount > 0)
                                                ৳ {{ number_format($issue->fine_amount, 2) }}
                                                @if($issue->fine_paid)
                                                    <br><span class="badge badge-success">পরিশোধিত</span>
                                                @else
                                                    <br><span class="badge badge-warning">বকেয়া</span>
                                                @endif
                                            @else
                                                -
                                            @endif
                                        </td>
                                        <td>
                                            @if($issue->status != 'returned')
                                                <button class="btn btn-sm btn-success return-book" 
                                                        data-id="{{ $issue->id }}"
                                                        data-book="{{ $issue->book->title_bn ?? $issue->book->title }}"
                                                        data-name="{{ $issue->student->name_bn ?? $issue->teacher->name_bn }}">
                                                    <i class="fa fa-undo"></i> ফেরত
                                                </button>
                                            @endif
                                            @if($issue->fine_amount > 0 && !$issue->fine_paid)
                                                <button class="btn btn-sm btn-warning pay-fine" data-id="{{ $issue->id }}">
                                                    <i class="fa fa-money-bill"></i> জরিমানা
                                                </button>
                                            @endif
                                        </td>
                                    </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                        
                        <!-- Pagination -->
                        <div class="mt-3">
                            {{ $issues->appends(request()->query())->links() }}
                        </div>
                    @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">
                @csrf
                <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">
                @csrf
                <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>
@endsection

@section('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', '{{ url("library/return") }}/' + id);
            
            $('#returnModal').modal('show');
        });
        
        // Pay fine
        $('.pay-fine').click(function() {
            var id = $(this).data('id');
            $('#fineForm').attr('action', '{{ url("library/fine/pay") }}/' + id);
            $('#fineModal').modal('show');
        });
    });
</script>
@endsection