<?php /** @var string $author */ ?>
<?php /** @var array $sales */ ?>
<?php /** @var int $totalSales */ ?>
<?php /** @var float $totalAmount */ ?>
<?php /** @var float $authorShare */ ?>
<?php /** @var float $adminShare */ ?>
<?php /** @var array $settlementSummary */ ?>

<?= $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-person-circle me-2"></i>Author Sales Report
        </h1>
        <p class="text-muted mb-0">
            Sales performance for <strong class="text-primary"><?= esc($author) ?></strong>
        </p>
    </div>
    <div class="d-flex gap-2">
        <a href="<?= site_url('admin/tip-sales/settlement-history/' . urlencode($author)) ?>" class="btn btn-outline-info">
            <i class="bi bi-clock-history me-1"></i>Settlement History
        </a>
        <a href="<?= site_url('admin/tip-sales') ?>" class="btn btn-outline-secondary">
            <i class="bi bi-arrow-left me-1"></i>Back to Sales
        </a>
    </div>
</div>

<!-- Author 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">
                            Author Earnings (60%)
                        </div>
                        <div class="h5 mb-0 font-weight-bold text-gray-800"><?= number_format($authorShare, 2) ?> UGX</div>
                    </div>
                    <div class="col-auto">
                        <i class="bi bi-wallet2 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($adminShare, 2) ?> UGX</div>
                    </div>
                    <div class="col-auto">
                        <i class="bi bi-gear fa-2x text-warning"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Settlement Summary Cards -->
<?php if ($settlementSummary): ?>
<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">
                            Settled Sales
                        </div>
                        <div class="h5 mb-0 font-weight-bold text-gray-800"><?= number_format($settlementSummary['settled_count']) ?></div>
                        <div class="text-xs text-muted"><?= number_format($settlementSummary['settled_amount'], 2) ?> UGX</div>
                    </div>
                    <div class="col-auto">
                        <i class="bi bi-check-circle 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-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">
                            Pending Settlement
                        </div>
                        <div class="h5 mb-0 font-weight-bold text-gray-800"><?= number_format($settlementSummary['unsettled_count']) ?></div>
                        <div class="text-xs text-muted"><?= number_format($settlementSummary['unsettled_amount'], 2) ?> UGX</div>
                    </div>
                    <div class="col-auto">
                        <i class="bi bi-clock fa-2x text-warning"></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">
                            Settled Earnings
                        </div>
                        <div class="h5 mb-0 font-weight-bold text-gray-800"><?= number_format($settlementSummary['settled_amount'] * 0.6, 2) ?> UGX</div>
                        <div class="text-xs text-muted">60% of settled amount</div>
                    </div>
                    <div class="col-auto">
                        <i class="bi bi-wallet2 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-danger 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-danger text-uppercase mb-1">
                            Pending Earnings
                        </div>
                        <div class="h5 mb-0 font-weight-bold text-gray-800"><?= number_format($settlementSummary['unsettled_amount'] * 0.6, 2) ?> UGX</div>
                        <div class="text-xs text-muted">60% of pending amount</div>
                    </div>
                    <div class="col-auto">
                        <i class="bi bi-hourglass-split fa-2x text-danger"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<?php endif; ?>

<!-- Revenue Distribution Visualization -->
<div class="row mb-4">
    <div class="col-md-6">
        <div class="card border-left-info shadow h-100">
            <div class="card-header py-3">
                <h6 class="m-0 font-weight-bold text-info">
                    <i class="bi bi-pie-chart me-2"></i>Revenue Distribution
                </h6>
            </div>
            <div class="card-body">
                <div class="mb-3">
                    <div class="d-flex justify-content-between align-items-center mb-2">
                        <span class="text-info fw-medium">Author Earnings (60%)</span>
                        <span class="fw-bold text-info"><?= number_format($authorShare, 2) ?> UGX</span>
                    </div>
                    <div class="progress mb-3" style="height: 8px;">
                        <div class="progress-bar bg-info" style="width: 60%"></div>
                    </div>
                </div>
                <div class="mb-3">
                    <div class="d-flex justify-content-between align-items-center mb-2">
                        <span class="text-warning fw-medium">Platform Share (40%)</span>
                        <span class="fw-bold text-warning"><?= number_format($adminShare, 2) ?> UGX</span>
                    </div>
                    <div class="progress" style="height: 8px;">
                        <div class="progress-bar bg-warning" style="width: 40%"></div>
                    </div>
                </div>
                <div class="text-center mt-3">
                    <small class="text-muted">
                        <i class="bi bi-info-circle me-1"></i>
                        Revenue sharing based on platform policy
                    </small>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-6">
        <div class="card border-left-success shadow h-100">
            <div class="card-header py-3">
                <h6 class="m-0 font-weight-bold text-success">
                    <i class="bi bi-trophy me-2"></i>Performance Metrics
                </h6>
            </div>
            <div class="card-body">
                <div class="row text-center">
                    <div class="col-6">
                        <div class="border-end">
                            <div class="h4 mb-0 text-success"><?= number_format($totalSales) ?></div>
                            <small class="text-muted">Total Sales</small>
                        </div>
                    </div>
                    <div class="col-6">
                        <div class="h4 mb-0 text-primary"><?= $totalSales > 0 ? number_format($totalAmount / $totalSales, 2) : '0.00' ?> UGX</div>
                        <small class="text-muted">Avg Sale Value</small>
                    </div>
                </div>
                <hr>
                <div class="text-center">
                    <div class="h5 mb-0 text-info"><?= number_format($authorShare, 2) ?> UGX</div>
                    <small class="text-muted">Total Author Earnings</small>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Settlement Actions -->
