<?php

/**
 * @version         1.9.7
 * @package        Joomla
 * @subpackage     EDocman
 * @author         Tuan Pham Ngoc
 * @copyright	   Copyright (C) 2011-2023 Ossolution Team
 * @license        GNU/GPL, see LICENSE.php
 */

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

require_once JPATH_ROOT.'/components/com_edocman/helper/file.class.php' ;
$user               = Factory::getUser();
$userId             = $user->id;
$bootstrapHelper    = $this->bootstrapHelper;
$rowFluidClass      = $bootstrapHelper->getClassMapping('row-fluid');
$span12Class        = $bootstrapHelper->getClassMapping('span12');
$config				= $this->config;
$documents          = $this->documents;
$controlGroupClass  = $bootstrapHelper->getClassMapping('control-group');
$controlLabelClass  = $bootstrapHelper->getClassMapping('control-label');
$controlsClass      = $bootstrapHelper->getClassMapping('controls');
$session			= Factory::getSession();
if(!$config->onetime_collect)
{
    $session->set('name','');
    $session->set('email','');
}
$name				= $session->get('name','');
$email				= $session->get('email','');
if ($config->collect_downloader_information && !$userId && ($name == '' || $email == ''))
{
    $showDownloadForm		= true;
}
else
{
    $showDownloadForm		= false;
}
$extralayoutCss = ((int)$config->theme_type == 1 ? "dark_layout" : "");
?>
<style>
.edocman-page-heading
{
	font-size: <?php echo ($this->config->heading_font_size == '') ? '30' : $this->config->heading_font_size; ?>px !important;
}
</style>
<div id="edocman-category-page-default" class="edocman-container massDownloadList <?php echo $extralayoutCss;?>">
	<?php
	if(count($documents))
	{
		?>
		<div id="edocman-category">
			<h1 class="edocman-page-heading" style="display: inline;">
				<?php
				echo Text::_('EDOCMAN_MASS_DOWNLOAD');
				?>
			</h1>
		</div>
		<div class="<?php echo $rowFluidClass?>">
			<div class="<?php echo $span12Class?>">
				<form method="post" action="<?php echo Uri::root()?>index.php?option=com_edocman" name="adminForm" id="adminForm">
					<table class="table-condensed table-document" id="table-document" style="width:100%;">
						<thead>
							<tr>
								<th class="edocman-document-thumb-col">
								</th>
								<th class="edocman-document-title-col" style="width:90%;">
									<?php echo Text::_('EDOCMAN_TITLE');?>
								</th>
								<th style="width:5%;">
									<?php echo Text::_('EDOCMAN_REMOVE');?>
								</th>
							</tr>
						</thead>
						<tbody>
							<?php
							$i = 0;
							foreach($documents as $document)
							{
								$i++;
								$document->data         = new EDocman_File($document->id,$document->filename, $config->documents_path) ;
								?>
								<tr>
									<td class="edocman-document-icon-td" data-label="">
										<?php EDocmanHelperHtml::showFileIcon($document->data); ?>
									</td>
									<td class="edocman-document-title-td" data-label="">
										<?php
											echo $document->title;
										?>
									</td>
									<td style="text-align: center;">
										<a href="javascript:removeDocumentMassDownload(<?php echo $document->id;?>)" title="<?php echo Text::_('EDOCMAN_REMOVE_DOCUMENT_OUT_OF_MASS_DOWNLOAD_LIST');?>">
											<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-trash3" viewBox="0 0 16 16">
											  <path d="M6.5 1h3a.5.5 0 0 1 .5.5v1H6v-1a.5.5 0 0 1 .5-.5M11 2.5v-1A1.5 1.5 0 0 0 9.5 0h-3A1.5 1.5 0 0 0 5 1.5v1H2.506a.58.58 0 0 0-.01 0H1.5a.5.5 0 0 0 0 1h.538l.853 10.66A2 2 0 0 0 4.885 16h6.23a2 2 0 0 0 1.994-1.84l.853-10.66h.538a.5.5 0 0 0 0-1h-.995a.59.59 0 0 0-.01 0zm1.958 1-.846 10.58a1 1 0 0 1-.997.92h-6.23a1 1 0 0 1-.997-.92L3.042 3.5zm-7.487 1a.5.5 0 0 1 .528.47l.5 8.5a.5.5 0 0 1-.998.06L5 5.03a.5.5 0 0 1 .47-.53Zm5.058 0a.5.5 0 0 1 .47.53l-.5 8.5a.5.5 0 1 1-.998-.06l.5-8.5a.5.5 0 0 1 .528-.47ZM8 4.5a.5.5 0 0 1 .5.5v8.5a.5.5 0 0 1-1 0V5a.5.5 0 0 1 .5-.5"/>
											</svg>
										</a>
									</td>
								</tr>
								<?php
							}
							?>
						</tbody>
					</table>
					<?php
					if($showDownloadForm && count($documents))
					{
						?>
						<div class="<?php echo $rowFluidClass ?>" id="collectDownloadMassForm">
							<div class="<?php echo $span12Class ?>">
								<h3><?php echo Text::_('EDOCMAN_MASS_DOWNLOAD_HEADING'); ?></h3>
								<div class="<?php echo $controlGroupClass; ?>">
									<label class="<?php echo $controlLabelClass; ?>" for="name">
										<?php echo Text::_('EDOCMAN_NAME') ?><span class="required">*</span>
									</label>
									<div class="<?php echo $controlsClass; ?>">
										<input type="text" name="name" id="edocman-name" class="input-large ilarge form-control" style="max-width:250px;" />
									</div>
								</div>

								<div class="<?php echo $controlGroupClass; ?>">
									<label class="<?php echo $controlLabelClass; ?>" for="name">
										<?php echo Text::_('EDOCMAN_EMAIL') ?><span class="required">*</span>
									</label>
									<div class="<?php echo $controlsClass; ?>">
										<input id="email" type="email" name="email" value="" class="input-large ilarge form-control" style="max-width:250px;"/>
									</div>
								</div>
								<?php
								if ($config->turn_on_privacy) {
									?>
									<div class="<?php echo $controlGroupClass; ?>">
										<input type="checkbox" name="privacy_agreement" id="privacy_agreement" value="0"/>
										<span id="privacy_agreement_span">
										<?php
										echo Text::_('EDOCMAN_AGREE_PRIVACY_POLICY_MESSAGE');
										?>
										</span>
									</div>
								<?php } ?>
							</div>
						</div>
						<?php
					}
					if($showDownloadForm)
					{
						$label = Text::_('EDOCMAN_SUBMIT')." & ".Text::_('EDOCMAN_DOWNLOAD');
					}
					else
					{
						$label = Text::_('EDOCMAN_DOWNLOAD');
					}
					?>
					<div class="<?php echo $rowFluidClass?>">
						<div class="<?php echo $span12Class?>" style="text-align:center;padding-top:10px;">
							<a id="massDownloadBtn" href="javascript:void(0)" class="btn btn-secondary" title="<?php echo Text::_('EDOCMAN_CLICK_HERE_TO_DOWNLOAD_DOCUMENTS');?>"><i class="edicon edicon-download"></i>&nbsp;<?php echo $label;?></a>
							<a href="javascript:void(0)" class="btn btn-secondary" title="<?php echo Text::_('EDOCMAN_BACK');?>" onClick="jvascript:window.history.back();"><?php echo Text::_('EDOCMAN_BACK');?></a>
						</div>
					</div>
					<input type="hidden" name="option" value="com_edocman"/>
					<input type="hidden" name="task" id="task" value="" />
				</form>
			</div>
		</div>
	<?php
	}
	else
	{
		Factory::getApplication()->enqueueMessage(Text::_('EDOCMAN_NO_MASS_DOWNLOAD'));
		?>
		<div class="<?php echo $rowFluidClass?>">
			<div class="<?php echo $span12Class?>" style="text-align:center;padding-top:10px;">
				<a href="javascript:void(0)" class="btn btn-secondary" title="<?php echo Text::_('EDOCMAN_BACK');?>" onClick="jvascript:window.history.back();"><?php echo Text::_('EDOCMAN_BACK');?></a>
			</div>
		</div>
		<?php
	}
	?>
