<?php $__env->startSection('content'); ?>
<section class="section-padding section-bg fix">
    <div class="container">
        <div class="dashboard-layout">
            <?php echo $__env->make('instructor.partials.sidebar', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>

            <div class="dashboard-main">
                <!-- Page Header -->
                <div class="dashboard-header wow fadeInUp">
                    <div class="section-title-area">
                        <div class="section-title">
                            <h6><?php echo e(__('instructor.earnings_overview')); ?></h6>
                            <h2><?php echo e(__('instructor.earnings_transactions')); ?></h2>
                        </div>
                        <div class="dashboard-header-actions mb-4">
                            <a href="<?php echo e(route('instructor.withdrawals')); ?>" class="theme-btn style-2">
                                <i class="fa-solid fa-wallet"></i>
                                <?php echo e(__('instructor.withdraw_funds')); ?>

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

                <!-- Stats Cards -->
                <div class="dashboard-stats-minimal wow fadeInUp" data-wow-delay="0.1s">
                    <div class="stat-card-minimal">
                        <div class="stat-icon-minimal stat-icon--theme">
                            <i class="fa-solid fa-dollar-sign"></i>
                        </div>
                        <div class="stat-content-minimal">
                            <div class="stat-value-minimal"><?php echo e(currency_format($stats['available_balance'])); ?></div>
                            <div class="stat-label-minimal"><?php echo e(__('instructor.available_balance')); ?></div>
                        </div>
                    </div>

                    <div class="stat-card-minimal">
                        <div class="stat-icon-minimal stat-icon--warning">
                            <i class="fa-solid fa-clock"></i>
                        </div>
                        <div class="stat-content-minimal">
                            <div class="stat-value-minimal"><?php echo e(currency_format($stats['pending_balance'])); ?></div>
                            <div class="stat-label-minimal"><?php echo e(__('instructor.pending_balance')); ?></div>
                        </div>
                    </div>

                    <div class="stat-card-minimal">
                        <div class="stat-icon-minimal stat-icon--success">
                            <i class="fa-solid fa-chart-line"></i>
                        </div>
                        <div class="stat-content-minimal">
                            <div class="stat-value-minimal"><?php echo e(currency_format($stats['this_month'])); ?></div>
                            <div class="stat-label-minimal"><?php echo e(__('instructor.earnings_this_month')); ?></div>
                        </div>
                    </div>

                    <div class="stat-card-minimal">
                        <div class="stat-icon-minimal stat-icon--violet">
                            <i class="fa-solid fa-coins"></i>
                        </div>
                        <div class="stat-content-minimal">
                            <div class="stat-value-minimal"><?php echo e(currency_format($stats['total_earnings'])); ?></div>
                            <div class="stat-label-minimal"><?php echo e(__('instructor.total_earnings_stat')); ?></div>
                        </div>
                    </div>
                </div>

                <!-- Earnings Chart -->
                <div class="analytics-chart-card wow fadeInUp analytics-card-spacing h-auto" data-wow-delay="0.2s">
                    <div class="chart-header">
                        <h3><?php echo e(__('instructor.monthly_earnings')); ?></h3>
                         <select class="chart-period" id="earningsRange">
                            <option value="last_3_months" <?php echo e($defaultRange === 'last_3_months' ? 'selected' : ''); ?>>
                                <?php echo e(__('instructor.last_3_months')); ?></option>
                            <option value="last_6_months" <?php echo e($defaultRange === 'last_6_months' ? 'selected' : ''); ?>>
                                <?php echo e(__('instructor.last_6_months')); ?></option>
                            <option value="last_12_months" <?php echo e($defaultRange === 'last_12_months' ? 'selected' : ''); ?>>
                                <?php echo e(__('instructor.last_12_months')); ?></option>
                            <option value="last_24_months" <?php echo e($defaultRange === 'last_24_months' ? 'selected' : ''); ?>>
                                <?php echo e(__('instructor.last_24_months')); ?></option>
                            <option value="this_year" <?php echo e($defaultRange === 'this_year' ? 'selected' : ''); ?>>
                                <?php echo e(__('instructor.this_year')); ?>

                            </option>
                            <option value="last_year" <?php echo e($defaultRange === 'last_year' ? 'selected' : ''); ?>>
                                <?php echo e(__('instructor.last_year')); ?>

                            </option>
                        </select>
                    </div>
                    <div class="chart-container analytics-chart-fixed">
                        <canvas id="earningsChart"></canvas>
                    </div>
                </div>

                <!-- Transactions Section -->
                <div class="dashboard-courses-section wow fadeInUp dashboard-section-mt" data-wow-delay="0.3s">
                    <div class="section-title-area section-title-area-compact">
                        <div class="section-title">
                            <h3><?php echo e(__('instructor.recent_transactions')); ?></h3>
                        </div>
                        <div class="reviews-filters">
                            <button class="filter-btn active" data-filter="all"><?php echo e(__('instructor.filter_all')); ?></button>
                            <button class="filter-btn" data-filter="sale"><?php echo e(__('instructor.filter_sales')); ?></button>
                            <button class="filter-btn" data-filter="subscription">Subscriptions</button>
                            <button class="filter-btn" data-filter="withdrawal"><?php echo e(__('instructor.filter_withdrawals')); ?></button>
                        </div>
                    </div>

                    <div class="transactions-table-wrapper">
                        <table class="students-table">
                            <thead>
                                <tr>
                                    <th><?php echo e(__('instructor.date_col')); ?></th>
                                    <th><?php echo e(__('instructor.type_col')); ?></th>
                                    <th><?php echo e(__('instructor.description_col')); ?></th>
                                    <th><?php echo e(__('instructor.student_col')); ?></th>
                                    <th><?php echo e(__('instructor.amount_col')); ?></th>
                                    <th><?php echo e(__('instructor.status_col')); ?></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $__empty_1 = true; $__currentLoopData = $paginatedTransactions; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $transaction): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                <tr class="transaction-row" data-type="<?php echo e($transaction['type']); ?>">
                                    <td><?php echo e(\Illuminate\Support\Carbon::parse($transaction['date'])->format('M d, Y')); ?>

                                    </td>
                                    <td>
                                        <span class="transaction-type-badge <?php echo e($transaction['type']); ?>">
                                            <?php if($transaction['type'] === 'sale'): ?>
                                                <i class="fa-solid fa-shopping-cart"></i> <?php echo e(__('instructor.sale_type')); ?>

                                            <?php elseif($transaction['type'] === 'subscription'): ?>
                                                <i class="fa-solid fa-repeat"></i> Subscription
                                            <?php else: ?>
                                                <i class="fa-solid fa-money-bill-transfer"></i> <?php echo e(__('instructor.withdrawal_type')); ?>

                                            <?php endif; ?>
                                        </span>
                                    </td>
                                    <td>
                                        <?php echo e($transaction['description']); ?>

                                        <?php if(!empty($transaction['sub_description'])): ?>
                                            <div style="font-size: 11px; color: #64748b; margin-top: 2px;">
                                                <i class="fa-solid fa-layer-group" style="font-size: 10px;"></i> <?php echo e($transaction['sub_description']); ?>

                                            </div>
                                        <?php endif; ?>
                                    </td>
                                    <td><?php echo e($transaction['student'] ?? '-'); ?></td>
                                    <td>
                                        <?php ($amountValue = (float) ($transaction['amount'] ?? 0)); ?>
                                        <span
                                            class="transaction-amount <?php echo e($transaction['type'] === 'withdrawal' ? 'negative' : 'positive'); ?>">
                                            <?php echo e($amountValue >= 0 ? '+' : '-'); ?><?php echo e(currency_format(abs($amountValue))); ?>

                                        </span>
                                        <?php if($transaction['type'] === 'sale' && !empty($transaction['discount_amount']) && $transaction['discount_amount'] > 0): ?>
                                            <div style="font-size: 11px; margin-top: 3px; font-weight: normal; color: #10b981;">
                                                <i class="fa-solid fa-tag"></i> 
                                                -<?php echo e(currency_format($transaction['discount_amount'])); ?>

                                                <?php if(!empty($transaction['coupon_code'])): ?>
                                                    <span style="font-size: 9px; padding: 1px 4px; background-color: #f1f5f9; border: 1px solid #cbd5e1; border-radius: 3px; color: #475569; margin-left: 3px;">
                                                        <?php echo e($transaction['coupon_code']); ?>

                                                    </span>
                                                <?php endif; ?>
                                            </div>
                                        <?php endif; ?>
                                    </td>
                                    <td>
                                        <span class="status-badge-table <?php echo e($transaction['status']); ?>">
                                            <?php echo e(ucfirst($transaction['status'])); ?>

                                        </span>
                                    </td>
                                </tr>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                <tr>
                                    <td colspan="6" class="text-center dashboard-empty-padding">
                                        <div class="dashboard-empty-state">
                                            <i class="fa-solid fa-receipt"></i>
                                            <h3><?php echo e(__('instructor.no_transactions')); ?></h3>
                                            <p><?php echo e(__('instructor.no_transactions_desc')); ?></p>
                                        </div>
                                    </td>
                                </tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>

                    <!-- Pagination -->
                    <?php if($pagination['lastPage'] > 1): ?>
                        <div class="table-pagination">
                            <div class="pagination-info">
                                <?php echo e(__('instructor.showing_info', [
                                    'from' => $pagination['from'],
                                    'to' => $pagination['to'],
                                    'total' => $pagination['total']
                                ])); ?>

                            </div>
                            <div class="pagination-links">
                                <?php if($pagination['currentPage'] > 1): ?>
                                    <a href="<?php echo e(request()->fullUrlWithQuery(['page' => $pagination['currentPage'] - 1])); ?>"
                                        class="pagination-btn">
                                        <i class="fa-solid fa-chevron-left"></i> <?php echo e(__('instructor.prev_btn')); ?>

                                    </a>
                                <?php else: ?>
                                    <span class="pagination-btn disabled">
                                        <i class="fa-solid fa-chevron-left"></i> <?php echo e(__('instructor.prev_btn')); ?>

                                    </span>
                                <?php endif; ?>

                                <div class="pagination-numbers">
                                    <?php for($i = max(1, $pagination['currentPage'] - 2); $i <= min($pagination['lastPage'], $pagination['currentPage'] + 2); $i++): ?>
                                        <?php if($i === $pagination['currentPage']): ?>
                                            <span class="pagination-number active"><?php echo e($i); ?></span>
                                        <?php else: ?>
                                            <a href="<?php echo e(request()->fullUrlWithQuery(['page' => $i])); ?>"
                                                class="pagination-number"><?php echo e($i); ?></a>
                                        <?php endif; ?>
                                    <?php endfor; ?>
                                </div>

                                <?php if($pagination['currentPage'] < $pagination['lastPage']): ?>
                                    <a href="<?php echo e(request()->fullUrlWithQuery(['page' => $pagination['currentPage'] + 1])); ?>"
                                        class="pagination-btn">
                                        <?php echo e(__('instructor.next_btn')); ?> <i class="fa-solid fa-chevron-right"></i>
                                    </a>
                                <?php else: ?>
                                    <span class="pagination-btn disabled">
                                        <?php echo e(__('instructor.next_btn')); ?> <i class="fa-solid fa-chevron-right"></i>
                                    </span>
                                <?php endif; ?>
                            </div>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</section>

