<!--============ Sidebar ==============-->
<div class="col-md-4" <?php if(get_theme_mod('charsoogh_ad_sticky_sidebar' , 'yes') == 'yes'): echo 'id="sidebar"'; endif; ?>>
    <aside class="sidebar">
      <?php
      $author_id = get_queried_object()->post_author;
      $author_hash = base64_encode($author_id);
      $user_info = get_userdata($author_id);
      if (!get_theme_mod( 'charsoogh_profile_page' ) && empty(get_theme_mod( 'charsoogh_profile_page' ))) :
      $page_for_posts = get_site_option('profile_page_id');
      else:
      $page_for_posts = get_theme_mod( 'charsoogh_profile_page' );
      endif;
      $user_profile = esc_url( add_query_arg( array('user' => $author_hash), get_page_link( $page_for_posts ) ) );
      $show_profile_plan = true;
      if(get_theme_mod('charsoogh_profile_active' , 'yes') == 'yes'){
        if(get_option('membership_plan_activation') == 'on'){
          if(get_option('general_users_have_profile') != 'on'){
            $userId = $author_id;
            $now_miliseconds = round(microtime(true) * 1000);
            $finish_miliseconds = get_user_meta($userId,'plan_finish',true );
            if($finish_miliseconds == "" || $finish_miliseconds == "false"){
               $finish_miliseconds = 0 ;
            }else{
               $finish_miliseconds = (float) $finish_miliseconds ;
            }
                   if($now_miliseconds > $finish_miliseconds){
                      /////////////////////////////////////// Show Error -> time
                      $show_profile_plan = false;
                   }else{
                     $user_plan = get_user_meta( $userId , 'user_plan' , true );
                     if($user_plan['plan_profile_status'] != 'yes'){
                       /////////////////////////////////////// SHow Error -> Package Limitiation
                        $show_profile_plan = false;
                     }
                   }
          }
        }
      }
       ?>
        <section>
            <div class="box">
              <?php if(get_theme_mod('charsoogh_profile_show' , 'yes') == 'yes' && get_theme_mod('charsoogh_profile_active' , 'yes') == 'yes' && $show_profile_plan != false){ ?>
              <a href="<?php echo $user_profile; ?>" target="_blank">
                <div class="profile-box" <?php if(get_theme_mod('charsoogh_profile_avatar' , 'yes') != 'yes') {echo 'style="padding: 15px 20px 20px 0"';} ?>>
                  <?php
                  if(!is_numeric($user_info->display_name)){
                  $display_name = $user_info->display_name;
                  }else{
                  $display_name = substr($user_info->display_name, 0, 4) . '****' . substr($user_info->display_name,  8);
                  }
                  if(get_theme_mod('charsoogh_profile_avatar' , 'yes') == 'yes'){
                  $attachment_id = get_user_meta( $author_id, 'user_avatar', true );
                  if ( !empty($attachment_id) ) {
                      $original_image_url = wp_get_attachment_url( $attachment_id );
                      echo wp_get_attachment_image( $attachment_id, 'thumbnail');
                  }else{
                    echo '<img src="'. get_template_directory_uri() . '/assets/img/avatar.png" alt="'. $display_name .'" />';
                  }
                }
                  echo $display_name;
                  echo '<span class="advertiser">'. __('Advertiser' , 'charsoogh') .'</span>';
                   ?>
                </div>
              </a>
            <?php } ?>
              <div class="row contact-box">
                <button type="button" class="btn btn-primary info-btn col-md-6 col-sm-12 pull-right contact_info"></i><?php _e('Contact' ,'charsoogh'); ?></button>
                <button data-toggle="tooltip" data-placement="top" title="<?php _e('Add to Featured' , 'charsoogh'); ?>" type="button" class="btn info-btn btn-primary btn-icon btn-framed col-md-6 col-sm-12 pull-right favorite_<?php echo $post->ID; ?>"><i class="fa fa-bookmark-o" aria-hidden="true"></i></button>
                <script> user_favorite(<?php echo $post->ID; ?>);</script>
                <?php
                $custom_phone = get_post_meta($post->ID , '_custom_phone' , true);
                $custom_email = get_post_meta($post->ID , '_custom_email' , true);
                $user = get_user_by( 'id', $author_id );
                $username =$user->user_login;
                $show_contact_info = true;
                $show_contact_info_msg = '';
                if(is_numeric($username)){
                 $authorNumber = $username;
                 }else {
                 $authorNumber = get_user_meta($author_id, "billing_phone", true);
                 }
                 if (!get_theme_mod( 'charsoogh_plans_page' ) && empty(get_theme_mod( 'charsoogh_plans_page' ))) :
                 $plan_page = get_site_option('plans_page_id');
                 else:
                 $plan_page = get_theme_mod( 'charsoogh_plans_page' );
                 endif;
                 if(get_option('membership_plan_activation') == 'on'){
                     if(get_option('general_users_see_contact_data') != 'on'){
                     $userId = get_current_user_id();
                     $user_plan = get_user_meta( $userId , 'user_plan' , true );
                     $now_miliseconds = round(microtime(true) * 1000);
                     $finish_miliseconds = get_user_meta($userId,'plan_finish',true );
                     if($finish_miliseconds == "" || $finish_miliseconds == "false"){
                        $finish_miliseconds = 0 ;
                     }else{
                        $finish_miliseconds = (float) $finish_miliseconds;
                     }
                            if($now_miliseconds > $finish_miliseconds){
                               /////////////////////////////////////// Show Error -> time
                               $show_contact_info = false;
                               $show_contact_info_msg =__('The membership plan or deadline for using your plan has Expired.' , 'charsoogh');
                            }else{

                              if($user_plan['plan_profile_status'] != 'yes'){
                                /////////////////////////////////////// SHow Error -> Package Limitiation
                                 $show_contact_info = false;
                                 $show_contact_info_msg =__('Visit Contact information is Restricted to your membership Plan.' , 'charsoogh');
                              }
                              if($user_plan['visit_contact_status'] != 'yes'){
                                /////////////////////////////////////// SHow Error -> Package Limitiation
                                 $show_contact_info = false;
                                 $show_contact_info_msg =__('You do not have access to view contact information.' , 'charsoogh');
                              }
                            }
                   }
               }
                 ?>
                <script>
                  jQuery('.favorited-ad').html('<i class="fa fa-bookmark" aria-hidden="true"></i>');
                  jQuery(".contact_info").click(function(){
                  <?php if($show_contact_info != false){ ?>
                    jQuery.confirm({
                      icon: 'fa fa-info-circle',
                        title: '<?php _e('Contact Information' ,'charsoogh'); ?>',
                        bgOpacity: 0.5,
                        backgroundDismiss: true,
                        <?php
                        if (get_theme_mod( 'charsoogh_contact_show' )) : $contact_show = get_theme_mod( 'charsoogh_contact_show'); else: $contact_show = 'all'; endif;
                        if ($contact_show != 'all' && $contact_show != '' && $contact_show != false && !is_user_logged_in()) : ?>
                        content: '' +
                        '<div class="user_contact_info">' +
                        '<p><?php _e('Ad contact information is visible to members only.' ,'charsoogh'); ?></p>' +
                        '</div>',
                        <?php else: ?>
                        content: '' +
                        '<div class="user_contact_info">' +
                        '<dt><i class="fa fa-phone" aria-hidden="true"></i> <?php _e('Phone Number:' ,'charsoogh'); ?></dt><dd id="ch-phone"><?php if($custom_phone && !empty($custom_phone)): echo '<a href="tel:'. $custom_phone .'">' . $custom_phone . '</a>'; else: if(!empty($authorNumber)){echo '<a href="tel:'. $authorNumber .'">' . $authorNumber . '</a>';}else{echo __('No contact information' , 'charsoogh');} endif; ?></dd>' +
                        <?php if(!empty(get_the_author_meta('user_email')) || !empty($custom_email)): ?>
                        '<dt><i class="fa fa-envelope-open" aria-hidden="true"></i> <?php _e('Email:' ,'charsoogh'); ?></dt><dd id="ch-category"><?php if($custom_email && !empty($custom_email)): echo '<a href="mailto:'. $custom_email .'">' . $custom_email . '</a>'; else: if(get_post_meta($post->ID, '_email_show', true) != 'on'){echo '<a href="mailto:'. get_the_author_meta('user_email') .'">' . get_the_author_meta('user_email') . '</a>';}else{echo '-';} endif; ?></dd>' +
                        <?php endif; ?>
                        '</div>',
                        <?php endif; ?>
                        buttons: {
                                cancel: {
                                    btnClass: 'btn-info',
                                    text: '<?php _e('Ok' ,'charsoogh'); ?>',
                                    action: function(){
                                        backgroundDismiss: true
                                    }
                                }
                            }
                    });
                    <?php }else{ ?>
                      jQuery.confirm({
                        icon: 'fa fa-info-circle',
                        title: '<?php _e('Contact Information' , 'charsoogh'); ?>',
                        content: '<?php echo $show_contact_info_msg; ?>',
                        bgOpacity: 0.5,
                        backgroundDismiss: true,
                        buttons: {
                            planOK: {
                                text: '<?php _e('Buy Memebership Plan' , 'charsoogh'); ?>', // text for button
                                btnClass: 'btn-blue', // class for the button
                                keys: ['enter', 'a'], // keyboard event for button
                                isHidden: false, // initially not hidden
                                isDisabled: false, // initially not disabled
                                action: function(planOKButton){
                                    window.location.href = '<?php echo esc_url( get_page_link( $plan_page ) ); ?>';
                                }
                            },
                            planCancel: {
                                text: '<?php _e('Cancel' , 'charsoogh'); ?>', // text for button
                                btnClass: 'btn-red', // class for the button
                                keys: ['esc', 'c'], // keyboard event for button
                                isHidden: false, // initially not hidden
                                isDisabled: false, // initially not disabled
                                action: function(planCancelButton){

                                }
                            },
                        }
                    });
                    <?php }  ?>

                    });
                </script>

              </div>

                <dl>
                    <?php if (get_theme_mod( 'charsoogh_visit_single_ad_show') != 'no') : ?>
                    <dt><?php _e('Visit' ,'charsoogh'); ?></dt>
                    <dd id="ad-visit"><?php echo getPostViews($post->ID); ?></dd>
                    <?php endif; ?>
                    <dt><?php _e('Category' ,'charsoogh'); ?></dt>
                    <dd id="ad-category"><?php the_terms( $post->ID, 'product_cat', '', " ", '' );?></dd>
                    <?php if(get_the_terms( $post->ID, 'city_categories')): ?>
                    <dt><?php _e('City / District' ,'charsoogh'); ?></dt>
                    <dd id="ad-city"><?php //the_terms( $post->ID, 'city_categories', '', " , ", '' );
        						$terms = wp_get_post_terms( $post->ID, 'city_categories', array(
        							'orderby'=>'term_id' ,
        							'order' => 'ASC','ignore_term_order'=>true) );

        						$sep = ', ';
        						for($i= 0 ; $i<sizeof($terms); $i++){
        							$term = $terms[$i];
        							?>
        							<a href="<?php echo get_term_link($term->term_id);?>"><?php echo $term->name; ?></a>
        							<?php

        							if($i != sizeof($terms)-1){
        								echo $sep;
        							}

        						}
        						?></dd>
                  <?php endif; ?>
                    <dt><?php _e('Publish date' ,'charsoogh'); ?></dt>
                    <dd>
                      <?php if (empty(get_theme_mod( 'charsoogh_date_type_single')) || get_theme_mod( 'charsoogh_date_type_single') == 'advanced') : ?>
                        <?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . __(' Ago' , 'charsoogh'); ?>
                      <?php else: ?>
                      <?php echo get_the_date(); ?>
                      <?php endif; ?>
                      <?php
                      if(get_post_meta($id , 'is_laddered', true) == 'yes'):
                        echo '<span class="is_laddered">'. __('Is Ladder' , 'charsoogh') .'</span>';
                      endif;
                        ?>
                    </dd>
                    <?php
                    require_once(get_template_directory() . '/includes/models/charsoogh_ads_posts.php');
                    $pClass = new charsoogh_posts();
                    $all_fields = $pClass->getPostFields($post->ID);
                    if(sizeof($all_fields) > 0){
                     foreach ($all_fields as $row) {
                       if(!isset($row['only_admin']) || $row['only_admin'] != 'yes'){
                          $meta_value = get_post_meta($id, $row['meta_key'], true);
                          if($meta_value && !empty($meta_value)){
                          echo '<dt>'. $row['row_name'] . '</dt>';
                          if($row['is_only'] == 'yes' && !is_user_logged_in()){
                          $theme_color = get_theme_mod( 'charsoogh_general_color');
                            echo '<dd style="color:'. $theme_color .';">'. __('For members only' , 'charsoogh') .'</dd>';
                          }else{
                          if(is_array($meta_value)){
                            $counter = 0;
                              echo '<dd>';
                            foreach ($meta_value as $checkbox) {
                              if($counter > 0){echo ' , ';}
                              echo $checkbox;
                              $counter++;
                            }
                            echo '</dd>';
                          }else{
                          $bHasLink_http = strpos($meta_value, 'http') !== false;
                          $bHasLink_www = strpos($meta_value, 'www.') !== false;
                          if($bHasLink_http){
                              echo '<dd><a href="'. $meta_value . '" title="'. get_the_title() .'" target="_blank">' . $meta_value . '</a></dd>';
                          }else if($bHasLink_www){
                            echo '<dd><a href="http://'. $meta_value . '" title="'. get_the_title() .'" target="_blank">' . $meta_value . '</a></dd>';
                          }else{
                              echo '<dd>' . $meta_value . '</dd>';
                          }
                           }
                         }
                       } // Check Meta Value
                    } //Check is Admin
                  }





                     }
                     ?>
                    <?php
                    $catID = wp_get_post_terms($post->ID , 'product_cat');
                    $none_price = get_option('none_price_cat');
                    $exist_cat = false;
                    if(is_array($none_price)){
                    foreach($catID as $single_cat){
                    	if(in_array($single_cat->term_id , $none_price)){$exist_cat = true;}
                     }
                    }
                    if(!$exist_cat){
                    ?>
                    <dt><?php _e('Price' , 'charsoogh'); ?></dt>
                    <dd><?php woocommerce_template_loop_price(); ?></dd>
                  <?php } ?>
                </dl>
                <script>
                jQuery(document).ready(function($) {
                jQuery('#ad-category a').attr("title", "<?php _e('All ads in this category' ,'charsoogh'); ?>").tooltip("_fixTitle");
                jQuery('#ad-city a').attr("title", "<?php _e('All ads in this city' ,'charsoogh'); ?>").tooltip("_fixTitle");
              });
                </script>
                <div class="warning_ad">
                <?php if (get_theme_mod( 'charsoogh_ad_warning_text')) : ?>
                    <?php echo __(get_theme_mod( 'charsoogh_ad_warning_text') , 'charsoogh'); ?>
                  <?php endif; ?>
                </div>
                <section class="report_ad">
                  <a href="#" data-toggle="modal" data-target="#report_ads"><i class="fa fa-flag" aria-hidden="true"></i> <?php _e('Report an ad problem' ,'charsoogh'); ?></a>
                </section>
            </div>
        </section>
        <?php if(empty(get_theme_mod( 'charsoogh_content_single_ad_show' , 'under_images' )) || get_theme_mod( 'charsoogh_content_single_ad_show' , 'under_images' ) == 'sidebar'){ ?>
        <!--Description-->
        <section class="box">
            <h2 class="box-title"><?php _e('Ad Description' ,'charsoogh'); ?></h2>
            <div class="b-line"></div>
            <?php the_content(); ?>
        </section>
        <!--end Description-->
        <?php } ?>
        <?php if(!empty(get_option('map_activation')) && get_option('map_activation') == 'on'){ ?>
        <?php if(empty(get_theme_mod( 'charsoogh_content_single_map_show' )) || get_theme_mod( 'charsoogh_content_single_map_show' ) == 'sidebar'){ ?>
        <?php if(!empty(get_post_meta($post->ID , '_ad_latitude' , true)) && !empty(get_post_meta($post->ID , '_ad_longitude' , true))){ ?>
        <section class="box mapbox" style="padding: 5px;">
          <div class="ribbon-featured"><div class="ribbon-start"></div><div class="ribbon-content"><?php _e('Ad Location on Map' ,'charsoogh'); ?></div><div class="ribbon-end"><figure class="ribbon-shadow"></figure></div></div>
          <div id='map' style='width: 100%; height: 300px;'></div>
          <a href="http://maps.google.com/maps?q=loc:<?php echo get_post_meta($post->ID , '_ad_latitude' , true); ?>,<?php echo get_post_meta($post->ID , '_ad_longitude' , true); ?> (<?php the_title(); ?>)" target="_blank">
          <button type="button" class="btn btn-primary info-btn location-send"></i> <?php _e('Navigation' , 'charsoogh'); ?></button>
          </a>
          <?php
          $mapbox_style = get_theme_mod( 'charsoogh_map_style' , 'light-v10');
          $mapbox_marker = get_theme_mod( 'charsoogh_map_marker' , IMAGES ."/" . "icon/marker-icon.png");
          ?>
          <script>
          jQuery(document).ready(function() {
          var token = "<?php echo get_option('mapbox_token_key'); ?>";
          var map = L.map('map').setView([<?php echo get_post_meta($post->ID , '_ad_latitude' , true); ?>, <?php echo get_post_meta($post->ID , '_ad_longitude' , true); ?>], 16);
          L.marker(
                 [<?php echo get_post_meta($post->ID , '_ad_latitude' , true); ?>, <?php echo get_post_meta($post->ID , '_ad_longitude' , true); ?>],
               {
              icon: new L.DivIcon({
              className: 'l-marker-icon',
              html: '<img class="l-marker-image" src="<?php echo $mapbox_marker; ?>" width="25px" height="41px"/>'+
           '<span class="l-marker-span"><?php echo wp_trim_words( get_the_title(), 3, '...' ); ?></span>',
              iconSize: [25, 41],
              iconAnchor: [25, 41],
              }),
               riseOnHover: true,
               draggable: false,
               autoPan: true,
              }
            ).addTo(map);
            L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token=' + token, {
                maxZoom: 18,
                minZoom: 5,
                id: 'mapbox/<?php echo $mapbox_style; ?>',
                tileSize: 512,
                zoomOffset: -1,
                accessToken: token
            }).addTo(map);


          });
          </script>
        </section>
        <?php
        } // Check Map Activation Lat & Long
       } // Check map Position
     } // Check Map Activation
       ?>
        <?php
           if ( is_active_sidebar( 'ads_sidebar_widgets' ) ) :
               dynamic_sidebar( 'ads_sidebar_widgets' );
             endif;
          ?>

          <?php if (get_theme_mod( 'charsoogh_ad_share_link') != 'no') : ?>
          <section class="share-link-box">
            <div class="short-link">
            <span class="post-link__button" onclick="copyLinkFunction()" data-toggle="tooltip" data-placement="<?php if(is_rtl()): echo 'right'; else: echo 'left'; endif; ?>" title="<?php _e('Copy Link' , 'charsoogh'); ?>"><i class="fa fa-files-o" aria-hidden="true"></i> <?php _e('Share link' , 'charsoogh'); ?></span>
            <input type="text" id="shortlink" value="<?php echo wp_get_shortlink(); ?>" />
            </div>
            <script>
            function copyLinkFunction() {
              var copyText = document.getElementById("shortlink");
              copyText.select();
              document.execCommand("copy");
              jQuery('.post-link__button').attr("title", "<?php _e('Copied' , 'charsoogh'); ?>").tooltip("_fixTitle").tooltip("show").attr("title", "<?php _e('Copy Link' , 'charsoogh'); ?>").tooltip("_fixTitle");
              }
            </script>
          </section>
          <?php endif; ?>

          <?php
          if (get_theme_mod( 'charsoogh_ad_share_icons') != 'no') :
          // Get current page URL
          $crunchifyURL = urlencode(get_permalink());
          // Get current page title
          $crunchifyTitle = str_replace( ' ', '%20', get_the_title());
          // Get Post Thumbnail for pinterest
          $crunchifyThumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
          // Construct sharing URL without using any script
          $twitterURL = 'https://twitter.com/intent/tweet?text='.$crunchifyTitle.'&amp;url='.$crunchifyURL.'&amp;via=Crunchify';
          $facebookURL = 'https://www.facebook.com/sharer/sharer.php?u='.$crunchifyURL;
          $whatsappURL = 'whatsapp://send?text='.$crunchifyTitle . ' ' . $crunchifyURL;
          ?>
          <div class="crunchify-social">
          <span>
            <i class="fa fa-share-alt" aria-hidden="true"></i> <?php _e('Share' , 'charsoogh'); ?>
          </span>
          <a class="crunchify-link crunchify-telegram" target="_blank" onclick="teleShare();"><i class="fa fa-telegram"></i></a>
          <a class="crunchify-link crunchify-facebook" href="<?php echo $facebookURL; ?>" target="_blank"><i class="fa fa-facebook-f"></i></a>
          <a class="crunchify-link crunchify-whatsapp" href="<?php echo $whatsappURL; ?>" target="_blank"><i class="fa fa-whatsapp"></i></a>
          <a class="crunchify-link crunchify-twitter" href="<?php echo $twitterURL; ?>" target="_blank"><i class="fa fa-twitter"></i></a>
          <script>
          function teleShare() {
          var r = confirm("<?php _e('Would you like to share in the telegram?' ,'charsoogh'); ?>");
          if (r == true) {
          window.location.replace('https://telegram.me/share/url?text=&url='+window.location.href);
          }
          }
          </script>
          </div>
          <?php endif; ?>

    </aside>
</div>
<!--============ End Sidebar =================-->
