<?php
/**
 * @author    Agence Dn'D <magento@dnd.fr>
 * @copyright Copyright (c) 2015 Agence Dn'D (http://www.dnd.fr)
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
?>

<?php $tasks = $this->getTasks(); ?>

<div class="task-command">
    <?php if(count($tasks)): ?>
        <select name="task-command" id="task-command">
            <option value=""><?php echo $this->__('Select') ?></option>
            <?php foreach($tasks as $command => $task): ?>
                <option value="<?php echo $command ?>"><?php echo $task['label'] ?></option>
            <?php endforeach; ?>
        </select>
    <?php else: ?>
        <p><?php echo $this->__('No task was found') ?></p>
    <?php endif; ?>
</div>

<div id="task-execute-zone">

    <form action="#" name="task-options" id="task-options"></form>

    <div id="tasks-types">
        <div id="type-file" class="task-execute" style="display:none">
            <div id="<?php echo $this->getHtmlId() ?>" class="uploader">
                <div class="buttons">
                    <div id="<?php echo $this->getHtmlId() ?>-install-flash" style="display:none">
                        <?php echo $this->__('This content requires last version of Adobe Flash Player.') ?>
                    </div>
                </div>
                <div class="clear"></div>
                <div class="no-display" id="<?php echo $this->getHtmlId() ?>-template">
                    <div id="{{id}}" class="file-row">
                    <span class="file-info">{{name}} ({{size}})</span>
                    <span class="delete-button"><?php echo $this->getDeleteButtonHtml() ?></span>
                    <span class="progress-text"></span>
                    <div class="clear"></div>
                    </div>
                </div>
                <div class="no-display" id="<?php echo $this->getHtmlId() ?>-template-progress">
                    {{percent}}% {{uploaded}} / {{total}}
                </div>
            </div>
        </div>
        <div id="type-button" class="task-execute" style="display:none">
            <?php echo $this->getExecuteButtonHtml() ?>
        </div>
    </div>

</div>

<script type="text/javascript">
//<![CDATA[
    maxUploadFileSizeInBytes = <?php echo $this->getDataMaxSizeInBytes() ?>;
    maxUploadFileSize = '<?php echo $this->getDataMaxSize() ?>';

    function initUploader() {

        $$('#type-file .complete').each(function(item) {
            item.remove();
        });

        <?php echo $this->getJsObjectName() ?> = new Flex.Uploader('<?php echo $this->getHtmlId() ?>', '<?php echo $this->getSkinUrl('media/uploader.swf') ?>', <?php echo $this->getConfigJson() ?>);
        <?php echo $this->getJsObjectName() ?>.onFilesComplete = function(completedFiles) {
            completedFiles.each(function(file){
                var id = <?php echo $this->getJsObjectName() ?>.getFileId(file);

                var progress = $(id).down('.progress-text');

                progress.update('<?php echo $this->getExecuteButtonHtml() ?>');
                progress.down().rel = file.response.replace(/"/g, "");

                var lastId = $$('.file-row').last().id;
                if (lastId !== id) {
                    $(id).down('.progress-text').update(Translator.translate('Complete'));
                }
            });
        };

        var flash = $('<?php echo $this->getHtmlId() ?>-flash');
        if (flash != undefined) { flash.setStyle({float:'none'}); }

    }

    $('task-command').observe('change', function() {
        initUploader();
    });

    var taskExecutor = new TaskExecutor(
        '<?php echo $this->getUrl('adminhtml/task/launch') ?>',
        '<?php echo $this->getUrl('adminhtml/task/options') ?>',
        <?php echo $this->getTasksJson() ?>);
//]]>
</script>
