<?php
function my_theme_enqueue_styles() { 
    wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );

add_action('wp_enqueue_scripts', 'qg_enqueue');
function qg_enqueue() {
  wp_enqueue_script( 'main', get_stylesheet_directory_uri().'/assets/js/main.js',array('jquery'),false,true);
}

add_action('acf/init', 'my_acf_op_init');
function my_acf_op_init() {

    // Check function exists.
    if( function_exists('acf_add_options_page') ) {

        // Add parent.
        $parent = acf_add_options_page(array(
            'page_title'  => __('General Settings'),
            'menu_title'  => __('General Settings'),
            'redirect'    => false,
        ));

        // Add sub page.
        $child = acf_add_options_page(array(
            'page_title'  => __('Homepage Promo'),
            'menu_title'  => __('Homepage Promo'),
            'parent_slug' => $parent['menu_slug'],
        ));
        
        // Add sub page.
        $child = acf_add_options_page(array(
            'page_title'  => __('Office details'),
            'menu_title'  => __('Office details'),
            'parent_slug' => $parent['menu_slug'],
        ));

        // Add sub page.
        $child = acf_add_options_page(array(
            'page_title'  => __('Property Single details'),
            'menu_title'  => __('Property Single details'),
            'parent_slug' => $parent['menu_slug'],
        ));
    }
}
// Top Right Menu
register_nav_menus( 
    array(
        'top-right-menu'   =>  __( 'Top Right Menu', 'hawk' ), 
    )
);
add_action('wp_footer', 'top_menu_data'); 
function top_menu_data() { 
      echo '<div class="fixed-section-menu">';     
       wp_nav_menu( array( 'theme_location' => 'top-right-menu', 'container_class' => 'fixed-section-menu-content'));
       echo '</div>';
    }
// Function to add subscribe text to posts and pages
function HelloWorldShortcode() {
     $image = get_field('image','option');
    $content = get_field('hp_promo_content','option');
    $bg = get_field('hp_promo_colour','option');
    $buttonlabel = get_field('hp_promo_button_label','option');
     $buttonurl = get_field('hp_promo_button_url','option');
     $buttonclass = get_field('hp_promo_button_class','option');
     if(get_field('hp-show','option')){
        $mainshow = 'yes';
     }else{
         $mainshow = 'no';            
     }
   $contentlist = '<div class="promo-home-wrap '.$mainshow.'" style="min-height: 100px; background: '.$bg.' ; margin: 40px auto 40px;">';
    if($image){
         $contentlist  .= '<div class="promo-left"> 
            <img loading="lazy" src="'.$image.'" alt="" >
            </div>';
       }   
       $contentlist  .= '<div class="promo-right"> 
        <div style="margin: auto;">
        '. $content;
        if($buttonlabel){
            $contentlist  .= '<a href="'. $buttonurl.'" class="btn-promo '. $buttonclass.'">'. $buttonlabel.'</a>';
        }
        $contentlist  .= '</div>
    </div>';
    return $contentlist;
}
add_shortcode('home-promo', 'HelloWorldShortcode');
// create shortcode similar properties

add_shortcode( "PH_Similar_Properties", "PH_Similar_Properties_Function" );

