<?php
if (!isset($_SESSION['FirstName'])) {
	session_start();
} 



if ($_SESSION['FirstName'] == "") {
	echo 'You are not authorized to view this page. <br><br>You may <a href="index.php"><font color="blue">log in here.</font></a>';
	exit;
}
    
include ('includes/db1config.php');
include ('../includes/mysql_connect.php');

echo "</div><h4>Customer</h4>";

?>
   
   <?php include("admin_globals.php"); ?>
    <style>
        .toPopDiv{
            height:400px !important;
        }
        .popup.add-menu-item:hover{
            cursor: pointer;
        }
    </style>

<div class="mainForm">
	<select name="customer" class="customerInvoice" >
		<option value="">Select Customer</option>
		<?php 
			$query = "SELECT * from guestLogin";
			$rt=mysql_query($query);  // query executed
			echo mysql_error();   
			
			while($nt2=mysql_fetch_array($rt)){
				echo '<option value="'.$nt2['UserKey'].'">'.$nt2['FirstName'].' '.$nt2['LastName'].'( '.$nt2['Address1'].' )</option>';
			}
		?>
	</select>
	
	<div style="margin-top:20%;" id="invoicemsg" >
		<center><h5>Please Select customer to view invoices</h5></center>
	</div>

	<div class="Customerdata"></div>

	<div id="popup_add" class="toPopDiv menu-item" style="display: none;"> 
        <div class="close"></div>
        <span class="ecs_tooltip" style="display: none;">Esc to close <span class="arrow"></span></span>
        <iframe style="width:100%;height:400px;" src="addInvoice.php" id="iframe-add" class="menu-item-iframe"></iframe>
    </div>

    <div id="popup_edit" class="toPopDiv menu-item" style="display: none;"> 
        <div class="close"></div>
        <span class="ecs_tooltip" style="display: none;">Esc to close <span class="arrow"></span></span>
        <iframe style="width:100%;height:400px;" src="addInvoice.php" id="iframe-add" class="menu-item-iframe"></iframe>
    </div>
</div>

<script type="text/javascript" charset="utf-8" async defer>
	$(document).ready(function() {
		$(document).on('change', '.customerInvoice', function(event) {
			event.preventDefault();
			if( $(this).val() != '' ){
				$.ajax({
					url      : '/cmsadmin/getInvoice.php',
					type     : 'POST',
					dataType : 'html',
					data     : { 
						id   : $(this).val()
					},
				}).done(function(res) {
					$('#invoicemsg').css("margin-top","0").html(res);
				});
			}
		});
	});

</script>
<script type="text/javascript">
	
		  
    $(document).on('click', '.popup', function(event) {
        var popupId = $(this).data('popup-id');
        var id      = $(this).data('user-id');	
        if( popupId == 'edit' ){
        	var custId = $(this).data('custid');	
        	$('.menu-item-iframe').attr('src','editInvoice.php?id='+id+'&custid='+custId+'');
        } else {
        	$('.menu-item-iframe').attr('src','addInvoice.php?id='+id+'');
        }

    });
    
    $(document).on('click', '.deletemaster', function(event) {
        var id     = $(this).attr('data-user-id');
        var custid = $(this).attr('data-custid');
        var invno  = $(this).attr('data-invno');

        if(!confirm('Are you sure you want to delete this invoice ?')){
            event.preventDefault();
            return false;
        } else {
            $.ajax({
                url      : '/cmsadmin/deleteInvoice.php',
                type     : 'POST',
                dataType : 'json',
                data     : { 
                    id             : id,
                    custid         : custid,
                    invoiceno      : invno
                },
            }).done(function(res) {
                if(res.msg == 1){
                    var custid = $('.customerInvoice').val();
                    $('.customerInvoice').val(custid).trigger("change"); 
                } else {
                    confirm('Something went wrong please try again ?')
                    return false;
                }
            });
            
            return true;
        }
    });

    $(document).on('click', '.SendInvoiceinMail', function(event) {
    	var id     = $(this).attr('data-user-id');
    	var custid = $(this).attr('data-custid');
    	var invno  = $(this).attr('data-invno');
        $(this).attr('disabled','disabled');
    	$.ajax({
    		url      : '/cmsadmin/sendInvoiceInmail.php',
            type     : 'POST',
    		dataType : 'json',
    		data     : { 
				id             : id,
				custid         : custid,
				invoiceno      : invno
			},
    	}).done(function(res) {
    		if(res == 1){
                alert('Invoice Email Sent.');
                $('.SendInvoiceinMail').removeAttr('disabled');
            } else {
                alert(res);
                $('.SendInvoiceinMail').removeAttr('disabled');
            }
    	});
    });
    

</script>

<?php 
    if( isset($_GET['custid']) && $_GET['custid'] != '' ){ ?>
        <script type="text/javascript" charset="utf-8" async defer>
            $(document).ready(function() {
                var id ='<?php echo $_GET["custid"];?>';
                $('.customerInvoice').val(id).trigger("change");
            });
        </script>
        <?php
    }
 ?>
 
<div style="clear: both;">&nbsp;</div>
			  <div style="text-align:center;"><?php echo $bottom_links; ?></div>