<?php

if(stripos($_SERVER['SCRIPT_NAME'],"/admin/")===false?false:true){
	include_once("../connect.php");
	include_once("../session.php");
}else{
	include_once("connect.php");
	include_once("session.php");
}

class Billboard{
	public $ID, $Title, $Text, $Link, $Type, $Content, $Button, $Position, $Active, $StartDate, $EndDate;
	public function __construct($ID) {
		global $con;
		$query = "SELECT * FROM BILLBOARD WHERE ID = $ID";
		$result = mysqli_query($con, $query);
		$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
		$this->ID = $ID;
		$this->Title = $row['Title'];
		$this->Text = $row['Text'];
		$this->Link = $row['Link'];
		$this->Type = $row['Type'];
		$this->Content = $row['Content'];
		$this->Button = $row['Button'];
		$this->Position = $row['Position'];
		$this->StartDate = $row['StartDate'];
		$this->EndDate = $row['EndDate'];
		$this->Active = $row['Active'];
	}
	public function getHTML(){
		$idTag = "";
		$disclaimer = "";
		$contentLine = "";
		if($this->Type == "image"){
			$contentLine = "<img style=\"\" id=\"promoModalContent" . $this->ID . "\" src=\"" . $this->Content . "\" />";
		}elseif($this->Type == "video"){
			$contentLine = "<iframe width=\"800\" height=\"488\" src=\"\" id=\"promoModalContent" . $this->ID . "\" frameborder=\"0\" allowfullscreen=\"0\"></iframe>";
		}elseif($this->Type == "special"){
			$idTag = "id=\"specials-body\"";
			$disclaimer = "
		<center>
		  <span>
			**Estos precios son NETOS y NO est&aacute;n sujetos a ning&uacute;n otro descuento**<br>
			***Venta Especial por tiempo limitado - Mientras dure la mercanc&iacute;a - No se guardar&aacute;n \"BACK ORDERS\" de la venta especial***
		  </span>
		</center>";
		}elseif($this->Type == "pdf"){
			$contentLine = "<iframe data-type=\"pdf\" style=\"max-height: 70%; max-width=100%;\" src=\"" . $this->Content . "\" id=\"promoModalContent" . $this->ID . "\" frameborder=\"0\" allowfullscreen=\"0\"></iframe>";
		}elseif($this->Type == "slideshow"){
			
		}
		?>
			<!-- PROMO MODAL -->
			<div class="modal fade <?= ($this->Position == -1)?"dropdownModal":"" ?>" id="promoModal<?= $this->ID ?>">
			  <div class="modal-dialog modal-lg">
				<div class="modal-content">
				  <div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
					<h4 class="modal-title" id="promoModalTitle<?= $this->ID ?>"><?= $this->Title ?></h4>
				  </div>
				  <div class="modal-body" <?= $idTag ?>>
					<p style="text-align: center;">
						<?= $contentLine ?>
						<br />
						<span class="">
						<?php
							if($this->Link != ""){
						?>
							<a id="promoModalLink<?= $this->ID ?>" href="<?= $this->Link ?>">
								<span id="promoModalText<?= $this->ID ?>"><?= $this->Text ?></span>
							</a>
						<?php
							} else {
						?>
							<span id="promoModalText<?= $this->ID ?>"><?= $this->Text ?></span>
						<?php
							}
						?>
						</span>
					</p>
				  </div>
				  <div class="modal-footer">
					<?= $disclaimer ?>
					<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
				  </div>
				</div><!-- /.modal-content -->
			  </div><!-- /.modal-dialog -->
			</div><!-- /.modal -->
			
			<!-- END PROMO MODAL -->
			<?php if($this->Type == "video"){
			?>
			<script>
				$('#promoModal<?= $this->ID ?>').on('show.bs.modal', function(){
						$('#promoModalContent<?= $this->ID ?>').attr('src', "https://www.youtube.com/embed/<?= $this->Content ?>?rel=0&autohide=1&autoplay=1&showinfo=0&html5=1");					
				});
				$('#promoModal<?= $this->ID ?>').on('hide.bs.modal', function(){
						$('#promoModalContent<?= $this->ID ?>').attr('src', "");					
				});
			</script>
			<?php
			}elseif($this->Type == "special"){
			?>
			<script>
				var isSpecialsLoaded = false;
				var specialsModal = $("#promoModal<?= $this->ID ?>");
				specialsModal.on('show.bs.modal', function (e) {
					fillSpecials();
				});
				
				function fillSpecials() {
					if (!isSpecialsLoaded) {
						$('#specials-body').html("");
						$.get('functions.php?action=getSpecialsData', function(data) {
							isSpecialsLoaded = true;
							data = JSON.parse(data);
							number = SKU.length;
							var html = "<table class=\"table\">"
									+"<thead>"
									+	"<tr>"
									+"      <th style=\"width: 5%\">Line<br>Group</th>"
									+"      <th style=\"width: 75%\">Item</th>"
									+"      <th style=\"width: 5%\">QTY</th>"
									+"      <th style=\"width: 5%\">Price</th>"
									+"      <th style=\"width: 15%\">Order</th>"
									+"	</tr>"
									+"</thead>"
									+"<tbody>";
							
							for (var i = 0; i < data.length; i++) {
								var item = data[i];
								var addButtonText = "Add";
								var addButtonStyle = "success";
								SKU[number] = item.SKU;
								Line[number] = item.Line;
								Group[number] = item.Group;
								if (item.OnOrder == null) {
									OnOrder[number] = "";	
									addButtonText = "Add";
									addButtonStyle = "success";
								}else {
									OnOrder[number] = item.OnOrder;
									addButtonText = "Update";
									addButtonStyle = "warning";
								}
								
								
								if (item.Qty > 50) {
									item.Qty = "50+";
								}
								if (item.Interchange.trim() != "") {
									item.Interchange = " (" + item.Interchange + ")";
								}
								html += "<tr>"
										+"   <td>"
										+"   		<span class=\"itemInfo\">" + item.Line + "</span>"
										+"   		<span class=\"itemInfo\">" + item.Group+ "</span>"
										+"   </td>"
										+"   <td>"
										+"   		<span class=\"itemInfo\"><b>" + item.SKU + "</b>" + item.Interchange + "</span><br>"
										+"   		<span class=\"itemInfo\">" + item.Description + " " + item.Notes + "</span>"
										+"   		<span class=\"itemInfo\"></span>"
										+"   </td>"
										+"   <td>" + item.Qty + "</td>"
										+"   <td><s>" + item.RegularPrice + "</s><br /><span style=\"color: red;\">" + item.Price + "</span></td>"
										+"   <td>"
										+"   		<button id=\"" + number + "AddButton\" class=\"btn btn-" + addButtonStyle + "\" onclick=\"askQty(" + number + ")\" type=\"button\">" + addButtonText + "</button>"
										+"   </td>"
										+" </tr>";
								
								number++;
							}
							html += "</tbody>"
								+	"</table>";
							$('#specials-body').append(html);
						});	
					}
				}
			</script>
			<?php
			}
			?>
<?php
	}
}
?>