</div>
<script type="text/javascript">
    <?php
    if(!$showDownloadForm)
    {
        ?>
        jQuery('#massDownloadBtn').click(function(){
            jQuery('#task').val('document.processMassDownload');
            jQuery('#adminForm').submit();
        });
    <?php
    }
    else
    {
        ?>
        jQuery('#massDownloadBtn').click(function(){
            var validate = validateForm();
            if(validate)
            {
				jQuery('#collectDownloadMassForm').slideUp();
                jQuery('#task').val('document.processMassDownload');
                jQuery('#adminForm').submit();
            }
        });
        <?php
    }
    ?>
    function validateForm()
    {
        var emailReg            = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,10})?$/;
        var names               = jQuery('#edocman-name').val();
        var email               = jQuery('#email').val();
        var inputVal            = new Array(names, email);

        var inputMessage = new Array("name", "email address");
        jQuery('.error').hide();
        if(inputVal[0] == ""){
            jQuery('#edocman-name').after('<span class="error"> Please enter your ' + inputMessage[0] + '</span>');
            return false;
        }
        if(inputVal[1] == ""){
            jQuery('#email').after('<span class="error"> Please enter your ' + inputMessage[1] + '</span>');
            return false;
        }
        else if(!emailReg.test(email)){
            jQuery('#email').after('<span class="error"> Please enter a valid email address</span>');
            return false;
        }
        <?php
        if($config->turn_on_privacy)
        {
        ?>
            if(document.getElementById('privacy_agreement').checked === false)
            {
                jQuery('#privacy_agreement_span').after('<span class="error"> <?php echo Text::_('EDOCMAN_AGREE_PRIVACY_POLICY_ERROR');?></span>');
                return false;
            }
        <?php } ?>
        return true;
    }
	function removeDocumentMassDownload(documentId)
	{
		var answer = confirm("<?php echo Text::_('EDOCMAN_DO_YOU_WANT_TO_REMOVE_DOCUMENT_OUT_OF_MASS_LIST');?>");
		if(answer)
		{
			location.href = "<?php echo Uri::root()?>index.php?option=com_edocman&task=document.removeDocumentMassList&id=" + documentId + "&Itemid=<?php echo $this->itemid; ?>";
		}
	}
</script>
