<?php
cs()->registerScriptFile(Yii::app()->baseUrl . '/js/permissionInheritance/permissionInheritance.js', CClientScript::POS_HEAD);

$form = $this->beginWidget('ext.PlGrid.PlActiveForm', array(
    'id' => 'addressbook-form',
    'gridId' => 'addressbook-grid',
    'afterSaveScript' => 'window.location.reload()',
    'action' => $model->isNewRecord ? array('create') : array()
        ));

echo $form->textFieldRow($model, 'title', array(
    'class' => 'span3',
    'data-translation-id' => 'ADDRESSBOOKS_MODEL_TITLE',
));

if ($model->isNewRecord) {
    $listdata = array(
        AddressbookType::personal => __('ADDRESSBOOK_MODEL_TYPE_PRIVATE'),
        AddressbookType::group => __('ADDRESSBOOK_MODEL_TYPE_GROUP'),
    );
    asort($listdata);
    
    $privateAddressbookCount = Addressbook::model()->countAllByAttributes(array('type'=> AddressbookType::personal));
    if (PhonalisaLicenseManager::privateAddressbooksMax() <= (int)$privateAddressbookCount && PhonalisaLicenseManager::privateAddressbooksMax() !== -1) {
        unset($listdata[AddressbookType::personal]);
    }
    
    $publicAddressbookCount = Addressbook::model()->countAllByAttributes(array('type'=> AddressbookType::group));
    if (PhonalisaLicenseManager::publicAddressbooksMax() <= (int)$publicAddressbookCount && PhonalisaLicenseManager::publicAddressbooksMax() !== -1) {
        unset($listdata[AddressbookType::group]);
    }


    echo $form->dropDownListRow($model, 'type', $listdata, array(
        'class' => 'span3',
        'data-translation-id' => 'ADDRESSBOOKS_MODEL_TYPE',
        'data-translation-target' => 'test',
        'options' => array(
            '1' => array(
                'value' => '1',
                'label' => __('ADDRESSBOOK_MODEL_TYPE_PRIVATE'),
                'data-translation-id' => 'ADDRESSBOOK_MODEL_TYPE_PRIVATE',
                'data-translation-target' => 'test',
            ),
            '2' => array(
                'value' => '2',
                'label' => __('ADDRESSBOOK_MODEL_TYPE_GROUP'),
                'data-translation-id' => 'ADDRESSBOOK_MODEL_TYPE_GROUP',
                'data-translation-target' => 'test',
            ),
        )
    ));
}
$icons = $this->getIconDropdown();
echo $form->dropDownListRow($model, 'icon', $icons, array(
    'class' => 'span3',
    'data-translation-id' => 'ADDRESSBOOKS_MODEL_ICON',
));

$groups = new Groups;
$this->widget('bootstrap.widgets.TbGridView', array(
    'id' => 'test-grid',
    'enableSorting' => false,
    'type' => 'striped bordered condensed',
    'itemsCssClass' => 'plTable',
    'dataProvider' => $groups->search('name', false),
    'filter' => $groups,
    'summaryText' => false,
    'columns' =>
    array(
        array(
            'name' => 'nestedName',
            'header' => $groups->getAttributeLabel('name'),
            'headerHtmlOptions' => array('data-translation-id' => 'USERGROUPS_MODEL_NAME'),
        ),
        array(
            'type' => 'raw',
            'id' => 'read',
            'header' => __('USERGROUP_MODEL_READ_PERMISSION'),
            'value' => array($model, 'renderPermissions'),
            'headerHtmlOptions' => array('data-translation-id' => 'USERGROUP_MODEL_READ_PERMISSION'),
            'htmlOptions' => array('data-custom-contexthelp-key' => 'ADDRESSBOOK_PERMISSIONS_READ_CONTEXT_HELP')
        ),
        array(
            'type' => 'raw',
            'id' => 'write',
            'header' => __('USERGROUP_MODEL_WRITE_PERMISSION'),
            'value' => array($model, 'renderPermissions'),
            'headerHtmlOptions' => array('data-translation-id' => 'USERGROUP_MODEL_WRITE_PERMISSION'),
            'htmlOptions' => array('data-custom-contexthelp-key' => 'ADDRESSBOOK_PERMISSIONS_WRITE_CONTEXT_HELP')
        ),
//        array(
//            'type' => 'raw',
//            'id' => 'sync',
//            'header' => __('USERGROUP_MODEL_SYNC_PERMISSION'),
//            'value' => array($model, 'renderPermissions'),
//            'headerHtmlOptions' => array('data-translation-id' => 'USERGROUP_MODEL_SYNC_PERMISSION'),
//        ),
        array(
            'type' => 'raw',
            'id' => 'csvImport',
            'header' => __('USERGROUP_MODEL_CSSV_IMPORT_PERMISSION'),
            'value' => array($model, 'renderPermissions'),
            'headerHtmlOptions' => array('data-translation-id' => 'USERGROUP_MODEL_SYNC_PERMISSION'),
            'htmlOptions' => array('data-custom-contexthelp-key' => 'ADDRESSBOOK_PERMISSIONS_CSVIMPORT_CONTEXT_HELP'),
            'visible' => $model->type != AddressBookType::intern,
        ),
    ),
));
$this->endWidget();
?>

<script>
    setAllUsedReadCheckBoxesToTrue(readSelector, writeSelector);
</script>