<?php
cs()->registerScriptFile(Yii::app()->baseUrl . '/js/addressbookAddresses/ajaxRequest.js', CClientScript::POS_HEAD);
Yii::app()->clientScript->registerCoreScript('jquery.ui');
cs()->registerScriptFile(Yii::app()->baseUrl . '/js/plDialog/plDialog.js', CClientScript::POS_HEAD);
Yii::app()->clientScript->registerScript(
        'autoHideAlert', 'setTimeout(function(){$(".alert").alert("close")}, 360000);', CClientScript::POS_READY
);
$this->beginWidget('CActiveForm', array(
    'id' => 'addressbook-form',
    'enableAjaxValidation' => true,
    'action' => $this->createUrl('admin'),
    'htmlOptions' => array(
        'onsubmit' => "return false;", /* Disable normal form submit */
    )
));

$header = '<h3 class="addressBookTitle">' . CHtml::image($addressBookModel->iconPath) . CHtml::encode($addressBookModel->title);
if ($writePermissions == true && $this->currentBook->type != AddressbookType::intern) {
    $header .= '<a id="createNewContact" class="pull-right" href="" rel="tooltip" title="' . __("ADDRESSBOOK_NAVBAR_NEW_ADDRESS") . '" data-placement="right" data-translation-id="ADDRESSBOOK_NAVBAR_NEW_ADDRESS" ><i class="icon-plus"></i></a>';
    if ($this->hasCsvImportPermission() == 'true') {
        $csvImportUrl = url(
                'addressbookImportExport/default/index', array(
            'params' => array(
                'model' => 'Addresses',
                'contact_tid' => $bookId,
                'returnUrl' => url('addressbookAddresses/default/index', array('id' => $bookId))
            )
                )
        );
        $header .= '<div class="pull-right btn-group" style="padding-top: 6px;">
		    <a
			style="font-size: 26px;line-height: 24px;padding-right: 6px;"
			id="createNewContact2" href="" rel="tooltip" title="' . __("ADDRESSBOOK_NAVBAR_WRENCH_DROPDOWN") . '"
			data-placement="right"
			data-translation-id="ADDRESSBOOK_NAVBAR_WRENCH_DROPDOWN"
			data-translation-target="ADDRESSBOOK_NAVBAR_WRENCH_DROPDOWN_TARGET"
		    >
			<i id="settings" class="icon-wrench"></i>
		    </a>
		    <ul id="yw1" class="dropdown-menu">
			<li
			    data-translation-id="IMPORT_CSV"
			    data-translation-target="ADDRESSBOOK_NAVBAR_WRENCH_DROPDOWN_TARGET"
			>
			  <a tabindex="-1" href="' . $csvImportUrl . '">' . __('IMPORT_CSV') . '</a>
			</li>';
        $header .= '	</ul>
		</div>';
    }
}
$header .= '</h3>';
echo $header;

$lastColumn = array('name' => 'org_name', 'header' => __('ADDRESSESS_MODEL_ORG_NAME'));
if ($this->currentBook->type == AddressbookType::intern) {
    $lastColumn = array('name' => 'tel_work', 'header' => __('ADDRESSESS_MODEL_TEL_WORK'));
}
$this->widget('ext.PlGrid.PlGridView', array(
    'id' => 'addresses-grid',
    'type' => 'condensed',
    'navBarHtmlOptions' => array('style' => 'display:none'),
    'afterAjaxUpdate' => 'function() { $("#selectAll").trigger("change")}',
    'filter' => $model,
    'dataProvider' => $dataProvider,
    'itemsCssClass' => 'plTable items',
    'selectionChanged' => 'function(){
            loadAddress($("#addresses-grid").yiiGridView("getSelection")[0],"' . $this->createUrl('getJsonAddress') . '", "' . $bookId . '", "' . $this->createUrl('photoshow', array('size' => 80)) . '")
        }',
    'summaryText' => '{start}-{end}/{count}',
    'columns' => array(
        array(
            'selectableRows' => 2,
            'class' => 'CCheckBoxColumn',
            'checkBoxHtmlOptions' => array('name' => 'selectedRows[]'),
        ),
        array('name' => 'n_given', 'header' => __('ADDRESSESS_MODEL_N_GIVEN')),
        array('name' => 'n_family', 'header' => __('ADDRESSESS_MODEL_N_FAMILY')),
        $lastColumn,
    ),
));


