<?php
// // // MyPhones  
$this->widget('ext.PlGrid.PlGridView', array(
    'id' => 'my-phones-grid',
    'headerTitle' => 'Header_title', // optional
    'addButtonTitle' => 'ADD_NEW_MY_PHONES', // optional
    'searchButtonTitle' => 'SEARCH_MY_PHONES', // optional
    'modalCreateHeader' => 'MY_PHONES_HEADER_CREATE', // optional
    'modalUpdateHeader' => 'MY_PHONES_HEADER_UPDATE', // optional
    'itemsCssClass' => 'plTable items',
    'enableAddButton' => false,
    'enableSearchButton' => false,
    'summaryText' => false,
    'dataProvider' => $phonesCurrentDataProvider,
    'columns' => array(
        array(
            'name' => 'phone',
            'header' => __('MY_PHONES_INDEX_PHONE_NAME'),
            'headerHtmlOptions' => array(
                'data-translation-id' => 'MY_PHONES_INDEX_PHONE_NAME'
            ),
        ),
        array(
            'name' => 'descr',
            'header' => __('MY_PHONES_INDEX_DESCR'),
            'headerHtmlOptions' => array(
                'data-translation-id' => 'MY_PHONES_INDEX_DESCR'
            ),
        ),
        array(
            'name' => 'logout',
            'header' => __('MY_PHONES_INDEX_PHONE_LOGOUT_LOGIN'),
            'headerHtmlOptions' => array(
                'data-translation-id' => 'MY_PHONES_INDEX_PHONE_LOGOUT_LOGIN'
            ),
            'value' => array($this, 'renderLogButton'),
        ),
    ),
        )
);

$this->renderStatusGrids($userID);

$visible = Yii::app()->params->devMode;
if ($visible) {
    $qrCodeHeader = __("PHONESETTINGS_MYPHONE_QRCODE_HEADER");
    $this->widget('bootstrap.widgets.TbGridView', array(
        'id' => 'authDevice-grid',
        'dataProvider' => $authenticateDevice,
        //'filter'=>$model,
        'summaryText' => false,
        'selectableRows' => 2,
        'type' => 'striped bordered condensed',
        'itemsCssClass' => 'plTable items',
        'columns' => array(
            array(
                'name' => 'tempToken',
                'header' => __('AUTHENTICATE_DEVICE_MODEL_TEMP_TOKEN'),
                'headerHtmlOptions' => array('data-translation-id' => 'AUTHENTICATE_DEVICE_MODEL_TEMP_TOKEN'),
                'visible' => $visible,
            ),
            array(
                'name' => 'accessToken',
                'header' => __('AUTHENTICATE_DEVICE_MODEL_ACCESS_TOKEN'),
                'headerHtmlOptions' => array('data-translation-id' => 'AUTHENTICATE_DEVICE_MODEL_ACCESS_TOKEN'),
                'visible' => $visible,
            ),
            array(
                'name' => 'deviceName',
                'header' => __('AUTHENTICATE_DEVICE_MODEL_DEVICE_NAME'),
                'headerHtmlOptions' => array('data-translation-id' => 'AUTHENTICATE_DEVICE_MODEL_DEVICE_NAME'),
            ),
            array(
                'name' => 'pushKey',
                'header' => __('AUTHENTICATE_DEVICE_MODEL_PUSH_KEY'),
                'headerHtmlOptions' => array('data-translation-id' => 'AUTHENTICATE_DEVICE_MODEL_PUSH_KEY'),
                'visible' => $visible,
            ),
            array(
                'name' => 'pushEnabled',
                'header' => __('AUTHENTICATE_DEVICE_MODEL_PUSH_ENABLED'),
                'headerHtmlOptions' => array('data-translation-id' => 'AUTHENTICATE_DEVICE_MODEL_PUSH_ENABLED'),
            ),
            array(
                'name' => 'deviceType',
                'header' => __('AUTHENTICATE_DEVICE_MODEL_DEVICE_TYPE'),
                'headerHtmlOptions' => array('data-translation-id' => 'AUTHENTICATE_DEVICE_MODEL_DEVICE_TYPE'),
            ),
            array(
                'name' => 'added',
                'header' => __('AUTHENTICATE_DEVICE_MODEL_ADDED'),
                'headerHtmlOptions' => array('data-translation-id' => 'AUTHENTICATE_DEVICE_MODEL_ADDED'),
                'value' => 'PhonalisaDate::userDateTime($data->added)',
            ),
            array(
                'name' => 'lastSeen',
                'header' => __('AUTHENTICATE_DEVICE_MODEL_LAST_SEEN'),
                'headerHtmlOptions' => array('data-translation-id' => 'AUTHENTICATE_DEVICE_MODEL_LAST_SEEN'),
                'value' => 'PhonalisaDate::userDateTime($data->lastSeen)',
            ),
            array(
                'class' => 'bootstrap.widgets.TbButtonColumn',
                'template' => '{view}{delete}',
                'htmlOptions' => array(
                    'class' => 'buttonColumnIcons',
                ),
                'buttons' => array(
                    'view' => array(
                        'icon' => 'icon-eye-open',
                        'url' => 'Yii::app()->createUrl("phonesettingsMyPhones/default/viewQRCode", array("id"=>$data->id))',
                        'visible' => '$data->tempToken != ""',
                        'click' => "function(data){
                                       $.get(this.href, function(data){
                                           $('.modal-body').html(data);
                                           $('#my-phones-grid_Modal .modal-header h4').html('" . $qrCodeHeader . "');
                                           $('#my-phones-grid_Modal #my-phones-grid_Modal_saveBtn').hide();
                                           $('#my-phones-grid_Modal').modal('show');
                                           return false;
                                        });
                                       return false;}",
                    ),
                    'delete' => array(
                        'icon' => 'icon-trash',
                        'url' => 'Yii::app()->createUrl("phonesettingsMyPhones/default/deleteAuthenticDevice", array("id"=>$data->id))',
                    ),
                ),
            ),
        ),
            )
    );

    $this->widget('bootstrap.widgets.TbButton', array(
        'buttonType' => 'ajaxLink',
        'type' => 'primary',
        'label' => __('PHONESETTINGS_MYPHONE_ADD_NEW_DEVICE'),
        'url' => url('phonesettingsMyPhones/default/requestTempToken', array('userID' => $userID)),
        'htmlOptions' => array(
            'name' => 'addNewDevice',
            'data-translation-id' => 'PHONESETTINGS_MYPHONE_ADD_NEW_DEVICE'
        ),
        'ajaxOptions' => array('type' => 'GET',
            'dataType' => 'html',
            'success' => "js:function(data){
                $('#my-phones-grid_Modal').find('.modal-body').html(data);
                $('#my-phones-grid_Modal .modal-header h4').html('" . $qrCodeHeader . "');
                $('#my-phones-grid_Modal #my-phones-grid_Modal_saveBtn').hide();
                $('#my-phones-grid_Modal').on('hidden', function () {
                   $.fn.yiiGridView.update('authDevice-grid');	
                });
                $('#my-phones-grid_Modal').modal('show');
                }"),
    ));
}

