@extends('layouts.user')
@section('content')
<div class="main-content">
        <div class="page-content dark:bg-zinc-700" style="padding-bottom: 200px;">
            
            <div class="container-fluid px-[0.625rem]">

                <div class="grid grid-cols-1 mb-5">
                    <div class="flex items-center justify-between">
                        <h4 class="text-lg font-semibold text-gray-800 mb-sm-0 grow dark:text-gray-100">Sales Bots</h4>
                        <nav class="flex" aria-label="Breadcrumb">
                            <span class="text-gray-600 dark:text-gray-100">Main Balance: <br><span class="font-bold gold-text">USDT {{ number_format(Auth::user()->balance, 2) }}</span></span>
                        </nav>
                    </div>
                </div>

                <x-alert/>

                <div class="w-full">
                    <div class="card dark:bg-zinc-800 dark:border-zinc-600">
                        <div class="pb-5 border-b card-body border-gray-50 dark:border-zinc-600">
                            <div class="flex items-center justify-between">
                                <h6 class="text-gray-600 text-15 dark:text-gray-100">You can run the Sales bot in than one Market Place at once. Begin Arbitrage!</h6>
                                    <div class="justify-self-center">
                                </div>
                            </div>
                        </div>
                        <div class="gap-4 mx-auto card-body">
                            <div id="">
                                <div id="monthly">
                                <div id="infoText" class="relative my-4 text-center text-gray-600 rounded dark:text-gray-100 alert" role="alert">
                                    <span class="block sm:inline"><small><i class="fas fa-exclamation-triangle"></i> You can only add capital to a bot instance from funds in your Main Balance alone!</small></span>
                                </div>
                                    <div class="grid w-full grid-cols-1 gap-4 lg:grid-cols-2">

                                    @foreach($plans as $plan)
                                        <div class="w-full mx-auto">
                                            <div class="rounded-xl card dark:bg-zinc-800 dark:border-zinc-600">
                                                <div class="card-body">
                                                    <div class="">
                                                        @php
                                                            $active_trades = App\Models\Investment::where('user_id', auth()->user()->id)->where('status', 2)->where('plan_id', $plan->id)->get();
                                                        @endphp
                                                        @if(count($active_trades) > 0)
                                                            @foreach($active_trades as $trade)
                                                                <div class="trade">
                                                                    <div class="flex items-center justify-between">
                                                                        <h5 class="text-base text-gray-600 dark:text-gray-100">{{ $plan->name }} <i class="fas fa-shopping-bag"></i><br>
                                                                            <div class="flex items-center mt-2">
                                                                                <small class="loading">Sales In Progress.......</small>
                                                                            </div>
                                                                        </h5>
                                                                        <div class="blink_me">
                                                                            <img src="{{ asset('images/bot.png') }}" class="h-12" alt="bot">
                                                                            <small class="text-center text-green-500" style="margin-left: 13px;">LIVE</small>
                                                                        </div>
                                                                    </div>
                                                                    <div class="my-4">
                                                                        <img class="w-full" src="{{ asset('markets/'. $plan->image) }}" style="height: 200px; object-fit: cover; border-radius: 15px;"/>
                                                                    </div>
                                                                    <h6 class="mt-2 text-gray-600 dark:text-gray-100">Total Trade Profits:</h5>
                                                                    <div class="flex items-center mx-auto text-center">
                                                                        <span class="text-lg gold-text">USDT {{ number_format($trade->balance, 2) }}</span>
                                                                        <small class="p-1 mt-3 mb-2 ml-4 text-center text-green-500 rounded w-fit bg-green-50/60 dark:text-green-500 dark:bg-green-500/30">+{{ $plan->percent_profit }}% /&nbsp; day</span></small>
                                                                    </div>
                                                                    <hr class="mb-2">
                                                                    <small class="text-gray-600 dark:text-gray-100">Entry Capital: USDT {{ number_format($trade->amount, 2) }}</small><br>
                                                                    <small class="text-gray-600 dark:text-gray-100">Current Capital: <span class="text-green-500">USDT {{ number_format($trade->balance + $trade->amount, 2) }}</span></small>
                                                                    <div class="flex items-center justify-around mt-4">
                                                                        <div class="flex flex-col">
                                                                            <small class="text-center text-green-600">Start Date</small>
                                                                            <div class="px-3 py-2 bg-slate-100" style="border-top: 6px solid green; border-radius: 10px;">
                                                                                <div class="text-xl text-center">{{ date('j', strtotime($trade->buy_date)) }}</div>
                                                                                <div class="text-xs text-center">{{ date('M Y', strtotime($trade->buy_date)) }} </div>
                                                                            </div>
                                                                        </div>
                                                                        <div class="flex flex-col">
                                                                            <small class="text-center text-red-600">End Date</small>
                                                                            <div class="px-3 py-2 bg-slate-100" style="border-top: 6px solid red; border-radius: 10px;">
                                                                                <div class="text-xl text-center">{{ date('j', strtotime($trade->end_date)) }}</div>
                                                                                <div class="text-xs text-center">{{ date('M Y', strtotime($trade->end_date)) }} </div>
                                                                            </div>
                                                                        </div>
                                                                    </div>
                                                                    <div class="pt-4 text-center">
                                                                        <button type="button" onclick="populateModal({{ $trade->id }}, '{{ $trade->balance }}', '{{$trade->plan->name}}');" class="order-2 w-full text-white btn border-violet-500 bg-violet-500 hover:bg-violet-700 hover:text-white focus:bg-violet-500 focus:text-white focus:ring focus:ring-violet-200" data-tw-toggle="modal" data-tw-target="#modal-id_form">Withdraw Profit To Balance</button>
                                                                    </div>
                                                                </div>
                                                            @endforeach
                                                        @else
                                                        <div class="no-trade">
                                                            <div class="flex items-center justify-between">
                                                                <h5 class="text-base text-gray-600 dark:text-gray-100">{{ $plan->name }} <i class="fas fa-shopping-bag"></i></h5>
                                                                <img src="{{ asset('images/bot.png') }}" class="h-12" alt="bot">
                                                            </div>
                                                            <div class="my-4">
                                                                <img class="w-full" src="{{ asset('markets/'. $plan->image) }}" style="height: 200px; object-fit: cover; border-radius: 15px;"/>
                                                            </div>
                                                            <h6 class="mt-3 text-gray-600 dark:text-gray-100"><span class="text-lg">USDT</span> {{ number_format($plan->min_amount, 2) }} &nbsp; <span class="text-lg">&rarr;</span> USDT</span> {{ number_format($plan->max_amount, 2) }}</h5>
                                                            <h2 class="flex w-full mt-6 text-gray-600 dark:text-gray-100">{{ $plan->percent_profit }}<i data-feather="percent" class="mt-3"></i> <span class="mt-3 ml-2 text-base text-gray-500 dark:text-zinc-100/60">/day for &nbsp; {{ $plan->profit_days }} days.</span></h2>
                                                            <hr>
                                                            <p class="mt-3 text-gray-500 dark:text-zinc-100/60 text-15">
                                                                You currently don't have a running instance!
                                                            </p>
                                                            <form action="{{ route('trade') }}" method="post">
                                                                @csrf
                                                                <div class="mt-4">
                                                                    <label class="block mb-2 font-medium dark:text-gray-100">Add Trading Capital :</label>
                                                                    <div class="flex border border-gray-100 rounded dark:border-zinc-600 dark:text-zinc-100">
                                                                        <select class="border-0 ltr:pl-1.5 rtl:pr-1.5 focus:border-violet-100 focus:ring focus:ring-violet-50/60 focus:z-40 dark:border-zinc-600 dark:bg-zinc-700 dark:text-zinc-100 dark:focus:ring-gray-500/10" style="max-width: 90px;">
                                                                            <option value="USDT" selected="">USDT</option>
                                                                        </select>
                                                                        <input type="hidden" name="plan_id" value="{{ $plan->id }}"/>
                                                                        <input type="number" id="{{ 'amount-'.$plan->description }}" min="{{ $plan->min_amount }}" max="{{ $plan->max_amount }}" step="any" required name="{{ 'amount-'.$plan->description }}" class=" border-0 w-full py-1.5 focus:border-violet-100 focus:ring focus:ring-violet-50/60 focus:z-40 dark:bg-zinc-700 dark:placeholder:text-gray-200 dark:focus:ring-gray-500/10" placeholder="Enter Amount">
                                                                    </div>
                                                                    @error('amount-'.$plan->description)
                                                                        <span class="text-red-600 invalid-feedback" role="alert">
                                                                            <small>{{ $message }}</small>
                                                                        </span>
                                                                    @enderror
                                                                </div>  
                                                                <div class="pt-2 text-center">
                                                                    <button type="submit" class="order-2 w-full text-white btn border-violet-500 bg-violet-500 hover:bg-violet-700 hover:text-white focus:bg-violet-500 focus:text-white focus:ring focus:ring-violet-200">Submit Capital</button>
                                                                </div> 
                                                            </form>
                                                        </div>
                                                        @endif
                                                        <div class="pt-2 mt-5 text-center">
                                                            <a href="{{ route('trading_history', $plan->id) }}" class="order-2 w-full text-white border-transparent btn" style="background-color: #ec595a">My Trading History</a>
                                                        </div> 
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    @endforeach

                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

                