if ($this->hasWritePermission() == 'true') {
    echo '<div id="bulkOperations">
        <label style="display:inline;" for="selectAll" data-translation-id="ADDRESSESS_INDEX_SELECT_ALL">' . __('ADDRESSESS_INDEX_SELECT_ALL') .
    CHtml::checkBox(
            'selectAll', false, array(
        'style' => 'margin:0px 0 3px;'
            )
    ) .
    '</label><br>';

    echo CHtml::hiddenField('id', $bookId);
    echo CHtml::dropDownList(
            'function', '', $this->getGroupActionListData(), array(
        'id' => 'group_actions_dropdown',
        'prompt' => __('ADDRESSES_GROUP_ACTIONS_MAIN_TITLE'),
        'options' => $this->getAddressbooksOptions($addressBookModel), //for translation
        'data-translation-target' => 'ADDRESSES_GROUP_ACTIONS',
        'onchange' => 'showConfirmDialog($(this).find(":selected").data("confirm"));',
            )
    );
    echo '</div>';
} else {
    ?>
    <style>
        .checkbox-column{
            display:none;
        }
        tr.filters td:first-child{
            display: none;
        }
    </style>
    <?php
}

$this->endWidget();

$this->beginWidget('bootstrap.widgets.TbModal', array(
    'options' => array(
        'backdrop' => 'static'
    ),
    'htmlOptions' => array(
        'id' => 'progressModal')
        )
);
?>

<div class="modal-header" >
    <a class="close" data-dismiss="modal">&times;</a>
    <h3 id="progressBarModalHeader"></h3>
</div>

<div class="modal-body" id="progressBarModalBody">
    <?php
    $this->widget('bootstrap.widgets.TbProgress', array(
        'type' => 'info', // 'info', 'success' or 'danger'
        'percent' => 0, // the progress
        'striped' => true,
        'animated' => true,
        'htmlOptions' => array('id' => 'progressBar')
    ));
    ?>
</div>

<div class="modal-footer">
    <?php
    $this->widget('bootstrap.widgets.TbButton', array(
        'id' => 'btnAbort',
        'label' => __("GLOBAL_CANCEL_BUTTON"),
        'type' => 'danger',
        'url' => '#',
        'htmlOptions' => array(
            'data-dismiss' => 'modal',
            'onclick' => 'stopProgress()'
        ),
    ));
    ?>
</div>
<?php
$this->endWidget();
?>
<script>

    function showConfirmDialog(message) {
        var options = {
            confirmMessage: message,
            dialogHeader: "<?php echo __('ADRESSBOOK_ADDRESSES_MODEL_HEADER'); ?>",
            confirmText: "<?php echo __('ADRESSBOOK_ADDRESSES_MODAL_CONFIRM_BUTTON'); ?>",
            cancelText: "<?php echo __('ADRESSBOOK_ADDRESSES_MODEL_CANCLE_BUTTON'); ?>",
            height: 'auto',
            width: '500',
            position: ['center', 250],
            confirmCmd: function () {
                startAjaxAction("<?php echo $this->createUrl("ajax/processAjaxAction"); ?>");
                closeConfirmModal();
            },
            cancelCmd: function () {
                closeConfirmModal();
            }
        };
        var plDialog = new $.plDialog(options);
        plDialog.showConfirmDialog();
    }

    function closeConfirmModal() {
        $('#group_actions_dropdown').prop("selectedIndex", 0);
        $('#dialog-confirm.modal-body').dialog("close");
    }

    $('#selectAll').change(function ()
    {
        if ($(this).is(':checked')) {
            checkAllGridCheckboxes();
            disableAllGridCheckboxes();
        } else {
            uncheckAllGridCheckboxes();
            enableAllGridCheckboxes();
        }
    });
</script>