<?php /** @var array $sales */ ?>
<?php /** @var int $totalSales */ ?>
<?php /** @var float $totalAmount */ ?>

<?= $this->extend('admin/layout') ?>
<?= $this->section('content') ?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="h3 mb-0 text-gray-800">
            <i class="bi bi-bar-chart me-2"></i>Tip Sales
        </h1>
        <p class="text-muted mb-0">Sales performance for the last 7 days</p>
    </div>
    <div class="d-flex gap-2">
        <a href="<?= site_url('admin/tip-sales/settlement-history') ?>" class="btn btn-outline-info">
            <i class="bi bi-clock-history me-1"></i>Settlement History
        </a>
        <form method="post" action="<?= site_url('admin/tip-sales/reset') ?>" 
              onsubmit="return confirm('Are you sure you want to reset all sales from the last 7 days?');">
            <button type="submit" class="btn btn-outline-danger">
                <i class="bi bi-arrow-clockwise me-1"></i>Reset Sales
            </button>
        </form>
        <form method="post" action="<?= site_url('admin/transaction/reconcile-missing-sales') ?>" 
              onsubmit="return confirm('Run reconciliation? This will backfill missing sales from past successful transactions.');">
            <button type="submit" class="btn btn-outline-warning">
                <i class="bi bi-arrow-repeat me-1"></i>Reconcile
            </button>
        </form>
    </div>
</div>

<!-- Stats Cards -->
<div class="row mb-4">
    <div class="col-xl-3 col-md-6 mb-4">
        <div class="card border-left-success shadow h-100 py-2">
            <div class="card-body">
                <div class="row no-gutters align-items-center">
                    <div class="col mr-2">
                        <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
                            Total Sales
                        </div>
                        <div class="h5 mb-0 font-weight-bold text-gray-800"><?= number_format($totalSales) ?></div>
                    </div>
                    <div class="col-auto">
                        <i class="bi bi-graph-up-arrow fa-2x text-success"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <div class="col-xl-3 col-md-6 mb-4">
        <div class="card border-left-primary shadow h-100 py-2">
            <div class="card-body">
                <div class="row no-gutters align-items-center">
                    <div class="col mr-2">
                        <div class="text-xs font-weight-bold text-primary text-uppercase mb-1">
                            Total Revenue
                        </div>
                        <div class="h5 mb-0 font-weight-bold text-gray-800"><?= number_format($totalAmount, 2) ?> UGX</div>
                    </div>
                    <div class="col-auto">
                        <i class="bi bi-cash-stack fa-2x text-primary"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <div class="col-xl-3 col-md-6 mb-4">
        <div class="card border-left-info shadow h-100 py-2">
            <div class="card-body">
                <div class="row no-gutters align-items-center">
                    <div class="col mr-2">
                        <div class="text-xs font-weight-bold text-info text-uppercase mb-1">
                            Authors Share (60%)
                        </div>
                        <div class="h5 mb-0 font-weight-bold text-gray-800"><?= number_format($totalAmount * 0.6, 2) ?> UGX</div>
                    </div>
                    <div class="col-auto">
                        <i class="bi bi-person-check fa-2x text-info"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <div class="col-xl-3 col-md-6 mb-4">
        <div class="card border-left-warning shadow h-100 py-2">
            <div class="card-body">
                <div class="row no-gutters align-items-center">
                    <div class="col mr-2">
                        <div class="text-xs font-weight-bold text-warning text-uppercase mb-1">
                            Platform Share (40%)
                        </div>
                        <div class="h5 mb-0 font-weight-bold text-gray-800"><?= number_format($totalAmount * 0.4, 2) ?> UGX</div>
                    </div>
                    <div class="col-auto">
                        <i class="bi bi-gear fa-2x text-warning"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Authors Table -->
