@extends('layouts.admin')

@section('css')
<link rel="stylesheet" href="https://cdn.datatables.net/1.11.3/css/jquery.dataTables.min.css">
@endsection

@section('content')
<main class="content">
    <div class="container-fluid p-0">

        <div class="row mb-2 mb-xl-3">
            <div class="col-auto d-none d-sm-block">
                <h3>Investments @if(!blank($client)) ({{ $client->email }}) @endif</h3>

                @if(!blank($client))
                <p><a href="{{ route('admin.user.single', $client->username) }}">Back to User Page</a></p>
                @endif
            </div>
        </div>

        @include('components.admin.error-message')

        <div class="card">
            <div class="card-datatable table-responsive">
                <table class="table table-striped">
                    <thead>
                        <tr>
                           
                            <th>Plan</th>
                            <th>Amount</th>
                            <th>Profit</th>
                            <th>Auto top up</th>
                            <th>Initiated At</th>
                            <th>Status</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($investments as $investment)
                        <tr>
                           
                            <td>{{ $investment->plan?->name }}</td>
                            <td>{{ number_format($investment->amount,2,'.',',') }} {{ $client?->currency }}</td>
                            <td>{{ number_format($investment->roi,2,'.',',') }} {{ $client?->currency }}</td>
                            <td>{{ ucfirst($investment->autot) }}</td>
                            <td>{{ \Carbon\Carbon::parse($investment->created_at)->toDayDateTimeString() }}</td>
                        
                            <td>{{ strtoupper($investment->status) }}</td>
                            <td>
                                <div class="dropdown">
                                    <button class="btn btn-sm btn-icon dropdown-toggle" data-bs-toggle="dropdown">
                                        <i class="bx bx-dots-vertical-rounded"></i>
                                    </button>
                                    <div class="dropdown-menu dropdown-menu-end">
                                        @if($investment->status == 'active')
                                            <a href="javascript:;" class="dropdown-item pause_plan" data-id="{{ $investment->id }}" data-name="{{ $investment->txn_id }}">Pause</a>
                                            <a href="javascript:;" class="dropdown-item end_plan" data-id="{{ $investment->id }}" data-name="{{ $investment->txn_id }}">End</a>
                                            @if($investment->autot == 'yes')
                                                <a href="javascript:;" class="dropdown-item end_auto" data-id="{{ $investment->id }}" data-name="{{ $investment->txn_id }}">End Auto</a>
                                            @else
                                                <a href="javascript:;" class="dropdown-item start_auto" data-id="{{ $investment->id }}" data-name="{{ $investment->txn_id }}">Start Auto</a>
                                            @endif
                                            <a href="javascript:;" class="dropdown-item" data-bs-toggle="modal" data-bs-target="#topPlan{{ $investment->id }}">Top Up</a>
                                        @elseif($investment->status == 'paused')
                                            <a href="javascript:;" class="dropdown-item resume_plan" data-id="{{ $investment->id }}" data-name="{{ $investment->txn_id }}">Resume</a>
                                        @endif
                                    </div>
                                </div>
                            </td>
                        </tr>

                        <!-- Top Up Modal -->
                        <div class="modal fade" id="topPlan{{ $investment->id }}" tabindex="-1" aria-hidden="true">
                            <div class="modal-dialog modal-lg modal-simple modal-edit-user">
                                <div class="modal-content p-3 p-md-5">
                                    <div class="modal-body">
                                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                        <div class="text-center mb-4">
                                            <h3>Top Up ( {{ $investment->plan?->name }} - {{ $investment->txn_id }} )</h3>
                                        </div>
                                        <form method="post" action="{{ route('admin.investment.topup') }}">
                                            @csrf
                                            <div class="col-12 col-md-12">
                                                <label class="form-label">Amount</label>
                                                <input type="number" name="amount" step="0.01" min="0.01" class="form-control" required>
                                            </div>
                                            <input type="hidden" name="gixd" value="{{ $investment->id }}">
                                            <div class="col-12 text-center mt-4">
                                                <button type="submit" class="btn btn-primary me-sm-3 me-1">Submit</button>
                                                <button type="reset" class="btn btn-label-secondary" data-bs-dismiss="modal">Cancel</button>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </div>

                        @endforeach
                    </tbody>
                </table>

                <div class="d-flex justify-content-between mt-3">
                    <div>
                        Showing {{ $investments->firstItem() ?? 0 }} to {{ $investments->lastItem() ?? 0 }} of {{ $investments->total() }} entries
                    </div>
                    <div>
                        {{ $investments->links() }}
                    </div>
                </div>
            </div>
        </div>
    </div>
</main>
@endsection

@section('script')
<script>
$(function(){
    $('.pause_plan').on('click', function(){
        const id = $(this).data('id');
        const name = $(this).data('name');
        if(confirm(`Are you sure you want to pause this plan - ${name}?`)){
            window.location.href = "{{ secure_url('admin/investment/pause') }}/" + id;
        }
    });

    $('.resume_plan').on('click', function(){
        const id = $(this).data('id');
        const name = $(this).data('name');
        if(confirm(`Are you sure you want to resume this plan - ${name}?`)){
            window.location.href = "{{ secure_url('admin/investment/resume') }}/" + id;
        }
    });

    $('.end_plan').on('click', function(){
        const id = $(this).data('id');
        const name = $(this).data('name');
        if(confirm(`Are you sure you want to end this plan - ${name}?`)){
            window.location.href = "{{ secure_url('admin/investment/end') }}/" + id;
        }
    });

    $('.start_auto').on('click', function(){
        const id = $(this).data('id');
        const name = $(this).data('name');
        if(confirm(`Are you sure you want to start auto top-up - ${name}?`)){
            window.location.href = "{{ secure_url('admin/investment/auto') }}/" + id;
        }
    });

    $('.end_auto').on('click', function(){
        const id = $(this).data('id');
        const name = $(this).data('name');
        if(confirm(`Are you sure you want to end auto top-up - ${name}?`)){
            window.location.href = "{{ secure_url('admin/investment/end_auto') }}/" + id;
        }
    });
});
</script>
@endsection
