@extends('layouts.app')

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header">
                    <div class="d-flex justify-content-between align-items-center">
                        <h5 class="mb-0">
                            <i class="fas fa-file-invoice-dollar"></i> ঋণ বিবরণী
                        </h5>
                        <div>
                            <a href="{{ route('loans.index') }}" class="btn btn-sm btn-secondary">
                                <i class="fas fa-arrow-left"></i> ফিরে যান
                            </a>
                            @if(in_array($loan->status, ['pending', 'rejected']))
                                <a href="{{ route('loans.edit', $loan) }}" class="btn btn-sm btn-warning">
                                    <i class="fas fa-edit"></i> সম্পাদনা
                                </a>
                            @endif
                            @if($loan->status == 'pending')
                                <button type="button" class="btn btn-sm btn-success" data-bs-toggle="modal" data-bs-target="#approveModal">
                                    <i class="fas fa-check"></i> অনুমোদন
                                </button>
                                <button type="button" class="btn btn-sm btn-danger" data-bs-toggle="modal" data-bs-target="#rejectModal">
                                    <i class="fas fa-times"></i> প্রত্যাখ্যান
                                </button>
                            @endif
                            @if($loan->status == 'approved')
                                <button type="button" class="btn btn-sm btn-primary" data-bs-toggle="modal" data-bs-target="#disburseModal">
                                    <i class="fas fa-money-bill-wave"></i> বিতরণ
                                </button>
                            @endif
                        </div>
                    </div>
                </div>
                
                <div class="card-body">
                    <!-- Success/Error Messages -->
                    @if(session('success'))
                        <div class="alert alert-success alert-dismissible fade show" role="alert">
                            <i class="fas fa-check-circle"></i> {{ session('success') }}
                            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                        </div>
                    @endif
                    
                    @if(session('error'))
                        <div class="alert alert-danger alert-dismissible fade show" role="alert">
                            <i class="fas fa-exclamation-circle"></i> {{ session('error') }}
                            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                        </div>
                    @endif
                    
                    <div class="row">
                        <!-- Loan Information -->
                        <div class="col-md-6">
                            <div class="card mb-4">
                                <div class="card-header bg-primary text-white">
                                    <h6 class="mb-0"><i class="fas fa-info-circle"></i> ঋণ তথ্য</h6>
                                </div>
                                <div class="card-body">
                                    <table class="table table-bordered">
                                        <tr>
                                            <th width="40%">লোন নম্বর:</th>
                                            <td><strong class="text-primary">{{ $loan->loan_number }}</strong></td>
                                        </tr>
                                        <tr>
                                            <th>সদস্য:</th>
                                            <td>{{ $loan->member->name ?? 'N/A' }} (ID: {{ $loan->member->member_id ?? 'N/A' }})</td>
                                        </tr>
                                        <tr>
                                            <th>ঋণের ধরন:</th>
                                            <td>
                                                <span class="badge bg-info">{{ $loan->loan_type_in_bangla }}</span>
                                            </td>
                                        </tr>
                                        <tr>
                                            <th>ঋণের অবস্থা:</th>
                                            <td>
                                                <span class="{{ $loan->status_class }}">{{ $loan->status_in_bangla }}</span>
                                            </td>
                                        </tr>
                                        <tr>
                                            <th>ঋণের পরিমাণ:</th>
                                            <td><strong class="text-success">৳ {{ $loan->formatted_loan_amount }}</strong></td>
                                        </tr>
                                        <tr>
                                            <th>সুদ হার:</th>
                                            <td>{{ $loan->interest_rate }}% (মাসিক)</td>
                                        </tr>
                                        <tr>
                                            <th>মেয়াদ:</th>
                                            <td>{{ $loan->duration_months }} মাস (৩ মাসের জন্য)</td>
                                        </tr>
                                        <tr>
                                            <th>আসল + সুদ:</th>
                                            <td><strong>৳ {{ $loan->formatted_total_payable }}</strong></td>
                                        </tr>
                                        <tr>
                                            <th>মোট সুদ:</th>
                                            <td>৳ {{ $loan->formatted_total_interest }}</td>
                                        </tr>
                                        <tr>
                                            <th>লোনের তারিখ:</th>
                                            <td>{{ \Carbon\Carbon::parse($loan->loan_date)->format('d/m/Y') }}</td>
                                        </tr>
                                        <tr>
                                            <th>শুরু তারিখ:</th>
                                            <td>{{ \Carbon\Carbon::parse($loan->start_date)->format('d/m/Y') }}</td>
                                        </tr>
                                        <tr>
                                            <th>শেষ তারিখ:</th>
                                            <td>{{ \Carbon\Carbon::parse($loan->end_date)->format('d/m/Y') }}</td>
                                        </tr>
                                    </table>
                                </div>
                            </div>
                        </div>
                        
                        <!-- Payment Information -->
                        <div class="col-md-6">
                            <div class="card mb-4">
                                <div class="card-header bg-success text-white">
                                    <h6 class="mb-0"><i class="fas fa-money-bill-wave"></i> অর্থ সংক্রান্ত তথ্য</h6>
                                </div>
                                <div class="card-body">
                                    <table class="table table-bordered">
                                        <tr>
                                            <th width="40%">পরিশোধিত:</th>
                                            <td>
                                                <strong class="text-success">৳ {{ $loan->formatted_paid_amount }}</strong>
                                                @if($loan->paid_amount > 0)
                                                    <div class="progress mt-2" style="height: 10px;">
                                                        <div class="progress-bar bg-success" role="progressbar" 
                                                             style="width: {{ ($loan->paid_amount / $loan->total_payable) * 100 }}%"
                                                             aria-valuenow="{{ ($loan->paid_amount / $loan->total_payable) * 100 }}" 
                                                             aria-valuemin="0" aria-valuemax="100">
                                                        </div>
                                                    </div>
                                                @endif
                                            </td>
                                        </tr>
                                        <tr>
                                            <th>বাকি:</th>
                                            <td><strong class="text-danger">৳ {{ $loan->formatted_remaining_balance }}</strong></td>
                                        </tr>
                                        <tr>
                                            <th>বকেয়া:</th>
                                            <td><strong class="text-warning">৳ {{ $loan->formatted_due_amount }}</strong></td>
                                        </tr>
                                        <tr>
                                            <th>গড় মাসিক কিস্তি:</th>
                                            <td>৳ {{ $loan->formatted_monthly_installment }}</td>
                                        </tr>
                                        @if($loan->next_due_date)
                                        <tr>
                                            <th>পরবর্তী কিস্তি তারিখ:</th>
                                            <td>
                                                <span class="badge bg-info">
                                                    {{ \Carbon\Carbon::parse($loan->next_due_date)->format('d/m/Y') }}
                                                </span>
                                            </td>
                                        </tr>
                                        @endif
                                    </table>
                                </div>
                            </div>
                            
                            <!-- Guarantor Information -->
                            <div class="card mb-4">
                                <div class="card-header bg-warning text-dark">
                                    <h6 class="mb-0"><i class="fas fa-user-shield"></i> গ্যারান্টর তথ্য</h6>
                                </div>
                                <div class="card-body">
                                    <table class="table table-bordered">
                                        <tr>
                                            <th width="40%">গ্যারান্টর ১:</th>
                                            <td>
                                                {{ $loan->guarantor1->name ?? 'N/A' }}
                                                @if($loan->guarantor1)
                                                    <br><small class="text-muted">ID: {{ $loan->guarantor1->member_id }}</small>
                                                @endif
                                            </td>
                                        </tr>
                                        <tr>
                                            <th>গ্যারান্টর ২:</th>
                                            <td>
                                                {{ $loan->guarantor2->name ?? 'N/A' }}
                                                @if($loan->guarantor2)
                                                    <br><small class="text-muted">ID: {{ $loan->guarantor2->member_id }}</small>
                                                @endif
                                            </td>
                                        </tr>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <!-- Purpose and Notes -->
                    <div class="row">
                        <div class="col-md-12">
                            <div class="card mb-4">
                                <div class="card-header bg-secondary text-white">
                                    <h6 class="mb-0"><i class="fas fa-sticky-note"></i> উদ্দেশ্য ও মন্তব্য</h6>
                                </div>
                                <div class="card-body">
                                    <h6>ঋণের উদ্দেশ্য:</h6>
                                    <p>{{ $loan->purpose }}</p>
                                    
                                    @if($loan->notes)
                                    <h6 class="mt-3">মন্তব্য:</h6>
                                    <p>{{ $loan->notes }}</p>
                                    @endif
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <!-- Installments Schedule -->
                    <div class="row">
                        <div class="col-md-12">
                            <div class="card">
                                <div class="card-header bg-info text-white">
                                    <div class="d-flex justify-content-between align-items-center">
                                        <h6 class="mb-0"><i class="fas fa-calendar-alt"></i> কিস্তির সময়সূচী</h6>
                                        @if(in_array($loan->status, ['disbursed', 'active']) && $loan->due_amount > 0)
                                            <button type="button" class="btn btn-sm btn-success" data-bs-toggle="modal" data-bs-target="#paymentModal">
                                                <i class="fas fa-money-check-alt"></i> কিস্তি পরিশোধ
                                            </button>
                                        @endif
                                    </div>
                                </div>
                                <div class="card-body">
                                    <div class="table-responsive">
                                        <table class="table table-bordered table-hover">
                                            <thead class="table-light">
                                                <tr>
                                                    <th>#</th>
                                                    <th>কিস্তির ধরন</th>
                                                    <th>আসল</th>
                                                    <th>সুদ</th>
                                                    <th>মোট</th>
                                                    <th>নির্ধারিত তারিখ</th>
                                                    <th>পরিশোধের তারিখ</th>
                                                    <th>অবস্থা</th>
                                                    <th>পরিশোধিত</th>
                                                    <th>কার্যক্রম</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                @foreach($installments as $installment)
                                                <tr>
                                                    <td>{{ $installment->installment_number }}</td>
                                                    <td>
                                                        @if($installment->installment_number == 1 || $installment->installment_number == 2)
                                                            শুধু মুনাফা
                                                        @else
                                                            মুনাফা + আসল
                                                        @endif
                                                    </td>
                                                    <td>৳ {{ number_format($installment->principal_amount, 2) }}</td>
                                                    <td>৳ {{ number_format($installment->interest_amount, 2) }}</td>
                                                    <td><strong>৳ {{ number_format($installment->amount, 2) }}</strong></td>
                                                    <td>
                                                        {{ \Carbon\Carbon::parse($installment->due_date)->format('d/m/Y') }}
                                                        @if($installment->due_date < now() && $installment->status == 'pending')
                                                            <br><span class="badge bg-danger">বকেয়া</span>
                                                        @endif
                                                    </td>
                                                    <td>
                                                        @if($installment->payment_date)
                                                            {{ \Carbon\Carbon::parse($installment->payment_date)->format('d/m/Y') }}
                                                        @else
                                                            -
                                                        @endif
                                                    </td>
                                                    <td>
                                                        @if($installment->status == 'paid')
                                                            <span class="badge bg-success">পরিশোধিত</span>
                                                        @elseif($installment->status == 'partially_paid')
                                                            <span class="badge bg-warning">আংশিক</span>
                                                        @else
                                                            <span class="badge bg-danger">বকেয়া</span>
                                                        @endif
                                                    </td>
                                                    <td>৳ {{ number_format($installment->paid_amount, 2) }}</td>
                                                    <td>
                                                        @if($installment->status != 'paid')
                                                            <button type="button" class="btn btn-sm btn-success pay-installment-btn" 
                                                                    data-installment-id="{{ $installment->id }}"
                                                                    data-installment-amount="{{ $installment->amount }}">
                                                                <i class="fas fa-money-check-alt"></i> পরিশোধ
                                                            </button>
                                                        @endif
                                                    </td>
                                                </tr>
                                                @endforeach
                                            </tbody>
                                            <tfoot>
                                                <tr class="table-secondary">
                                                    <th colspan="3">মোট</th>
                                                    <th>৳ {{ number_format($installments->sum('interest_amount'), 2) }}</th>
                                                    <th>৳ {{ number_format($installments->sum('amount'), 2) }}</th>
                                                    <th colspan="3"></th>
                                                    <th>৳ {{ number_format($installments->sum('paid_amount'), 2) }}</th>
                                                    <th></th>
                                                </tr>
                                            </tfoot>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Approve Modal -->
