<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBHG__8J44NVgGqyh8svfwOmqqh9msNyaA&libraries=places"></script>
<?php
$duration= $tourData['duration']; 
$totalDuration = 0; // Initialize to prevent "Undefined variable" warning
$getmaps=$conn->query( "SELECT tour_itinerary.tid,tour_itinerary.oid, tour_itinerary.destination,tour_itinerary.pickupdescription,
tour_itinerary.pickupdescription,tour_itinerary.pcfromtime,tour_itinerary.pcendtime,tour_itinerary.itineraryData,tour_itinerary.dropfromtime,
tour_itinerary.dropdescription,tour_itinerary.dropendtime,tour_itinerary_maps.mapdata FROM tour_itinerary,tour_itinerary_maps where  tour_itinerary_maps.tid=tour_itinerary.tid and tour_itinerary.tid='".$_REQUEST['tourid']."'");

$getquery=$conn->query( "SELECT * from tour_itinerary_maps where tid='".$_REQUEST['tourid']."'");
$singlemaps=$getquery -> fetch_assoc();
$checkmapdata = ( ! empty( $singlemaps ) && isset( $singlemaps['mapdata'] ) ) ? json_decode( $singlemaps['mapdata'] ) : array();

$i=1;
$row_cnt = $getmaps->num_rows;
$fetchmaps=$getmaps -> fetch_assoc();
$decodempdata = ( ! empty( $fetchmaps ) && isset( $fetchmaps['mapdata'] ) ) ? json_decode( $fetchmaps['mapdata'] ) : array();
$tid=$_REQUEST['tourid'];
$itinerary_data_string = isset($fetchmaps['itineraryData']) ? $fetchmaps['itineraryData'] : '';
$decoded_itinerary = json_decode($itinerary_data_string, true);
$checkduration = is_array($decoded_itinerary) ? count($decoded_itinerary) : 0;
if($duration>$checkduration)
{
 
    $totalDuration=$duration-$checkdeuration;
}
elseif($duration==$checkduration)
{
}
else
{   
    //  $sql2 =  "UPDATE tour_itinerary  SET itineraryData=''  WHERE  tid='$tid'";
    //  $conn->query($sql2);
}
?>
<div class="container">
  <div class="row" >
    <div class="col-sm-12 formbg">
        <div class="tresponse"></div>
      <form  id="touritinerary">
          <h4 style="display:inline-block">Tour Start Country:
Select the country where this tour begins  <span class="mandtory" style="top: 23px; !important">Required</span></h4>
           <div class="form-group desination">
                <span class="sperror" style="display:none">Please Select at least one</span>
                <?php
                echo categories_meta_box($post, array( 'args' => array( 'taxonomy' => 'travel_locations' )),  (isset($fetchmaps['destination']) ? $fetchmaps['destination'] : ''), $tid);
                ?>
           </div>
        <div class="form-group">
            <div class="row ">
                  <div class="col-sm-3 trmap">
                      <?php if($trtype==1 || $_REQUEST['thistrtype']==1):?>
                           <label for="name">Tour Map <span class="mandtory">Required</span>
                              </label>  
                      <?php else:?>
                         <label for="name">Add Tour Location <span class="mandtory">Required</span>
                           </label>
                      <?php endif;?>            
                                
                                    <div class="thisval placed">
                                      <input id="searchbox" type="text" class="form-control" placeholder="Add Destination">
                                    </div>
                                     <input type="hidden" id="mapaction" value="mapaction"> 
                                     <input type="hidden" id="getplacename" name="location">
                                     <input type="hidden" id="lat" class="form-control"> 
                                     <input type="hidden" id="lng"  class="form-control">  
                                     <?php if($decodempdata || $checkmapdata){?>
                                            <input type="hidden" id="minlocation" value="1"> 
                                     <?php
                                     }
                                     else{?>
                                            <input type="hidden" id="minlocation" value="1"> 
                                    <?php
                                     }
                                     ?>
                           </label>
                </div>               
               <div class="col-sm-8">
                         <div class="e1dedb6f0 css-1k5xo9g e1uvdqdt0"></div>
                                 <div class="css-14bghfh e1u829440">
                                 <div class="css-1rskh6o e1u829441"></div>
                                 <div class="css-omv1s3 eubkn3g0">
                                       <?php if($trtype==1 || $_REQUEST['thistrtype']==1):?>
                                       Add tour route points, We will create a map to visualise the route of your tour on your tour listing page on Adventure Republiq.
                                       <?php else:?>
                                       Add tour location and tour route points if any
                                       <?php endif;?>
                                      </div>
                          </div>
                    </div>
                </div>
             </div>
           <div class="form-group">
               <div class="test"></div>
                    <div class="row mapresonse">
                       <div class="col-sm-4"> 
                        <ul class="loclist" id="post_list">
                          <?php
                          if($row_cnt==0)
                          {
                              
                              $i=1;
                              $j=0;
                            
                               foreach($checkmapdata as $showloc){ ?>
                                  <li data-post-id="<?php echo $j; ?>" data-tid="<?php echo $tid;?>" <?php if(count($decodempdata)==1):?> class="firstElement"  <?php endif;?>   data-userid="<?php echo $user_id;?>">
                                      <?php  echo stripslashes ($showloc->location);?> 
                                      <span class="del-loc-check" data-locid="<?php echo $j; ?>" data-tid="<?php echo $tid;?>"  data-userid="<?php echo $user_id;?>"> 
                                          <i class="fa fa-trash" aria-hidden="true"></i> 
                                        </span>
    
                                    </li>
                                 <?php
                                 $i++;
                                 
                                 $j++;
                              }
                          }
                          else
                          {
                          $i=1;
                          $j=0;
                    
                           foreach($decodempdata as $showloc){ ?>
                              <li data-post-id="<?php echo $j; ?>" <?php if(count($decodempdata)==1):?> class="firstElement"  <?php endif;?>  data-tid="<?php echo $tid;?>" data-userid="<?php echo $user_id;?>">
                                  <?php  echo stripslashes ($showloc->location);?> 
                                  <span class="del-loc-check" data-locid="<?php echo $j; ?>" data-tid="<?php echo $tid;?>"  data-userid="<?php echo $user_id;?>"> 
                                      <i class="fa fa-trash" aria-hidden="true"></i> 
                                    </span>

                                </li>
                             <?php
                             $i++;
                             $j++;
                             } 
                          }
                           ?>
                        </ul> 
                     </div>
                       <div class="col-sm-8"><div class="reordermap"><?php showroutes($tid, $user_id);?></div></div>
                </div>
            </div>
       <h4>Pickup/Meeting Point</h4>
        <hr>
        <div class="form-group col-sm-8 np">
               <label for="name">Description <span class="mandtory">Required</span> </label>
               <input type="text" name="pickupdescription" required class="form-control required" placeholder="e.g Airport, Hotel, Street Address, etc.." value="<?php echo  (isset($fetchmaps['pickupdescription']) ? $fetchmaps['pickupdescription'] : '');?>">
        </div> 
        <div class="form-group">
               <label for="name">Add Time :</label>
              <div class="row">
                <div class="col-sm-2">  
                     <label for="name">From :</label>
                    <input type="time" name="pcfromtime" class="form-control" placeholder="e.g From Time" value="<?php echo  (isset($fetchmaps['pcfromtime']) ? $fetchmaps['pcfromtime'] : '');?>">
                </div>
                 <div class="col-sm-2">  
                      <label for="name">To :</label>
                    <input type="time" name="pcendtime" class="form-control" placeholder="e.g End Time" value="<?php echo  (isset($fetchmaps['pcendtime']) ? $fetchmaps['pcendtime'] : '');?>">
                </div>
              </div> 
        </div>  
         <h4>Drop-off/Ending Point</h4>
         <hr>
        <div class="form-group col-sm-8 np">
               <label for="name">Description <span class="mandtory">Required</span> </label>
               <input type="text" name="dropdescription" required class="form-control " placeholder="e.g Airport, Hotel, Street Address, etc.." value="<?php echo  (isset($fetchmaps['dropdescription']) ? $fetchmaps['dropdescription'] : '');?>">
        </div> 
        <div class="form-group">
               <label for="name">Add Time :</label>
              <div class="row">
                  <div class="col-sm-2">  
                     <label for="name">From :</label>
                      <input type="time" name="dropfromtime" class="form-control" placeholder="e.g From Time" value="<?php echo  (isset($fetchmaps['dropfromtime']) ? $fetchmaps['dropfromtime'] : '');?>">
                  </div>
                   <div class="col-sm-2">  
                     <label for="name">To :</label>
                      <input type="time" name="dropendtime" class="form-control" placeholder="e.g End Time" value="<?php echo (isset($fetchmaps['dropendtime']) ? $fetchmaps['dropendtime'] : '');?>">
                  </div>
              </div> 
          </div>
           <?php if($trtype==1 || $_REQUEST['thistrtype']==1){?> 
           <h4 style="margin-top: 52px; position:relative;" id="topthis">Detailed Itinerary 
                <span class="mandtory">Required</span> 
                      
                </span>
             </h4>
               <hr>
               <?php
              // echo $fetchmaps['itineraryData'];
               if(!empty($fetchmaps['itineraryData'])){
                    $decodeData=json_decode($fetchmaps['itineraryData']);
                    $j=1;
               
                   foreach($decodeData as $itineraryData){?>
                          <div class="form-group np ">
                              <div class="row">
                                   <div class="col-sm-8">
                                        <label for="name">Day  <?php echo $j;?></label>
                                         <div class="form-group errodesp">
                                          <input type="text" required name="gettitle[]" class="form-control required" placeholder="Day Plan" value="<?php echo  $itineraryData->gettitle;?>">
                                           </div> 
                                            <label for="name">Description :</label>
                                             <textarea id="decp<?php echo $j;?>"  style="resize: none;height: 250px;"required name="getdescription[]"  class="form-control required"><?php echo stripslashes($itineraryData->description);?></textarea>
                                     </div>
                                    <div class="col-sm-4">
                                                <?php if($j==1):?>
                                                 <div class="e1dedb6f0 css-1k5xo9g e1uvdqdt0"></div>
                                                         <div class="css-14bghfh e1u829440">
                                                         <div class="css-1rskh6o e1u829441"></div>
                                                         <div class="css-omv1s3 eubkn3g0">
                                                              Explain what travelers can expect each day on tour. 
                                                          </div>
                                                  </div>
                                                  <?php endif;?>
                                    </div>
                                     
                                  </div>      
                            </div>          
                         
                     <script>
                     
                         tinymce.init({
                            selector: "#decp<?php echo $j;?>",
                            menubar: false,
                            plugins: 'lists',
                            toolbar: "bold italic underline bullist",
                            resize: false,
                          });
                          
                         
                    
                   </script>
                   <?php 
                      $j++;
                      }
                }
                else
                 {
                     $j=1;
                 }
                  if($totalDuration){?>
                 
                       <?php for($k=$j; $k<=$duration; $k++){?>
                        <div class="form-group np ">
                              <div class="row">
                                   <div class="col-sm-8">
                                          <label for="name">  Day <?php echo $k;?></label>
                                                <div class="form-group">
                                                          <input type="text" required name="gettitle[]" class="form-control required" placeholder="Day Plan" value="<?php echo  stripslashes($itineraryData->gettitle);?>">
                                                
                                            </div>            
                                        <div class="form-group errodesp">
                                         <label for="name">Description </label>
                                              <textarea id="decp<?php echo $k;?>"  style="resize: none;height: 250px;"  name="getdescription[]"   class="form-control required "><?php echo  stripslashes($itineraryData->description);?></textarea>
                                      </div>  
                                  </div>
                                  <div class="col-sm-4">
                                                <?php if($k==1):?>
                                                 <div class="e1dedb6f0 css-1k5xo9g e1uvdqdt0"></div>
                                                         <div class="css-14bghfh e1u829440">
                                                         <div class="css-1rskh6o e1u829441"></div>
                                                         <div class="css-omv1s3 eubkn3g0">
                                                              Explain what travelers can expect each day on tour. 
                                                          </div>
                                                  </div>
                                                  <?php endif;?>
                                    </div>
                                  </div>
                        </div>          
                 <script>
                         tinymce.init({
                            selector: "#decp<?php echo $k;?>",
                            menubar: false,
                            plugins: 'lists',
                            toolbar: "bold italic underline bullist",
                            resize: false,
                          });
                   </script>
                  <?php } }
                   }
                   ?>
            <!--    //else { ?>-->
            <!--   <div class="loaddiv">-->
            <!--       <?php //for($i=1; $i<=$duration; $i++){?>-->
                  
            <!--          <div class="form-group col-sm-8 np">-->
            <!--                 <label for="name"> Daywwww <?php echo $i;?></label>-->
                         
            <!--             <div class="form-group">-->
            <!--                      <input type="text" required name="gettitle[]" class="form-control" placeholder="Day Plan" value="">-->
            <!--            </div>  -->
                       
            <!--            <div class="form-group">-->
            <!--                 <label for="name">Description :</label>-->
            <!--                 <textarea name="getdescription[]"  id="decp<?php echo $i;?>" class="form-control"></textarea>-->
            <!--            </div>  -->
            <!--          </div>-->
            <!--         <script>-->
            <!--             tinymce.init({-->
            <!--                selector: "#decp<?php echo $i;?>",-->
            <!--                menubar: false,-->
            <!--                plugins: 'lists',-->
            <!--                toolbar: "bold italic underline bullist",-->
            <!--                resize: false,-->
            <!--              });-->
            <!--       </script>-->
            <!--  <?php //}?>-->
            <!--  </div> -->
            <!--<?php // } ?>-->
     
           
            <input type="hidden" name="oid" id="oid" value="<?php echo get_current_user_id();?>">
            <input type="hidden" name="Action" value="AddItinerary">
            <input type="hidden" name="tid" id="tid" value="<?php echo $_REQUEST['tourid'];?>">
            <input type="hidden" name="thistrtype"  id="thistrtype"  value="<?php echo  stripslashes($_REQUEST['thistrtype']);?>">  
            <button type="submit" class="btn btn-primary itrcheck">Save</button>
            <!-- <button type="submit"  data-decpid="<?php //echo count($decodeData);?>" class="btn btn-primary itrcheck">Save</button> -->
 
            <input type="hidden"  id="savedata">
 </form>
</div>
</div>
</div>

<script>
    
     $(function() {
            $('.selectit input').attr('name', 'tax_input[]');
            
});

</script>





