<?php

$this->widget('ext.PlGrid.PlGridView', array(
    'id' => 'addressbook-grid',
    'headerTitle' => 'Header_title', // optional
    'addButtonTitle' => 'ADD_NEW_ADDRESSBOOKS', // optional
    'searchButtonTitle' => 'SEARCH_ADDRESSBOOKS', // optional
    'modalCreateHeader' => 'ADDRESSBOOKS_HEADER_CREATE', // optional
    'modalUpdateHeader' => 'ADDRESSBOOKS_HEADER_UPDATE', // optional
    'showAlertBlock' => true,
    'headerAttribute' => 'title', // TEST 
    'itemsCssClass' => 'plTable items',
    'modalSize' => 'large', // null, 'large', 'small', 'huge'
    'dataProvider' => $model->search(),
    'filter' => $model,
    'columns' => array(
        array(
            'name' => 'icon',
            'type' => 'raw',
            'value' => array($model, 'renderIcon'),
            'header' => $model->getAttributeLabel('icon'),
            'htmlOptions' => array('style' => 'width:50px;text-align:center;'),
            'headerHtmlOptions' => array('data-translation-id' => 'ADDRESSBOOKS_MODEL_ICON'),
            'disableSearchField' => true
        ),
        array(
            'name' => 'title',
            'header' => $model->getAttributeLabel('title'),
            'headerHtmlOptions' => array('data-translation-id' => 'ADDRESSBOOKS_MODEL_TITLE')
        ),
        array(
            'name' => 'type',
            'header' => $model->getAttributeLabel('type'),
            'value' => array($model, 'renderTypeName'),
            'headerHtmlOptions' => array('data-translation-id' => 'ADDRESSBOOKS_MODEL_TYPE'),
            'enableCustomDropdownFilter' => true,
            'filter' => $model->humanReadableSearchFilterForType(),
        ),
        array(
            'class' => 'ext.PlGrid.PlCButtonColumn',
            'template' => '{ajaxUpdate}{deleteAddressbook}',
            'buttons' => array(
                'deleteAddressbook' => array(
                    'icon' => 'trash',
                    'url' => 'url("addressbookGeneral/default/JsonBeforeDelete/",array("bookId"=>$data->id))',
                    'visible' => '$data->type!=' . AddressbookType::intern,
                    'options' => array(
                        'data-translation-id' => 'ADDESSBOOKGENERAL_ADMIN_DELETE_CONFIRM_MESSAGE',
                        'onclick' => 'deleteAddresses(this);return false;',
                        'title' =>  __('GLOBAL_DELETE_BUTTON')
                    ),
                ),
            ),
        )
    ),
        )
);
$js = '
function deleteAddresses(linkEl) {
    if (confirm("' . __('ADDESSBOOKGENERAL_ADMIN_DELETE_CONFIRM_MESSAGE') . '")){
	var deleteAddressbookUrl = linkEl.href;
	$.ajax({
	  type: "POST",
	  url: deleteAddressbookUrl,
	  data: {
	    ' . Yii::app()->request->csrfTokenName . '  : "' . Yii::app()->request->csrfToken . '"
	  },
	  dataType: "json",
	  beforeSend: function ( xhr ) {
	    //xhr.overrideMimeType("text/plain; charset=x-user-defined");
	  }
	}).success(function ( data ) {
	    location.reload();
	})
    }
}
';
Yii::app()->clientScript->registerScript('jsBeforeDeleteAddressbook', $js, 0);
if (app()->user->translateMode) {
    echo "<i data-translation-id='ADDRESSBOOK_GENERAL_ADDRESSES_EXISTS_ERROR_MESSAGE'>ADDRESSBOOK_GENERAL_ADDRESSES_EXISTS_ERROR_MESSAGE => " . __("ADDRESSBOOK_GENERAL_ADDRESSES_EXISTS_ERROR_MESSAGE") . "</i></br>";
}

$publicAddressbookCount = $model->countAllByAttributes(array("type"=>  AddressbookType::group));
$personalAddressbookCount = $model->countAllByAttributes(array("type"=>  AddressbookType::personal));
$publicAddressbooksAllowed = $publicAddressbookCount >= PhonalisaLicenseManager::publicAddressbooksMax() && PhonalisaLicenseManager::publicAddressbooksMax() !== -1;
$personalAddressbooksAllowed =($personalAddressbookCount >= PhonalisaLicenseManager::privateAddressbooksMax() && PhonalisaLicenseManager::privateAddressbooksMax() !== -1);
?>
<script>
    function setCreateBtnVisibility()
    {
       if (
        '<?php echo $publicAddressbooksAllowed ?>' == true && 
        '<?php echo $personalAddressbooksAllowed ?>' == true
        ) {
            $('#addressbook-gridCreateBtn').hide();
        } else {
            $('#addressbook-gridCreateBtn').show();
        }

    }
    setCreateBtnVisibility();
</script>