function PH_Similar_Properties_Function( $atts ) {

        $atts = shortcode_atts( array(
            'per_page'                  => '3',
            'columns'                   => '20',
            'price_percentage_bounds'   => 50,
            'bedroom_bounds'            => 0,
            'property_id'               => '',
            'availability_id'   => '',
            'no_results_output' => '',
        ), $atts, 'similar_properties' );

        if ($atts['property_id'] != '')
        {
            $department = get_post_meta( $atts['property_id'], '_department', true );

            $price = get_post_meta( $atts['property_id'], '_price_actual', true );
            $lower_price = $price;
            $higher_price = $price;
            $atts['price_percentage_bounds'] = str_replace("%", "", $atts['price_percentage_bounds']);
            if ( isset($atts['price_percentage_bounds']) && $atts['price_percentage_bounds'] != '' && is_numeric($atts['price_percentage_bounds']) && $atts['price_percentage_bounds'] > 0 )
            {
                $lower_price = $price - ($price * $atts['price_percentage_bounds'] / 100);
                $higher_price = $price + ($price * $atts['price_percentage_bounds'] / 100);
            }

            $bedrooms = get_post_meta( $atts['property_id'], '_bedrooms', true );
            $lower_bedrooms = $bedrooms;
            $higher_bedrooms = $bedrooms;
            if ( isset($atts['bedroom_bounds']) && $atts['bedroom_bounds'] != '' && is_numeric($atts['bedroom_bounds']) && $atts['bedroom_bounds'] > 0 )
            {
                $lower_bedrooms = $bedrooms - $atts['bedroom_bounds'];
                $higher_bedrooms = $bedrooms + $atts['bedroom_bounds'];
            }

            $args = array(
                'post_type'             => 'property',
                'post__not_in'          => array($atts['property_id']),
                'post_status'           => ( ( is_user_logged_in() && current_user_can( 'manage_propertyhive' ) ) ? array('publish', 'private') : 'publish' ),
                'ignore_sticky_posts'   => 1,
                'posts_per_page'        => $atts['per_page'],

            );

            $meta_query = array();

            $meta_query[] = array(
                'key'       => '_department',
                'value'     => $department,
            );

            $meta_query[] = array(
                'key'       => '_on_market',
                'value'     => 'yes',
            );

            if ( isset($atts['bedroom_bounds']) && is_numeric($atts['bedroom_bounds']) )
            {
                $meta_query[] = array(
                    'key'       => '_bedrooms',
                    'value'     => $lower_bedrooms,
                    'compare'   => '>=',
                    'type'      => 'NUMERIC'
                );

                $meta_query[] = array(
                    'key'       => '_bedrooms',
                    'value'     => $higher_bedrooms,
                    'compare'   => '<=',
                    'type'      => 'NUMERIC'
                );
            }

            if ( isset($atts['price_percentage_bounds']) && is_numeric($atts['price_percentage_bounds']) )
            {
                $meta_query[] = array(
                    'key'       => '_price_actual',
                    'value'     => $lower_price,
                    'compare'   => '>=',
                    'type'      => 'NUMERIC'
                );

                $meta_query[] = array(
                    'key'       => '_price_actual',
                    'value'     => $higher_price,
                    'compare'   => '<=',
                    'type'      => 'NUMERIC'
                );
            }

            $args['meta_query'] = $meta_query;

            $tax_query = array();


            if ( isset($atts['orderby']) && $atts['orderby'] == 'date' )
            {
                $args['orderby'] = 'meta_value';
                $args['meta_key'] = '_on_market_change_date';
            }

            ob_start();

            $properties = new WP_Query( apply_filters( 'propertyhive_shortcode_similar_properties_query', $args, $atts ) );

            $propertyhive_loop['columns'] = $atts['columns'];

            if ( $properties->have_posts() ) : ?>
                <div class="similar_properties_wrapper">
                <div class="similar-prop-slider">
                <!-- <?php propertyhive_property_loop_start(); ?> -->

                    <?php while ( $properties->have_posts() ) : $properties->the_post(); ?>
                        <?php  global $property, $post; 
                            $gallery_images =$property->get_gallery_attachment_ids();
                            $bedroom = get_post_meta( $post->ID, $key = '_bedrooms', $single = true );
                            $restroom = get_post_meta( $post->ID, $key = '_reception_rooms', $single = true );
                            $bethroom = get_post_meta( $post->ID, $key = '_bathrooms', $single = true );
                            $availability = get_the_terms( $post->ID, 'availability');
                            $property_type = get_the_terms( $post->ID, 'property_type');
                            $address_street = get_post_meta( $post->ID, '_address_street', $single = true);
                            $town_name = get_post_meta( $post->ID, '_address_three', $single = true);
                            $othr_img1 = wp_get_attachment_image_src( $gallery_images['0'], "full" );
                        ?>
                        <div class="prop-slide">
                            <div class="prop-img thumbnail">
                            <a href="<?php the_permalink(); ?>">
                                    <?php
                                    echo '<img src="'.$othr_img1['0'].'" alt="">';
                                ?>
                                <?php if($availability['0']->name == 'Sold STC'){ ?>
                                    <span><?php  echo $availability['0']->name; ?></span>
                                 <?php } ?>
                                <h3><?php echo $property->get_formatted_price(); ?></h3>
                                </a>
                            </div>
                            <div class="details">
                                <a href="<?php the_permalink(); ?>"> 
                                    <?php 
                                    if($town_name && $address_street){
                                        $commasec = ', ';
                                        } 
                                       ?>   
                                    <h2><?php echo $address_street.$commasec.$town_name ?></h2>
                                    <p><?php echo strtolower($bedroom.' bedroom '.$property_type['0']->name) ?></p> 
                                    <?php if($bedroom || $bethroom ){ ?>
                                    <div class="availability-section">
                                        <ul>
                                            <?php if($bedroom){ ?>
                                            <li>
                                                <img src="<?php echo get_stylesheet_directory_uri(); ?>/images/Shape.svg" alt=""> <?php echo $bedroom ?></li>
                                            <?php } 
                                            if($bethroom){
                                            ?>  
                                                <li>
                                                   <img src="<?php echo get_stylesheet_directory_uri(); ?>/images/bthrooms.svg" alt=""> <?php echo $bethroom; ?>
                                                </li>
                                            <?php }  if($restroom){ ?>  
                                                <li> <img src="<?php echo get_stylesheet_directory_uri(); ?>/images/sofa.svg" alt=""> <?php echo  $restroom; ?></li>
                                            <?php } ?>
                                        </ul>   
                                    </div>
                                    <?php } ?>
                                </a>   
                            </div>
                        </div>
                    <?php endwhile; // end of the loop. ?>

                <!-- <?php propertyhive_property_loop_end(); ?> -->
                    </div>
                </div>
            <?php else: ?>

                <?php echo $atts['no_results_output']; ?>

            <?php endif;

            wp_reset_postdata();
        }
        else
        {
            echo 'No property_id passed into similar_properties shortcode';
        }
$shortcode_output = ob_get_clean();

        
        return apply_filters( 'propertyhive_similar_properties_shortcode_output', '<div class="propertyhive propertyhive-similar-properties-shortcode columns-' . $atts['columns'] . '">' . $shortcode_output . '</div>', $shortcode_output );
    }
    
