<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover" />
    <title>My Products - TATA</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" />
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600&display=swap" rel="stylesheet">

    <style>
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
            -webkit-tap-highlight-color: transparent;
        }

        body {
            font-family: 'Poppins', sans-serif;
            background: linear-gradient(-45deg, #0f2027, #203a43, #2c5364, #ff8c00);
            background-size: 400% 400%;
            animation: gradientBG 15s ease infinite;
            color: #fff;
            min-height: 100vh;
            display: flex;
            justify-content: center;
            padding-top: 20px;
            padding-bottom: 40px;
        }

        @keyframes gradientBG {
            0% {
                background-position: 0% 50%;
            }

            50% {
                background-position: 100% 50%;
            }

            100% {
                background-position: 0% 50%;
            }
        }

        .app-container {
            width: 92%;
            max-width: 380px;
        }

        .header-nav {
            display: flex;
            align-items: center;
            gap: 15px;
            margin-bottom: 20px;
        }

        .header-nav a {
            color: #fff;
            text-decoration: none;
            font-size: 18px;
        }

        .header-nav h2 {
            font-size: 17px;
            font-weight: 600;
        }

        .summary-card {
            background: linear-gradient(135deg, #FFD700 0%, #FFA500 100%);
            padding: 15px;
            border-radius: 18px;
            color: #222;
            display: flex;
            justify-content: space-around;
            text-align: center;
            margin-bottom: 25px;
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);
        }

        .summary-item h3 {
            font-size: 18px;
            font-weight: 800;
        }

        .summary-item p {
            font-size: 10px;
            font-weight: 600;
            opacity: 0.8;
            text-transform: uppercase;
        }

        .purchase-card {
            background: rgba(255, 255, 255, 0.12);
            backdrop-filter: blur(15px);
            border-radius: 20px;
            padding: 15px;
            margin-bottom: 18px;
            border: 1px solid rgba(255, 255, 255, 0.2);
            position: relative;
            overflow: hidden;
        }

        .status-badge {
            position: absolute;
            top: 12px;
            right: 12px;
            font-size: 9px;
            font-weight: 700;
            padding: 3px 10px;
            border-radius: 20px;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .status-active {
            background: #28c76f;
            color: #fff;
            box-shadow: 0 0 10px rgba(40, 199, 111, 0.4);
        }

        .status-expired {
            background: rgba(255, 255, 255, 0.2);
            color: #ccc;
        }

        .card-top {
            display: flex;
            gap: 15px;
            align-items: center;
            margin-bottom: 12px;
        }

        .card-top img {
            width: 60px;
            height: 60px;
            border-radius: 12px;
            object-fit: cover;
            background: #fff;
        }

        .card-top .name-info h4 {
            font-size: 14px;
            font-weight: 600;
            color: #FFD700;
        }

        .card-top .name-info p {
            font-size: 11px;
            opacity: 0.7;
        }

        .detail-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 10px;
            padding-top: 10px;
            border-top: 1px solid rgba(255, 255, 255, 0.1);
        }

        .detail-item {
            font-size: 11px;
        }

        .detail-item span {
            display: block;
            opacity: 0.6;
            margin-bottom: 2px;
        }

        .detail-item b {
            font-weight: 600;
            color: #fff;
        }

        .text-orange {
            color: #FFD700 !important;
        }

        .no-data {
            text-align: center;
            margin-top: 50px;
            font-size: 14px;
            opacity: 0.5;
        }

        .claim-btn {
            width: 100%;
            margin-top: 12px;
            padding: 10px 5px;
            border-radius: 10px;
            border: none;
            background: linear-gradient(135deg, #FFD700 0%, #FFA500 100%);
            color: #000;
            font-weight: 700;
            font-size: 13px;
            cursor: pointer;
            text-transform: uppercase;
        }

        .countdown-timer {
            font-size: 12px;
            color: #FFD700;
            background: rgba(255, 215, 0, 0.1);
            padding: 10px 5px;
            border-radius: 8px;
            text-align: center;
            margin-top: 10px;
        }
    </style>
</head>

<body>

    <div class="app-container">

        <div class="header-nav">
            <a href="/my-profile"><i class="fa-solid fa-chevron-left"></i></a>
            <h2>My Investments</h2>
        </div>

        <?php
            $totalAssets = \App\Models\Purchase::where('user_id', auth()->id())
                ->where('status', 'active')
                ->sum('amount');

            $activePackages = \App\Models\Purchase::where('user_id', auth()->id())
                ->where('status', 'active')
                ->with('package')
                ->get();

            $inactivePackages = \App\Models\Purchase::where('user_id', auth()->id())
                ->where('status', 'inactive')
                ->with('package')
                ->get();
        ?>

        <div class="summary-card">
            <div class="summary-item">
                <h3><?php echo e(price($totalAssets)); ?></h3>
                <p>Total Assets</p>
            </div>
            <div class="summary-item" style="border-left: 1px solid rgba(0,0,0,0.1); padding-left: 20px;">
                <h3><?php echo e(price(auth()->user()->product_income)); ?></h3>
                <p>Total Income</p>
            </div>
        </div>

        <div class="order-list">
            <?php $__empty_1 = true; $__currentLoopData = $activePackages; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $purchase): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                <div class="purchase-card">
                    <div class="status-badge status-active">Running</div>
                    <div class="card-top">
                        <img src="<?php echo e(asset($purchase->package?->photo ?? 'images/default-package.png')); ?>" alt="Package Photo">
                        <div class="name-info">
                            <h4><?php echo e($purchase->package->name); ?></h4>
                            <p>Order ID: #<?php echo e($purchase->id); ?></p>
                        </div>
                    </div>

                    <div class="detail-grid">
                        <div class="detail-item"><span>Invested</span><b><?php echo e(price($purchase->amount)); ?></b></div>
                        <div class="detail-item"><span>Daily Profit</span><b
                                class="text-orange"><?php echo e(price($purchase->daily_income)); ?></b></div>
                        <div class="detail-item"><span>Current
                                Income</span><b><?php echo e(price($purchase->daily_income * now()->diffInDays($purchase->created_at))); ?></b>
                        </div>
                        <div class="detail-item"><span>Cycle</span><b><?php echo e($purchase->package->validity); ?> Days</b></div>
                        <div class="detail-item" style="grid-column: span 2;"><span>Expire
                                Date</span><b><?php echo e($purchase->expire_time); ?></b></div>
                    </div>

                    <div class="action-area">
                        <?php
                            $now = now();
                            $packageType = $purchase->package->category;
                            $nextClaim = \Carbon\Carbon::parse($purchase->date);
                            $validityEnd = \Carbon\Carbon::parse($purchase->validity);

                            $remainingSeconds = $now->diffInSeconds($nextClaim, false);
                            $isReady = $remainingSeconds <= 0;
                        ?>

                        <?php if($packageType === 'fixed_vip' || $packageType === 'vip'): ?>
                            <?php if($now->greaterThanOrEqualTo($validityEnd)): ?>
                                <form action="<?php echo e(route('user.received_amount', $purchase->id)); ?>" method="POST">
                                    <?php echo csrf_field(); ?>
                                    <button class="claim-btn">Claim Total Profit</button>
                                </form>
                            <?php else: ?>
                                <div class="countdown-timer">
                                    <i class="fa-solid fa-lock"></i> Fixed Plan
                                </div>
                            <?php endif; ?>
                        <?php else: ?>
                            <?php if($isReady): ?>
                                <form action="<?php echo e(route('user.received_amount', $purchase->id)); ?>" method="POST">
                                    <?php echo csrf_field(); ?>
                                    <button class="claim-btn">Claim Profit</button>
                                </form>
                            <?php else: ?>
                                <div class="countdown-timer-box" id="timer-container-<?php echo e($purchase->id); ?>">
                                    <div class="countdown-timer js-countdown" data-seconds="<?php echo e($remainingSeconds); ?>"
                                        data-id="<?php echo e($purchase->id); ?>">
                                        <i class="fa-regular fa-clock"></i>
                                        <span class="timer-display">Loading...</span>
                                    </div>
                                </div>
                            <?php endif; ?>
                        <?php endif; ?>
                    </div>
                </div>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                <p class="no-data">You have no active investments.</p>
            <?php endif; ?>
        </div>

        <script>
            document.addEventListener('DOMContentLoaded', function() {
                function updateAllTimers() {
                    const timers = document.querySelectorAll('.js-countdown');

                    timers.forEach(function(timer) {
                        let seconds = parseInt(timer.getAttribute('data-seconds'));
                        const display = timer.querySelector('.timer-display');
                        const purchaseId = timer.getAttribute('data-id');

                        if (seconds > 0) {
                            seconds--;
                            timer.setAttribute('data-seconds', seconds);

                            let h = Math.floor(seconds / 3600);
                            let m = Math.floor((seconds % 3600) / 60);
                            let s = seconds % 60;

                            display.textContent =
                                `Next Claim: ${(h < 10 ? "0"+h : h)}h ${(m < 10 ? "0"+m : m)}m ${(s < 10 ? "0"+s : s)}s`;
                        } else {
                            const container = document.getElementById('timer-container-' + purchaseId);
                            if (container) {
                                container.innerHTML = `
                            <form action="/received-amount/${purchaseId}" method="POST">
                                <input type="hidden" name="_token" value="<?php echo e(csrf_token()); ?>">
                                <button class="claim-btn">Claim Profit Now</button>
                            </form>`;
                            }
                        }
                    });
                }

                setInterval(updateAllTimers, 1000);
                updateAllTimers();
            });
        </script>

        <?php $__currentLoopData = $inactivePackages; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $purchase): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
            <div class="purchase-card" style="opacity: 0.8;">
                <div class="status-badge status-expired">Expired</div>
                <div class="card-top">
                    <img src="<?php echo e(asset($purchase->package->photo)); ?>" style="filter: grayscale(1);">
                    <div class="name-info">
                        <h4><?php echo e($purchase->package->name); ?></h4>
                        <p>Closed Order</p>
                    </div>
                </div>
                <div class="detail-grid">
                    <div class="detail-item"><span>Invested</span><b><?php echo e(price($purchase->amount)); ?></b></div>
                    <div class="detail-item"><span>Total
                            Earned</span><b><?php echo e(price($purchase->daily_income * $purchase->package->validity)); ?></b>
                    </div>
                </div>
            </div>
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
    </div>

    </div>

    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
</body>

</html>
<?php /**PATH /home/goldenyi/public_html/resources/views/app/main/ordered.blade.php ENDPATH**/ ?>