<?php if ($settlementSummary && $settlementSummary['unsettled_count'] > 0): ?>
<div class="card border-left-warning shadow mb-4">
    <div class="card-header py-3">
        <h6 class="m-0 font-weight-bold text-warning">
            <i class="bi bi-gear me-2"></i>Settlement Actions
        </h6>
    </div>
    <div class="card-body">
        <div class="row align-items-center">
            <div class="col-md-8">
                <p class="mb-2">
                    <strong><?= number_format($settlementSummary['unsettled_count']) ?> sales</strong> are pending settlement 
                    (<?= number_format($settlementSummary['unsettled_amount'] * 0.6, 2) ?> UGX for author).
                </p>
                <small class="text-muted">
                    <i class="bi bi-info-circle me-1"></i>
                    Mark sales as settled once payment has been made to the author
                </small>
            </div>
            <div class="col-md-4 text-end">
                <form method="post" action="<?= site_url('admin/tip-sales/settle-all/' . urlencode($author)) ?>" 
                      onsubmit="return confirm('Mark all <?= number_format($settlementSummary['unsettled_count']) ?> unsettled sales as settled?');">
                    <button type="submit" class="btn btn-success">
                        <i class="bi bi-check-circle me-1"></i>Settle All Pending
                    </button>
                </form>
            </div>
        </div>
    </div>
</div>
<?php endif; ?>

<!-- Sales Details 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-list-ul me-2"></i>Individual Sales Details
        </h6>
        <div class="text-muted small">
            <i class="bi bi-calendar3 me-1"></i>Recent sales activity
        </div>
    </div>
    <div class="card-body">
        <form method="post" action="<?= site_url('admin/tip-sales/mark-settled') ?>" id="settlementForm">
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead class="table-light">
                        <tr>
                            <th class="border-0">
                                <input type="checkbox" id="selectAll" class="form-check-input">
                            </th>
                            <th class="border-0">
                                <i class="bi bi-hash me-1"></i>Tip ID
                            </th>
                            <th class="border-0">
                                <i class="bi bi-phone me-1"></i>Customer Phone
                            </th>
                            <th class="border-0">
                                <i class="bi bi-currency-exchange me-1"></i>Amount
                            </th>
                            <th class="border-0">
                                <i class="bi bi-wallet2 me-1"></i>Your Share
                            </th>
                            <th class="border-0">
                                <i class="bi bi-calendar3 me-1"></i>Date
                            </th>
                            <th class="border-0">
                                <i class="bi bi-tag me-1"></i>Tag
                            </th>
                            <th class="border-0">
                                <i class="bi bi-check-circle me-1"></i>Status
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($sales)): ?>
                            <tr>
                                <td colspan="8" class="text-center py-4 text-muted">
                                    <i class="bi bi-inbox display-6 d-block mb-2"></i>
                                    No sales records found for this author
                                </td>
                            </tr>
                        <?php else: ?>
                            <?php foreach ($sales as $sale): ?>
                                <tr>
                                    <td>
                                        <?php if (!$sale['is_settled']): ?>
                                            <input type="checkbox" name="sale_ids[]" value="<?= $sale['id'] ?>" class="form-check-input sale-checkbox">
                                        <?php endif; ?>
                                    </td>
                                    <td>
                                        <span class="badge bg-primary">#<?= $sale['tip_id'] ?></span>
                                    </td>
                                    <td>
                                        <span class="font-monospace text-muted"><?= esc($sale['msisdn']) ?></span>
                                    </td>
                                    <td>
                                        <span class="fw-bold text-success"><?= number_format($sale['amount'], 2) ?> UGX</span>
                                    </td>
                                    <td>
                                        <span class="fw-bold text-info"><?= number_format($sale['amount'] * 0.6, 2) ?> UGX</span>
                                    </td>
                                    <td>
                                        <span class="text-muted"><?= date('M d, Y H:i', strtotime($sale['created_at'])) ?></span>
                                    </td>
                                    <td>
                                        <?php if (!empty($sale['tag'])): ?>
                                            <span class="badge bg-secondary"><?= esc($sale['tag']) ?></span>
                                        <?php else: ?>
                                            <span class="text-muted">-</span>
                                        <?php endif; ?>
                                    </td>
                                    <td>
                                        <?php if ($sale['is_settled']): ?>
                                            <span class="badge bg-success">
                                                <i class="bi bi-check-circle me-1"></i>Settled
                                            </span>
                                            <?php if ($sale['settled_at']): ?>
                                                <br><small class="text-muted"><?= date('M d, Y', strtotime($sale['settled_at'])) ?></small>
                                            <?php endif; ?>
                                        <?php else: ?>
                                            <span class="badge bg-warning">
                                                <i class="bi bi-clock me-1"></i>Pending
                                            </span>
                                        <?php endif; ?>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
            
            <?php if (!empty($sales) && array_filter($sales, function($sale) { return !$sale['is_settled']; })): ?>
            <div class="mt-3 p-3 bg-light rounded">
                <div class="row align-items-center">
                    <div class="col-md-8">
                        <div class="form-group mb-0">
                            <label for="notes" class="form-label">Settlement Notes (Optional):</label>
                            <textarea name="notes" id="notes" class="form-control" rows="2" placeholder="Add any notes about the settlement..."></textarea>
                        </div>
                    </div>
                    <div class="col-md-4 text-end">
                        <button type="submit" class="btn btn-success" id="settleSelected" disabled>
                            <i class="bi bi-check-circle me-1"></i>Mark Selected as Settled
                        </button>
                    </div>
                </div>
            </div>
            <?php endif; ?>
        </form>
    </div>