if (Yii::app()->user->translateMode) {
    echo '</br>';
    echo '<i data-translation-target="PHONESETTINGS_MYPHONE_QRCODE_HEADER" data-translation-id="PHONESETTINGS_MYPHONE_QRCODE_HEADER" >';
    echo 'PHONESETTINGS_MYPHONE_QRCODE_HEADER => ' . __("PHONESETTINGS_MYPHONE_QRCODE_HEADER");
    echo '</i></br>';

    echo '<i data-translation-target="GLOBAL_ACKNOWLEDGE_BUTTON" data-translation-id="GLOBAL_ACKNOWLEDGE_BUTTON" >';
    echo 'GLOBAL_ACKNOWLEDGE_BUTTON => ' . __("GLOBAL_ACKNOWLEDGE_BUTTON");
    echo '</i></br>';

    echo '<i data-translation-target="PHONESETTINGS_MYPHONE_QRCODE_DESCRIPTION" data-translation-id="PHONESETTINGS_MYPHONE_QRCODE_DESCRIPTION" >';
    echo 'PHONESETTINGS_MYPHONE_QRCODE_DESCRIPTION => ' . __("PHONESETTINGS_MYPHONE_QRCODE_DESCRIPTION");
    echo '</i></br>';

    echo '<i data-translation-target="PHONESETTINGS_MYPHONE_QRCODE_TIMELEFT_INFOTEXT" data-translation-id="PHONESETTINGS_MYPHONE_QRCODE_TIMELEFT_INFOTEXT" >';
    echo 'PHONESETTINGS_MYPHONE_QRCODE_TIMELEFT_INFOTEXT => ' . __("PHONESETTINGS_MYPHONE_QRCODE_TIMELEFT_INFOTEXT");
    echo '</i></br>';

    echo '<i data-translation-target="MY_PHONES_HUMAN_READABLE_DND_STATUS_OFF" data-translation-id="MY_PHONES_HUMAN_READABLE_DND_STATUS_OFF" >';
    echo 'MY_PHONES_HUMAN_READABLE_DND_STATUS_OFF => ' . __("MY_PHONES_HUMAN_READABLE_DND_STATUS_OFF");
    echo '</i></br>';

    echo '<i data-translation-target="MY_PHONES_HUMAN_READABLE_DND_STATUS_ON" data-translation-id="MY_PHONES_HUMAN_READABLE_DND_STATUS_ON" >';
    echo 'MY_PHONES_HUMAN_READABLE_DND_STATUS_ON => ' . __("MY_PHONES_HUMAN_READABLE_DND_STATUS_ON");
    echo '</i></br>';
}
?>
<script>
    refreshGrid();

    function refreshGrid() {
        setTimeout(function () {
            $.fn.yiiGridView.update('authDevice-grid');
            refreshGrid();
        }, 3000);
    }

    $("body").on("click", ".LoginLogoutCheckBox", function () {
        var url = '<?php echo url('phonesettingsMyPhones/default/toggleQueueStatus') ?>';
        var tokenName = '<?php echo Yii::app()->request->csrfTokenName; ?>';
        var token = '<?php echo Yii::app()->request->csrfToken; ?>';
        var tierId = $(this).attr('data-tierid');

        $.post(url,
                {
                    id: tierId,
                    tokenName: token
                });
    });
</script>