@extends('layouts.app')

@section('title', 'Savings Product Details')

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">{{ $savingsProduct->name }}</h3>
                    <div class="card-tools">
                        <a href="{{ route('savings.products.edit', $savingsProduct) }}" class="btn btn-warning btn-sm">
                            <i class="fas fa-edit"></i> Edit
                        </a>
                        <a href="{{ route('savings.products.index') }}" class="btn btn-secondary btn-sm">
                            <i class="fas fa-arrow-left"></i> Back
                        </a>
                    </div>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <table class="table table-borderless">
                                <tr>
                                    <th>Name:</th>
                                    <td>{{ $savingsProduct->name }}</td>
                                </tr>
                                <tr>
                                    <th>Code:</th>
                                    <td>{{ $savingsProduct->code }}</td>
                                </tr>
                                <tr>
                                    <th>Type:</th>
                                    <td><span class="badge badge-info">{{ ucfirst($savingsProduct->type) }}</span></td>
                                </tr>
                                <tr>
                                    <th>Status:</th>
                                    <td>
                                        @if($savingsProduct->is_active)
                                            <span class="badge badge-success">Active</span>
                                        @else
                                            <span class="badge badge-secondary">Inactive</span>
                                        @endif
                                    </td>
                                </tr>
                                <tr>
                                    <th>Description:</th>
                                    <td>{{ $savingsProduct->description ?: 'N/A' }}</td>
                                </tr>
                            </table>
                        </div>
                        <div class="col-md-6">
                            <table class="table table-borderless">
                                <tr>
                                    <th>Interest Rate:</th>
                                    <td>{{ $savingsProduct->formatted_interest_rate }}</td>
                                </tr>
                                <tr>
                                    <th>Interest Posting:</th>
                                    <td>{{ ucfirst($savingsProduct->interest_posting) }}</td>
                                </tr>
                                <tr>
                                    <th>Min Opening Balance:</th>
                                    <td>{{ number_format($savingsProduct->minimum_opening_balance, 2) }}</td>
                                </tr>
                                <tr>
                                    <th>Min Balance:</th>
                                    <td>{{ number_format($savingsProduct->minimum_balance, 2) }}</td>
                                </tr>
                                <tr>
                                    <th>Max Balance:</th>
                                    <td>{{ $savingsProduct->maximum_balance ? number_format($savingsProduct->maximum_balance, 2) : 'Unlimited' }}</td>
                                </tr>
                            </table>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-6">
                            <h5>Fees</h5>
                            <table class="table table-sm">
                                <tr>
                                    <td>Opening Fee:</td>
                                    <td>{{ number_format($savingsProduct->opening_fee, 2) }}</td>
                                </tr>
                                <tr>
                                    <td>Closing Fee:</td>
                                    <td>{{ number_format($savingsProduct->closing_fee, 2) }}</td>
                                </tr>
                                <tr>
                                    <td>Monthly Fee:</td>
                                    <td>{{ number_format($savingsProduct->monthly_fee, 2) }}</td>
                                </tr>
                                <tr>
                                    <td>Withdrawal Fee:</td>
                                    <td>{{ number_format($savingsProduct->withdrawal_fee, 2) }}</td>
                                </tr>
                            </table>
                        </div>
                        <div class="col-md-6">
                            <h5>Settings</h5>
                            <table class="table table-sm">
                                <tr>
                                    <td>Allows Withdrawals:</td>
                                    <td>{{ $savingsProduct->allows_withdrawals ? 'Yes' : 'No' }}</td>
                                </tr>
                                <tr>
                                    <td>Allows Deposits:</td>
                                    <td>{{ $savingsProduct->allows_deposits ? 'Yes' : 'No' }}</td>
                                </tr>
                                <tr>
                                    <td>Requires Approval:</td>
                                    <td>{{ $savingsProduct->requires_approval ? 'Yes' : 'No' }}</td>
                                </tr>
                                <tr>
                                    <td>Created By:</td>
                                    <td>{{ $savingsProduct->creator->name ?? 'N/A' }}</td>
                                </tr>
                                <tr>
                                    <td>Created At:</td>
                                    <td>{{ $savingsProduct->created_at->format('d/m/Y H:i') }}</td>
                                </tr>
                            </table>
                        </div>
                    </div>

                    @if($savingsProduct->savingsAccounts->count() > 0)
                    <div class="row">
                        <div class="col-12">
                            <h5>Associated Savings Accounts ({{ $savingsProduct->savingsAccounts->count() }})</h5>
                            <div class="table-responsive">
                                <table class="table table-sm table-bordered">
                                    <thead>
                                        <tr>
                                            <th>Account Number</th>
                                            <th>Client</th>
                                            <th>Balance</th>
                                            <th>Status</th>
                                            <th>Created</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @foreach($savingsProduct->savingsAccounts as $account)
                                        <tr>
                                            <td>{{ $account->account_number }}</td>
                                            <td>{{ $account->client->name }}</td>
                                            <td>{{ number_format($account->current_balance, 2) }}</td>
                                            <td>
                                                <span class="badge badge-{{ $account->status == 'active' ? 'success' : 'secondary' }}">
                                                    {{ ucfirst($account->status) }}
                                                </span>
                                            </td>
                                            <td>{{ $account->created_at->format('d/m/Y') }}</td>
                                        </tr>
                                        @endforeach
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