</div>

<!-- Revenue Policy 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-2">
                    <i class="bi bi-info-circle me-2"></i>Revenue Sharing Policy
                </h6>
                <div class="row">
                    <div class="col-md-6">
                        <p class="text-muted mb-2">
                            <strong>Author Share:</strong> You receive <span class="text-info fw-bold">60%</span> of each sale
                        </p>
                        <p class="text-muted mb-0">
                            <strong>Platform Share:</strong> <span class="text-warning fw-bold">40%</span> supports platform maintenance
                        </p>
                    </div>
                    <div class="col-md-6">
                        <div class="bg-light p-3 rounded">
                            <div class="text-center">
                                <div class="h5 mb-1 text-info"><?= number_format($authorShare, 2) ?> UGX</div>
                                <small class="text-muted">Total Earnings</small>
                            </div>
                        </div>
                    </div>
                </div>
            </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;
}
.border-left-danger {
    border-left: 4px solid #dc3545 !important;
}
.text-xs {
    font-size: 0.75rem;
}
.fa-2x {
    font-size: 2em;
}
.card:hover {
    transform: translateY(-2px);
    transition: transform 0.2s;
}
.font-monospace {
    font-family: 'Courier New', monospace;
}
</style>

<script>
// Handle select all functionality
document.getElementById('selectAll').addEventListener('change', function() {
    const checkboxes = document.querySelectorAll('.sale-checkbox');
    checkboxes.forEach(checkbox => {
        checkbox.checked = this.checked;
    });
    updateSettleButton();
});

// Handle individual checkbox changes
document.querySelectorAll('.sale-checkbox').forEach(checkbox => {
    checkbox.addEventListener('change', updateSettleButton);
});

function updateSettleButton() {
    const checkedBoxes = document.querySelectorAll('.sale-checkbox:checked');
    const settleButton = document.getElementById('settleSelected');
    
    if (settleButton) {
        settleButton.disabled = checkedBoxes.length === 0;
        if (checkedBoxes.length > 0) {
            settleButton.textContent = `Mark ${checkedBoxes.length} as Settled`;
        } else {
            settleButton.textContent = 'Mark Selected as Settled';
        }
    }
}

// Update select all when individual checkboxes change
document.querySelectorAll('.sale-checkbox').forEach(checkbox => {
    checkbox.addEventListener('change', function() {
        const allCheckboxes = document.querySelectorAll('.sale-checkbox');
        const checkedCheckboxes = document.querySelectorAll('.sale-checkbox:checked');
        const selectAllCheckbox = document.getElementById('selectAll');
        
        if (checkedCheckboxes.length === 0) {
            selectAllCheckbox.indeterminate = false;
            selectAllCheckbox.checked = false;
        } else if (checkedCheckboxes.length === allCheckboxes.length) {
            selectAllCheckbox.indeterminate = false;
            selectAllCheckbox.checked = true;
        } else {
            selectAllCheckbox.indeterminate = true;
        }
    });
});
</script>

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