<?php
/**
 * Template file for metabox "Nahrat soubor".
 *
 * @author  Ondřej Doněk, <ondrejd@gmail.com>
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License 3.0
 * @link https://github.com/ondrejd/od-downloads-plugin for the canonical source repository
 * @package odwp-downloads_plugin
 * 
 * @todo Limit count of uploaded files just to one!
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit();
}

/* === */
/**
 * @todo Move into {@see odwpdp_show_metabox_3()}.
 */
$max_size  = (int) ( wp_max_upload_size() / 1048576 );
$file_name = get_post_meta( get_the_ID(), 'odwpdp-metabox-3', true );
$file_path = sprintf( '%s/%s', ODWPDP_UPLOADS, $file_name );
$file_size = file_exists( $file_path )
        ? number_format( filesize( $file_path ) / 1025, 2 ) . ' KB'
        : sprintf( '<em style="color: darkred;">%s</em>', __( 'Připojený soubor chybí nebo je smazán', ODWPDP_SLUG ) );

$afiles = array( 0 => $file_name );
/* === */

wp_nonce_field( 'odwpdp-metabox3-nonce', 'odwpdp-metabox-3-nonce' );

?><div>
    <label class="screen-reader-text" for="odwpdp-metabox-2-jj"><?php _e( 'Zadejte datum sejmutí:', ODWPDP_SLUG ); ?></label>
    <div>
        <input type="hidden" name="file[files][]" value="<?php echo is_array($afiles) && isset($afiles[0]) ? $afiles[0] : ''; ?>" id="wpdmfile">
        <div class="cfile-container">
            <div class="cfile" id="cfl">
                <?php if ( ! empty( $file_name ) ) : ?>
                <div class="cfile-info"><strong><?php echo $file_name; ?></strong><br/><?php echo $file_size; ?> <a href="#" id="dcf" title="<?php _e( 'Smazat soubor', ODWPDP_SLUG ); ?>" style=""><span class="dashicons dashicons-no"></span></a></div>
                <?php else: ?>
                <span class="nofile"><?php _e( 'Zatím nebyl nahrán žádný soubor.', ODWPDP_SLUG ); ?></span>
                <?php endif; ?>
                <div style="clear: both;"></div>
            </div>
        </div>
        <div class="tab-content">
            <div class="tab-pane active" id="upload">
                <div id="plupload-upload-ui" class="hide-if-no-js">
                    <div id="drag-drop-area">
                        <div class="drag-drop-inside">
                            <p class="drag-drop-info"><?php _e( 'Přesuňte soubor sem', ODWPDP_SLUG ); ?></p>
                            <p>&mdash; <?php _ex( 'nebo', 'Přesuňte soubor sem - nebo - Vyberte soubor', ODWPDP_SLUG ); ?> &mdash;</p>
                            <p class="drag-drop-buttons">
                                <input id="plupload-browse-button" type="button" value="<?php esc_attr_e( 'Vyberte soubor', ODWPDP_SLUG ); ?>" class="button" /><br/>
                                <small><?php printf( __( 'Maximální velikost: %d MB', ODWPDP_SLUG ), $max_size ); ?></small>
                            </p>
                        </div>
                    </div>
                </div>
                <?php
                $plupload_init = array(
                    'runtimes'            => 'html5,silverlight,flash,html4',
                    'browse_button'       => 'plupload-browse-button',
                    'container'           => 'plupload-upload-ui',
                    'drop_element'        => 'drag-drop-area',
                    'file_data_name'      => 'package_file',
                    'multiple_queues'     => true,
                    'max_file_size'       => wp_max_upload_size() . 'b',
                    'url'                 => admin_url( 'admin-ajax.php' ),
                    'flash_swf_url'       => includes_url( 'js/plupload/plupload.flash.swf' ),
                    'silverlight_xap_url' => includes_url( 'js/plupload/plupload.silverlight.xap' ),
                    'filters'             => array( array( 'title' => __( 'Podporované typy souborů', ODWPDP_SLUG ), 'extensions' => '*' ) ),
                    'multipart'           => true,
                    'urlstream_upload'    => true,
                    'multipart_params'    => array(
                        '_ajax_nonce'     => wp_create_nonce( 'odwpdp-metabox-3-nonce' ),
                        'action'          => ODWPDP_AJAX_UP,
                    ),
                );
                ?>
                <script type="text/javascript">
jQuery(document).ready(function($){
    // create the uploader and pass the config from above
    var uploader = new plupload.Uploader(<?php echo json_encode($plupload_init); ?>);

    // checks if browser supports drag and drop upload, makes some css adjustments if necessary
    uploader.bind('Init', function(up){
        var uploaddiv = jQuery('#plupload-upload-ui');

        if(up.features.dragdrop){
            uploaddiv.addClass('drag-drop');
            jQuery('#drag-drop-area')
                .bind('dragover.wp-uploader', function(){ uploaddiv.addClass('drag-over'); })
                .bind('dragleave.wp-uploader, drop.wp-uploader', function(){ uploaddiv.removeClass('drag-over'); });

        }else{
            uploaddiv.removeClass('drag-drop');
            jQuery('#drag-drop-area').unbind('.wp-uploader');
        }
    });

    uploader.init();

    // a file was added in the queue
    uploader.bind('FilesAdded', function(up, files){
        plupload.each(files, function(file){
            jQuery('#filelist').append(
                '<div class="file" id="' + file.id + '"><b>' +

                file.name + '</b> (<span>' + plupload.formatSize(0) + '</span>/' + plupload.formatSize(file.size) + ') ' +
                '<div class="progress progress-success progress-striped active"><div class="bar fileprogress"></div></div></div>');
        });

        up.refresh();
        up.start();
    });

    uploader.bind('UploadProgress', function(up, file) {
        jQuery('#' + file.id + " .fileprogress").width(file.percent + "%");
        jQuery('#' + file.id + " span").html(plupload.formatSize(parseInt(file.size * file.percent / 100)));
    });

    // a file was uploaded
    uploader.bind('FileUploaded', function(up, file, response) {
        jQuery('#' + file.id ).remove();
        var d = new Date();
        var ID = d.getTime();
        response = response.response;
        var data = response.split("|||");
        jQuery('#wpdmfile').val(data[1]);
        jQuery('#cfl').html('<div class="cfile-info"><strong>'+data[1]+'</strong> <a href="#" id="dcf" title="<?php _e( 'Smazat soubor', ODWPDP_SLUG ); ?>"><span class="dashicons dashicons-no"></span></a>').slideDown();
    });
});
                </script>
                <div id="filelist"></div>

                <div class="clear"></div>
            </div>
        </div>
    </div>
</div>