<?php
	include_once "system_start.php";
	if ( isset( $_REQUEST[ "id" ] ) ) {
		$array_deal = query_array_row( "SELECT * FROM `priv_deals` LEFT JOIN `priv_stores` ON `stor_id` = `deal_store` LEFT JOIN `priv_users` ON `user_id` = `deal_user` WHERE `stor_link` = '" . res( $_REQUEST[ "store" ] ) . "' AND `deal_id` = '" . res( $_REQUEST[ "id" ] ) . "' " . ( $array_system_site[ "site_networkonly" ] == 1 ? "AND ( SELECT SUM( 1 ) FROM `priv_networks_affiliations` LEFT JOIN `priv_networks_stores` ON `nest_id` = `neaf_network_store` WHERE `neaf_approved` = '1' AND `nest_store` = `stor_id` ) > 0 " : "" ) );
		if ( count( $array_deal ) > 0 ) {
			if ( $array_deal[ "deal_active" ] == 1 ) {
				$row_deal_followers = query_array_row( "SELECT IFNULL( SUM( 1 ), 0 ) AS `count` FROM `priv_deals_followers` WHERE `foll_owner` = '" . res( $array_deal[ "deal_id" ] ) . "'" );
				$row_deal_uses = query_array_row( "SELECT IFNULL( SUM( 1 ), 0 ) AS `count` FROM `priv_deals_views` WHERE `devi_deal` = '" . res( $array_deal[ "deal_id" ] ) . "'" );
				$row_deal_positives = query_array_row( "SELECT IFNULL( SUM( 1 ), 0 ) AS `count` FROM `priv_deals_votes` WHERE `vote_owner` = '" . res( $array_deal[ "deal_id" ] ) . "' AND `vote_value` > '0'" );
				$row_deal_negatives = query_array_row( "SELECT IFNULL( SUM( 1 ), 0 ) AS `count` FROM `priv_deals_votes` WHERE `vote_owner` = '" . res( $array_deal[ "deal_id" ] ) . "' AND `vote_value` < '0'" );
				$row_store_networks = query_array_row( "SELECT `neaf_link` FROM `priv_networks_affiliations` LEFT JOIN `priv_networks_stores` ON `nest_id` = `neaf_network_store` LEFT JOIN `priv_networks_accounts` ON `neac_id` = `neaf_account` LEFT JOIN `priv_networks_profiles` ON `nepr_id` = `neac_profile` LEFT JOIN `priv_stores` ON `stor_id` = `nest_store` LEFT JOIN `priv_networks` ON `netw_id` = `neac_network` WHERE `nest_store` = '" . res( $array_deal[ "stor_id" ] ) . "' AND `stor_active` = '1' AND `netw_active` = '1' AND `neaf_active` = '1' AND `nepr_active` = '1' AND `nest_active` = '1' AND `neac_active` = '1' AND `neaf_approved` = '1' ORDER BY `nepr_priority` DESC, `netw_priority` DESC, RAND()" );
				if ( isset( $_SESSION[ "user_id" ] ) ) {
					$row_deal_follower = query_array_row( "SELECT IFNULL( SUM( 1 ), 0 ) AS `count` FROM `priv_deals_followers` WHERE `foll_owner` = '" . res( $array_deal[ "deal_id" ] ) . "' AND `foll_user` = '" . res( $_SESSION[ "user_id" ] ) . "'" );
					$row_deal_liked	= query_array_row( "SELECT IFNULL( SUM( 1 ), 0 ) AS `count` FROM `priv_deals_votes` WHERE `vote_owner` = '" . res( $array_deal[ "deal_id" ] ) . "' AND `vote_user` = '" . res( $_SESSION[ "user_id" ] ) . "' AND `vote_value` > '0'" );
					$row_deal_disliked = query_array_row( "SELECT IFNULL( SUM( 1 ), 0 ) AS `count` FROM `priv_deals_votes` WHERE `vote_owner` = '" . res( $array_deal[ "deal_id" ] ) . "' AND `vote_user` = '" . res( $_SESSION[ "user_id" ] ) . "' AND `vote_value` < '0'" );
				}
				query( "INSERT INTO `priv_deals_views` ( `devi_site`, `devi_deal`, `devi_user` ) VALUES( '" . res( $array_system_site[ "site_id" ] ) . "', '" . res( $array_deal[ "deal_id" ] ) . "', " . isnullforquery( user_id() ) . " )" );
				echo "<!doctype html>";
				echo "<html lang='" . htm( $array_system_site[ "sila_code" ] ) . "'>";
					echo "<head>";
						include_once "system_heads.php";
						$title = htm( $array_deal[ "stor_name" ] . " - " . $array_deal[ "deal_title" ] );
						$description = htm( "Best deals and coupons of " . $array_deal[ "stor_name" ] . " - " . $array_deal[ "deal_title" ] . ". Newest and most accurate coupons, deals and offers we find from all online stores. Discount code" );
						$domain = "https://" . $_SERVER[ "HTTP_HOST" ];
						$image = $domain . "/" . image_store( $array_deal[ "stor_logo_name" ] );
						$url = $domain . $_SERVER[ "REQUEST_URI" ];
						echo "<meta name='keywords' content='" . $array_system_site[ "site_keywords" ] . "'>";
						echo "<meta name='robots' content='" . ( $robot = "noindex, follow" ) . "'>";
						echo "<meta name='googlebot' content='" . $robot . "'>";
						echo "<meta name='bingbot' content='" . $robot . "'>";
						echo "<meta name='description' content='" . $description . "'>";
						echo "<meta property='og:title' content='" . $title . "'>";
						echo "<meta property='og:image' content='" . $image . "'>";
						echo "<meta property='og:description' content='" . $description . "'>";
						echo "<meta property='og:url' content='" . $url . "'>";
						echo "<meta property='og:type' content='website'>";
						echo "<meta name='twitter:card' content='summary_large_image'>";
						echo "<meta name='twitter:site' content='" . $domain . "'>";
						echo "<meta name='twitter:title' content='" . $title . "'>";
						echo "<meta name='twitter:description' content='" . $description . "'>";
						echo "<meta name='twitter:creator' content='@couponifier1'>";
						echo "<meta name='twitter:image' content='" . $image . "'>";
						echo "<meta name='twitter:image' content='" . $domain . "/deal_image.php?id=" . $array_deal[ "deal_id" ] . "&width=500&height=254'>";
						echo "<meta name='twitter:domain' content='" . $domain . "'>";
						echo "<link rel='canonical' href='https://" . $array_system_site[ "site_domain" ] . "/" . strtolower( tra( "deal" ) ) . "/" . $array_deal[ "stor_link" ] . "/" . $array_deal[ "deal_id" ] . "'>";
						echo "<title>" . $array_deal[ "stor_name" ] . " - " . $array_deal[ "deal_title" ] . " - " . $array_system_site[ "site_title" ] . "</title>";
					echo "</head>";
					echo "<body>";
						echo "<main>";
							include_once "system_header.php";
							echo "<content>";
								echo "<div class='container'>";
									echo "<div class='row'>";
										echo "<div class='col'>";

											echo "<div class='card'>";
												echo "<div class='card-body text-center'>";
													echo "<img src='" . image_store( $array_deal[ "stor_logo_name" ] ) . "' width='200' height='100' class='object-fit-contain bg-white border rounded shadow p-2' alt='" . htm( $array_deal[ "stor_name" ] ) . "' title='" . htm( $array_deal[ "stor_name" ] ) . "'><br><br>";
													echo "<h1><a href='/" . strtolower( tra( "store" ) ) . "/" . $array_deal[ "stor_link" ] . "'>" . htm( $array_deal[ "stor_name" ] ) . " " . tra( "Discount Coupons" ) . "</a></h1>";
													echo "<h3>" . htm( $array_deal[ "deal_title" ] ) . " " . tra( "at" ) ." " . htm( $array_deal[ "stor_name" ] ) . "</h3>";
													echo "<h4>" . tra( "COPY CODE" ) .": <a href='" . ( count( $row_store_networks ) > 0 ? $row_store_networks[ "neaf_link" ] : "https://" . $array_deal[ "stor_link" ] ) . "' onclick='copyToClipboard( `" . $array_deal[ "deal_code" ] . "` ); window.open( `/deal/" . $array_deal[ "stor_link" ] . "/" . $array_deal[ "deal_id" ] . "` );" . "' rel='nofollow'><i class='fas fa-copy'></i> " . strtoupper( $array_deal[ "deal_code" ] ) . "</a></h4><br>";
													echo "<a href='/" . image_deal( $array_deal[ "deal_id" ] ) . "' target='_BLANK' rel='nofollow'><img src='" . image_deal( $array_deal[ "deal_id" ] ) . "' width='300' height='150' alt='" . htm( $array_deal[ "stor_name" ] ) . " coupon code' class='object-fit-contain bg-white border rounded shadow p-2' title='" . htm( $array_deal[ "stor_name" ] ) . "'></a><br>";
													echo "<br>" . tra( "Share in" ) . " ";
													echo "<a href='https://www.facebook.com/sharer/sharer.php?u=https://" . $_SERVER[ "HTTP_HOST" ] . $_SERVER[ "REQUEST_URI" ] . "' target='_BLANK' rel='nofollow'><i class='fab fa-facebook'></i> Facebook</a> ";
													echo "<a href='https://twitter.com/share?text=" . urlencode( $array_deal[ "stor_name" ] . " - " . $array_deal[ "deal_title" ] ) . "&url=https://" . $_SERVER[ "HTTP_HOST" ] . $_SERVER[ "REQUEST_URI" ] . "&hashtags=couponifier,deal,coupon' target='_BLANK' rel='noreferrer'><i class='fab fa-twitter'></i> Twitter</a> ";
													echo "<a href='mailto:?subject=" . $array_deal[ "stor_name" ] . " - " . $array_deal[ "deal_title" ] . "&body=" . $array_deal[ "stor_name" ] . " - " . $array_deal[ "deal_title" ] . " - https://" . $_SERVER[ "HTTP_HOST" ] . $_SERVER[ "REQUEST_URI" ] . "' target='_BLANK' rel='nofollow'><i class='fas fa-envelope'></i> " . tra( "Mail" ) . "</a> ";
													echo "<a href='https://www.facebook.com/dialog/send?app_id=XXX&link=https://" . $_SERVER[ "HTTP_HOST" ] . $_SERVER[ "REQUEST_URI" ] . "&redirect_uri=https://" . $_SERVER[ "HTTP_HOST" ] . "' target='_BLANK' rel='nofollow'><i class='fab fa-facebook-messenger'></i> Messenger</a> ";
													echo "<a href='https://api.whatsapp.com/send?text=" . $array_deal[ "stor_name" ] . " - " . $array_deal[ "deal_title" ] . " - https://" . $_SERVER[ "HTTP_HOST" ] . $_SERVER[ "REQUEST_URI" ] . "' target='_BLANK' rel='nofollow'><i class='fab fa-whatsapp'></i> Whatsapp</a> ";
													echo "<a href='/report/deals/" . $array_deal[ "deal_id" ] . "/" . base64url_encode( $array_deal[ "stor_name" ] . " - ". $array_deal[ "deal_title" ] ) . "' rel='nofollow'><i class='fas fa-flag'></i> " . tra( "Report" ) . "</a> ";
													// echo "<a href='/system_report.php?type=deals&id=" . $array_deal[ "deal_id" ] . "&name=" . base64url_encode( $array_deal[ "stor_name" ] . " - ". $array_deal[ "deal_title" ] ) . "' rel='nofollow'><i class='fas fa-flag'></i> " . tra( "Report" ) . "</a> ";
													if ( user_can( "admin_deal_insert.php" ) ) {
														echo "<a href='/admin_deal_insert.php?step=deal&store=" . $array_deal[ "stor_id" ] . "' rel='nofollow'><i class='fas fa-plus'></i> Add</a> ";
														echo "<a href='/admin_deal_insert.php?step=edit&deal=" . $array_deal[ "deal_id" ] . "' rel='nofollow'><i class='fas fa-edit'></i> Edit</a> ";
													}
													$array_deal_votes = query_array_row( "SELECT SUM( IF( `vote_value` > 0, 1, 0 ) ) AS `positives`, SUM( IF( `vote_value` < 0, 1, 0 ) ) AS `negatives`, SUM( `vote_value` ) AS `diference`, COUNT( `vote_id` ) AS `total` FROM `priv_deals_votes` WHERE `vote_owner` = '" . res( $array_deal[ "deal_id" ] ) . "'" );
													if ( isset( $_SESSION[ "user_id" ] ) ) {
														$array_deal_vote = query_array_row( "SELECT `vote_value` FROM `priv_deals_votes` WHERE `vote_user` = '" . res( $_SESSION[ "user_id" ] ) . "' AND `vote_owner` = '" . res( $array_deal[ "deal_id" ] ) . "'" );
													} else {
														$array_deal_vote = array();
													}
													echo "<a href='" . ( isset( $_SESSION[ "user_id" ] ) ? "/vote/deals/" . $array_deal[ "deal_id" ] . "/" . "-1/" . md5( $array_deal[ "deal_id" ] . "-1" . $_SESSION[ "user_token" ] ) : "/login/" . base64url_encode( $_SERVER[ "REQUEST_URI" ] ) ) . "' class='text-" . ( ( count( $array_deal_vote ) > 0 and $array_deal_vote[ "vote_value" ] < 0 ) ? "danger"  : "secondary" ) . "' rel='nofollow'><i class='fas fa-thumbs-down'></i> " . tra( "Dislike" ) . " </a> ";
													echo "<a href='" . ( isset( $_SESSION[ "user_id" ] ) ? "/vote/deals/" . $array_deal[ "deal_id" ] . "/" .  "1/" . md5( $array_deal[ "deal_id" ] .  "1" . $_SESSION[ "user_token" ] ) : "/login/" . base64url_encode( $_SERVER[ "REQUEST_URI" ] ) ) . "' class='text-" . ( ( count( $array_deal_vote ) > 0 and $array_deal_vote[ "vote_value" ] > 0 ) ? "primary" : "secondary" ) . "' rel='nofollow'><i class='fas fa-thumbs-up'  ></i> " . tra( "Like" ) . "    </a> ";
													$row_deal_follower = query_array_row( "SELECT IFNULL( SUM( 1 ), 0 ) AS `count` FROM `priv_deals_followers` WHERE `foll_owner` = '" . res( $array_deal[ "deal_id" ] ) . "' AND `foll_user` = '" . res( $_SESSION[ "user_id" ] ) . "'" );
													echo "<a class='text-" . ( $row_deal_follower[ "count" ] > 0 ? "primary" : "secondary" ) . "' href='" . ( isset( $_SESSION[ "user_id" ] ) ? "/follow/deals/" . $array_deal[ "deal_id" ] . "/" . md5( $array_deal[ "deal_id" ] . $_SESSION[ "user_token" ] ) : "/login/" . base64url_encode( $_SERVER[ "REQUEST_URI" ] ) ) . "' rel='nofollow'><i class='fas fa-heart'></i> " . tra( "Follow" ) . "</a> ";
												echo "</div>";
											echo "</div>";

											comment_section( "deals", $array_deal[ "deal_id" ] );
											
											$array_moredeals = query_array_all( "SELECT * FROM ( SELECT * FROM `priv_deals` LEFT JOIN `priv_stores` ON `stor_id` = `deal_store` WHERE `deal_active` = '1' AND `deal_store` = '" . res( $array_deal[ "stor_id" ] ) . "' AND `deal_id` <> '" . res( $array_deal[ "deal_id" ] ) . "' ORDER BY `deal_insert` DESC LIMIT 50 ) AS `xxx` ORDER BY RAND() LIMIT 9" );
											if ( count( $array_moredeals ) > 0 ) {
												echo "<hr><h2>" . tra( "More deals" ) . "</h2>";
												echo "<div class='row row-cols-3'>";
													foreach ( $array_moredeals as $array_moredeal ) {
														echo "<div class='col'>";
															echo "<div class='card'>";
																echo "<div class='card-body'>";
																	echo "<img src='" . image_deal( $array_moredeal[ "deal_id" ] ) . "' width='150' height='75' alt='" . htm( $array_moredeal[ "stor_name" ] ) . " coupon code' class='object-fit-contain bg-white border rounded shadow p-2' title='" . htm( $array_moredeal[ "stor_name" ] ) . "'>";
																	echo "<h6 class='card-title'><a href='/" . strtolower( tra( "deal" ) ) . "/" . $array_moredeal[ "stor_link" ] . "/" . $array_moredeal[ "deal_id" ] . "' rel='nofollow'>" . htm( $array_moredeal[ "deal_title" ] ) . "</a></h6>";
																echo "</div>";
															echo "</div>";
														echo "</div>";
													}
												echo "</div>";
											}

											echo "<hr><h2>" . tra( "Recommended stores" ) . "</h2>";
											echo "<div class='row justify-content-center'>";
												$array_stores = query_array_all( "SELECT `stor_id` FROM `priv_networks_affiliations` LEFT JOIN `priv_networks_stores` ON `nest_id` = `neaf_network_store` LEFT JOIN `priv_stores` ON `stor_id` = `nest_store` WHERE `neaf_approved` = '1' AND `neaf_coupon` IS NOT NULL AND `stor_active` = '1' ORDER BY RAND() LIMIT 16" );
												if ( count( $array_stores ) > 0 ) {
													foreach ( $array_stores as $array_store ) {
														store_card( $array_store[ "stor_id" ] );
													}
												}
											echo "</div>";

											echo "<hr><h2>" . tra( "New stores" ) . "</h2>";
											echo "<div class='row justify-content-center'>";
												$array_stores = query_array_all( "SELECT * FROM ( SELECT `stor_id` FROM `priv_stores` WHERE `stor_active` = '1' " . ( $array_system_site[ "site_networkonly" ] == 1 ? "AND ( SELECT SUM( 1 ) FROM `priv_networks_affiliations` LEFT JOIN `priv_networks_stores` ON `nest_id` = `neaf_network_store` WHERE `neaf_approved` = '1' AND `nest_store` = `stor_id` ) > 0 " : "" ) . "ORDER BY `stor_insert` DESC LIMIT 1000 ) AS `table1` ORDER BY RAND() LIMIT 4" );
												if ( count( $array_stores ) > 0 ) {
													foreach ( $array_stores as $array_store ) {
														store_card( $array_store[ "stor_id" ] );
													}
												}
											echo "</div>";
											
										echo "</div>";
									echo "</div>";
								echo "</div>";
							echo "</content>";
							include_once "system_footer.php";
						echo "</main>";
						include_once "system_scripts.php";
					echo "</body>";
				echo "</html>";
			} else {
				header( "location: /store/" . htm( $array_deal[ "stor_link" ] ) );
			}
		} else {
			header( "location: /" );
		}
	} else {
		header( "location: /" );
	}
	include_once "system_end.php";
?>