<div class="card shadow mb-4">
    <div class="card-header py-3 d-flex justify-content-between align-items-center">
        <h6 class="m-0 font-weight-bold text-primary">
            <i class="bi bi-people me-2"></i>Authors Performance
        </h6>
        <div class="text-muted small">
            <i class="bi bi-info-circle me-1"></i>Last 7 days
        </div>
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-hover">
                <thead class="table-light">
                    <tr>
                        <th class="border-0">
                            <i class="bi bi-person me-1"></i>Author
                        </th>
                        <th class="border-0">
                            <i class="bi bi-hash me-1"></i>Sales Count
                        </th>
                        <th class="border-0">
                            <i class="bi bi-currency-exchange me-1"></i>Total Amount
                        </th>
                        <th class="border-0">
                            <i class="bi bi-piggy-bank me-1"></i>Author Share (60%)
                        </th>
                        <th class="border-0">
                            <i class="bi bi-check-circle me-1"></i>Settlement Status
                        </th>
                        <th class="border-0 text-center">
                            <i class="bi bi-gear me-1"></i>Actions
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($authors)): ?>
                        <tr>
                            <td colspan="6" class="text-center py-4 text-muted">
                                <i class="bi bi-inbox display-6 d-block mb-2"></i>
                                No sales data available for the last 7 days
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php foreach ($authors as $author): ?>
                            <?php 
                            $settledAmount = $author['settled_amount'] ?? 0;
                            $unsettledAmount = $author['unsettled_amount'] ?? $author['total_amount'];
                            $settledCount = $author['settled_count'] ?? 0;
                            $unsettledCount = $author['unsettled_count'] ?? $author['sales_count'];
                            ?>
                            <tr>
                                <td>
                                    <div class="d-flex align-items-center">
                                        <div class="rounded-circle bg-primary text-white d-flex align-items-center justify-content-center me-2" 
                                             style="width: 32px; height: 32px; font-size: 14px;">
                                            <?= strtoupper(substr($author['author'], 0, 1)) ?>
                                        </div>
                                        <span class="fw-medium"><?= esc($author['author']) ?></span>
                                    </div>
                                </td>
                                <td>
                                    <span class="badge bg-secondary"><?= number_format($author['sales_count']) ?></span>
                                </td>
                                <td>
                                    <span class="fw-bold text-success"><?= number_format($author['total_amount'], 2) ?> UGX</span>
                                </td>
                                <td>
                                    <span class="fw-bold text-info"><?= number_format($author['total_amount'] * 0.6, 2) ?> UGX</span>
                                </td>
                                <td>
                                    <?php if ($unsettledCount > 0): ?>
                                        <div class="d-flex flex-column">
                                            <span class="badge bg-success mb-1"><?= number_format($settledCount) ?> Settled</span>
                                            <span class="badge bg-warning"><?= number_format($unsettledCount) ?> Pending</span>
                                        </div>
                                    <?php else: ?>
                                        <span class="badge bg-success">All Settled</span>
                                    <?php endif; ?>
                                </td>
                                <td class="text-center">
                                    <div class="btn-group" role="group">
                                        <a href="<?= site_url('admin/tip-sales/author/' . urlencode($author['author'])) ?>" 
                                           class="btn btn-primary btn-sm">
                                            <i class="bi bi-eye me-1"></i>View Details
                                        </a>
                                        <?php if ($unsettledCount > 0): ?>
                                            <form method="post" action="<?= site_url('admin/tip-sales/settle-all/' . urlencode($author['author'])) ?>" 
                                                  class="d-inline" 
                                                  onsubmit="return confirm('Mark all unsettled sales for <?= esc($author['author']) ?> as settled?');">
                                                <button type="submit" class="btn btn-success btn-sm">
                                                    <i class="bi bi-check-circle me-1"></i>Settle All
                                                </button>
                                            </form>
                                        <?php endif; ?>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<!-- Revenue Distribution Info -->