<!-- Modals -->
<div class="relative z-50 hidden modal" id="modal-id_form" aria-labelledby="modal-title" role="dialog" aria-modal="true">
    <div class="fixed inset-0 z-50 overflow-y-auto">
        <div class="absolute inset-0 transition-opacity bg-black bg-opacity-50 modal-overlay"></div>
        <div class="p-4 mx-auto animate-translate sm:max-w-lg">
            <div class="relative overflow-hidden text-left transition-all transform bg-white rounded-lg shadow-xl dark:bg-zinc-600">
                <div class="bg-white dark:bg-zinc-700">
                    <button type="button" class="absolute top-3 right-2.5 text-gray-400 border-transparent hover:bg-gray-50/50 hover:text-gray-900 dark:text-gray-100 rounded-lg text-sm px-2 py-1 ltr:ml-auto rtl:mr-auto inline-flex items-center dark:hover:bg-zinc-600" data-tw-dismiss="modal">
                        <i class="text-xl text-gray-500 mdi mdi-close dark:text-zinc-100/60"></i>
                    </button>
                    <div class="p-5">
                        <h3 class="mb-4 text-xl font-medium text-gray-700 dark:text-gray-100">Withdraw Profit To Main Balance</h3>
                        <small class="text-gray-600 dark:text-gray-100" id="planName"></small>
                        <div class="text-gray-600 dark:text-gray-100">Your Total Trade Profit: <span class="gold-text" id="tradeBalance"></span></div>
                        <form class="space-y-4" action="{{ route('move_to_balance') }}" method="post">
                            @csrf                               
                            <input type="hidden" id="tradeId" name="trade_id" value=""/>
                            <div class="mt-4">
                                <label class="block mb-2 font-medium dark:text-gray-100">Enter profit to withdraw :</label>
                                <div class="flex border border-gray-100 rounded dark:border-zinc-600 dark:text-zinc-100">
                                    <select class="border-0 ltr:pl-1.5 rtl:pr-1.5 focus:border-violet-100 focus:ring focus:ring-violet-50/60 focus:z-40 dark:border-zinc-600 dark:bg-zinc-700 dark:text-zinc-100 dark:focus:ring-gray-500/10" style="max-width: 90px;">
                                        <option value="USDT" selected="">USDT</option>
                                    </select>
                                    <input type="number" step="any" required name="amount" class=" border-0 w-full py-1.5 focus:border-violet-100 focus:ring focus:ring-violet-50/60 focus:z-40 dark:bg-zinc-700 dark:placeholder:text-gray-200 dark:focus:ring-gray-500/10" placeholder="Enter Amount">
                                </div>
                            </div>
                            <div class="mt-6">
                                <button type="submit" class="w-full text-white border-transparent btn" style="background-color: #ec595a">Withdraw Profit</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    function populateModal(tradeId, balance, planName){
        // console.log(tradeId, balance, planName);
        document.getElementById('tradeBalance').innerHTML = 'USDT '+balance;
        document.getElementById('tradeId').value = tradeId;
        document.getElementById('planName').innerHTML = planName;
    }
</script>
@endsection