<?php

/**
 * @version        1.11.3
 * @package        Joomla
 * @subpackage     Edocman
 * @author         Dang Thuc Dam
 * @copyright      Copyright (C) 2011 - 2024 Ossolution Team
 * @license        GNU/GPL, see LICENSE.php
 */

// no direct access
defined('_JEXEC') or die;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Session\Session;

// Load the tooltip behavior.
HTMLHelper::_('behavior.keepalive');
HTMLHelper::_('bootstrap.tooltip', '.hasTooltip', ['placement' => 'left']);

$extralayoutCss     = ((int)$this->config->theme_type == 1 ? "dark_layout" : "");

$bootstrapHelper	= $this->bootstrapHelper;
$rowFluidClass   	= $bootstrapHelper->getClassMapping('row-fluid');
$span12Class		= $bootstrapHelper->getClassMapping('span12');
$span3Class		    = $bootstrapHelper->getClassMapping('span3');
$span6Class		    = $bootstrapHelper->getClassMapping('span6');
$controlLabelClass 	= $bootstrapHelper->getClassMapping('control-label');
$controlsClass     	= $bootstrapHelper->getClassMapping('controls');
$controlGroupClass 	= $bootstrapHelper->getClassMapping('control-group');

//in case the Upload limit is turned on
if(EDocmanHelper::isUploadLimitTurnedOn())
{
    $session = Factory::getApplication()->getSession();
    $session->set('files', array());
    $session->set('filesize', null);
    $session->set('originalFiles', null);
    $session->set('fileid', null);
}
if (version_compare(JVERSION, '4.0.0-dev', 'lt'))
{
?>
<script type="text/javascript">
	function submitDocument()
    {
		if (document.formvalidator.isValid(document.id('item-form')))
		{
            if (document.getElementById('jformcategory_id').value == 0)
            {
                alert("<?php echo Text::_('EDOCMAN_PLEASE_SELECT_CATEGORY'); ?>");
                document.getElementById('jformcategory_id').focus();
                return;
            }
			var answer = confirm('<?php echo Text::_('EDOCMAN_SAVE_DOCUMENT_CONFIRM');?>');
			if(answer == 1)
			{
				Joomla.submitform('upload.edit_documents', document.getElementById('item-form'));
			}
		}
		else
		{
			alert('<?php echo $this->escape(Text::_('JGLOBAL_VALIDATION_FORM_FAILED'));?>');
		}
	}
</script>
<?php
}
else
{
	$document = Factory::getApplication()->getDocument();
	$document->getWebAssetManager()->addInlineScript("
		function submitDocument() {
			if (document.getElementById('jformcategory_id').value == 0)
            {
                alert('".Text::_('EDOCMAN_PLEASE_SELECT_CATEGORY')."');
                document.getElementById('jformcategory_id').focus();
                return;
            }
			Joomla.submitform('upload.edit_documents', document.getElementById('item-form'));
		}
	");
}
?>
<script type="text/javascript">
	function cancelSubmit() {
		var form = document.getElementById('item-form') ;
		form.task.value = 'document.cancel' ;
		form.submit() ;
	}
</script>

<form action="<?php echo Route::_('index.php?option=com_edocman&layout=edit&id=0&Itemid='.Factory::getApplication()->getInput()->getInt('Itemid')); ?>" method="post" name="adminForm" id="item-form" class="form-validate <?php echo $extralayoutCss;?>" enctype="multipart/form-data">
	<?php
	if ($this->params->get('page_heading'))
	{
		$heading = $this->params->get('page_heading');
	}
	else
	{
		$heading = Text::_('EDOCMAN_UPLOAD_DOCUMENT');
	}
	?>
	<h1 class="edocman-page-heading"><?php echo $heading; ?></h1>
	<?php
	if($this->header_text != ""){
		?>
		<strong><?php echo $this->header_text;?></strong>
		<?php
	}
	?>
    <div id="edit-document-form" class="<?php echo $rowFluidClass;?> <?php echo $extraClass;?>">
		<div class="<?php echo $span12Class?>">
        <?php
            if ($this->catId)
            {
            ?>
                <div class="<?php echo $controlGroupClass;?>">
					<label class="<?php echo $controlLabelClass;?>">
                        <?php echo $this->form->getLabel('category_id'); ?>
                    </label>
					<div class="<?php echo $controlsClass;?>">
                        <input type="hidden" name="jform[category_id]" id="jformcategory_id" value="<?php echo $this->catId; ?>" />
                        <?php echo $this->categoryTitle; ?>
                    </div>
				</div>
            <?php
            }
            else
            {
            ?>
                <div class="<?php echo $controlGroupClass;?>">
					<label class="<?php echo $controlLabelClass;?>">
						<?php echo $this->form->getLabel('category_id'); ?>
					</label>
					<div class="<?php echo $controlsClass;?>">
						<?php //
							echo EdocmanHelper::getChoicesJsSelect($this->form->getInput('category_id')); 
						?>
					</div>
				</div>
            <?php
            }
        ?>
			<div class="<?php echo $rowFluidClass;?>">
				<div class="<?php echo $span12Class?>">
					<div id="uploader"><p>Your browser doesn't have Flash, Silverlight or HTML5 support.</p></div>
				</div>
			</div>
		</div>
		<div class="<?php echo $rowFluidClass;?>">
			<div class="<?php echo $span12Class?>" style="text-align:right;padding-top:15px;">
				<input type="button" class="btn btn-warning" onclick="cancelSubmit();" value="<?php echo Text::_('EDOCMAN_CANCEL'); ?>" />
				<input type="button" class="btn btn-success" onclick="submitDocument();" value="<?php echo Text::_('EDOCMAN_NEXT_STEP'); ?>"  />
			</div>
		</div>
	</div>
	<input type="hidden" name="task" value="" />
    <input type="hidden" name="Itemid" value="<?php echo Factory::getApplication()->getInput()->getInt('Itemid',0); ?>" />
	<input type="hidden" name="return" value="<?php echo Factory::getApplication()->getInput()->getString('return',''); ?>" />
	<?php echo HTMLHelper::_('form.token'); ?>
</form>
<script type="text/javascript">
    Edocman.jQuery(function($) {
        $("#uploader").pluploadQueue({
            runtimes : 'html5,flash,silverlight,html4',
            url : '<?php echo Uri::root() ?>index.php?option=com_edocman&task=upload.upload&format=json',
            max_file_size : '<?php echo $this->maxFilesize; ?>mb',
            unique_names : false,
            flash_swf_url : '<?php echo Uri::root(); ?>components/com_edocman/assets/js/plupload/plupload.flash.swf',
            multipart_params : {
                "<?php echo Session::getFormToken();?>" : "1"
            },
            // Specify what files to browse for
            filters : [
                {title : "<?php echo Text::_('EDOCMAN_ALLOWED_FILE_TYPES'); ?>", extensions : "<?php echo $this->allowedFiletypes; ?>"}
            ],

            // Rename files by clicking on their titles
            rename: true,
            init: EdocmanMediaUploaderCallBacks
        });

        function EdocmanMediaUploaderCallBacks( uploader )
        {
			
            uploader.bind('BeforeUpload', function(up, file) {
                var categoryId = $('#jformcategory_id').val();
                up.settings.multipart_params = {"category_id":  categoryId, "<?php echo Session::getFormToken();?>" : "1"};
            });

			uploader.bind('FilesAdded', function(up, file) {
                up.start();
            });

            uploader.bind( 'Error', function( up, args ) {
				//alert(args.code);
            } );

            uploader.bind( 'FileUploaded', function( up, file, response ) {
                var res = response.response;
                if ( res )
                {
                    var objResponse = jQuery.parseJSON( res );
                    if ( typeof objResponse.error != 'undefined')
                    {
                        up.trigger( 'Error', {
                            code:    -300,
                            message: 'Upload Failed',
                            details: file.name + ' failed',
                            file:    file
                        } );
                        return false;
                    }
                    else
                    {
                    }
                }
            } );
        }
    });
</script>
