<?php
/*
 * Template Name: wosmpl partner 
 * Template Post Type: wosmpl_partners
 */

 get_header();  
 global $post;
 $geo_cats = get_option('wosmpl_geo_cats', []);
 $metadata = get_post_meta( $post->ID);
 $catid = $metadata['wosmpl_geo_cat'][0];
 $presta_type = $metadata['wosmpl_presta_type'][0];
 $cat = NULL;
 $post_addr = $metadata['wosmpl_post_addr'][0];
 $latlon = $metadata['wosmpl_latlon'][0];
 $pict_url =  $metadata['wosmpl_partner_pict'][0];
 if (strlen($pict_url) > 0){
   $pict_url = str_replace('.','-350.',$pict_url);
 }
 $open_hours = $metadata['wosmpl_partner_open_hours'][0];
 $specialty = $metadata['wosmpl_partner_specialty'][0];
 $challenge = $metadata['wosmpl_partner_challenge'][0];
 $phone = NULL;
 if ($metadata['wosmpl_partner_phone_visible'][0] == 'yes') {
     $phone = $metadata['wosmpl_partner_phone'][0];
 }
 $email = NULL;
 if ($metadata['wosmpl_partner_email_visible'][0] == 'yes') {
     $email = $metadata['wosmpl_partner_email'][0];
 }
 $website = NULL;
 if ($metadata['wosmpl_partner_website_visible'][0] == 'yes') {
     $website = $metadata['wosmpl_partner_website'][0];
 }
foreach ($geo_cats as $gc) {
    if ($gc['id'] == $catid) {
        $cat = $gc['name'];
    }
}

 ?>

 
 <link rel="stylesheet" id="google-fonts-1-css" href="https://fonts.googleapis.com/css?family=Voltaire:100,100italic,200,200italic,300,300italic,400,400italic,500,500italic,600,600italic,700,700italic,800,800italic,900,900italic|Roboto:100,100italic,200,200italic,300,300italic,400,400italic,500,500italic,600,600italic,700,700italic,800,800italic,900,900italic" type="text/css" media="all">
 <style type="text/css">
  #primary h1 {padding-top:180px;
               background-color: #0aa245;
               border-bottom-left-radius: 450px 50px;
               border-bottom-right-radius: 450px 50px;
               text-align: center;
               color: #FFF;
              }
  .partner_details .coord {float:left;}
  .partner_details .picture {float:right;}
  .partner_details .desc {clear:both;}
  .hours div {float:left;}
  .desc {margin-top:15px;}
  @media only screen and (max-width: 375px) {
    #primary h1 {max-height: 70px;padding-top:0px;}
 }

</style>
 <div id="primary" class="content-area">
        <h1><?php echo $post->post_title; ?></h1>   
        <div class="container partner_details">
           <?php if ($cat) :?>
            <h3><?php echo $cat;?></h3>
           <?php endif; ?>
           <?php if ($presta_type): ?>
             <h4 class="presta-type">> <?php echo $presta_type;?></h4>
           <?php endif; ?>
           <div class="coord">
            <?php if ($post_addr):?>
             <p>
                Adresse : <?php echo $post_addr; ?>
             </p>
            <?php endif; ?>
            <?php if ($phone) : ?>
            <p>
                Téléphone : <?php echo $phone; ?>
            </p>
            <?php endif; ?> 
            <?php if ($email) : ?>
            <p>
                Email : <?php echo $email; ?>
            </p>
            <?php endif; ?>  
            <?php if ($website) : ?>
            <p>
                Site Web : <a href="<?php echo $website; ?>" target="_blank"><?php echo $website; ?></a>
            </p>
            <?php endif; ?>  
            <?php if ($open_hours) :?>
                <p class="hours">
                    Horaires d'ouverture : <br /><?php echo nl2br (esc_html($open_hours)); ?>
                </p>
            <?php endif; ?>    
           </div>
           <div class="picture">
             <?php if ($pict_url):?>
            <img width="350" src="<?php echo $pict_url;?>" alt="Photo" />
             <?php endif; ?>
           </div>
           <div class="desc">
            <?php if(strlen($post->post_content) >0) :?>
                <h3>Description</h3>
                <?php echo nl2br($post->post_content); ?>
            <?php endif; ?>
           </div> 
           <div>
            <?php if($specialty): ?>
                <h3>Spécialité</h3>
                <?php echo nl2br (esc_html($specialty)); ?>
            <?php endif; ?>
           </div>
           <div>
            <?php if($challenge): ?>
                <h3>Défi</h3>
                <?php echo nl2br (esc_html($challenge)); ?>
            <?php endif; ?>
           </div>

           </div>
           <div id="wosmpl_partner_detail_map" style="margin:15px auto;">
           </div>
           <?php if($latlon): ?>
           <script type="text/javascript">
            (function( $ ) {
              $( window ).load(function() {
                  var partner_latlng = [<?php echo $latlon;?>];
                  var map_div = jQuery('#wosmpl_partner_detail_map');
                  var width = map_div.prev('div').innerWidth();
                  if (!isNaN(width)) {
                    if (width > 375) {
                        width = parseInt(width / 1.5,10);
                    }
                    var height = parseInt(width * 9 /16,10);
                    map_div.css({'width':width + 'px','height': height + 'px'});

                    var map = L.map('wosmpl_partner_detail_map').setView(partner_latlng, 16);
                    var osmUrl='https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
                    var osmAttrib='Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors';
                    var osm = new L.TileLayer(osmUrl, {minZoom: 9, maxZoom: 19, attribution: osmAttrib});
                    map.addLayer(osm);
                    var southWest = L.latLng(43.2022, 2.5887),
                        northEast = L.latLng(43.9562, 4.2586);
                    var bounds = L.latLngBounds(southWest, northEast);
                    map.setMaxBounds(bounds);
                    L.marker(partner_latlng).addTo(map);
                  }
              });
            })( jQuery );
           </script> 
           <?php endif; ?>
        </div><!-- #content -->        
       
        <?php /* var_dump($metadata);*/ ?>
    </div><!-- #primary -->

<?php

get_footer();
