<?php
echo "<h4 data-translation-id='PHONE_PARAMETERS_UPLOADABLEFILES_HEADER'>".__('PHONE_PARAMETERS_UPLOADABLEFILES_HEADER')."</h4>";

$inputRows = array();

foreach($phoneModel->getUploadableFiles() as $file) {
    $row = array(
        'id' => '',
        'name' => $file['name'],
        'description' => generateDescriptionForFile($file),
        'input' => generateInputForFile($file),
        'download' => generateImageForFile($file, $profile),
    );
    
    $inputRows[] = $row;
}

$gridDataProvider = new CArrayDataProvider($inputRows);




$this->widget('bootstrap.widgets.TbGridView', array(
    'type'=>'striped bordered condensed',
    'dataProvider'=>$gridDataProvider,
    'template'=>"{items}",
    'htmlOptions' => array(
        'class' => 'plTable'
    ),
    'columns'=>array(
        array(
            'name'=>'name',
            'header'=>__('PHONE_PARAMETERS_UPLOADABLEFILES_NAME'),
            'headerHtmlOptions'=>array('data-translation-id'=> 'PHONE_PARAMETERS_UPLOADABLEFILES_NAME'),
        ),
        array(
            'name'=>'description',
            'type' => 'raw',
            'header'=>__('PHONE_PARAMETERS_UPLOADABLEFILES_DESCRIPTION'),
            'headerHtmlOptions'=>array('data-translation-id'=> 'PHONE_PARAMETERS_UPLOADABLEFILES_DESCRIPTION'),
        ),
        array(
            'name'=>'input',
            'type' => 'raw',
            'header'=>__('PHONE_PARAMETERS_UPLOADABLEFILES_FILE'),
            'headerHtmlOptions'=>array('data-translation-id'=> 'PHONE_PARAMETERS_UPLOADABLEFILES_FILE'),
        ),
        array(
            'name'=>'download',
            'type' => 'raw',
            'header'=>__('PHONE_PARAMETERS_UPLOADABLEFILES_DOWNLOAD'),
            'headerHtmlOptions'=>array('data-translation-id'=> 'PHONE_PARAMETERS_UPLOADABLEFILES_DOWNLOAD'),
        ),
/*
        array(
            'class'=>'bootstrap.widgets.TbButtonColumn',
            'htmlOptions'=>array('style'=>'width: 50px'),
        ),
*/
    ),
));


function generateDescriptionForFile($fileConfig) {
    $description = '';
    
    if($fileConfig['type'] == 'image') {
        // show image dimensions
        if(isset($fileConfig['details']['width']) && isset($fileConfig['details']['height'])){
            $description .= __('PHONE_PARAMETERS_UPLOADABLEFILES_IMAGE_RESOLUTION').' ';
            $description .= $fileConfig['details']['width'] . 'x' . $fileConfig['details']['height'];
            $description .= '<br>';
        }

    }
    
    if(isset($fileConfig['maxFileSize'])){
        $description .= __('PHONE_PARAMETERS_UPLOADABLEFILES_MAX_FILE_SIZE').' ';
        $description .= formatSize($fileConfig['maxFileSize']);
        $description .= '<br>';        
    }
    
    $description .= __('PHONE_PARAMETERS_UPLOADABLEFILES_MIME_TYPE').' ';
    $description .= $fileConfig['mimeType'];
    $description .= '<br>';  
    
    return $description;
}

function generateInputForFile($fileConfig) {
    return CHtml::fileField(
        'UploadableFiles['.$fileConfig['name'].']'
    );
}

function generateImageForFile($file, $phoneModel) {
    $data = null;
    $length = null;
    $fileExists = $phoneModel->getBinaryField($file['name'], $data, $length);
    
    if(!$fileExists) {
        return;
    }
    
    //dump($phoneModel->getBinaryField($file['name'], $data, $length));
    

    $imgUrl = Yii::app()->createUrl('provisioningProfile/default/getDownloadableFile', array('id'=>$_GET['id'], 'name' => $file['name']));
    return CHtml::image(
        $imgUrl,
        '',
        array(
            'class' => 'imagePreview',
            'width'=>24,
            //'data-title'=>'A Title',
            'data-content'=>CHtml::image($imgUrl),
            'data-trigger'=>'hover',
            'data-placement'=>'left',
            'data-html'=>true,
            'rel'=>'popover'
        )
    );    
}





	if (Yii::app()->user->translateMode) { ?>
		<span data-translation-id="PHONE_PARAMETERS_UPLOADABLEFILES_MAX_FILE_SIZE">PHONE_PARAMETERS_UPLOADABLEFILES_MAX_FILE_SIZE: <?php echo __('PHONE_PARAMETERS_UPLOADABLEFILES_MAX_FILE_SIZE');?></span><br />
		<span data-translation-id="PHONE_PARAMETERS_UPLOADABLEFILES_IMAGE_RESOLUTION">PHONE_PARAMETERS_UPLOADABLEFILES_IMAGE_RESOLUTION: <?php echo __('PHONE_PARAMETERS_UPLOADABLEFILES_IMAGE_RESOLUTION');?></span><br />
		<span data-translation-id="UPLOADED_FILE_IS_TO_LARGE">UPLOADED_FILE_IS_TO_LARGE: <?php echo __('UPLOADED_FILE_IS_TO_LARGE');?></span><br />
		<span data-translation-id="INVALID_IMAGE_RESOLUTION">INVALID_IMAGE_RESOLUTION: <?php echo __('INVALID_IMAGE_RESOLUTION');?></span><br />
		<span data-translation-id="PHONE_PARAMETERS_UPLOADABLEFILES_MIME_TYPE">PHONE_PARAMETERS_UPLOADABLEFILES_MIME_TYPE: <?php echo __('PHONE_PARAMETERS_UPLOADABLEFILES_MIME_TYPE');?></span><br />
<?php 
	} 
?>
<script>
$('.imagePreview').popover();
</script>