<?php


  if(stripos($_SERVER['SCRIPT_NAME'],"/admin/")===false?false:true){
		
?>
	<!-- ADMIN SELECT DAYS -->
				<div class="modal fade" id="lastCustomerReportDays" tabindex="-1" role="dialog" aria-labelledby="lastCustomerReportDays" aria-hidden="true">
          <div class="modal-dialog modal-sm">
            <div class="modal-content">
              <div class="modal-header">
								<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
								<h4 class="modal-title" id="lastCustomerReportDaysTitle">Please Select Days for Report</h4>
              </div>
              <div class="modal-body">
								<div class="container-fluid">
									<div class="row">
										<form id="lastCustomerReportDaysForm" >
											<div class="form-group">
												<div class="radio">
													<label><input type="radio" name="days" value="30">30 Days</label>
												</div>
												<div class="radio">
													<label><input type="radio" name="days" value="60">60 Days</label>
												</div>
												<div class="radio">
													<label><input type="radio" name="days" value="90">90+ Days</label>
												</div>
												
											</div>
										</form>
									</div>
								</div>
							</div>
              <div class="modal-footer">
								<span class="pull-left" id="dismissCustomerNoticeNumber"></span>
	              <a id="lastCustomerReportDaysGo" class="btn btn-primary" onclick="lastCustomerReportDaysGo();">Go</a>
	              <br>
							</div>
            </div>
          </div>
        </div>
				<script>
					function lastCustomerReportDaysGo(){
						
						window.location = "mod.php?p=lastcustomerpurchase&a=report&days=" + $("input[name='days']:checked").val();
					}
				</script>
	
	<!-- END SELECT DAYS -->
	<!-- ADMIN SELECT DAYS 2 -->
				<div class="modal fade" id="lastCustomerReportDays2" tabindex="-1" role="dialog" aria-labelledby="lastCustomerReportDays2" aria-hidden="true">
          <div class="modal-dialog modal-sm">
            <div class="modal-content">
              <div class="modal-header">
								<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
								<h4 class="modal-title" id="lastCustomerReportDays2Title">Please Select Days for Report</h4>
              </div>
              <div class="modal-body">
								<div class="container-fluid">
									<div class="row">
										<form id="lastCustomerReportDays2Form" >
											<label for="lastCustomerReportDays2Days">Customers without orders for more than:</label>
											<input class="form-control" id="lastCustomerReportDays2Days" name="days" type="number"/> days
										</form>
									</div>
								</div>
							</div>
              <div class="modal-footer">
								
	              <a id="lastCustomerReportDays2Go" class="btn btn-primary" onclick="lastCustomerReportDays2Go();">Go</a>
	              <br>
							</div>
            </div>
          </div>
        </div>
				<script>
					function lastCustomerReportDays2Go(){
						
						window.location = "mod.php?p=lastcustomerpurchase&a=simplereport&days=" + $("#lastCustomerReportDays2Days").val();
					}
				</script>
	
	<!-- END SELECT DAYS 2 -->
	
	<!-- ADMIN SHOW REPORTS -->
				<div class="modal fade" id="lastCustomerSalesmanReport" tabindex="-1" role="dialog" aria-labelledby="lastCustomerSalesmanReport" aria-hidden="true">
          <div class="modal-dialog modal-md">
            <div class="modal-content">
              <div class="modal-header">
								<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
								<h4 class="modal-title" id="lastCustomerSalesmanReportTitle">Salesman Report</h4>
              </div>
              <div class="modal-body">
								<div class="container-fluid">
									<div class="row">
											<div calss="form">
												<div class="form-group">
													<label for="lastCustomerSalesmanReportCustomer">Customer</label>
													<input type="text" readonly class="form-control" id="lastCustomerSalesmanReportCustomer" placeholder="">
												</div>
												<div class="form-group">
													<label for="lastCustomerSalesmanReportDate">Report Date</label>
													<input type="text" readonly class="form-control" id="lastCustomerSalesmanReportDate" placeholder="">
												</div>
												<div class="form-group">
													<label for="lastCustomerSalesmanReportLastOrder">Last Order</label>
													<input type="text" readonly class="form-control" id="lastCustomerSalesmanReportLastOrder" placeholder="">
												</div>
												<div class="form-group">
													<label for="lastCustomerSalesmanReportSalesman">Salesman</label>
													<input type="text" readonly class="form-control" id="lastCustomerSalesmanReportSalesman">
												</div>
												<div class="form-group">
													<label for="lastCustomerSalesmanReportText">Report</label>
													<textarea rows="8" readonly class="form-control" id="lastCustomerSalesmanReportText">
													</textarea>	
												</div>
												
											</div>
									</div>
								</div>
							</div>
              <div class="modal-footer">
	              <a id="lastCustomerSalesmanReportsClose" data-dismiss="modal" class="btn btn-danger">Close</a>
	              <br>
							</div>
            </div>
          </div>
        </div>
				<script>
					function loadCustomerSalesmanReport(id){
							$.get('mod.php?p=lastcustomerpurchase&a=functions&f=getSalesmanReport&id=' + id, function(data){
									data = JSON.parse(data);
									console.log(data);
									$('#lastCustomerSalesmanReportDate').val(data.ReportDate);
									$('#lastCustomerSalesmanReportLastOrder').val(data.LastOrder);
									$('#lastCustomerSalesmanReportSalesman').val(data.Salesman);
									$('#lastCustomerSalesmanReportText').val(data.Reason);
									$('#lastCustomerSalesmanReportCustomer').val(data.AccountNumber);
									$('#lastCustomerSalesmanReport').modal('show');
								});
						
					}
				</script>
	
	<!-- END SHOW REPORTS -->
<?php
  }elseif(!$isCustomer){
?>

<!-- SALESMAN Customers without orders MODAL -->
    	<div class="modal fade" id="lastCustomerPurchaseActivity" tabindex="-1" role="dialog" aria-labelledby="lastCustomerPurchaseActivity" aria-hidden="true">
          <div class="modal-dialog modal-lg">
            <div class="modal-content">
              <div class="modal-header">
                      <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                      <h4 class="modal-title" id="lastCustomerPurchaseActivityTitle">Customers without Orders in the Last 30 Days</h4>
              </div>
              <div class="modal-body">
								<div class="container-fluid">
									<div class="row">
										<table class="table table-condensed">
											<thead>
												<tr>
													<th>Account Number</th>
													<th>Name</th>
													<th>Last Order</th>
													<th>Last Login</th>
													<th></th>
													<th></th>
												</tr>
											</thead>
											<tbody id="lastCustomerPurchaseActivityTableBody">
												
												
											</tbody>
											
										</table>
									</div>
								</div>
							</div>
              <div class="modal-footer">
								<a id="dismissCustomerReasonClose" data-dismiss="modal" class="btn btn-danger">Close</a>
							</div>
            </div>
          </div>
        </div>
        <script type="text/javascript">
				var customersWOOrders;
        function openCustomerActivity(){
					$('#lastCustomerPurchaseActivity').modal('show');
					$.get('mod.php?p=lastcustomerpurchase&a=functions&f=getCustomerActivity&sid=' + "<?= $_SESSION['SalesmanID']  ?>", function(data){
							data = JSON.parse(data);
							customersWOOrders = data;
							$('#lastCustomerPurchaseActivityTableBody').html('');
							for(var i=0; i< data.length; i++){
								var tag = 'warning';
								if(data[i].ReportCount == "1"){
									tag="danger";
								}
								badge = "";
								data[i].LastActivityDays = parseInt(data[i].LastActivityDays);
								if(data[i].LastActivityDays > "30"){
									badge = '<span class="badge" style="background-color: #dc0d17; color: white;">' + data[i].LastActivityDays + '</span>';
								}
								$('#lastCustomerPurchaseActivityTableBody').append('<tr class=" ' + tag + '" id="dismissCustomerRow-' + i + '"><td>' + data[i].AccountNumber +'</td><td>' + data[i].Name +'</td><td>' + data[i].LastOrderDate +'</td><td>' + data[i].LastLogin +'</td><td>' + badge + '</td><td><button onclick="dismissCustomer(\'' + i + '\')" class="btn btn-success">Ok</button></td></tr>');	
							}
							
							
							
					});
				}
        function dismissCustomer(index){
					$('#lastCustomerDismiss').modal('show');
					
					$('#dismissCustomerIndex').val(index);
					$('#dismissCustomerNumber').val(customersWOOrders[index].AccountNumber);
					$('#dismissCustomerName').val(customersWOOrders[index].Name);
					if(parseInt(customersWOOrders[index].ReportCount) == 0){
						$('#dismissCustomerNoticeNumber').html("<b class='warning'>Primer Aviso</b>");	
					}else{
						$('#dismissCustomerNoticeNumber').html("<b>Segundo Aviso</b>");	
					}
					
				}
            
        </script>
        <!-- END SALESMAN customer MODAL -->
				
<!-- SALESMAN DISMISS CUSTOMER MODAL -->
    	<div class="modal fade" id="lastCustomerDismiss" tabindex="-1" role="dialog" aria-labelledby="lastCustomerDismiss" aria-hidden="true">
          <div class="modal-dialog modal-md">
            <div class="modal-content">
              <div class="modal-header">
								<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
								<h4 class="modal-title" id="lastCustomerPurchaseActivityDismissTitle">Dismiss Customer Without Activity</h4>
              </div>
              <div class="modal-body">
								<div class="container-fluid">
									<div class="row">
										<form id="dismissCustomerForm" >
											<input type="hidden" readonly class="form-control" id="dismissCustomerIndex" />
											<div class="form-group">
												<label for="dismissCustomerNumber">Account Number</label>
												<input type="text" readonly class="form-control" id="dismissCustomerNumber" />
											</div>
											<div class="form-group">
												<label for="dismissCustomerName">Name</label>
												<input type="text" readonly class="form-control" id="dismissCustomerName" />
											</div>
											<div class="form-group">
												<label for="dismissCustomerReason">Reason</label>
												<textarea class="form-control" id="dismissCustomerReason" rows="3"></textarea>
											</div>
										</form>
									</div>
								</div>
							</div>
              <div class="modal-footer">
								<span class="pull-left" id="dismissCustomerNoticeNumber"></span>
	              <a id="dismissCustomerReasonSave" disabled class="btn btn-primary" onclick="saveDismissCustomer();">Save</a>
	              <br>
							</div>
            </div>
          </div>
        </div>
			<script>
				$('#dismissCustomerReason').on('change', function(){
					if($("#dismissCustomerReason").val().trim() !== ""){
						$('#dismissCustomerReasonSave').removeAttr('disabled');
					}else{
						$('#dismissCustomerReasonSave').attr('disabled', true);
					}
				});
				$('#lastCustomerDismiss').on('hidden.bs.modal	', function(){
					$("#dismissCustomerReason").val('');
					setTimeout(function(){ 
						$('body').addClass('modal-open');
					}, 450);	
				});
				function saveDismissCustomer(){
					var index = $('#dismissCustomerIndex').val();
					$.post('mod.php?p=lastcustomerpurchase&a=functions&f=saveDismissCustomerActivity', { "AccountNumber": customersWOOrders[index].AccountNumber, "Reason": $('#dismissCustomerReason').val(), "LastOrder": customersWOOrders[index].LastOrderDate }).done(function(data){
							if(data.trim() == "true"){
								$('#lastCustomerDismiss').modal('hide');
								$('#lastCustomerDismiss').on('hidden.bs.modal', function(){
									$('tr#dismissCustomerRow-' + index).remove();
									$('body').addClass('modal-open');
									$('body').addClass('modal-open');
									$('body').addClass('modal-open');	
								});
								
							}
					});
					
				}
				<?php
				if(isset($_SESSION['ShownCustomerActivity']) && $_SESSION['ShownCustomerActivity'] == false){
					echo "openCustomerActivity();";
					$_SESSION['ShownCustomerActivity'] = true;	
				}
				
				?>
			</script>
<?php
		
  }

?>