<?php
$columns = array();
$dontRender = array('pin', 'type', 'id', 'scriptExtension'); // FIXME : later remove the type attribute from this 
$customRendered = array(
    'type' => 'renderType',
    'state' => 'renderState',
);

foreach ($model->attributes as $attributeName => $attributeValue) {

    if (in_array($attributeName, $dontRender)) {
        continue;
    }

    if (!array_key_exists($attributeName, $customRendered)) {
        $columns[] = array(
            'name' => $attributeName,
            'header' => $model->getAttributeLabel($attributeName),
            'headerHtmlOptions' => array('data-translation-id' => strtoupper('EXTERNAL_AGENT') . '_MODEL_' . strtoupper($attributeName)),
        );
    } else {
        $columns[] = array(
            'name' => $attributeName,
            'value' => array($this, $customRendered[$attributeName]),
            'header' => __("EXTERNAL_AGENT_GRID_HEADER_STATUS"),
            'headerHtmlOptions' => array(
                'data-translation-id' => 'EXTERNAL_AGENT_GRID_HEADER_STATUS',
                'data-translation-target' => 'EXTERNAL_AGENT_MODEL_TARGET_' . strtoupper($attributeName)
            ),
            'enableCustomDropdownFilter' => true,
            'filter' => $model->humanReadableSearchFilterForState(),
        );
    }
}
$columns[] = array(
    'class' => 'ext.PlGrid.PlCButtonColumn',
    'template' => '{ajaxUpdate}{delete}',
);

$this->widget('ext.PlGrid.PlGridView', array(
    'id' => 'external-agent-grid',
    'headerTitle' => 'Header_title', // optional
    'addButtonTitle' => 'ADD_NEW_EXTERNAL_AGENT', // optional
    'searchButtonTitle' => 'SEARCH_EXTERNAL_AGENT', // optional
    'modalCreateHeader' => 'EXTERNAL_AGENT_HEADER_CREATE', // optional
    'modalUpdateHeader' => 'EXTERNAL_AGENT_HEADER_UPDATE', // optional
    'headerAttribute' => 'name',
    'modalSize' => 'large',
    'itemsCssClass' => 'plTable items',
    'dataProvider' => $model->search(),
    'enableAddButton' => $enableAddButton,
    'filter' => $model,
    'afterAjaxUpdate' => 'function(){
        setCreateButtonVisibility();
    }',
    'columns' => $columns,
        )
);
$this->renderPartial('translationTags');

$routingExternalAgentsMax = PhonalisaLicenseManager::routingExternalAgentsMax();
?>
<script>
    function setCreateButtonVisibility() {
        if (
                $("#external-agent-grid tbody tr").length >= <?php echo $routingExternalAgentsMax; ?>
        && !$("#external-agent-grid tbody tr td:first-child").hasClass("empty")
                && <?php echo $routingExternalAgentsMax; ?> >= 0
                ) {
            $("#external-agent-gridCreateBtn").hide();
        } else {
            $("#external-agent-gridCreateBtn").show();
        }
    }
    setCreateButtonVisibility();
</script>
