<?php
    session_start();
    global $post;
    $err_msg = '';
    $war_msg = '';
    $suc_msg = '';

    function addhttp($url) {
        if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
            $url = "http://" . $url;
        }
        return $url;
    }

    if(isset($_POST['quick_contact'])){
        if(empty($_POST['cus_name'])){
            $err_msg = 'Name is required.';
        }else if(empty($_POST['cus_email'])){
            $err_msg = 'Email address is required.';
        }else if(filter_var($_POST['cus_email'], FILTER_VALIDATE_EMAIL) === FALSE){
            $err_msg = 'Please enter a valid email address.';
        }else if(empty($_POST['cus_message'])){
            $err_msg = 'Message is required.';
        }
        
        if($err_msg == ''){
            if(isset($_POST['answar']) && $_POST['answar'] == $_POST['ver_code']){
                $to = (get_post_meta($post->ID, 'email_address', TRUE) <> '') ? get_post_meta($post->ID, 'email_address', TRUE) : $userdata->user_email;
//                $to = get_the_author_meta('user_email', $post->post_author);
//                $display_name = get_the_author_meta('display_name', $post->post_author);
                $display_name = (get_post_meta($post->ID, 'contact_person', TRUE) <> '') ? get_post_meta($post->ID, 'contact_person', TRUE) : get_the_author_meta('display_name', $post->post_author);
                $from = esc_attr($_POST['cus_email']);
                $from_name = esc_attr($_POST['cus_name']);
                $message = $_POST['cus_message'];
                $directory_title = get_the_title($post->ID);
                $blogname = get_option('blogname');
                $headers = "From: $from_name <$from>\r\n";
                $headers .= "Reply-To: $display_name <$to>\r\n";
                $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
                $subject = "{$blogname} :: {$from_name} has made an enquiry from your {$directory_title} directory";
                $msg = "Dear $display_name,<br/><br/>";
                $msg .= "Someone has made an enquiry into your {$directory_title} directory.<br/> ";
                $msg .= "Please find the details below.<br/><br/>";
                $msg .= "Name: {$from_name} <br/>";
                $msg .= "Email Address: {$from} <br/>";
                $msg .= "Message: {$message} <br/><br/>";

                if(wp_mail( $to, $subject, $msg, $headers )){
                    unset($_POST);
                    $suc_msg = 'Your message has been successfully submitted.';
                }else{
                    $err_msg = 'Error occured! Please try again later.';
                }
            }else{
                $err_msg = 'Invalid captcha!';
            }
        }
    }
    $userdata = get_userdata($post->post_author);
    get_header(); 
    if(have_posts()):
        while(have_posts()) :
            the_post();
            setPostViews(get_the_ID());
?>
<?php echo mini_banner_listing(); ?>
<div class="main-container clearfix">
    <section class="main clearfix">
        <div class="single-container clearfix">
            <?php if(isset($_SESSION['breadcrumb'])): ?>
            <div class="breadcrumbs wrapper" style="margin-top: 25px">
                <a href="<?php echo $_SESSION['breadcrumb']['term_link']; ?>"><?php echo $_SESSION['breadcrumb']['term']; ?></a> <span class="active"><?php the_title(); ?></span>
            </div>
            <?php endif; ?>
			<div class="wrapper"><a href='javascript:history.back(1);'><i class="fa fa-angle-left"></i> Back</a>	</div>		
			<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), 'full'); ?>
			<div class="full" style="height: 350px; width: 100%; margin-bottom: 35px; background-image: url(<?=$image['0'];?>); background-position: center; background-size: cover; background-repeat: no-repeat;">
			</div>

            <div class="single-content wrapper clearfix">
                <div class="single-content-area clearfix border-bottom">
                    <div class="grid-row">
                        <div class="grid-row-7a">
                            <div class="single-grid-block">
                                <header class="site-heading">
                                    <h2><?php the_title(); ?></h2>
                                </header>
                                <div>
                                    <?php the_content(); ?>
                                </div>
                            </div>
                        </div>