<div class="modal fade" id="approveModal" tabindex="-1" aria-labelledby="approveModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <form action="{{ route('loans.approve', $loan) }}" method="POST">
                @csrf
                <div class="modal-header">
                    <h5 class="modal-title" id="approveModalLabel">লোন অনুমোদন</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="mb-3">
                        <label for="remarks" class="form-label">মন্তব্য (ঐচ্ছিক)</label>
                        <textarea class="form-control" id="remarks" name="remarks" rows="3" placeholder="অনুমোদন সংক্রান্ত মন্তব্য..."></textarea>
                    </div>
                    <div class="alert alert-info">
                        <i class="fas fa-info-circle"></i> আপনি <strong>{{ $loan->loan_number }}</strong> নম্বর লোনটি অনুমোদন করতে চলেছেন।
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">বাতিল</button>
                    <button type="submit" class="btn btn-success">অনুমোদন করুন</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Reject Modal -->
<div class="modal fade" id="rejectModal" tabindex="-1" aria-labelledby="rejectModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <form action="{{ route('loans.reject', $loan) }}" method="POST">
                @csrf
                <div class="modal-header">
                    <h5 class="modal-title" id="rejectModalLabel">লোন প্রত্যাখ্যান</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="mb-3">
                        <label for="reason" class="form-label">প্রত্যাখ্যানের কারণ *</label>
                        <textarea class="form-control" id="reason" name="reason" rows="3" required placeholder="প্রত্যাখ্যানের কারণ লিখুন..."></textarea>
                    </div>
                    <div class="alert alert-warning">
                        <i class="fas fa-exclamation-triangle"></i> আপনি <strong>{{ $loan->loan_number }}</strong> নম্বর লোনটি প্রত্যাখ্যান করতে চলেছেন।
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">বাতিল</button>
                    <button type="submit" class="btn btn-danger">প্রত্যাখ্যান করুন</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Disburse Modal -->