remove_action( 'propertyhive_single_property_summary', 'propertyhive_template_single_title', 5 );
remove_action( 'propertyhive_single_property_summary', 'propertyhive_template_single_floor_area', 7 );
remove_action( 'propertyhive_single_property_summary', 'propertyhive_template_single_price', 10 );
remove_action( 'propertyhive_single_property_summary', 'propertyhive_template_single_meta', 20 );
remove_action( 'propertyhive_single_property_summary', 'propertyhive_template_single_sharing', 30 );

remove_action( 'propertyhive_after_single_property_summary', 'propertyhive_template_single_actions', 10 );
remove_action( 'propertyhive_after_single_property_summary', 'propertyhive_template_single_features', 20 );
remove_action( 'propertyhive_after_single_property_summary', 'propertyhive_template_single_summary', 30 );
remove_action( 'propertyhive_after_single_property_summary', 'propertyhive_template_single_description', 40 );

add_action( 'propertyhive_single_property_summary', 'property_content_summary', $priority = 10);
function property_content_summary(){
    global $post, $propertyhive, $property;
    $bedroom = get_post_meta( $post->ID, $key = '_bedrooms', $single = true );
    $bethroom = get_post_meta( $post->ID, $key = '_bathrooms', $single = true );
    $availability = get_the_terms( $post->ID, 'availability');
     $restroom = get_post_meta( $post->ID, $key = '_reception_rooms', $single = true );
    $property_type = get_the_terms( $post->ID, 'property_type');
    $address_street = get_post_meta( $post->ID, '_address_street', $single = true);
    $town_name = get_post_meta( $post->ID, '_address_three', $single = true);
    $brochure = $property->get_brochure_attachment_ids();
     $epc = $property->get_epc_attachment_ids();
     $department = get_post_meta( $post->ID, '_department', $single = true);
     $office_id = $property->office_id;
     if($department == 'residential-sales'){
        $tel = get_post_meta($office_id, '_office_telephone_number_sales', TRUE);
     }
        if($department == 'residential-lettings'){
        $tel = get_post_meta($office_id, '_office_telephone_number_lettings', TRUE);
     }
    if($department == 'commercial'){
        $tel = get_post_meta($office_id, '_office_telephone_number_commercial', TRUE);
     }
     if($department == 'rooms'){
        $tel = get_post_meta($office_id, '_office_telephone_number_rooms', TRUE);
     }
    ?>

    <!-- AddToAny END -->
    <!-- <div class="flex_row"> -->
        <div class="top-section">
            <div class="flex_row " style="gap: 30px; position: relative;z-index: 1;">
                <div class="flex-four-col">
                    <h1 class="prop_address"><?php echo $address_street; ?></h1>
                    <h3><?php echo $town_name; ?></h3>
                    <div class="price">
                        <h2 class="property_title entry-title">
                            <?php echo $property->get_formatted_price(); 
                            ?>
                            <?php
                                if (
                                    ( $property->department == 'residential-sales' || $property->department == 'commercial' ) &&
                                    $property->price_qualifier != ''
                                )
                                {
                                    echo ' <span class="price-qualifier">' . $property->price_qualifier . '</span>';
                                }

                                if ( $fees != '' )
                                {
                                    echo ' <span class="lettings-fees"><a data-fancybox data-src="#propertyhive_lettings_fees_popup" href="javascript:;">' . __( 'Tenancy Info', 'propertyhive' ) . '</a></span>';

                                    echo '<div id="propertyhive_lettings_fees_popup" style="display:none; max-width:500px;"><h3>' . __( 'Tenancy Info', 'propertyhive' ) . '</h3>' . $fees . '</div>';
                                }
                            ?>
                        </h2>
                    </div> 
                    <div class="bed-bath-count">
                       <ul>
                            <?php if($bedroom){ ?>
                            <li>
                                <img src="<?php echo get_stylesheet_directory_uri(); ?>/images/Shape.svg" alt=""> <?php echo $bedroom ?></li>
                            <?php } 
                            if($bethroom){
                            ?>  
                                <li>
                                   <img src="<?php echo get_stylesheet_directory_uri(); ?>/images/bthrooms.svg" alt=""> <?php echo $bethroom; ?>
                                </li>
                            <?php }  if($restroom){ ?>  
                                <li> <img src="<?php echo get_stylesheet_directory_uri(); ?>/images/sofa.svg" alt=""> <?php echo  $restroom; ?></li>
                            <?php } ?>
                        </ul>           
                     </div>       
                </div>
                <div class="flex-double-col">
                    <div class="call-section">
                        <?php if($tel){ ?>
                            <div class="get-in-touch">
                                <a href="tel:<?php echo $tel; ?>">    
                                    <span>Interested? Get in touch!</span>
                                    <div class="tel-call">
                                        <?php echo $tel; ?>
                                    </div>
                                </a>    
                            </div>
                        <?php } ?>
                        <div class="shortlist common-lft">
                            <?php echo do_shortcode('[shortlist_button]') ?>
                        </div>
                        <div class="email-to-friend common-lft">
                            <a data-fancybox="" data-src="#sendToFriend18" href="javascript:;">EMAIL TO A FRIEND</a>
                            <div id="sendToFriend18" style="display: none;" class="fancybox-content">
                                <?php echo do_shortcode('[send_to_friend_form]');?>
                            </div>    
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="flex_row property-details" style="gap: 30px; position: relative;">
            <div class="flex-four-col">
                <div class="property_info">
                    <h4 class="prop-heading prop-desc-heading">Property Details</h4>
                    <p><?php echo $property->post_excerpt; ?></p>
                    <?php 
                    $features = $property->get_features();
                    if ( !empty($features) ){ ?>
                        <div class="features">
                            <h4><?php _e( 'Features', 'propertyhive' ); ?></h4>
                            <ul>
                                <?php foreach ($features as $feature) { ?>
                                    <li><?php echo $feature; ?></li>
                                            <?php
                                    }
                                ?>
                            </ul>
                        </div>
                    <?php
                    }
                     ?>
                     <h4 class="prop-heading prop-desc-heading">Location</h4>
                     <div class="map-top"><?php echo do_shortcode('[property_map height="250"  zoom="18"  id="'.$post->ID.'"]');?></div>
                     <div class="button-epc">
                        <?php  $epc_url = wp_get_attachment_url($epc[0]); ?>
                        <?php if($epc_url){ ?>
                             <div class="epc-button btnsec"><a href="<?php echo $epc_url; ?>" data-fancybox="images" >VIEW EPC</a></div>
                         <?php } ?>
                         <?php  $brochure_url = wp_get_attachment_url($brochure[0]); ?>
                         <?php if($brochure_url){ ?>
                             <div class="pdf-button btnsec"><a href="<?php echo $brochure_url; ?>" download>DOWNLOAD BROCHURE</a></div>
                        <?php } ?>
                     </div>
                </div>
            </div>
            <?php if( get_field('content_arrange','option') || get_field('form_arrange','option') ){ ?>
                <div class="flex-double-col">
                    <div class="form-section-arranging">
                        <?php echo get_field('content_arrange','option'); ?>
                        <?php echo do_shortcode(get_field('form_arrange','option')); ?>
                    </div>
                </div>
             <?php } ?>
        </div>
        <div class="similar_properties">
            <h2 class="section-heading">Similar <span>Properties</span></h2>
            <?php echo do_shortcode("[PH_Similar_Properties property_id=" . $property->id . "]"); ?>
        </div>
        <?php if(get_field('hp_promo_content_single','option') || get_field('hp_promo_button_label_single','option')) { ?>
        <div class="love-little">
            <div class="content-love-little">
                    <div class="img-little">
                        <img src="<?php echo get_stylesheet_directory_uri(); ?>/images/sale.svg" alt="">
                    </div>
                    <?php if(get_field('hp_promo_content_single','option')) { ?>
                        <div class="conten-little">
                            <?php echo get_field('hp_promo_content_single','option'); ?>
                        </div>
                    <?php  } ?>
                    <?php if(get_field('hp_promo_button_label_single','option')) { ?>
                    <div class="btn-litle">
                        <a href="<?php echo get_field('hp_promo_button_url_single','option'); ?>"><?php echo get_field('hp_promo_button_label_single','option'); ?></a>
                    </div>
                   <?php } ?>
            </div>
        </div>
        <?php } ?> 
    <!-- </div> -->
    <?php
}
//team shortcode
function team_sales( $atts ) {
   $atts = shortcode_atts( array(
    'department' => '',
    'office' => ''
  ), $atts );
  
    $tax_query = array();
    $args['tax_query'] = array( 'relation'=>'AND' );
    if($atts['department']){
            $tax_query[] = array(
                'taxonomy'  => 'department',
                'field'     => 'slug',
                'terms'     => $atts['department']
            );
          }
     if($atts['office']){
            $tax_query[] = array(
                'taxonomy'  => 'offices',
                'field'     => 'slug',
                'terms'     => $atts['office']
            );
          }
     $args['tax_query'] = $tax_query;          
    $loop = new WP_Query( array(
        'posts_per_page'    => -1,
        'post_type'         => 'team_members',
        'orderby'           => 'menu_order title',
        'order'             => 'ASC',
        'tax_query'         => $tax_query
    ) ); 
    $content = '<div class="team-wrap">';
     if($loop->have_posts()) {
        while($loop->have_posts()) : $loop->the_post();
            $name = get_field('name');
             $position = get_field('position');
              $image = get_field('image');
               $email_address = get_field('email_address');
            $content .= '<div class="team-cover">';
            if($image){
                    $content .= '<div class="team-image-cover"><img class="team-image" src="'. $image.'"></div>';
                }
              if($name){   
                    $content .= '<h3>'.get_the_title().'</h3>';
                }
                if($position){   
                    $content .= '<p>'.$position.'</p>';
                   } 
               if($email_address){    
                    $content .= '<a class="email-me" href="mailto:'.$email_address.'"><span class="et-pb-icon email"></span></a>';
                 }    
             $content .= '</div>';    
        endwhile;
     }
     $content .= '</div>';
     return $content; 
}
add_shortcode( 'Team','team_sales' );