<div class="grid-row-5a">
                            <div class="single-grid-block">
                                <div class="single-quick-con-area site-heading" data-email="<?php echo get_post_meta($post->ID, 'email_address', TRUE); ?>" data-title="<?php the_title(); ?>">
                                    <h2>Contact Info</h2>

                                            <?php /*$contact_email = (get_post_meta($post->ID, 'email_address', TRUE) <> '') ? get_post_meta($post->ID, 'email_address', TRUE) : $userdata->user_email; ?>
<!--                                            <p class="website-icon">
                                                <a href="mailto:<?php //echo $contact_email; ?>">
                                                    <?php //echo $contact_email; ?>
                                                </a>
                                            </p>
											<?php */ ?>
											<p <?php if( empty( get_post_meta( $post->ID, 'geo_location', true ) ) ) : ?>style="display:none;"<?php endif; ?>><img src="/wp-content/uploads/2019/08/meeting-point.svg"> <a target="_blank" href="https://www.google.com/maps/place/<?php echo urlencode(get_post_meta($post->ID, 'geo_location', TRUE)); ?>"><?php echo get_post_meta($post->ID, 'geo_location', TRUE); ?></a></p>
											<p <?php if( empty( get_post_meta( $post->ID, 'phone', true ) ) ) : ?>style="display:none;"<?php endif; ?> ><img src="/wp-content/uploads/2019/08/contact.svg"> <a href="tel: <?=get_post_meta($post->ID, 'phone', TRUE); ?>"><?=get_post_meta($post->ID, 'phone', TRUE); ?></a></p>
											
											<p <?php if( empty( get_post_meta( $post->ID, 'website_title_dir', true ) ) ) : ?>style="display:none;"<?php endif; ?>><?php $website = (get_post_meta($post->ID, 'website_title_dir', TRUE) <> '') ? get_post_meta($post->ID, 'website_title_dir', TRUE) : get_post_meta($post->ID, 'website', TRUE); ?>
											<img src="/wp-content/uploads/2019/08/www.svg"> <a href="<?=addhttp($website); ?>" target="_blank"><?=$website;?></a></p>
											
								<!--h4 style="margin-top: 50px">Send Enquiry</h4-->
                                    <?php // echo do_shortcode( '[contact-form-7 title="Quick Contact" html_id="quick_contatc"]' ); ?>
                                </div>
                            </div>
                        </div>
                        <?php if(has_post_thumbnail()): ?>
                        <!--<div class="grid-row-5a">
                            <div class="single-grid-block directory_additional_image">
                                
                            	<?php if( get_field('directory_additional_image') ): ?>

								<?php
									// vars
									$image = get_field('directory_additional_image');
									$size = 'gallery-big';
									$full = 'gallery-big';
									?>
										<?php echo wp_get_attachment_image( $image, $size ) ?>	

								<?php endif; ?>
                            </div>
                        </div>-->
                        <?php endif; ?>
                    </div>
                </div>

            </div>
        </div>
    </section>
<div class="single-content wrapper mapHeading"><header class="site-heading"><h2>Where to find them</h2></header></div>
<div class="single-grid-block">
                                <div class="single-map-area">
                                    <div id="map-single" style="width: 627px; height: 410px;"></div>
                                    <!--<a href="#larger_map" class="click-large-view-map">Click to view larger map <i class="icon icon-search"></i></a>-->
                                    <?php if(get_post_meta($post->ID, 'geo_location', TRUE) <> ''): ?>
                                    <!--<a href="https://www.google.com/maps/place/<?php echo urlencode(get_post_meta($post->ID, 'geo_location', TRUE)); ?>" target="_blank" class="click-large-view-map">Click to view larger map <i class="icon icon-search"></i></a>-->
                                    <?php endif; ?>
                                </div>
                            </div>
                            <!--<div id="larger_map" class="zoom-anim-dialog mfp-hide perth-popup-container">
                                <div id="map-single" style="width: 800px; height: 400px;"></div>
                            </div>-->
</div>
<?php
        endwhile;
    endif;
?>
<!-- END -->
<?php  get_footer(); ?>