@extends('layouts.app')
@section('content')
    @include('admin.topmenu')
    @include('admin.sidebar')
    <div class="main-panel">
        <div class="content">
            <div class="page-inner">
                <div class="mt-2 mb-4 d-flex justify-content-between align-items-center">
                    <h1 class="title1">{{ $mode === 'create' ? 'Add investment plan' : 'Edit investment plan' }}</h1>
                    <a href="{{ route('admin.managed-investments.index') }}" class="btn btn-secondary btn-sm">
                        <i class="fa fa-arrow-left"></i> Back
                    </a>
                </div>
                <x-danger-alert />
                <x-success-alert />

                <div class="card shadow-sm">
                    <div class="card-body">
                        <form method="post" action="{{ $mode === 'create' ? route('admin.managed-investments.store') : route('admin.managed-investments.update', $plan) }}">
                            @csrf
                            @if($mode === 'edit')
                                @method('PUT')
                            @endif

                            <div class="form-row">
                                <div class="form-group col-md-6">
                                    <h5>Plan name</h5>
                                    <input class="form-control" type="text" name="name" value="{{ old('name', $plan->name) }}" required placeholder="Plan name">
                                </div>
                                <div class="form-group col-md-6">
                                    <h5>Plan price / max amount ({{ $settings->currency }})</h5>
                                    <input class="form-control" type="number" step="0.01" name="max_amount" value="{{ old('max_amount', $plan->max_amount) }}" required>
                                    <small class="text-muted">Maximum a user can invest in this plan.</small>
                                </div>
                                <div class="form-group col-md-6">
                                    <h5>Minimum amount ({{ $settings->currency }})</h5>
                                    <input class="form-control" type="number" step="0.01" name="min_amount" value="{{ old('min_amount', $plan->min_amount) }}" required>
                                </div>
                                <div class="form-group col-md-6">
                                    <h5>Minimum return (%)</h5>
                                    <input class="form-control" type="number" step="any" name="min_roi_percent" value="{{ old('min_roi_percent', $plan->min_roi_percent) }}" required>
                                    <small class="text-muted">Shown to customers; payout uses top-up amount below.</small>
                                </div>
                                <div class="form-group col-md-6">
                                    <h5>Maximum return (%)</h5>
                                    <input class="form-control" type="number" step="any" name="max_roi_percent" value="{{ old('max_roi_percent', $plan->max_roi_percent) }}" required>
                                </div>
                                <div class="form-group col-md-6">
                                    <h5>Gift bonus ({{ $settings->currency }})</h5>
                                    <input class="form-control" type="number" step="0.01" name="gift_bonus" value="{{ old('gift_bonus', $plan->gift_bonus ?? 0) }}" required>
                                    <small class="text-muted">One-time credit to balance when they subscribe.</small>
                                </div>
                                <div class="form-group col-md-6">
                                    <h5>Top up interval</h5>
                                    <select class="form-control" name="topup_interval" required>
                                        @foreach(['Monthly','Weekly','Daily','Hourly','Every 30 Minutes','Every 10 Minutes'] as $int)
                                            <option value="{{ $int }}" {{ old('topup_interval', $plan->topup_interval ?? 'Monthly') === $int ? 'selected' : '' }}>{{ $int }}</option>
                                        @endforeach
                                    </select>
                                    <small class="text-muted">How often profit is credited to the user’s balance.</small>
                                </div>
                                <div class="form-group col-md-6">
                                    <h5>Top up type</h5>
                                    <select class="form-control" name="topup_type" required>
                                        <option value="Percentage" {{ old('topup_type', $plan->topup_type ?? 'Percentage') === 'Percentage' ? 'selected' : '' }}>Percentage</option>
                                        <option value="Fixed" {{ old('topup_type', $plan->topup_type ?? 'Percentage') === 'Fixed' ? 'selected' : '' }}>Fixed</option>
                                    </select>
                                </div>
                                <div class="form-group col-md-6">
                                    <h5>Top up amount (% or {{ $settings->currency }})</h5>
                                    <input class="form-control" type="number" step="any" name="topup_amount" value="{{ old('topup_amount', $plan->topup_amount) }}" required>
                                    <small class="text-muted">Per interval: % of principal or fixed amount added to balance.</small>
                                </div>
                                <div class="form-group col-md-12">
                                    <h5>Investment duration</h5>
                                    <input class="form-control" type="text" name="investment_duration" value="{{ old('investment_duration', $plan->investment_duration ?? '12 Months') }}" required placeholder="e.g. 12 Months, 1 Years">
                                    <small class="text-muted">Format: number + space + unit with <strong>s</strong> (e.g. <code>1 Days</code>, <code>6 Months</code>).</small>
                                </div>
                                <div class="form-group col-md-12">
                                    <h5>Description (optional)</h5>
                                    <textarea class="form-control" name="description" rows="3" placeholder="Short summary for customers">{{ old('description', $plan->description) }}</textarea>
                                </div>
                                <div class="form-group col-md-6">
                                    <h5>Sort order</h5>
                                    <input class="form-control" type="number" name="sort_order" value="{{ old('sort_order', $plan->sort_order ?? 0) }}" min="0">
                                </div>
                                <div class="form-group col-md-6 d-flex align-items-center pt-4">
                                    <div class="custom-control custom-checkbox">
                                        <input type="checkbox" class="custom-control-input" id="is_active" name="is_active" value="1" @if(old('is_active', $plan->is_active ?? true)) checked @endif>
                                        <label class="custom-control-label" for="is_active">Plan is active (visible to users)</label>
                                    </div>
                                </div>
                                <div class="form-group col-md-12">
                                    <button type="submit" class="btn btn-primary">{{ $mode === 'create' ? 'Create plan' : 'Save changes' }}</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>

                <div class="modal fade" id="durationModal" tabindex="-1">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-body">
                                <p>Use a digit, space, then timeframe with capital first letter and trailing <strong>s</strong>.</p>
                                <p class="mb-0"><strong>Examples:</strong> 1 Days, 3 Weeks, 6 Months, 1 Years</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection
