<?php $title = ___('Affiliate stats'); $this->setLayout('member/layout.phtml');
echo $this->blocks('aff/top');
echo $this->blocks('aff/stats/top');
if ($rows) :
    $this->enableReports();
    $rows = array_reverse($rows, true);
?>
<h2><?php __e('Commission') ?></h2>
<?php echo $reportStat; ?>
<h2><?php __e('Unique Clicks/All Clicks/Sales') ?></h2>
<?php echo $reportClick; ?>
<?php if (!empty($reportStatProduct)) : ?>
<h2><?php __e('Commission By Product') ?></h2>
<?php echo $reportStatProduct; ?>
<?php endif; ?>
<h2><?php __e('Report') ?><?php if (isset($period)) :?> (<?php p(amDate($period[0])); ?>&ndash;<?php p(amDate($period[1])) ?>)<?php endif;?></h2>
<?php if (isset($period)) : ?>
<p><a href="<?php echo $this->url('aff/member/stats') ?>"><?php __e('back to full report') ?></a></p>
<?php endif; ?>
<div class="am-grid-container">
    <table class="am-grid">
        <tr>
            <th><?php __e('Date') ?></th>
            <th style="text-align:right"><?php __e('Commission Earned') ?></th>
            <th style="text-align:right"><?php __e('All/Unique Clicks') ?></th>
        </tr>
        <?php foreach ($rows as $key => $row) : ?>
        <tr class="am-grid-row">
            <td><?php echo $row['date_href'] ? sprintf('<a href="%s" class="%s">%s</a>',
                $row['date_href'],
                (strpos($row['date_href'], 'javascript:') === 0 ? 'local' : ''),
                $this->escape($row['date'])) : $this->escape($row['date']); ?></td>
            <?php $comm = doubleval($row['commission']) >= 0 ?
                Am_Currency::render($row['commission']) :
                '- ' . Am_Currency::render(abs(doubleval($row['commission']))); ?>
            <td style="text-align:right"><?php p($comm) ?></td>
            <?php $click = !$row['clicks_all'] ? '&ndash;' : sprintf('%d/%d', $row['clicks_all'], $row['clicks']); ?>
            <td style="text-align:right"><?php echo $row['clicks_href'] ? sprintf('<a href="%s" class="local aff-detail">%s</a>',$row['clicks_href'], $click) : $click ?></td>
        </tr>
        <tr class="am-grid-row" id="detail-<?php p($key) ?>" class="aff-details" style="display: none">
            <td colspan="3">
                <div style="float: right;">
                    <a href="javascript:;" class="local detail-close"><?php __e('Close') ?></a>
                </div>
                <div class="content"></div>
            </td>
        </tr>
        <?php endforeach; ?>
        <tr class="am-grid-row" style="font-weight: bold">
            <td><?php __e('TOTAL') ?></td>
            <?php $comm = $totals['commission'] >= 0 ?
                Am_Currency::render($totals['commission']) :
                '- ' . Am_Currency::render(abs(floatval($totals['commission']))); ?>
            <td style="text-align:right"><?php p($comm) ?></td>
            <td style="text-align:right"><?php echo !$totals['clicks_all'] ? '&ndash;' : sprintf('%d/%d', $totals['clicks_all'], $totals['clicks']); ?></td>
        </tr>
    </table>
</div>
<div id="aff-detail"></div>
<script type="text/javascript">
    jQuery(document).ready(function($) {
        jQuery("a.detail-close").click(function(){
            jQuery(this).parents("tr").hide();
        });
    });
    function showAffDetails(date)
    {
        var url = "<?php echo j($this->url('aff/member/stats', ['detailDate'=>'DATE'], false)) ?>";
        jQuery("tr#detail-"+date).toggle();
        jQuery("tr#detail-"+date+":visible .content").load(url.replace(/DATE/, date));
    }
    function affDetail(from, to, label)
    {
        var url = "<?php j($this->url('aff/member/clicks-detail', ['from' => 'FROMDATE', 'to' => 'TODATE'], false)) ?>";
        jQuery("#aff-detail").load(url.replace(/FROMDATE/, encodeURIComponent(from)).replace(/TODATE/, encodeURIComponent(to)), function(){
            jQuery("#aff-detail").amPopup({
                title: '<?php __e('Affiliate Clicks for') ?>' + ' ' + label
                ,height: 400
            });
        });
    }
</script>
<div class="am-aff-stats-export">
    <a class="am-aff-stats-export-report" href="<?php echo $this->url('aff/member/stats', "monthyear=$monthyear&csv=1") ?>"><?php __e('Download Report (CSV File)') ?></a>
    <?php if ($canViewDetails) : ?>
    |
    <a class="am-aff-stats-export-details" href="<?php echo $this->url('aff/member/export-details', "monthyear=$monthyear") ?>"><?php __e('Download Transaction (CSV File)') ?></a>
    <?php endif; ?>
</div>
<?php else : //$rows ?>
<div class="am-block-nodata">
    <p><?php __e('There is not any statistical data yet'); ?></p>
</div>
<?php endif;
echo $this->blocks('aff/stats/bottom');