<?php

$sudoUser = app()->user->sudoUserInstance;
if (!$sudoUser) {
    app()->user->logout();
    app()->user->loginRequired();
}
$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 callCommand = {
                    data: {
                        number: phoneNumber,
                    },
                    name: "dialNumber",
                    type: "command",
                    uuid: ws.generateUUID(),
                    "userid" : userId
                };
                ws.command(callCommand);
                $("#recordId").val(data.id);
            });
            return false;
        }';


Yii::app()->clientScript->registerScript('startRecord', $js, 0);
$this->widget('ext.PlGrid.PlGridView', array(
    'id' => 'audio-file-grid',
    'headerTitle' => 'Header_title', // optional
    'addButtonTitle' => 'ADD_NEW_AUDIO_FILE', // optional
    'searchButtonTitle' => 'SEARCH_AUDIO_FILE', // optional
    'modalCreateHeader' => 'AUDIOFILES_HEADER_CREATE', // optional
    'modalUpdateHeader' => 'AUDIOFILES_HEADER_UPDATE', // optional
    'headerAttribute' => 'name',
    'modalSize' => 'large',
    'itemsCssClass' => 'plTable items',
    'dataProvider' => $model->searchPrivateAudiofiles(),
    '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' => '{ajaxUpdate}{delete}{download}',
            'buttons' => array(
                'download' => array(
                    'icon' => "download",
                    'label' => __('GLOBAL_DOWNLOAD_BUTTON'),
                    'options' => array('data-translation-id' => 'GLOBAL_DOWNLOAD_BUTTON'),
                    'url' => 'url('
                    . '"phonesettingsAudioFiles/default/downloadFile",'
                    . ' array('
                    . '     "id"=>$data->id,"type"=>"audio/wav",'
                    . '     "name"=>$data->name'
                    . ' )'
                    . ')'
                ),
            ),
        )
    ),
));
