@extends('layouts.app')

@section('title', 'শিক্ষক বেতন')

@section('content')
<div class="container-fluid">
    <!-- Month Selection -->
    <div class="row mb-3">
        <div class="col-12">
            <div class="card">
                <div class="card-body">
                    <form action="{{ route('teachers.salary') }}" method="GET" class="form-inline justify-content-center">
                        <div class="form-group mx-2">
                            <label class="mr-2">মাস:</label>
                            <select name="month" class="form-control">
                                @foreach(range(1, 12) as $m)
                                <option value="{{ $m }}" {{ $currentMonth == $m ? 'selected' : '' }}>
                                    {{ DateTime::createFromFormat('!m', $m)->format('F') }}
                                </option>
                                @endforeach
                            </select>
                        </div>
                        <div class="form-group mx-2">
                            <label class="mr-2">বছর:</label>
                            <select name="year" class="form-control">
                                @foreach(range(date('Y')-2, date('Y')+1) as $y)
                                <option value="{{ $y }}" {{ $currentYear == $y ? 'selected' : '' }}>{{ $y }}</option>
                                @endforeach
                            </select>
                        </div>
                        <button type="submit" class="btn btn-primary mx-2">
                            <i class="fa fa-search"></i> দেখুন
                        </button>
                        <a href="{{ route('teachers.salary.generate') }}" class="btn btn-success mx-2" onclick="return confirm('মাসিক বেতন জেনারেট করবেন?')">
                            <i class="fa fa-cog"></i> বেতন জেনারেট
                        </a>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <!-- Salary Table -->
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header bg-primary text-white">
                    <h5 class="mb-0"><i class="fa fa-money-bill"></i> {{ DateTime::createFromFormat('!m', $currentMonth)->format('F') }} {{ $currentYear }} মাসের বেতন</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>
                                    <th>নেট পেবল</th>
                                    <th>পরিশোধিত</th>
                                    <th>বকেয়া</th>
                                    <th>স্ট্যাটাস</th>
                                    <th width="200">একশন</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($teachers as $index => $teacher)
                                @php
                                    $salary = $salaries[$teacher->id] ?? null;
                                @endphp
                                <tr>
                                    <td>{{ $index + 1 }}</td>
                                    <td>{{ $teacher->teacher_id }}</td>
                                    <td>{{ $teacher->name_bn }}</td>
                                    <td>{{ Teacher::getDesignations()[$teacher->designation] ?? $teacher->designation }}</td>
                                    <td class="text-right">{{ number_format($teacher->total_salary, 2) }}</td>
                                    <td class="text-right">{{ $salary ? number_format($salary->deduction, 2) : '0.00' }}</td>
                                    <td class="text-right font-weight-bold">{{ $salary ? number_format($salary->net_payable, 2) : number_format($teacher->total_salary, 2) }}</td>
                                    <td class="text-right text-success">{{ $salary ? number_format($salary->paid_amount, 2) : '0.00' }}</td>
                                    <td class="text-right {{ $salary && $salary->due_amount > 0 ? 'text-danger font-weight-bold' : '' }}">
                                        {{ $salary ? number_format($salary->due_amount, 2) : number_format($teacher->total_salary, 2) }}
                                    </td>
                                    <td>
                                        @if(!$salary)
                                            <span class="badge badge-warning">জেনারেট হয়নি</span>
                                        @elseif($salary->payment_status == 'paid')
                                            <span class="badge badge-success">পরিশোধিত</span>
                                        @elseif($salary->payment_status == 'partial')
                                            <span class="badge badge-info">আংশিক</span>
                                        @else
                                            <span class="badge badge-danger">বকেয়া</span>
                                        @endif
                                    </td>
                                    <td>
                                        @if($salary && $salary->due_amount > 0)
                                            <button class="btn btn-sm btn-success pay-salary" data-id="{{ $salary->id }}" data-name="{{ $teacher->name_bn }}" data-amount="{{ $salary->due_amount }}">
                                                <i class="fa fa-credit-card"></i> পরিশোধ
                                            </button>
                                        @endif
                                        
                                        @if($salary && $salary->paid_amount > 0)
                                            <a href="{{ route('teachers.salary.slip', $salary->id) }}" class="btn btn-sm btn-info" target="_blank">
                                                <i class="fa fa-download"></i> স্লিপ
                                            </a>
                                        @endif
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Payment Modal -->
<div class="modal fade" id="paymentModal" 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="paymentForm">
                @csrf
                <div class="modal-body">
                    <p><strong>শিক্ষক:</strong> <span id="teacherName"></span></p>
                    <p><strong>বকেয়া:</strong> <span id="dueAmount"></span> টাকা</p>
                    
                    <div class="form-group">
                        <label>পরিশোধের পরিমাণ</label>
                        <input type="number" name="amount" id="amount" class="form-control" step="0.01" required>
                    </div>
                    
                    <div class="form-group">
                        <label>পদ্ধতি</label>
                        <select name="payment_method" class="form-control" required>
                            <option value="cash">নগদ</option>
                            <option value="bKash">বিকাশ</option>
                            <option value="bank">ব্যাংক</option>
                            <option value="check">চেক</option>
                        </select>
                    </div>
                    
                    <div class="form-group">
                        <label>লেনদেন আইডি</label>
                        <input type="text" name="transaction_id" class="form-control">
                    </div>
                    
                    <div class="form-group">
                        <label>পরিশোধের তারিখ</label>
                        <input type="date" name="payment_date" class="form-control" value="{{ date('Y-m-d') }}" required>
                    </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>

@section('scripts')
<script>
    $(document).ready(function() {
        $('.pay-salary').click(function() {
            var id = $(this).data('id');
            var name = $(this).data('name');
            var amount = $(this).data('amount');
            
            $('#teacherName').text(name);
            $('#dueAmount').text(amount);
            $('#amount').val(amount).attr('max', amount);
            $('#paymentForm').attr('action', '{{ route("teachers.salary.pay", "") }}/' + id);
            
            $('#paymentModal').modal('show');
        });
    });
</script>
@endsection
@endsection