<?php
$columns = array();
$customRenderActions = array(
    'target' => 'renderTarget',
    'type' => 'renderType',
    'start_timestamp' => 'renderStartTimestamp'
);
$notRendered = array(
    'logmessages',
    'previous_cdr_id',
    'answered_timestamp',
    'forwarded_target',
    'error_code',
    'forwarded_target'
);
$filter = $model->humanReadableTypeSearchFilter();
$enableCustomDropdownFilter = true;
$enableCustomDateTimeSearchFilter = true;

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

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

    if (array_key_exists($attributeName, $customRenderActions)) {

        $htmlOptions = array();
        $htmlOptions['data-translation-id'] = strtoupper('CDR') . '_MODEL_' . strtoupper($attributeName);
        if ($attributeName == 'type') {
            $htmlOptions['data-translation-target'] = 'TYPE_TRANSLATION_TARGET';
        }
        $columns[] = array(
            'filter' => $attributeName == 'type' ? $filter : null,
            'name' => $attributeName,
            'header' => $model->getAttributeLabel($attributeName),
            'value' => array($this, $customRenderActions[$attributeName]),
            'headerHtmlOptions' => $htmlOptions,
            'enableCustomDropdownFilter' => $attributeName == 'type' ? $enableCustomDropdownFilter : false,
            'enableCustomDateTimeSearchFilter' => $attributeName == 'start_timestamp' ? $enableCustomDateTimeSearchFilter : false,
        );
    } else {

        $columns[] = array(
            'name' => $attributeName,
            'header' => $model->getAttributeLabel($attributeName),
            'headerHtmlOptions' => array('data-translation-id' => strtoupper('CDR') . '_MODEL_' . strtoupper($attributeName)),
        );
    }
}
$columns[] = array(
    'class' => 'ext.PlGrid.PlCButtonColumn',
    'htmlOptions' => array('class' => 'buttonColumnIcons', 'style' => 'width:55px;'),
    'template' => '{search}', // {view}{delete}
    'buttons' => array(
        'search' => array(
            'imageUrl' => false,
            'label' => '<div class="icon-search"></div>',
            'options' => array('title' => __('CDR_SEARCH'), 'data-translation-id' => 'CDR_SEARCH'),
            'url' => 'url(
                                    "systemCDR/default/logMessage",
                                    array(
                                        "id"=>$data->id,
                                        "page"=>@$_REQUEST["CDR_page"],
                                        "sort"=>@$_REQUEST["sort"]
                                    )
                                )',
        ),
    ),
);

$this->widget('ext.PlGrid.PlGridView', array(
    'id' => 'cdr-grid',
    'headerTitle' => 'Header_title', // optional
    'addButtonTitle' => 'ADD_NEW_CDR', // optional
    'searchButtonTitle' => 'SEARCH_CDR', // optional
    'modalCreateHeader' => 'CDR_HEADER_CREATE', // optional
    'modalUpdateHeader' => 'CDR_HEADER_UPDATE', // optional
    'itemsCssClass' => 'plTable items',
    'ajaxUpdate' => false,
    'enableHistory' => true,
    'dataProvider' => $model->search(),
    'customButtons' => array(
        'label' => __("CDR_CSV_EXPORT"),
        'url' => '',
        'items' => array(
            array(
                'label' => '1 ' . __("GLOBAL_EXPORT_DAY"),
                'url' => url('systemCDR/default/cdrExport', array('days' => '1') + $searchFor),
            ),
            array(
                'label' => '2 ' . __("GLOBAL_EXPORT_DAYS"),
                'url' => url('systemCDR/default/cdrExport', array('days' => '2') + $searchFor),
            ),
            array(
                'label' => '7 ' . __("GLOBAL_EXPORT_DAYS"),
                'url' => url('systemCDR/default/cdrExport', array('days' => '7') + $searchFor),
            ),
            array(
                'label' => '14 ' . __("GLOBAL_EXPORT_DAYS"),
                'url' => url('systemCDR/default/cdrExport', array('days' => '14') + $searchFor),
            ),
            array(
                'label' => '30 ' . __("GLOBAL_EXPORT_DAYS"),
                'url' => url('systemCDR/default/cdrExport', array('days' => '30') + $searchFor),
            ),
            array(
                'label' => '365 ' . __("GLOBAL_EXPORT_DAYS"),
                'url' => url('systemCDR/default/cdrExport', array('days' => '365') + $searchFor),
            ),
        ),
        'linkOptions' => array(
            'data-translation-target' => 'GLOBAL_EXPORT_ITEMS'
        )
    ),
    'filter' => $model,
    'enableAddButton' => false,
    'columns' => $columns,
        )
);
if (app()->user->translateMode) {
    $this->renderPartial('translationTags');
}
?>
<script>
    $(".errorCode").live("mouseover", function () {
        $(this).popover('show');
    });
</script>