@extends('layouts.app')

@section('title', 'ফি/বিল ব্যবস্থাপনা')

@section('content')
<div class="container-fluid">
    <!-- Search Section - NESCO Style -->
    <div class="row">
        <div class="col-12">
            <div class="card text-center card-primary card-outline">
                <div class="card-header bg-primary text-white">
                    <h5 class="mb-0"><i class="fa fa-search"></i> ফি/বিল অনুসন্ধান</h5>
                </div>
                <div class="card-body" style="background-color: #f0f9ff;">
                    <form class="form-horizontal" method="POST" action="{{ route('fees.search') }}">
                        @csrf
                        <div class="form-group row justify-content-center">
                            <div class="col-md-3">
                                <label class="font-weight-bold">গ্রাহক/ছাত্র আইডি</label>
                                <input type="text" 
                                       class="form-control form-control-lg text-center @error('student_id') is-invalid @enderror" 
                                       name="student_id" 
                                       placeholder="যেমন: 2026001"
                                       value="{{ old('student_id') }}"
                                       required>
                                @error('student_id')
                                    <span class="invalid-feedback">{{ $message }}</span>
                                @enderror
                            </div>
                        </div>
                        
                        <div class="form-group row justify-content-center mt-3">
                            <div class="col-md-8">
                                <button type="submit" name="action" value="due" class="btn btn-danger btn-lg">
                                    <i class="fa fa-exclamation-triangle"></i> বকেয়া ফি
                                </button>
                                <button type="submit" name="action" value="all" class="btn btn-info btn-lg">
                                    <i class="fa fa-list"></i> সকল ফি
                                </button>
                                <button type="submit" name="action" value="summary" class="btn btn-success btn-lg">
                                    <i class="fa fa-chart-pie"></i> ফি সামারি রিপোর্ট
                                </button>
                                <button type="button" id="reset_button" class="btn btn-secondary btn-lg">
                                    <i class="fa fa-undo"></i> রিসেট
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <!-- Quick Actions -->
    <div class="row mt-3">
        <div class="col-md-6">
            <div class="card card-success">
                <div class="card-header">
                    <h5 class="mb-0"><i class="fa fa-bolt"></i> দ্রুত অ্যাকশন</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <button type="button" class="btn btn-outline-primary btn-block mb-2" data-toggle="modal" data-target="#bulkFeeModal">
                                <i class="fa fa-layer-group"></i> মাসিক ফি জেনারেট
                            </button>
                        </div>
                        <div class="col-md-6">
                            <a href="{{ route('fees.due-report') }}" class="btn btn-outline-danger btn-block mb-2">
                                <i class="fa fa-file-invoice"></i> বকেয়া রিপোর্ট
                            </a>
                        </div>
                        <div class="col-md-6">
                            <a href="{{ route('fees.collection-report', ['from_date' => date('Y-m-01'), 'to_date' => date('Y-m-d')]) }}" 
                               class="btn btn-outline-success btn-block mb-2">
                                <i class="fa fa-chart-line"></i> কালেকশন রিপোর্ট
                            </a>
                        </div>
                        <div class="col-md-6">
                            <button type="button" class="btn btn-outline-info btn-block mb-2" onclick="window.print()">
                                <i class="fa fa-print"></i> প্রিন্ট
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Today's Collection -->
        <div class="col-md-6">
            <div class="card card-warning">
                <div class="card-header">
                    <h5 class="mb-0"><i class="fa fa-clock"></i> আজকের কালেকশন</h5>
                </div>
                <div class="card-body">
                    @php
                        $todayCollection = App\Models\Payment::whereDate('payment_date', today())->sum('amount');
                        $todayCount = App\Models\Payment::whereDate('payment_date', today())->count();
                    @endphp
                    <h3 class="text-success">{{ number_format($todayCollection, 2) }} টাকা</h3>
                    <p>মোট {{ $todayCount }} টি লেনদেন</p>
                </div>
            </div>
        </div>
    </div>

    <!-- Recent Payments -->
    @if(isset($recentPayments) && $recentPayments->count() > 0)
    <div class="row mt-3">
        <div class="col-12">
            <div class="card">
                <div class="card-header bg-info text-white">
                    <h5 class="mb-0"><i class="fa fa-history"></i> সাম্প্রতিক পেমেন্ট</h5>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-bordered table-striped">
                            <thead class="bg-secondary text-white">
                                <tr>
                                    <th>ছাত্র আইডি</th>
                                    <th>নাম</th>
                                    <th>পরিমাণ</th>
                                    <th>পদ্ধতি</th>
                                    <th>তারিখ</th>
                                    <th>রশিদ</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($recentPayments as $fee)
                                <tr>
                                    <td>{{ $fee->student->student_id }}</td>
                                    <td>{{ $fee->student->name_bn }}</td>
                                    <td class="text-success font-weight-bold">{{ number_format($fee->paid_amount, 2) }}</td>
                                    <td>{{ $fee->payment_method }}</td>
                                    <td>{{ $fee->updated_at->format('d-m-Y h:i A') }}</td>
                                    <td>
                                        <a href="{{ route('fees.receipt', $fee->id) }}" class="btn btn-sm btn-info" target="_blank">
                                            <i class="fa fa-download"></i> রশিদ
                                        </a>
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    @endif

    <!-- Bulk Fee Generation Modal -->
    <div class="modal fade" id="bulkFeeModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header bg-primary text-white">
                    <h5 class="modal-title">মাসিক ফি জেনারেট</h5>
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                </div>
                <form action="{{ route('fees.bulk-generate') }}" method="POST">
                    @csrf
                    <div class="modal-body">
                        <div class="form-group">
                            <label>শ্রেণি <span class="text-danger">*</span></label>
                            <select name="class_id" class="form-control" required>
                                <option value="">শ্রেণি নির্বাচন করুন</option>
                                @foreach($classes as $class)
                                <option value="{{ $class->id }}">
                                    {{ $class->class_name_bn }} @if($class->section)({{ $class->section }})@endif
                                </option>
                                @endforeach
                            </select>
                        </div>
                        <div class="form-group">
                            <label>মাস <span class="text-danger">*</span></label>
                            <select name="month" class="form-control" required>
                                <option value="">মাস নির্বাচন করুন</option>
                                @foreach(range(1, 12) as $m)
                                <option value="{{ $m }}" {{ $m == date('n') ? 'selected' : '' }}>
                                    {{ DateTime::createFromFormat('!m', $m)->format('F') }}
                                </option>
                                @endforeach
                            </select>
                        </div>
                        <div class="form-group">
                            <label>বছর <span class="text-danger">*</span></label>
                            <select name="year" class="form-control" required>
                                @foreach(range(date('Y')-1, date('Y')+1) as $y)
                                <option value="{{ $y }}" {{ $y == date('Y') ? 'selected' : '' }}>{{ $y }}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">বাতিল</button>
                        <button type="submit" class="btn btn-primary">জেনারেট করুন</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
@endsection

@section('scripts')
<script>
    $(document).ready(function() {
        $('#reset_button').click(function() {
            $('input[name="student_id"]').val('');
        });
        
        // Form target based on action
        $('form').submit(function(e) {
            var action = $(document.activeElement).val();
            if (action === 'summary') {
                $(this).attr('target', '_blank');
            } else {
                $(this).attr('target', '_self');
            }
        });
    });
</script>
@endsection