<?php
$this->widget('ext.PlGrid.PlGridView', array(
    'id' => 'sip-accounts-grid',
    'headerTitle' => 'SIPACCOUNT_NAVBAR_TITLE', // optional
    'addButtonTitle' => 'SIPACCOUNT_NAVBAR_NEW_SIPACCOUNT', // optional
    'searchButtonTitle' => 'SIPACCOUNT_NAVBAR_SEARCH', // optional
    'modalCreateHeader' => 'SIPACCOUNTS_HEADER_CREATE', // optional
    'modalUpdateHeader' => 'SIPACCOUNTS_HEADER_UPDATE', // optional
    'afterAjaxUpdate' => "function() {
        setCreateBtnVisibility();
    }",
    'headerAttribute' => 'authname', //TEST
    'itemsCssClass' => 'plTable items',
    'modalSize' => 'large', // null, 'large', 'small', 'huge'
    'dataProvider' => $model->search(),
    'filter' => $model,
    'columns' => array(
        array(
            'name' => 'state',
            'value' => array($model, 'renderState'),
            'header' => $model->getAttributeLabel('state'),
            'headerHtmlOptions' => array('data-translation-id' => 'SIPACCOUNT_MODEL_STATE'),
            "htmlOptions"=>array("class"=>"state"),
            'disableSearchField' => true
        ),
        array(
            'name' => 'authname',
            'header' => $model->getAttributeLabel('authname'),
            'headerHtmlOptions' => array('data-translation-id' => 'SIPACCOUNT_MODEL_AUTHNAME'),
            "htmlOptions"=>array("class"=>"authname"),
        ),
        array(
            'type' => 'raw',
            'name' => 'last_contact',
            'value' => array($model, 'renderLastContact'),
            'header' => $model->getAttributeLabel('last_contact'),
            'headerHtmlOptions' => array('data-translation-id' => 'SIPACCOUNT_MODEL_LAST_CONTACT'),
            "htmlOptions"=>array("class"=>"last_contact"),
            'disableSearchField' => true
        ),
        array(
            'name' => 'expires',
            'header' => $model->getAttributeLabel('expires'),
            'headerHtmlOptions' => array('data-translation-id' => 'SIPACCOUNT_MODEL_EXPIRES'),
            "htmlOptions"=>array("class"=>"expires"),
        ),
        array(
            'name' => 'current_ip',
            'type' => 'raw',
            'value' => array($model, 'renderCurrentIP'),
            'header' => $model->getAttributeLabel('current_ip'),
            'headerHtmlOptions' => array('data-translation-id' => 'SIPACCOUNT_MODEL_CURRENT_IP'),
            "htmlOptions"=>array("class"=>"current_ip"),
        ),
        array(
            'name' => 'device_name',
            'header' => $model->getAttributeLabel('device_name'),
            'headerHtmlOptions' => array('data-translation-id' => 'SIPACCOUNT_MODEL_DEVICE_NAME'),
            "htmlOptions"=>array("class"=>"device_name"),
        ),
        array(
            'name' => 'user_id',
            'value' => array($model, 'renderUser'),
            'header' => $model->getAttributeLabel('user_id'),
            'headerHtmlOptions' => array('data-translation-id' => 'SIPACCOUNT_MODEL_USER_ID'),
            'enableCustomDropdownFilter' => true,
            'filter' => $model->humanReadableSearchFilterForUserID(),
            "htmlOptions"=>array("class"=>"user_id"),
        ),
        array(
            'class' => 'ext.PlGrid.PlCButtonColumn',
            'template' => '{ajaxUpdate}{delete}',
            'buttons' => array(
                'ajaxUpdate' => array(
                    'visible' => '$data->user_id',
                    'label' => '<i class="icon-pencil"></i>',
                    'imageUrl' => false,
                    'options' => array(
                        'title' => 'Update',
                    ),
                    'url' => 'Yii::app()->createUrl("adminSipAccounts/default/update", array("id"=>"$data->id"))',
                    'click' => 'js:function(){
                                $.get(this.href, function(data){
                                $(".modal-body").html(data);                                
                                $("#modalHeader").html("' . __('SIPACCOUNTS_HEADER_UPDATE') . '");
                                $("#newSipModal").modal("show");
                                return false;
                            });
                        return false;
                        }',
                ),
            ),
        ),
    ),
        )
);
?>
<script>
    function setCreateBtnVisibility() {
        $.ajax({
            url: "<?php echo url("adminSipAccounts/default/ajaxSetCreateBtnVisibility") ?>",
            type: "POST",
            dataType: "json",
            success: function (data) {
                if (data.visibility === true) {
                    $('#sip-accounts-gridCreateBtn').show();
                } else {
                    $('#sip-accounts-gridCreateBtn').hide();
                }
            }
        });
    }
    setCreateBtnVisibility();
    
    var defaultOnMessage = ws.onmessage;
    ws.onmessage = function (e) {
        var msg = jQuery.parseJSON(e.data);
        if(msg.name === "login") {
            subscribeForSipAccountEvents();
        } else if (msg.name === "sipAccountChanged") {
            updateSipAccount(msg.data.sipAccount);
        }
        defaultOnMessage(e);
    };
    
    function subscribeForSipAccountEvents()
    {
        ws.command({name:"subscribeSipAccountEvents", type:"command"});
    }
    
    function updateSipAccount(sipAccount)
    {
        var id = sipAccount.id;
        var row = $("[data-sipaccountid='"+sipAccount.id+"']").closest("tr");
        $.each(sipAccount, function (key, value) {
            if (key === "state") {
                var stateCell = row.find("."+key);
                stateCell.text("");
                stateCell.append(getStateLabel(value, id));
                
            } else {
                if (key !== "last_contact" && key !== "user_id") {
                    row.find("."+key).text(value);
                }
            }
        });
    }
    
    function getStateLabel(value, id)
    {
        var label = $("<span />").addClass("label").attr("data-sipaccountid", id);
        if (value === "online") {
            label.addClass("label-success");
            label.text("Online");
        } else {
            label.text("N/a");
        }
        return label;
    }
</script>