<div class="modal fade" id="disburseModal" tabindex="-1" aria-labelledby="disburseModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <form action="{{ route('loans.disburse', $loan) }}" method="POST">
                @csrf
                <div class="modal-header">
                    <h5 class="modal-title" id="disburseModalLabel">লোন বিতরণ</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="mb-3">
                        <label for="disbursement_method" class="form-label">বিতরণ পদ্ধতি *</label>
                        <select class="form-control" id="disbursement_method" name="disbursement_method" required>
                            <option value="">পদ্ধতি নির্বাচন করুন</option>
                            <option value="cash">নগদ</option>
                            <option value="bank_transfer">ব্যাংক ট্রান্সফার</option>
                            <option value="cheque">চেক</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label for="transaction_id" class="form-label">ট্রানজেকশন আইডি (ঐচ্ছিক)</label>
                        <input type="text" class="form-control" id="transaction_id" name="transaction_id" placeholder="ট্রানজেকশন আইডি">
                    </div>
                    <div class="mb-3">
                        <label for="remarks" class="form-label">মন্তব্য (ঐচ্ছিক)</label>
                        <textarea class="form-control" id="remarks" name="remarks" rows="3" placeholder="বিতরণ সংক্রান্ত মন্তব্য..."></textarea>
                    </div>
                    <div class="alert alert-info">
                        <i class="fas fa-info-circle"></i> বিতরণের পরিমাণ: <strong>৳ {{ $loan->formatted_loan_amount }}</strong>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">বাতিল</button>
                    <button type="submit" class="btn btn-primary">বিতরণ করুন</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Payment Modal -->
