<?php
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" 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-text">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-text">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="qrcode">
      <div class="author">
        <span class="green-bg">ressource</span>
        <span class="green-bg">proposée par</span>
        <strong>{$structure}</strong>     
      </div>
      {$qrcodeurl}
    </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.jpg" alt="instructions recto" />
</div>
<div class="full-portrait-a4 indications">
  <img src="custom/templates/bazar/assets/Instructions_pliage_verso.jpg" alt="instructions verso" />
</div>
<button style="margin-top:1em;" class="btn btn-primary no-print" onclick="window.print()"><i class="fa fa-print" aria-hidden="true"></i> Imprimer cette fiche</button>

<style>
  .full-portrait-a4 {
    display: grid;
    grid-template-columns: repeat(2, minmax(50%, 50%));
    gap: 0;
    justify-content: center;
    width: 210mm;
    height: 297mm;
    border: 1px dotted #aaa;
  }
  .indications {
    display: block;
  }
  .indications img {
    width: 208mm;
    height: 295mm;
    text-align:center;
  }
  .left,
  .right {
    padding:8mm;
    height: 297mm;
    position: relative;
  }
  .right {
    border-left: 1px dotted #aaa;
  }
  img {
    border-radius:0;
  }
  /* RECTO GAUCHE */

  /* RECTO DROITE */
  .recto .right {
    display: grid;
    grid-template-columns: 1fr;
    grid-template-rows: 20mm 16mm 89mm 89mm 10mm 30mm 26mm;
  }
  .logo {
    display: flex;
    align-items: flex-start;
    justify-content: flex-end;
  }
  .main-logo {
    width:36mm;
    margin-right:-2.7mm;
  }
  .main-title {
    background-color: #759440;
    color:white;
    font-size: 22px;
    text-transform: uppercase;
    padding: 2mm;
    margin: 0;
    line-height: 1.17em;
    align-content: center;
    overflow: hidden; 
  }
  .description, .reponse {
    font-size:18px;
    padding: 2mm;
  }
  .description {
    align-content: start;
  }
  .credit-photo {
    align-content: start;
    text-align: right;
    font-size: 15px;
    font-style: italic;
  }
  .footer-fiche {
    display: flex;
  justify-content: space-between;
  align-content: end;
}
.link {
  color: #759440;
  align-content: end;
  font-weight:bolder;
    text-decoration: underline;
}

.qrcode {
align-content: end;
align-items: end;
display: flex;
justify-content: end;
gap: 0;
}
  .recto .qrcode {
    display: flex;
    justify-content: space-between;
  }

.green-bg {
  color:white;
  background: #759440;
padding: .5mm 2mm;
  font-style: italic;
  }
  .author .green-bg {
  display: block;
  width: fit-content;
  padding: 0 2mm;
}
  .rotated-text {
background: #759440;
color: white;
padding: .5mm 2mm;
transform: rotate(-0.25turn) translateX(6mm) translateY(4mm);
font-size: 15px;
}
  .recto .rotated-text {
    margin-right:-13mm;
  }
.recto .right .footer-fiche img {
width: 20mm;
margin-right: -3mm;
margin-bottom: -7mm;
}

  /* VERSO GAUCHE */

  /* VERSO DROITE */
  .verso .right {
    background: url('custom/templates/bazar/assets/petits_pas.svg') repeat-y 3mm 14mm;
    padding-left: 24mm;
    background-size: 60px;
    display: grid;
    grid-template-columns: 1fr;
    grid-template-rows: 148mm 72mm 10mm 36mm 16mm;
  }
  .verso .footer-fiche {
    margin-left: -24mm;
    margin-right: -8mm;
    background: white;
    padding: 0 8mm;
  }
  .petit-pimp { 
    width:30mm;
    margin-bottom: -6mm;
    position: relative;
    display: block;
    background: white;
  }
  .trou {
    display: none;
    background-color: black;
    width:16px;
    height:16px;
    border-radius: 50%;
    position: absolute;
    top:16mm;
    left:16mm
  }

@media print {
  @page {
    size: A4 portrait;
    margin:0;
  }

  ::-webkit-scrollbar {
    display: none;
  }

  body {
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
    margin: 0;
  }
  a[href]::after {
    content: none;
  }
  .footer,
  #yw-topnav,
  #yw-header,
  #yw-footer,
  .filters-col,
  .debug,
  .BAZ_fiche_info,
  .embed-responsive,
  .no-print {
    display: none !important;
  }
  .right,.full-portrait-a4 {
    border:none;
  }
  #yw-main {
    padding: 0;
  }
  .indications img {
    width: 208mm;
    height: 295mm;
  }

}
</style>

<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;
}
}
?> 

