<?php
/*
 * cass_calendar_global - Shortcode for calendar global
 *
 * This shortcode is used to display the calendar global
 */

add_action('init', 'cass_register_shortcode_calendar_global');

function cass_register_shortcode_calendar_global()
{
    add_shortcode('cass_calendar_global', 'cass_shortcode_calendar_global');
}

function cass_shortcode_calendar_global($atts){
   //Default
   $initialView = 'dayGridMonth';
   $viewChangeVisible = true;
   $IDGroup = 0;
   $viewgroupvisible = true;
   $viewgroupvisibleHiddenTag='';

   foreach ($atts as $key => $value) {
      switch ($key) {
         case 'initialview':
            $initialView = $value;
            break;
         case 'viewchangevisible':
            $viewChangeVisible = $value;
            break;

         case 'idgroup':
            $IDGroup = $value;
            break;
         case 'viewgroupvisible':
            if($value == 'false') {
               $viewgroupvisible = false;
               $viewgroupvisibleHiddenTag = 'hidden';
            }
            break;
      }
   }

        
        $HTML = 
        
        "
        <script>
            window.adminPostUrl =" . json_encode(esc_attr(admin_url('admin-post.php'))) . ";
            window.siteUrl =" . json_encode(get_site_url()) . ";
            window.userID = " . json_encode(get_current_user_id())."
            window.initialView = '".$initialView."';
            window.viewChangeVisible = '".$viewChangeVisible."';
            window.IDGroup = '".$IDGroup."';
            window.viewgroupvisible='".$viewgroupvisible."';    
       </script>

     
       <!-- link rel='stylesheet' href='https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css' -->
       <script src='https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.slim.min.js'></script>
       <script src='https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js'></script>
       <script src='https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js'></script>
          
        <!-- A VOIR - IL faut mettre methode pour que ce soit cass member ou cass rl (mettre variable booléene dans le vue js?) -->
        <div id='cass_shortcode_calendar'>
          <div class='pure-control-group'>

          <div id='groupFilter' ".$viewgroupvisibleHiddenTag." hidden>
            <label for='cassGroup' class='block mb-2 text-sm font-medium text-gray-900 dark:text-white'>Filtrer par groupe</label>
            <select id='cassGroup' v-model='selectedValue' required @change='groupFilter()' class='bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500'>
               <option value=0>Tous les groupes</option>
               <option v-for='group in groups' :key='group.IDGroup' :value='group.IDGroup'>{{ group.name }}</option>
            </select>
         </div>
          </br>       
   
          <div id='progress-bar-container' class='progress-bar-container'>
          <div id='progress-bar' class='progress-bar'>
              <div class='progress-bar-value'></div>
          </div>
        </div>

          
         </div>
         <div id='calendar'></div>      
       
       <!--Details course -->
       <div class='cass-box-center'>
       <div id='raceInfo' hidden>
      
       <br>
       <br>
       <h3 align='center' id='raceName'>Informations de la course</h3>       
       <br>   
       <br>  
       <h5>Informations de la course</h5>
             <p id='raceStartEnd'></p>    
             <p id='racePlaceLocality'></p>    
             <p id='raceMeeting'></p>                
             <p id='raceState'></p> 
             <p id='raceLeader'></p> 
             <p id='raceTypeLevel'></p>
             <p id='raceGroup'></p>         
             <p id ='raceSubscriptionStartEnd'></p>
             <p id='raceMaterial'>                 
            
            <h5>Description de la course</h5>
            <p id='raceDescription'></p>


            <h5>Description (membres inscrits)</h5>
            <p id='raceDescriptionMemberSubscribed'></p>

            <h5>Inscriptions</h5>     
            <p id ='raceSubscriptionStartEndMeetingPlaceParticipiant'></p>
            
             <div id='inscription' hidden>
             <h5>Inscription à la course</h5>
             <p id='raceParticipantsMinMax'></p>
             <div id='notLoggedIn' hidden>
                <p>Connectez-vous afin de pouvoir vous inscrire à cette course.</p>
             </div>
             <div id='alreadySignedUp' hidden>
                <p>Vous êtes déjà inscrit. Vous pouvez modifier votre inscription dans les informations de la course.</p>
             </div>     
             <div id='unsubscribe' hidden> 
             <form action='".esc_attr( admin_url( 'admin-post.php' ) )."' method='post' class='pure-form pure-form-stacked'>
                                <input type='hidden' name='action' value='course_info_unscription' />
                                <input type='hidden' name='IDRace' value=0 />
                                <textarea id='commentUnscription' name='commentUnscription' rows='4' cols='50' placeholder='Commentaire désinscription'></textarea>
                                <button type='submit'class='pure-button pure-button-primary' >Se désinscrire</button>
                            </form>
             </div>                  
             <div id='subscriptionClosed' hidden>
                <p>Les inscriptions pour cette course sont fermées.</p>
             </div>          
             <div id='subscription' hidden>          
             <form action='".esc_attr( admin_url( 'admin-post.php' ) )."' method='post' class='pure-form pure-form-stacked'>
                                 <input type='hidden' name='user_id' value=".get_current_user_id()." />
                                <input type='hidden' name='IDRace' id='IDRace' value=IDRace />
                                <input type='hidden' name='state' value='1' />
                                <input type='hidden' name='action' value='course_info_inscription' />                                           
                                <input type='hidden' id='pageUrl' name='pageUrl' value='".get_permalink()."/?id=".intval($_GET['id'])."' />
                                <textarea id='commentInscription' name='commentInscription' rows='4' cols='50' placeholder='Commentaire inscription'></textarea>
                                <button type='submit' class='pure-button pure-button-primary' >S'inscrire</button>
                            </form>
             </div>
             </div>
             
              <br/>";

              if ( current_user_can( 'CASS_ADMIN' ) || current_user_can( 'CASS_RL' ) ) {
                  $HTML .= "
                  <h5>Administration</h5>
                  <a id='linkManageUser' href='' target='_blank'>
                  Liste des participants de la course";
              }
               $HTML .= "
      
         </a>

             <br/>
             <br/>

             <button class='pure-button pure-button-primary' @click='loadInfoCourse()'>Information et gestion de la course</button>
             
            
             </div> 
         
        </div>
        </div> 
        <br>
        <br>
       ";

      return $HTML;
    }

