<?php

$sudoUser = User:: getCurrentSudoUser();
$js = '
        function startRecord(model) {

            var recordId  = $("#recordId").val();

            $.ajax({
                url : "' . url('phonesettingsAudioFiles/default/ajaxBeforeRecord') . '",
                dataType : "json",
                type : "POST",
                data: {
                        model : model,
                        recordId: recordId,
                        ' . Yii::app()->request->csrfTokenName . '  : "' . Yii::app()->request->csrfToken . '"
                      }
            }).success(function(data) {

                var phoneNumber = "sysrec-"+data.id;
                var userId = "' . $sudoUser->id . '";
                var string = \'{ "command" : { "data" : "\'+phoneNumber+\'", "typ" : "call", "autoanswer" : "yes", "userid" : "\'+userId+\'" } }\';
                $("#recordId").val(data.id);
                $(\'#AudioFile_data\').val(\'record\');
                ws.send(string);
            });
        }
        
    
';
Yii::app()->clientScript->registerScript('startRecord', $js, 0);
$this->widget('ext.PlGrid.PlGridView', array(
    'id' => 'playlist-grid',
    'headerTitle' => __('SYSTEMMUSICONHOLD_PLAYLIST_NAVBAR_TITLE') . ' ' . CHtml::encode($playlist->name), // optional
    'enableHeaderTranslation' => false,
    'navBarHtmlOptions' => array('data-translation-id' => 'SYSTEMMUSICONHOLD_PLAYLIST_NAVBAR_TITLE'),
    'addButtonTitle' => 'ADD_NEW_PLAYLIST_NAVBAR', // optional
    'searchButtonTitle' => 'PLAYLIST_NAVBAR_SEARCH', // optional
    'modalCreateHeader' => 'PLAYLIST_HEADER_CREATE', // optional
    'modalUpdateHeader' => 'PLAYLIST_HEADER_UPDATE', // optional
    'headerAttribute' => 'name',
    'modalSize' => 'large',
    'itemsCssClass' => 'plTable items',
    'headerCreateUrl' => Yii::app()->createUrl("systemMusicOnHold/default/createPlaylist", array('playlistID' => $playlist->id)),
    'dataProvider' => $model->search('name', $playlist->id),
    'filter' => $model,
    'columns' => array(
        array(
            'name' => 'name',
            'header' => $model->getAttributeLabel('name'),
            'headerHtmlOptions' => array('data-translation-id' => 'AUDIOFILES_MODEL_NAME'),
        ),
        array(
            'name' => 'fileName',
            'header' => $model->getAttributeLabel('fileName'),
            'headerHtmlOptions' => array('data-translation-id' => 'AUDIOFILES_MODEL_FILE_NAME'),
        ),
        array(
            'name' => 'descr',
            'header' => $model->getAttributeLabel('descr'),
            'headerHtmlOptions' => array('data-translation-id' => 'AUDIOFILES_MODEL_DESCR'),
        ),
        array(
            'name' => 'created',
            'header' => $model->getAttributeLabel('created'),
            'headerHtmlOptions' => array('data-translation-id' => 'AUDIOFILES_MODEL_CREATED'),
            'value' => array($model, 'getFormattedCreatedDate'),
            'enableCustomDateTimeSearchFilter' => true
        ),
        array(
            'class' => 'ext.PlGrid.PlCButtonColumn',
            'template' => '{updatePlaylist}{deletePlaylist}{download}',
            'buttons' => array(
                'updatePlaylist' => array(
                    'label' => Yii::t('zii', 'Update'),
                    'imageUrl' => 'imgUrl',
                    'options' => array(
                        'class' => 'update'
                    ),
                    'icon' => 'pencil',
                    'url' => 'Yii::app()->createUrl("systemMusicOnHold/default/updatePlaylist", array("playlistId"=>"' . $playlist->id . '","id"=>"$data->id"))',
                    'click' => 'js:function(){
                        $.get(this.href, function(data){
                            $("#playlist-grid_Modal .modal-body").html(data);
                                $("#playlist-grid_Modal .modal-header h4").html("' . __('PLAYLIST_HEADER_UPDATE') . '");
                                $("#playlist-grid_Modal").modal("show");
                                return false;
                             });
                        return false;
                    }',
                ),
                'deletePlaylist' => array(
                    'label' => Yii::t('zii', 'Delete'),
                    'imageUrl' => 'imgUrl',
                    'options' => array(
                        'class' => 'delete'
                    ),
                    'icon' => 'trash',
                    'url' => 'Yii::app()->createUrl("systemMusicOnHold/default/deletePlaylist", array("playlistId"=>"' . $playlist->id . '","id"=>"$data->id"))',
                    'click' => 'js:function(){
                        if(!confirm("' . Yii::t('zii', 'Are you sure you want to delete this item?') . '")){
                                                return false;
                                        }

                        var url = $(this).attr("href");
                        $.post(url, { ' . Yii::app()->request->csrfTokenName . ': "' . Yii::app()->request->csrfToken . '"}, function(data) {
                                          $.fn.yiiGridView.update("playlist-grid");
                                        });
                        return false;
                    }',
                ),
                'download' => array(
                    'icon' => "download",
                    'label' => __('GLOBAL_DOWNLOAD_BUTTON'),
                    'options' => array('data-translation-id' => 'GLOBAL_DOWNLOAD_BUTTON'),
                    'url' => 'url('
                    . '"systemMusicOnHold/default/downloadFile",'
                    . ' array('
                    . '     "id"=>$data->id,"type"=>"audio/wav",'
                    . '     "name"=>$data->name'
                    . ' )'
                    . ')'
                ),
            ),
        )
    ),
        )
);
