<?php /** @var array $settledSales */ ?>
<?php /** @var string $author */ ?>

<?= $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-clock-history me-2"></i>Settlement History
        </h1>
        <p class="text-muted mb-0">
            <?php if ($author): ?>
                Settlement history for <strong class="text-primary"><?= esc($author) ?></strong>
            <?php else: ?>
                All settlement history for the last 30 days
            <?php endif; ?>
        </p>
    </div>
    <div>
        <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>

<!-- Settlement Summary -->
<?php if (!empty($settledSales)): ?>
<?php 
$totalSettled = count($settledSales);
$totalAmount = array_sum(array_column($settledSales, 'amount'));
$totalAuthorShare = $totalAmount * 0.6;
$totalPlatformShare = $totalAmount * 0.4;
?>
<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 Settled
                        </div>
                        <div class="h5 mb-0 font-weight-bold text-gray-800"><?= number_format($totalSettled) ?></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-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 Amount
                        </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 Paid (60%)
                        </div>
                        <div class="h5 mb-0 font-weight-bold text-gray-800"><?= number_format($totalAuthorShare, 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($totalPlatformShare, 2) ?> UGX</div>
                    </div>
                    <div class="col-auto">
                        <i class="bi bi-gear fa-2x text-warning"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<?php endif; ?>

<!-- Settlement History 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>Settled Sales History
        </h6>
        <div class="text-muted small">
            <i class="bi bi-calendar3 me-1"></i>Last 30 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-hash me-1"></i>Tip ID
                        </th>
                        <th class="border-0">
                            <i class="bi bi-person me-1"></i>Author
                        </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>Author Share
                        </th>
                        <th class="border-0">
                            <i class="bi bi-calendar3 me-1"></i>Settled Date
                        </th>
                        <th class="border-0">
                            <i class="bi bi-person-check me-1"></i>Settled By
                        </th>
                        <th class="border-0">
                            <i class="bi bi-tag me-1"></i>Tag
                        </th>
                        <th class="border-0">
                            <i class="bi bi-chat-text me-1"></i>Notes
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($settledSales)): ?>
                        <tr>
                            <td colspan="9" class="text-center py-4 text-muted">
                                <i class="bi bi-inbox display-6 d-block mb-2"></i>
                                No settlement history found
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php foreach ($settledSales as $sale): ?>
                            <tr>
                                <td>
                                    <span class="badge bg-primary">#<?= $sale['tip_id'] ?></span>
                                </td>
                                <td>
                                    <div class="d-flex align-items-center">
                                        <div class="rounded-circle bg-info text-white d-flex align-items-center justify-content-center me-2" 
                                             style="width: 24px; height: 24px; font-size: 12px;">
                                            <?= strtoupper(substr($sale['author'], 0, 1)) ?>
                                        </div>
                                        <span class="fw-medium"><?= esc($sale['author']) ?></span>
                                    </div>
                                </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['settled_at'])) ?></span>
                                </td>
                                <td>
                                    <span class="badge bg-secondary"><?= esc($sale['settled_by']) ?></span>
                                </td>
                                <td>
                                    <?php if (!empty($sale['tag'])): ?>
                                        <span class="badge bg-light text-dark"><?= esc($sale['tag']) ?></span>
                                    <?php else: ?>
                                        <span class="text-muted">-</span>
                                    <?php endif; ?>
                                </td>
                                <td>
                                    <?php if (!empty($sale['settlement_notes'])): ?>
                                        <span class="text-muted small" title="<?= esc($sale['settlement_notes']) ?>">
                                            <?= strlen($sale['settlement_notes']) > 30 ? substr($sale['settlement_notes'], 0, 30) . '...' : $sale['settlement_notes'] ?>
                                        </span>
                                    <?php else: ?>
                                        <span class="text-muted">-</span>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<!-- Settlement 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>Settlement Policy
                </h6>
                <div class="row">
                    <div class="col-md-6">
                        <p class="text-muted mb-2">
                            <strong>Author Share:</strong> Authors receive <span class="text-info fw-bold">60%</span> of each settled 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($totalAuthorShare ?? 0, 2) ?> UGX</div>
                                <small class="text-muted">Total Paid to Authors</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;
}
.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>

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