<?php $__env->startPush('scripts'); ?>
    <script src="<?php echo e(asset('assets/front/js/chart.js')); ?>"></script>
    <script>
        $(document).ready(function () {
            const chartSeries = <?php echo json_encode($chartSeries, 15, 512) ?>;
            const chartCurrency = '<?php echo e(currency_code()); ?>';
            let currentRange = '<?php echo e($defaultRange); ?>';

            function getDataset(range) {
                const fallback = chartSeries['last_12_months'] || [];
                return chartSeries[range] && chartSeries[range].length ? chartSeries[range] : fallback;
            }

            function mapLabels(dataset) {
                return dataset.map(item => item.month_label || item.month);
            }

            function mapData(dataset) {
                return dataset.map(item => {
                    const numeric = parseFloat(item.earnings ?? 0);
                    return Number.isNaN(numeric) ? 0 : parseFloat(numeric.toFixed(2));
                });
            }

            // Earnings Chart
            const ctx = document.getElementById('earningsChart');
            if (ctx) {
                const initialDataset = getDataset(currentRange);
                const earningsChart = new Chart(ctx, {
                    type: 'line',
                    data: {
                        labels: mapLabels(initialDataset),
                        datasets: [{
                            label: 'Earnings (' + chartCurrency + ')',
                            data: mapData(initialDataset),
                            borderColor: 'rgb(0, 79, 68)',
                            backgroundColor: 'rgba(0, 79, 68, 0.1)',
                            tension: 0.4,
                            fill: true,
                            pointBackgroundColor: 'rgb(0, 79, 68)',
                            pointBorderColor: '#fff',
                            pointBorderWidth: 2,
                            pointRadius: 5,
                            pointHoverRadius: 7,
                        }]
                    },
                    options: {
                        responsive: true,
                        maintainAspectRatio: false,
                        plugins: {
                            legend: {
                                display: false
                            }
                        },
                        scales: {
                            y: {
                                beginAtZero: true,
                                ticks: {
                                    callback: function (value) {
                                        const numeric = Number(value);
                                        if (Number.isNaN(numeric)) {
                                            return chartCurrency + ' 0.00';
                                        }
                                        return chartCurrency + ' ' + numeric.toFixed(2);
                                    }
                                }
                            }
                        }
                    }
                });

                const rangeSelect = document.getElementById('earningsRange');
                if (rangeSelect) {
                    rangeSelect.addEventListener('change', function () {
                        currentRange = this.value;
                        const dataset = getDataset(currentRange);
                        earningsChart.data.labels = mapLabels(dataset);
                        earningsChart.data.datasets[0].data = mapData(dataset);
                        earningsChart.update();
                    });
                }
            }

            // Transaction Filter
            $('.reviews-filters .filter-btn').on('click', function () {
                const filter = $(this).data('filter');

                // Update active button
                $('.reviews-filters .filter-btn').removeClass('active');
                $(this).addClass('active');

                // Filter transaction rows
                if (filter === 'all') {
                    $('.transaction-row').fadeIn(300);
                } else {
                    $('.transaction-row').each(function () {
                        if ($(this).data('type') === filter) {
                            $(this).fadeIn(300);
                        } else {
                            $(this).fadeOut(300);
                        }
                    });
                }
            });
        });
    </script>
<?php $__env->stopPush(); ?>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.instructor', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /Users/mahmudul/Codecanyone/edu_ex_full/Eduex_laravel/resources/views/instructor/earnings.blade.php ENDPATH**/ ?>