<div class="modal fade" id="paymentModal" tabindex="-1" aria-labelledby="paymentModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <form action="{{ route('loans.pay-installment', $loan) }}" method="POST">
                @csrf
                <div class="modal-header">
                    <h5 class="modal-title" id="paymentModalLabel">কিস্তি পরিশোধ</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="mb-3">
                        <label for="installment_id" class="form-label">কিস্তি নির্বাচন করুন *</label>
                        <select class="form-control" id="installment_id" name="installment_id" required>
                            <option value="">কিস্তি নির্বাচন করুন</option>
                            @foreach($installments->where('status', '!=', 'paid') as $installment)
                                <option value="{{ $installment->id }}" 
                                        data-amount="{{ $installment->amount }}"
                                        data-paid="{{ $installment->paid_amount }}">
                                    কিস্তি #{{ $installment->installment_number }} - 
                                    ৳ {{ number_format($installment->amount - $installment->paid_amount, 2) }} বকেয়া
                                    (নির্ধারিত তারিখ: {{ \Carbon\Carbon::parse($installment->due_date)->format('d/m/Y') }})
                                </option>
                            @endforeach
                        </select>
                    </div>
                    <div class="mb-3">
                        <label for="amount" class="form-label">পরিমাণ *</label>
                        <input type="number" class="form-control" id="amount" name="amount" step="0.01" min="1" required>
                        <small class="text-muted" id="dueAmountText"></small>
                    </div>
                    <div class="mb-3">
                        <label for="payment_method" class="form-label">পরিশোধের পদ্ধতি *</label>
                        <select class="form-control" id="payment_method" name="payment_method" required>
                            <option value="">পদ্ধতি নির্বাচন করুন</option>
                            <option value="cash">নগদ</option>
                            <option value="bank_transfer">ব্যাংক ট্রান্সফার</option>
                            <option value="cheque">চেক</option>
                            <option value="mobile_banking">মোবাইল ব্যাংকিং</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label for="transaction_id" class="form-label">ট্রানজেকশন আইডি (ঐচ্ছিক)</label>
                        <input type="text" class="form-control" id="transaction_id" name="transaction_id" placeholder="ট্রানজেকশন আইডি">
                    </div>
                    <div class="mb-3">
                        <label for="payment_date" class="form-label">পরিশোধের তারিখ *</label>
                        <input type="date" class="form-control" id="payment_date" name="payment_date" value="{{ date('Y-m-d') }}" required>
                    </div>
                    <div class="mb-3">
                        <label for="remarks" class="form-label">মন্তব্য (ঐচ্ছিক)</label>
                        <textarea class="form-control" id="remarks" name="remarks" rows="2" placeholder="পরিশোধ সংক্রান্ত মন্তব্য..."></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">বাতিল</button>
                    <button type="submit" class="btn btn-success">পরিশোধ রেকর্ড করুন</button>
                </div>
            </form>
        </div>
    </div>
</div>
@endsection

@section('scripts')
<script>
    $(document).ready(function() {
        // Installment select change
        $('#installment_id').change(function() {
            var selectedOption = $(this).find('option:selected');
            var dueAmount = selectedOption.data('amount') - selectedOption.data('paid');
            $('#amount').val(dueAmount.toFixed(2));
            $('#dueAmountText').text('বকেয়া পরিমাণ: ৳ ' + dueAmount.toFixed(2));
        });
        
        // Manual installment payment button
        $('.pay-installment-btn').click(function() {
            var installmentId = $(this).data('installment-id');
            var amount = $(this).data('installment-amount');
            
            $('#installment_id').val(installmentId);
            $('#amount').val(amount);
            $('#dueAmountText').text('বকেয়া পরিমাণ: ৳ ' + amount);
            
            $('#paymentModal').modal('show');
        });
        
        // Payment date default to today
        $('#payment_date').val(new Date().toISOString().split('T')[0]);
    });
</script>
@endsection