<?php if (!$this->licenses): ?>
<div class="softsale-no-licenses"><?php __e('No licenses assigned yet'); ?></div>
<?php else: ?>
<div class="am-grid-container">
<table class="am-grid">
<tr>
    <th><?php __e('Application') ?></th>
    <th><?php __e('License Key')?></th>
    <th><?php __e('Created')?></th>
    <th><?php __e('Expires')?></th>
    <?php if (!empty($this->hasActivation)):?>
    <th><?php __e('Activation')?></th>
    <?php endif ?>
</tr>
<?php foreach ($this->licenses as $license): ?>
<tr class="am-grid-row<?php echo (@$rownum++%2) ? ' odd' : '' ?>">
    <td><?php p($license->getScheme()->title) ?></td>
    <td class="softsale-license-td"><?php echo $license->renderKey(32) ?></td>
    <td><?php p(amDate($license->created)) ?></td>
    <td><?php p(date('Y-m-d', strtotime($license->expires))  == Am_Period::MAX_SQL_DATE ? ___('Lifetime') : amDate($license->expires)) ?></td>
    <?php if (!empty($this->hasActivation)):?>
    <td><?php echo amRenderActivation($license)?></td>
    <?php endif ?>
</tr>
<?php endforeach; ?>
</table>
</div>
<?php endif; ?>
<?php
function amRenderActivation(License $license)
{
    if (!$license->needActivation())
        return '';
    if ($license->isActivated())
    {
        if (Am_Di::getInstance()->config->get('softsale.show_activation_details'))
        { // show license activation details if enabled in module config
            $ret = "";
            foreach ($license->getActivations() as $licenseActivation)
            {
                if(Am_Di::getInstance()->config->get('softsale.hide_disabled_activation') && $licenseActivation->is_disabled) continue;
                $s = implode(", ", array_values($licenseActivation->getRequest()));
                $ret .= Am_Html::escape($s);
                if(!$licenseActivation->is_disabled && Am_Di::getInstance()->config->get('softsale.allow_manual_deactivate'))
                    $ret .= "&nbsp;<a class='local-link disable-activation' href='".Am_Di::getInstance()->url('softsale/license/disable-activation',
                        array('id'=>Am_Di::getInstance()->security->obfuscate($licenseActivation->pk())))."'>".  ___('disable')."</a>";
                $ret .= "<br />\n";
            }
            return $ret;
        } else { // just show number of activations
            return ___('Active') . ' (' . $license->countActivations(0) . ')';
        }
    }
}
?>
<script type="text/javascript">
function softsaleActivateLicense(license_id)
{
    var url = amUrl('/softsale/license/render-activation/id/' + license_id, 1);
    var onResponse;
    jQuery.get(url, onResponse = function(response){
        jQuery(".manual-activator-popup").parents(".am-popup").remove();
        var el = jQuery("<div class='manual-activator-popup'>" + response + "</div>");
        el.find("form").submit(function(event){
            event.stopPropagation();
            jQuery.post(url[0], jQuery.merge(jQuery(this).serializeArray(), url[1]), onResponse);
            return false;
        });
        el.amPopup();
    });
}
jQuery(document).on('click',"a.disable-activation", function(){
    if (!confirm('Really deactivate?')) return false;
    return true;
});
</script>