<?php
use YesWiki\Core\Service\AssetsManager;
$GLOBALS['wiki']->services->get(AssetsManager::class)->AddCSSFile('custom/templates/bazar/assets/activites.css');
if (empty($_GET['print']) || $_GET['print'] != 1) {
  $renderedEntry = '';
  foreach ($form['prepared'] as $field) {
     $renderedEntry .= $field->renderStaticIfPermitted($fiche, $GLOBALS['wiki']->GetUsername());
  }                  
  echo $renderedEntry;
  echo '<a class="btn btn-primary no-print new-window" href="'.$GLOBALS['wiki']->href('', '', 'print=1&returnTo='.$GLOBALS['wiki']->href('', '')).'"><i class="fa fa-print" aria-hidden="true"></i> Imprimer cette fiche</a>';
} else {
  $imgwidth = $imgheight = 600;
  $imgCarree = '';
if (isset($fiche['imagebf_image']) && $fiche['imagebf_image']!='') {
  $imgCarree = '<img class="img-responsive img-carree" alt="'.$fiche['bf_titre'].'" src="'.redimensionner_image('files/'.$fiche['imagebf_image'], 'cache/image_'.$imgwidth.'x'.$imgheight.'_'.$fiche['imagebf_image'], $imgwidth, $imgheight, 'crop').'" />';
}

$imgCarree2 = '';
if (isset($fiche['imagebf_image1']) && $fiche['imagebf_image1']!='') {
  $imgCarree2 = '<img class="img-responsive img-carree" alt="'.$fiche['bf_titre'].'" src="'.redimensionner_image('files/'.$fiche['imagebf_image1'], 'cache/image_'.$imgwidth.'x'.$imgheight.'_'.$fiche['imagebf_image1'], $imgwidth, $imgheight, 'crop').'" />';
} else {
  $imgCarree2 = $imgCarree;
  $fiche['bf_credit2'] = $fiche['bf_credit'];
}

$qrcode = '<span class="rotated-img"><img src="custom/templates/bazar/assets/vers-fiche.png" alt="vers fiche" /></span><span class="qrcode-container">'.$GLOBALS['wiki']->format('{{qrcode text="' . $GLOBALS['wiki']->href('', $fiche['id_fiche']) . '"}}').'</span>';
$qrcodeurl = '';
if (!empty($fiche['bf_url'])) {
$qrcodeurl = '<span class="rotated-img"><img src="custom/templates/bazar/assets/l-info-en-plus.png" alt="l\'info en +" /></span><span class="qrcode-container">'.$GLOBALS['wiki']->format('{{qrcode text="'.$fiche['bf_url'].'"}}').'</span>';
} else {
  $qrcodeurl = $qrcode;
}

$structure = !empty($fiche['bf_structure']) ? $fiche['bf_structure'] : $fiche['bf_nom_contributeur'];

echo <<<TEMPLATE
<div class="full-portrait-a4 recto">
  <div class="left">
  </div>
  <div class="right">
    <div class="trou"></div>
  <div class="logo"><img class="main-logo" src="custom/templates/bazar/assets/PIMP_TA_BALADE_vert.svg" alt="Pimp ta balade" /></div>
    <h1 class="main-title">{$fiche['bf_titre']}</h1>
    <div class="description">{$html['bf_description']}</div>
    <div class="image-carree">{$imgCarree}</div>
    <div class="credit-photo">{$fiche['bf_credit']}</div>
    <div class="infos">
      <div class="author">
        <span class="green-bg">ressource</span>
        <span class="green-bg">proposée par</span>
        <strong>{$structure}</strong>     
      </div>
      <div class="qrcode">
        {$qrcodeurl}
      </div>
    </div>
    <div class="footer-fiche">
      <a class="link" href="https://www.pimp-ta-balade.be">www.pimp-ta-balade.be</a>
      <img src="custom/templates/bazar/assets/coq_wallonie_soutien.svg" alt="Région Wallonie" />
    </div>
  </div>
</div>
<div class="full-portrait-a4 verso">
  <div class="left"></div>
  <div class="right">
    <div class="trou"></div>
    <div class="reponse">{$html['bf_reponse']}</div>
    <div class="image-carree2">{$imgCarree2}</div>
    <div class="credit-photo">{$fiche['bf_credit2']}</div>
    <div class="qrcode">{$qrcode}</div>
    <div class="footer-fiche">
      <img class="petit-pimp" src="custom/templates/bazar/assets/PIMP_vert.svg" alt="Pimp" />
      <a class="link" href="https://www.pimp-ta-balade.be">www.pimp-ta-balade.be</a>
    </div>
  </div>
</div>
<div class="full-portrait-a4 indications">
  <img src="custom/templates/bazar/assets/Instructions_pliage_recto.webp" alt="instructions recto" />
</div>
<div class="full-portrait-a4 indications2">
  <img src="custom/templates/bazar/assets/Instructions_pliage_verso.webp" alt="instructions verso" />
</div>
<button style="margin-top:1em;" class="btn btn-primary no-print new-window" onclick="window.print()"><i class="fa fa-print" aria-hidden="true"></i> Imprimer cette fiche</button>
<script>
// un hack tres aléatoire des css print de bootstrap
//document.addEventListener('DOMContentLoaded', function(event) {
//	document.styleSheets[0].cssRules[40].cssRules[0].style= []
//})
</script>

TEMPLATE;
if (!empty($_GET['returnTo'])) {
  echo <<<JS
<script>
document.addEventListener('DOMContentLoaded', function(event) {
  window.onafterprint = function () {
    window.location.href = "{$_GET['returnTo']}";
  }
  const goBack = () => {
    window.location.href = "{$_GET['returnTo']}";
    window.removeEventListener('focus', goBack);
  };

  window.addEventListener('focus', goBack);
  window.print()
})
</script>
JS;
}
}
?> 

