@extends('dashboard.admin.layouts.app')
@section('content')
    <div class="page-container">

        <div class="page-title-head d-flex align-items-sm-center flex-sm-row flex-column gap-2">
            <div class="flex-grow-1">
                <h4 class="fs-18 fw-semibold mb-0">{{ $title }}</h4>
            </div>

            <div class="text-end">
                <x-dashboard.breadcrumbs :breadcrumbs="$breadcrumbs" />
            </div>
        </div>

        <div class="row">
            @include('dashboard.admin.user.partials.account_options_and_status')

            <div class="col-lg-12">
                <x-dashboard.card title="{{ $title }}">

                    <form action="{{ route('admin.user.profit_balance.store', $user->uuid) }}" method="post">
                        @csrf

                        <div class="row g-3">

                            {{-- User (read-only context) --}}
                            <div class="col-md-6">
                                <label for="user" class="form-label fw-medium">User</label>
                                <div class="input-group">
                                    <span class="input-group-text bg-body-tertiary">
                                        <i class="ti ti-user"></i>
                                    </span>
                                    <input type="text" id="user" class="form-control" value="{{ $user->name }}"
                                        readonly>
                                </div>
                            </div>

                            {{-- Current Profit Balance (display only) --}}
                            <div class="col-md-6">
                                <label for="current_balance" class="form-label fw-medium">Current Profit Balance</label>
                                <div class="input-group">
                                    <span class="input-group-text bg-body-tertiary">{{ currency($user->currency) }}</span>
                                    <input type="text" id="current_balance" class="form-control"
                                        value="{{ formatAmount($user->profit_balance) }}" readonly>
                                </div>
                            </div>

                            {{-- Plan --}}
                            <div class="col-md-6">
                                <label for="plan_id" class="form-label fw-medium">Plan <span
                                        class="text-danger">*</span></label>
                                <div class="input-group">
                                    <span class="input-group-text bg-body-tertiary">
                                        <i class="ti ti-stack-2"></i>
                                    </span>
                                    <select name="plan_id" id="plan_id"
                                        class="form-select @error('plan_id') is-invalid @enderror" required>
                                        <option value="" disabled {{ old('plan_id') ? '' : 'selected' }}>Select a plan
                                        </option>
                                        @foreach ($userPlans as $userPlan)
                                            <option value="{{ $userPlan->plan->id }}"
                                                {{ old('plan_id') == $userPlan->plan->id ? 'selected' : '' }}>
                                                {{ $userPlan->plan->name }}
                                            </option>
                                        @endforeach
                                    </select>
                                    @error('plan_id')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                                <div class="form-text">Select the investment plan this profit is attributed to.</div>
                            </div>

                            {{-- Amount --}}
                            <div class="col-md-6">
                                <label for="amount" class="form-label fw-medium">Amount to Add <span
                                        class="text-danger">*</span></label>
                                <div class="input-group">
                                    <span class="input-group-text bg-body-tertiary">{{ currency($user->currency) }}</span>
                                    <input type="number" step="0.01" min="0.01" name="amount" id="amount"
                                        class="form-control @error('amount') is-invalid @enderror"
                                        value="{{ old('amount') }}" placeholder="0.00" required>
                                    @error('amount')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                                <div class="form-text">Enter the profit amount to credit this user's balance.</div>
                            </div>

                        </div>

                        <hr class="my-4">

                        <div class="d-flex justify-content-end">
                            <button type="submit" class="btn btn-primary"
                                onclick="return confirm('Are you sure you want to add this amount to the user\'s profit balance?');">
                                <i class="ti ti-cash-banknote me-1"></i> Add Profit Balance
                            </button>
                        </div>

                    </form>

                </x-dashboard.card>
            </div>
        </div>
        <!-- end col -->
    </div>
    <!-- end row -->

    </div>
@endsection
