<?php
/**
 * archive-store.php
 * @package WordPress
 * @subpackage Froday
 * @since Froday 1.0
 */
?>

<?php get_header(); ?>

        <main id="mainContent" class="main-content">
            <!-- Page Container -->
            <div class="page-container ptb-60">
                <div class="container">
                    <div class="row row-rl-10 row-tb-20">
                        <div class="page-sidebar col-sm-4 col-md-3">
                            <aside class="store-header-area panel t-center">
                                <div class="row">
                                    <div class="col-xs-12">
                                        <figure class="pt-10 pl-10">
                                            <?php echo froday_store_thumbnail(); ?>
                                        </figure>
                                    </div>
									
                                    <div class="col-xs-12">
                                        <div class="store-about ptb-30 prl-10">
                                            <h3 class="mb-10"><?php echo froday_store_title(); ?></h3>
                                            <div class="rating mb-10">
                                                <?php froday_store_rating(); ?>
                                            </div>
                                            <p class="mb-15"><?php echo froday_store_description(); ?></p>
                                        </div>
                                    </div>
									
									
                                    <div class="col-xs-12">
                                        <div class="store-splitter-left">
                                            <header class="left-splitter-header prl-10 ptb-20 bg-lighter">
                                                <div class="row">
                                                    <div class="col-xs-6">
                                                        <h2><?php froday_store_count('product'); ?></h2>
                                                        <p><?php esc_html_e('Deals','froday'); ?></p>
                                                    </div>
                                                    <div class="col-xs-6">
														<h2><?php froday_store_count('shop_coupon'); ?></h2>
                                                        <p><?php esc_html_e('Coupons','froday'); ?></p>
                                                    </div>
                                                </div>
                                            </header>
                                            <div class="left-splitter-body prl-20 ptb-20">
                                                <div class="row row-rl-10 row-tb-10">
												<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
													<?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) { ?>
														<div class="col-md-6 col-sm-4 col-xs-6">
															<?php the_post_thumbnail('post-thumb'); ?>
														</div>
													<?php } ?>
												<?php endwhile; ?>
												<?php endif; ?>
                                                </div>
                                            </div>
											<?php  
												$att=get_post_thumbnail_id();
												$image_src = wp_get_attachment_image_src( $att, 'full' );
												$image_src = $image_src[0]; 
											?>
                                            <footer class="left-splitter-social prl-20 ptb-20">
                                                <ul class="list-inline social-icons social-icons--colored t-center">
                                                    <li class="social-icons__item">
                                                        <a href="http://www.facebook.com/sharer.php?u=<?php the_permalink(); ?>"><i class="fa fa-facebook"></i></a>
                                                    </li>
                                                    <li class="social-icons__item">
                                                        <a href="https://twitter.com/intent/tweet?text=<?php the_title(); ?>&nbsp;<?php the_permalink(); ?>"><i class="fa fa-twitter"></i></a>
                                                    </li>
                                                    <li class="social-icons__item">
                                                        <a href="https://plus.google.com/share?url=<?php the_title(); ?>&nbsp;<?php the_permalink(); ?>"><i class="fa fa-google-plus"></i></a>
                                                    </li>
                                                    <li class="social-icons__item">
                                                        <a href="http://pinterest.com/pin/create/button/?url=<?php the_permalink(); ?>;media=<?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) { ?><?php echo esc_url($image_src); ?><?php } ?>;description=<?php the_title(); ?>"><i class="fa fa-pinterest"></i></a>
                                                    </li>
                                                </ul>
                                            </footer>
                                        </div>
                                    </div>
                                </div>
                            </aside>
                        </div>
                        <div class="page-content col-sm-8 col-md-9">

                            <!-- Store Tabs Area -->
                            <div class="section store-tabs-area">
                                <div class="tabs tabs-v1">
                                    <!-- Nav tabs -->
                                    <ul class="nav nav-tabs panel" role="tablist">
                                        <li role="presentation" class="active">
                                            <a href="#deals" aria-controls="deals" role="tab" data-toggle="tab" aria-expanded="false"><i class="fa fa-comment mr-10"></i><?php esc_html_e('Deals','froday'); ?></a>
                                        </li>
                                        <li role="presentation">
                                            <a href="#coupons" aria-controls="coupons" role="tab" data-toggle="tab" aria-expanded="true"><i class="fa fa-star mr-10"></i><?php esc_html_e('Coupons','froday'); ?></a>
                                        </li>
                                    </ul>

                                    <!-- Tab panes -->
                                    <div class="tab-content">
                                        <div role="tabpanel" class="tab-pane ptb-20 active" id="deals">
                                            <section class="section deals-area">
                                                <div class="row row-masnory row-tb-20">


												<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
						
													<?php wc_get_template_part( 'content', 'store-archive' ); ?>
													
												<?php endwhile; ?>
												<?php get_template_part( 'post-format/pagination' ); ?>
												<?php endif; ?>
                                                    
                                                </div>
                                            </section>
                                        </div>
                                        <div role="tabpanel" class="tab-pane ptb-20" id="coupons">
                                            <section class="section coupons-area coupons-area-grid">
                                                <div class="row row-masnory row-tb-20">
												
												<?php $terms = get_the_terms( get_the_ID(), 'store');
												if($terms){
												foreach($terms as $term) {
													global $post;
													global $product;
													$paged    = (get_query_var('paged')) ? get_query_var('paged') : 1;

													$args = array(
														'post_type' => 'shop_coupon',
														'posts_per_page' => 10,
														"paged" => $paged,
														'tax_query' => array(
															array(
																'taxonomy' => 'store',
																'field' => 'term_id',
																'terms' => $term->term_id,
															)
														)
													);
													$stores = get_posts( $args );
													if ( $stores ) {
														$count = 0;
														foreach ( $stores as $post ) { ?>
														
													<?php 
														$post_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "full" ); 
														$coupon_amount = get_post_meta( get_the_ID(), 'coupon_amount', true );
														$expiry_date = get_post_meta( get_the_ID(), 'expiry_date', true );
														$destination_url = get_post_meta( get_the_ID(), 'klb_coupon_destination_url', true );
														$mustlogin = get_post_meta( get_the_ID(), 'klb_must_login', true );

													?>	
														
                                                    <div class="col-sm-6">
                                                        <div class="coupon-single panel t-center">
                                                            <div class="row">
                                                                <div class="col-xs-12">
                                                                    <div class="text-center p-20">
                                                                        <img class="store-logo" src="<?php echo esc_url($post_image[0]); ?>" alt="<?php the_title(); ?>">
                                                                    </div>
                                                                </div>
                                                                <div class="col-xs-12">
                                                                    <div class="panel-body">
                                                                        <ul class="deal-meta list-inline mb-10">
                                                                            <?php echo froday_location_name(); ?>
                                                                            <li class="color-muted"><i class="ico lnr lnr-store mr-5"></i><?php echo froday_store_title(); ?></li>
                                                                        </ul>
                                                                        <h4 class="color-green mb-10 t-uppercase"><?php echo esc_html($coupon_amount); ?><?php esc_html_e('% OFF','froday'); ?></h4>
                                                                        <h5 class="deal-title mb-10">
																			<a href="#"><?php the_excerpt(); ?></a>
																		</h5>
																		<?php if($expiry_date){ ?>
                                                                        <p class="mb-15 color-muted mb-20 font-12"><i class="lnr lnr-clock mr-10"></i><?php esc_html_e('Expires On','froday'); ?> <?php echo esc_html($expiry_date); ?></p>
																		<?php } ?>
                                                                        <div class="showcode">
																			<?php $titleid = get_the_title();
																			$strid =  preg_replace('/[^a-zA-Z0-9-_\.]/','', $titleid); ?>
                                                                            <button class="show-code btn btn-sm btn-block" data-toggle="modal" data-target="#<?php echo esc_attr($strid); ?>"><?php esc_html_e('Get Coupon Code','froday'); ?></button>
                                                                            
																				<?php 
																					if($mustlogin){
																						if(is_user_logged_in()){
																							echo '<div class="coupon-hide">'.get_the_title().'</div>';
																						} else {
																							echo '<div class="coupon-hide">'.esc_html__('Please Log In','froday').'</div>';
																						}
																					} else {
																						echo '<div class="coupon-hide">'.get_the_title().'</div>';
																					}
																			
																				?>

																			
                                                                        </div>
                                                                    </div>
                                                                </div>
                                                                <!-- end col -->
                                                            </div>
                                                            <!-- end row -->
                                                        </div>

														<script>
														function frodayCopy_<?php echo esc_html($count); ?>() {
														  var copyText = document.getElementById("<?php echo esc_attr($strid); ?><?php echo esc_html($count); ?>");
														  copyText.select();
														  document.execCommand("copy");
														  <?php if($destination_url){ ?>
															window.open('<?php echo esc_url($destination_url); ?>','_blank');
														  <?php } ?>
														}
														</script>

                                                        <div class="modal fade get-coupon-area" tabindex="-1" role="dialog" id="<?php echo esc_attr($strid); ?>">
                                                            <div class="modal-dialog">
                                                                <div class="modal-content panel">
                                                                    <div class="modal-body">
                                                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                                                                        </button>
                                                                        <div class="row row-v-10">
                                                                            <div class="col-md-10 col-md-offset-1">
                                                                                <?php echo froday_store_thumbnail(); ?>
                                                                                <h3 class="mb-20"><?php the_excerpt(); ?></h3>
                                                                                <?php echo get_post_field('post_content', $post->ID); ?>
                                                                            </div>
                                                                            <div class="col-md-10 col-md-offset-1">
                                                                                <h6 class="color-mid t-uppercase"><?php esc_html_e('Click below to get your coupon code','froday'); ?></h6>
																				<div class="copy-coupon-wrap">
																					 <?php if($mustlogin){
																								if(is_user_logged_in()){
																									echo '<input type="text" value="'.get_the_title().'" id="'.$strid . esc_html($count).'" class="coupon-code">
																									<button onclick="frodayCopy_'.$count.'()">'.esc_html__('Copy Coupon','froday').'</button>';
																									} else {
																									 echo '<input type="text" value="'.esc_html__('Please Log In','froday').'" id="'.$strid . esc_html($count).'" class="coupon-code">
																									<button onclick="frodayCopy_'.$count.'()">'.esc_html__('Copy Coupon','froday').'</button>';
																								}
																							} else {
																								echo '<input type="text" value="'.get_the_title().'" id="'.$strid . esc_html($count).'" class="coupon-code">
																								<button onclick="frodayCopy_'.$count.'()">'.esc_html__('Copy Coupon','froday').'</button>';
																							}
																					
																					   ?>
																				</div>
                                                                            </div>
                                                                        </div>
                                                                    </div>
                                                                    <div class="modal-footer footer-info t-center ptb-40 prl-30">
                                                                        <h4 class="mb-15"><?php echo esc_html(ot_get_option('froday_coupon_subscribe_title')); ?></h4>
                                                                        <p class="color-mid mb-20"><?php echo esc_html(ot_get_option('froday_coupon_subscribe_subtitle')); ?></p>
                                                                        <?php echo do_shortcode('[mc4wp_form id="'.ot_get_option('froday_coupon_subscribe_form_id').'"]'); ?>
                                                                    </div>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
													<?php $count++; ?>
													<?php } ?>
													<?php wp_reset_postdata(); ?>
													<?php } } } ?>
                                                </div>
                                            </section>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <!-- End Store Tabs Area -->

                        </div>
                    </div>
                </div>
            </div>
            <!-- End Page Container -->

        </main>
<?php get_footer(); ?>