<div class="card border-left-info shadow">
    <div class="card-body">
        <div class="row align-items-center">
            <div class="col">
                <h6 class="font-weight-bold text-info mb-1">
                    <i class="bi bi-pie-chart me-2"></i>Revenue Distribution
                </h6>
                <p class="text-muted mb-0">
                    Authors receive <strong>60%</strong> of sales revenue, while <strong>40%</strong> is allocated for platform maintenance and operations.
                </p>
            </div>
            <div class="col-auto">
                <i class="bi bi-info-circle fa-2x text-info"></i>
            </div>
        </div>
    </div>
</div>

<!-- Sale Details Modal -->
<div class="modal fade" id="saleModal" tabindex="-1" aria-labelledby="saleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header bg-primary text-white">
                <h5 class="modal-title" id="saleModalLabel">
                    <i class="bi bi-receipt me-2"></i>Sale Details
                </h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body" id="saleModalBody">
                <!-- Content will be injected by JS -->
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                    <i class="bi bi-x-circle me-1"></i>Close
                </button>
            </div>
        </div>
    </div>
</div>

<style>
.border-left-primary {
    border-left: 4px solid #007bff !important;
}
.border-left-success {
    border-left: 4px solid #28a745 !important;
}
.border-left-info {
    border-left: 4px solid #17a2b8 !important;
}
.border-left-warning {
    border-left: 4px solid #ffc107 !important;
}
.text-xs {
    font-size: 0.75rem;
}
.fa-2x {
    font-size: 2em;
}
.card:hover {
    transform: translateY(-2px);
    transition: transform 0.2s;
}
</style>

<script>
function showSaleModal(sale) {
    var total = parseFloat(sale.amount || 0);
    var authorShare = (total * 0.6).toFixed(2);
    var adminShare = (total * 0.4).toFixed(2);
    
    var html = `
        <div class="row">
            <div class="col-md-6">
                <div class="card border-0 bg-light">
                    <div class="card-body">
                        <h6 class="card-title text-primary">
                            <i class="bi bi-receipt me-2"></i>Sale Information
                        </h6>
                        <div class="mb-2">
                            <strong>Tip ID:</strong> 
                            <span class="badge bg-primary">#${sale.tip_id}</span>
                        </div>
                        <div class="mb-2">
                            <strong>Author:</strong> 
                            <span class="text-dark">${sale.author}</span>
                        </div>
                        <div class="mb-2">
                            <strong>Amount:</strong> 
                            <span class="fw-bold text-success">${total} UGX</span>
                        </div>
                        <div class="mb-2">
                            <strong>Tag:</strong> 
                            <span class="badge bg-secondary">${sale.tag || 'No tag'}</span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-6">
                <div class="card border-0 bg-light">
                    <div class="card-body">
                        <h6 class="card-title text-info">
                            <i class="bi bi-pie-chart me-2"></i>Revenue Distribution
                        </h6>
                        <div class="mb-3">
                            <div class="d-flex justify-content-between align-items-center mb-1">
                                <span>Author Share (60%)</span>
                                <span class="fw-bold text-success">${authorShare} UGX</span>
                            </div>
                            <div class="progress" style="height: 6px;">
                                <div class="progress-bar bg-success" style="width: 60%"></div>
                            </div>
                        </div>
                        <div class="mb-3">
                            <div class="d-flex justify-content-between align-items-center mb-1">
                                <span>Platform Share (40%)</span>
                                <span class="fw-bold text-primary">${adminShare} UGX</span>
                            </div>
                            <div class="progress" style="height: 6px;">
                                <div class="progress-bar bg-primary" style="width: 40%"></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="alert alert-info mt-3">
            <i class="bi bi-info-circle me-2"></i>
            <strong>Revenue Sharing:</strong> Authors receive 60% of each sale, while 40% supports platform maintenance and operations.
        </div>
    `;
    
    document.getElementById('saleModalBody').innerHTML = html;
    var modal = new bootstrap.Modal(document.getElementById('saleModal'));
    modal.show();
}
</script>

<?= $this->endSection() ?>